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