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