1: \ UTF-8 handling 12dec04py
2:
3: \ Copyright (C) 2004,2005 Free Software Foundation, Inc.
4:
5: \ This file is part of Gforth.
6:
7: \ Gforth is free software; you can redistribute it and/or
8: \ modify it under the terms of the GNU General Public License
9: \ as published by the Free Software Foundation; either version 2
10: \ of the License, or (at your option) any later version.
11:
12: \ This program is distributed in the hope that it will be useful,
13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: \ GNU General Public License for more details.
16:
17: \ You should have received a copy of the GNU General Public License
18: \ along with this program; if not, write to the Free Software
19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20:
21: \ short: u8 means utf-8 encoded address
22:
23: s" malformed UTF-8 character" exception Constant UTF-8-err
24:
25: $80 Value max-single-byte
26:
27: : u8len ( u8 -- n )
28: dup max-single-byte u< IF drop 1 EXIT THEN \ special case ASCII
29: $800 2 >r
30: BEGIN 2dup u>= WHILE 5 lshift r> 1+ >r dup 0= UNTIL THEN
31: 2drop r> ;
32:
33: : u8@+ ( u8addr -- u8addr' u )
34: count dup max-single-byte u< ?EXIT \ special case ASCII
35: dup $C2 u< IF UTF-8-err throw THEN \ malformed character
36: $7F and $40 >r
37: BEGIN dup r@ and WHILE r@ xor
38: 6 lshift r> 5 lshift >r >r count
39: dup $C0 and $80 <> IF UTF-8-err throw THEN
40: $3F and r> or
41: REPEAT rdrop ;
42:
43: : u8!+ ( u u8addr -- u8addr' )
44: over max-single-byte u< IF tuck c! 1+ EXIT THEN \ special case ASCII
45: >r 0 swap $3F
46: BEGIN 2dup u> WHILE
47: 2/ >r dup $3F and $80 or swap 6 rshift r>
48: REPEAT $7F xor 2* or r>
49: BEGIN over $80 u>= WHILE tuck c! 1+ REPEAT nip ;
50:
51: \ plug-in so that char and '<char> work for UTF-8
52:
53: [ifundef] char@ \ !! bootstrapping help
54: Defer char@ ( addr u -- char addr' u' )
55: :noname over c@ -rot 1 /string ; IS char@
56: [then]
57:
58: :noname ( addr u -- char addr' u' )
59: \ !! the if here seems to work around some breakage, but not
60: \ entirely; e.g., try 'ç' with LANG=C.
61: dup 1 u<= IF defers char@ EXIT THEN
62: over + >r u8@+ swap r> over - ; IS char@
63:
64: \ scan to next/previous character
65:
66: \ alternative names: u8char+ u8char-
67:
68: : u8>> ( u8addr -- u8addr' ) u8@+ drop ;
69: : u8<< ( u8addr -- u8addr' )
70: BEGIN 1- dup c@ $C0 and max-single-byte <> UNTIL ;
71:
72: \ utf key and emit
73:
74: : u8key ( -- u )
75: defers key dup max-single-byte u< ?EXIT \ special case ASCII
76: dup $C2 u< IF UTF-8-err throw THEN \ malformed character
77: $7F and $40 >r
78: BEGIN dup r@ and WHILE r@ xor
79: 6 lshift r> 5 lshift >r >r defers key
80: dup $C0 and $80 <> IF UTF-8-err throw THEN
81: $3F and r> or
82: REPEAT rdrop ;
83:
84: : u8emit ( u -- )
85: dup max-single-byte u< IF defers emit EXIT THEN \ special case ASCII
86: 0 swap $3F
87: BEGIN 2dup u> WHILE
88: 2/ >r dup $3F and $80 or swap 6 rshift r>
89: REPEAT $7F xor 2* or
90: BEGIN dup $80 u>= WHILE defers emit REPEAT drop ;
91:
92: \ utf-8 stuff for xchars
93:
94: : +u8/string ( c-addr1 u1 -- c-addr2 u2 )
95: over dup u8>> swap - /string ;
96:
97: : -u8/string ( c-addr1 u1 -- c-addr2 u2 )
98: over dup u8<< swap - /string ;
99:
100: : u8@ ( c-addr -- u )
101: u8@+ nip ;
102:
103: : u8!+? ( xc xc-addr1 u1 -- xc-addr2 u2 f )
104: >r over u8len r@ over u< if ( xc xc-addr1 len r: u1 )
105: \ not enough space
106: drop nip r> false
107: else
108: >r u8!+ r> r> swap - true
109: then ;
110:
111: : u8addrlen ( u8-addr -- u )
112: \ length of UTF-8 char starting at u8-addr (accesses only u8-addr)
113: c@
114: dup $80 u< if drop 1 exit endif
115: dup $c0 u< if UTF-8-err throw endif
116: dup $e0 u< if drop 2 exit endif
117: dup $f0 u< if drop 3 exit endif
118: dup $f8 u< if drop 4 exit endif
119: dup $fc u< if drop 5 exit endif
120: dup $fe u< if drop 6 exit endif
121: UTF-8-err throw ;
122:
123: : -u8trailing-garbage ( addr u1 -- addr u2 )
124: 2dup + dup u8<< ( addr u1 end1 end2 )
125: 2dup dup u8addrlen + = if \ last character ok
126: 2drop
127: else
128: nip nip over -
129: then ;
130:
131: [IFUNDEF] wcwidth
132: : wcwidth abort ;
133: [THEN]
134:
135: : u8width ( xcaddr u -- n )
136: 0 rot rot over + swap ?DO
137: I xc@+ swap >r wcwidth +
138: r> I - +LOOP ;
139:
140: : set-encoding-utf-8 ( -- )
141: ['] u8emit is xemit
142: ['] u8key is xkey
143: ['] u8>> is xchar+
144: ['] u8<< is xchar-
145: ['] +u8/string is +x/string
146: ['] -u8/string is -x/string
147: ['] u8@ is xc@
148: ['] u8!+? is xc!+?
149: ['] u8@+ is xc@+
150: ['] u8len is xc-size
151: [ [IFDEF] x-width ]
152: ['] u8width is x-width
153: [ [THEN] ]
154: ['] -u8trailing-garbage is -trailing-garbage
155: ;
156:
157: : utf-8-cold ( -- )
158: s" LC_ALL" getenv 2dup d0= IF 2drop
159: s" LC_CTYPE" getenv 2dup d0= IF 2drop
160: s" LANG" getenv 2dup d0= IF 2drop
161: s" C" THEN THEN THEN
162: s" UTF-8" search nip nip
163: IF set-encoding-utf-8 ELSE set-encoding-fixed-width THEN ;
164:
165: ' utf-8-cold INIT8 chained
166:
167: utf-8-cold
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>