author | Claus Gittinger <cg@exept.de> |
Tue, 08 Mar 2005 11:58:56 +0100 | |
changeset 8773 | 267612096a52 |
parent 8460 | f4d333135e1d |
child 9928 | 46cf4350beb2 |
permissions | -rw-r--r-- |
8148 | 1 |
" |
2 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
8411 | 3 |
All Rights Reserved |
8148 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
8081 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
15 |
"{ NameSpace: CharacterEncoderImplementations }" |
|
16 |
||
17 |
TwoByteEncoder subclass:#ISO10646_to_UTF8 |
|
18 |
instanceVariableNames:'' |
|
19 |
classVariableNames:'' |
|
20 |
poolDictionaries:'' |
|
21 |
category:'Collections-Text-Encodings' |
|
22 |
! |
|
23 |
||
24 |
!ISO10646_to_UTF8 class methodsFor:'documentation'! |
|
25 |
||
8148 | 26 |
copyright |
27 |
" |
|
28 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
8411 | 29 |
All Rights Reserved |
8148 | 30 |
|
31 |
This software is furnished under a license and may be used |
|
32 |
only in accordance with the terms of that license and with the |
|
33 |
inclusion of the above copyright notice. This software may not |
|
34 |
be provided or otherwise made available to, or used by, any |
|
35 |
other person. No title to or ownership of the software is |
|
36 |
hereby transferred. |
|
37 |
" |
|
38 |
! |
|
39 |
||
8081 | 40 |
examples |
41 |
" |
|
42 |
Encoding (unicode to utf8) |
|
8411 | 43 |
ISO10646_to_UTF8 encodeString:'hello'. |
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
44 |
|
8081 | 45 |
|
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
46 |
Decoding (utf8 to unicode): |
8081 | 47 |
|t| |
48 |
||
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
49 |
t := ISO10646_to_UTF8 encodeString:'Helloś'. |
8411 | 50 |
ISO10646_to_UTF8 decodeString:t. |
8081 | 51 |
" |
52 |
! ! |
|
53 |
||
54 |
!ISO10646_to_UTF8 methodsFor:'encoding & decoding'! |
|
55 |
||
56 |
decode:aCode |
|
57 |
self shouldNotImplement "/ no single byte conversion possible |
|
58 |
! |
|
59 |
||
60 |
decodeString:aStringOrByteCollection |
|
61 |
"given a string in UTF8 encoding, |
|
62 |
return a new string containing the same characters, in 16bit (or more) encoding. |
|
63 |
Returns either a normal String, a TwoByteString or a FourByteString instance. |
|
64 |
Only useful, when reading from external sources. |
|
65 |
This only handles up-to 30bit characters. |
|
66 |
||
8411 | 67 |
If you work a lot with utf8 encoded textFiles, |
8081 | 68 |
this is a first-class candidate for a primitive." |
69 |
||
8411 | 70 |
|sz anyAbove7BitAscii nBitsRequired |
8081 | 71 |
ascii "{ Class: SmallInteger }" |
72 |
byte "{ Class: SmallInteger }" |
|
73 |
s newString idx next6Bits last6Bits |
|
74 |
errorReporter| |
|
75 |
||
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
76 |
"/ avoid creation of new strings |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
77 |
aStringOrByteCollection isString ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
78 |
aStringOrByteCollection contains8BitCharacters ifFalse:[^ aStringOrByteCollection]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
79 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
80 |
|
8773 | 81 |
errorReporter := [:msg | |
82 |
DecodingError newException |
|
83 |
defaultValue:aStringOrByteCollection; |
|
84 |
raiseWith:aStringOrByteCollection errorString:msg. |
|
85 |
]. |
|
8081 | 86 |
|
87 |
next6Bits := [ |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
88 |
| byte | |
8411 | 89 |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
90 |
byte := s nextByte. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
91 |
byte isNil ifTrue:[^ errorReporter value:'short utf8 string']. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
92 |
ascii := (ascii bitShift:6) bitOr:(byte bitAnd:2r00111111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
93 |
(byte bitAnd:2r11000000) ~~ 2r10000000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
94 |
^ errorReporter value:'illegal followbyte'.]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
95 |
]. |
8081 | 96 |
|
97 |
last6Bits := [ |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
98 |
| a byte | |
8081 | 99 |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
100 |
byte := s nextByte. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
101 |
byte isNil ifTrue:[^ errorReporter value:'short utf8 string']. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
102 |
a := (ascii bitShift:6) bitOr:(byte bitAnd:2r00111111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
103 |
(a > 16r3FFFFFFF) ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
104 |
"/ ST/X can only represent 30 bit unicode characters. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
105 |
errorReporter value:'unicode character out of range'. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
106 |
a := 16r3FFFFFFF. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
107 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
108 |
ascii := a. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
109 |
(byte bitAnd:2r11000000) ~~ 2r10000000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
110 |
^ errorReporter value:'illegal followbyte'.]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
111 |
]. |
8081 | 112 |
|
113 |
nBitsRequired := 8. |
|
8411 | 114 |
anyAbove7BitAscii := false. |
8081 | 115 |
sz := 0. |
116 |
s := aStringOrByteCollection readStream. |
|
117 |
[s atEnd] whileFalse:[ |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
118 |
byte := ascii := s nextByte. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
119 |
(byte bitAnd:16r80) ~~ 0 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
120 |
anyAbove7BitAscii := true. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
121 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
122 |
"/ 80 .. 7FF |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
123 |
ascii := (byte bitAnd:2r00011111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
124 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
125 |
ascii > 16rFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
126 |
nBitsRequired := nBitsRequired max:16 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
127 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
128 |
"/ a strict utf8 decoder does not allow overlong sequences |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
129 |
ascii < 16r80 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
130 |
errorReporter value:'overlong utf8 sequence' |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
131 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
132 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
133 |
(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
134 |
"/ 800 .. FFFF |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
135 |
ascii := (byte bitAnd:2r00001111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
136 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
137 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
138 |
ascii > 16rFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
139 |
nBitsRequired := nBitsRequired max:16 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
140 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
141 |
ascii < 16r800 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
142 |
errorReporter value:'overlong utf8 sequence' |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
143 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
144 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
145 |
(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
146 |
"/ 10000 .. 1FFFFF |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
147 |
ascii := (byte bitAnd:2r00000111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
148 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
149 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
150 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
151 |
ascii > 16rFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
152 |
ascii > 16rFFFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
153 |
nBitsRequired := nBitsRequired max:32 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
154 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
155 |
nBitsRequired := nBitsRequired max:16 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
156 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
157 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
158 |
ascii < 16r10000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
159 |
errorReporter value:'overlong utf8 sequence' |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
160 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
161 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
162 |
(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
163 |
"/ 200000 .. 3FFFFFF |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
164 |
ascii := (byte bitAnd:2r00000011). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
165 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
166 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
167 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
168 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
169 |
ascii > 16rFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
170 |
ascii > 16rFFFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
171 |
nBitsRequired := nBitsRequired max:32 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
172 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
173 |
nBitsRequired := nBitsRequired max:16 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
174 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
175 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
176 |
ascii < 200000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
177 |
errorReporter value:'overlong utf8 sequence' |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
178 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
179 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
180 |
(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
181 |
"/ 4000000 .. 7FFFFFFF |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
182 |
ascii := (byte bitAnd:2r00000001). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
183 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
184 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
185 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
186 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
187 |
last6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
188 |
ascii > 16rFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
189 |
ascii > 16rFFFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
190 |
nBitsRequired := nBitsRequired max:32 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
191 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
192 |
nBitsRequired := nBitsRequired max:16 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
193 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
194 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
195 |
ascii < 16r4000000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
196 |
errorReporter value:'overlong utf8 sequence' |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
197 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
198 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
199 |
errorReporter value:'invalid utf8 encoding' |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
200 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
201 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
202 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
203 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
204 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
205 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
206 |
sz := sz + 1. |
8081 | 207 |
]. |
208 |
nBitsRequired == 8 ifTrue:[ |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
209 |
anyAbove7BitAscii ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
210 |
"/ can return the original string |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
211 |
aStringOrByteCollection isString ifTrue:[^ aStringOrByteCollection]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
212 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
213 |
newString := String uninitializedNew:sz |
8081 | 214 |
] ifFalse:[ |
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
215 |
nBitsRequired <= 16 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
216 |
newString := Unicode16String new:sz |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
217 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
218 |
newString := Unicode32String new:sz |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
219 |
] |
8081 | 220 |
]. |
221 |
||
222 |
next6Bits := [ |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
223 |
|byte| |
8081 | 224 |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
225 |
byte := s nextByte. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
226 |
ascii := (ascii bitShift:6) bitOr:(byte bitAnd:2r00111111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
227 |
]. |
8081 | 228 |
|
229 |
s := aStringOrByteCollection readStream. |
|
230 |
idx := 1. |
|
231 |
[s atEnd] whileFalse:[ |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
232 |
byte := ascii := s nextByte. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
233 |
(byte bitAnd:2r10000000) ~~ 0 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
234 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
235 |
ascii := (byte bitAnd:2r00011111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
236 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
237 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
238 |
(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
239 |
ascii := (byte bitAnd:2r00001111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
240 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
241 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
242 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
243 |
(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
244 |
ascii := (byte bitAnd:2r00000111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
245 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
246 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
247 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
248 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
249 |
(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
250 |
ascii := (byte bitAnd:2r00000011). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
251 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
252 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
253 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
254 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
255 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
256 |
(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
257 |
ascii := (byte bitAnd:2r00000001). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
258 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
259 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
260 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
261 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
262 |
last6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
263 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
264 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
265 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
266 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
267 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
268 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
269 |
newString at:idx put:(Character value:ascii). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
270 |
idx := idx + 1. |
8081 | 271 |
]. |
8773 | 272 |
self assert:newString isString. |
8081 | 273 |
^ newString |
274 |
||
275 |
" |
|
8411 | 276 |
CharacterArray fromUTF8Bytes:#[ 16r41 16r42 ] |
277 |
CharacterArray fromUTF8Bytes:#[ 16rC1 16r02 ] |
|
278 |
CharacterArray fromUTF8Bytes:#[ 16rE0 16r81 16r02 ] |
|
279 |
CharacterArray fromUTF8Bytes:#[ 16rEF 16rBF 16rBF ] |
|
8081 | 280 |
|
281 |
rfc2279 examples: |
|
8411 | 282 |
CharacterArray fromUTF8Bytes:#[ 16r41 16rE2 16r89 16rA2 16rCE 16r91 16r2E ] |
283 |
CharacterArray fromUTF8Bytes:#[ 16rED 16r95 16r9C 16rEA 16rB5 16rAD 16rEC 16r96 16rB4 ] |
|
284 |
CharacterArray fromUTF8Bytes:#[ 16rE6 16r97 16rA5 16rE6 16r9C 16rAC 16rE8 16rAA 16r9E ] |
|
8081 | 285 |
|
286 |
invalid: |
|
8411 | 287 |
CharacterArray fromUTF8Bytes:#[ 16rC0 16r80 ] |
288 |
CharacterArray fromUTF8Bytes:#[ 16rE0 16r80 16r80 ] |
|
8081 | 289 |
" |
290 |
! |
|
291 |
||
292 |
encode:aCode |
|
293 |
self shouldNotImplement "/ no single byte conversion possible |
|
294 |
! |
|
295 |
||
296 |
encodeString:aUnicodeString |
|
297 |
"return the UTF-8 representation of a aUnicodeString. |
|
298 |
The resulting string is only useful to be stored on some external file, |
|
299 |
not for being used inside ST/X. |
|
300 |
||
8411 | 301 |
If you work a lot with utf8 encoded textFiles, |
8081 | 302 |
this is a first-class candidate for a primitive." |
303 |
||
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
304 |
|s| |
8081 | 305 |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
306 |
"/ avoid creation of new strings |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
307 |
aUnicodeString contains8BitCharacters ifFalse:[^ aUnicodeString]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
308 |
|
8221 | 309 |
s := WriteStream on:(String uninitializedNew:aUnicodeString size). |
8081 | 310 |
aUnicodeString do:[:eachCharacter | |
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
311 |
|codePoint b1 b2 b3 b4 b5 v "{Class: SmallInteger }"| |
8081 | 312 |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
313 |
codePoint := eachCharacter codePoint. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
314 |
codePoint <= 16r7F ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
315 |
s nextPut:eachCharacter. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
316 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
317 |
b1 := Character value:((codePoint bitAnd:16r3F) bitOr:2r10000000). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
318 |
v := codePoint bitShift:-6. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
319 |
v <= 16r1F ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
320 |
s nextPut:(Character value:(v bitOr:2r11000000)). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
321 |
s nextPut:b1. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
322 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
323 |
b2 := Character value:((v bitAnd:16r3F) bitOr:2r10000000). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
324 |
v := v bitShift:-6. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
325 |
v <= 16r0F ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
326 |
s nextPut:(Character value:(v bitOr:2r11100000)). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
327 |
s nextPut:b2; nextPut:b1. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
328 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
329 |
b3 := Character value:((v bitAnd:16r3F) bitOr:2r10000000). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
330 |
v := v bitShift:-6. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
331 |
v <= 16r07 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
332 |
s nextPut:(Character value:(v bitOr:2r11110000)). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
333 |
s nextPut:b3; nextPut:b2; nextPut:b1. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
334 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
335 |
b4 := Character value:((v bitAnd:16r3F) bitOr:2r10000000). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
336 |
v := v bitShift:-6. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
337 |
v <= 16r03 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
338 |
s nextPut:(Character value:(v bitOr:2r11111000)). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
339 |
s nextPut:b4; nextPut:b3; nextPut:b2; nextPut:b1. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
340 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
341 |
b5 := Character value:((v bitAnd:16r3F) bitOr:2r10000000). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
342 |
v := v bitShift:-6. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
343 |
v <= 16r01 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
344 |
s nextPut:(Character value:(v bitOr:2r11111100)). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
345 |
s nextPut:b5; nextPut:b4; nextPut:b3; nextPut:b2; nextPut:b1. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
346 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
347 |
"/ cannot happen - we only support up to 30 bit characters |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
348 |
self error:'ascii value > 31bit in utf8Encode'. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
349 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
350 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
351 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
352 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
353 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
354 |
]. |
8081 | 355 |
]. |
356 |
||
357 |
^ s contents |
|
358 |
||
359 |
" |
|
360 |
(self encodeString:'hello') asByteArray #[104 101 108 108 111] |
|
361 |
(self encodeString:(Character value:16r40) asString) asByteArray #[64] |
|
362 |
(self encodeString:(Character value:16r7F) asString) asByteArray #[127] |
|
363 |
(self encodeString:(Character value:16r80) asString) asByteArray #[194 128] |
|
8411 | 364 |
(self encodeString:(Character value:16rFF) asString) asByteArray #[195 191] |
365 |
(self encodeString:(Character value:16r100) asString) asByteArray #[196 128] |
|
366 |
(self encodeString:(Character value:16r200) asString) asByteArray #[200 128] |
|
367 |
(self encodeString:(Character value:16r400) asString) asByteArray #[208 128] |
|
368 |
(self encodeString:(Character value:16r800) asString) asByteArray #[224 160 128] |
|
369 |
(self encodeString:(Character value:16r1000) asString) asByteArray #[225 128 128] |
|
370 |
(self encodeString:(Character value:16r2000) asString) asByteArray #[226 128 128] |
|
371 |
(self encodeString:(Character value:16r4000) asString) asByteArray #[228 128 128] |
|
372 |
(self encodeString:(Character value:16r8000) asString) asByteArray #[232 128 128] |
|
373 |
(self encodeString:(Character value:16rFFFF) asString) asByteArray #[239 191 191] |
|
8081 | 374 |
" |
375 |
! ! |
|
376 |
||
8163 | 377 |
!ISO10646_to_UTF8 methodsFor:'queries'! |
378 |
||
379 |
nameOfEncoding |
|
380 |
^ #'utf8' |
|
381 |
! ! |
|
382 |
||
8081 | 383 |
!ISO10646_to_UTF8 class methodsFor:'documentation'! |
384 |
||
385 |
version |
|
8773 | 386 |
^ '$Header: /cvs/stx/stx/libbasic/CharacterEncoderImplementations__ISO10646_to_UTF8.st,v 1.11 2005-03-08 10:58:56 cg Exp $' |
8081 | 387 |
! ! |