-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
38 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,46 @@ | ||
\ simple tools to convert UTF-8 into UTF-16 and back | ||
|
||
: .utf16 ( xchar -- ) | ||
\ Copyright (C) 2015 Free Software Foundation, Inc. | ||
|
||
\ This file is part of Gforth. | ||
|
||
\ Gforth is free software; you can redistribute it and/or | ||
\ modify it under the terms of the GNU General Public License | ||
\ as published by the Free Software Foundation, either version 3 | ||
\ of the License, or (at your option) any later version. | ||
|
||
\ This program is distributed in the hope that it will be useful, | ||
\ but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
\ GNU General Public License for more details. | ||
|
||
\ You should have received a copy of the GNU General Public License | ||
\ along with this program. If not, see http://www.gnu.org/licenses/. | ||
|
||
: .utf16 ( xchar -- ) 0 { w^ ucs16 } | ||
dup $10000 u>= IF | ||
$10000 - >r r@ 10 rshift $3FF and $D800 + recurse | ||
r> $3FF and $DC00 + | ||
$10000 - >r r@ 10 rshift $3FF and $D800 + | ||
ucs16 w! ucs16 2 type \ high surrogate | ||
r> $3FF and $DC00 + \ low surrogate | ||
THEN | ||
dup $FF and emit 8 rshift $FF and emit ; | ||
: typex16 ( addr u -- ) | ||
\ type UTF-8 string as UCS16 string | ||
ucs16 w! ucs16 2 type ; | ||
: typeu16 ( addr u -- ) | ||
\ type UTF-8 string as UTF-16 string, byte order: host, no BOM | ||
bounds ?DO | ||
I xc@ .utf16 | ||
I I' over - x-size +LOOP ; | ||
: $utf16 ( addr1 u1 -- addr2 u2 ) | ||
[: typex16 0 .utf16 ;] $tmp 2 - ; | ||
: typex8 ( addr u -- ) | ||
: >utf16 ( addr1 u1 -- addr2 u2 ) | ||
\g convert UTF-8 string to UTF-16 | ||
[: typeu16 0 .utf16 ;] $tmp 2 - ; | ||
|
||
: typeu8 ( addr u -- ) | ||
\g print UTF-16 string as UTF-8, byte order: host, BOM ignored | ||
bounds ?DO | ||
I uw@ dup $D800 $DC00 within IF | ||
$3FF and 10 lshift I 2 + uw@ | ||
$3FF and or $10000 + xemit 4 \ no check for sanity | ||
ELSE xemit 2 THEN | ||
+LOOP ; | ||
: $utf8 ( addr1 u1 -- addr2 u2 ) | ||
[: typex8 0 emit ;] $tmp 1- ; | ||
: >utf8 ( addr1 u1 -- addr2 u2 ) | ||
\g convert UTF-16 string to UTF-8 | ||
[: typeu8 0 emit ;] $tmp 1- ; |