author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 17 Jun 2015 06:22:00 +0100 | |
branch | jv |
changeset 18487 | 8735bd9eee2f |
parent 18120 | e3a375d5f6a8 |
child 18608 | 7d521f25267c |
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 |
" |
|
8081 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
14 |
"{ NameSpace: CharacterEncoderImplementations }" |
|
15 |
||
16 |
TwoByteEncoder subclass:#ISO10646_to_UTF8 |
|
17 |
instanceVariableNames:'' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Text-Encodings' |
|
21 |
! |
|
22 |
||
23 |
!ISO10646_to_UTF8 class methodsFor:'documentation'! |
|
24 |
||
8148 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
8411 | 28 |
All Rights Reserved |
8148 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
8081 | 39 |
examples |
40 |
" |
|
41 |
Encoding (unicode to utf8) |
|
8411 | 42 |
ISO10646_to_UTF8 encodeString:'hello'. |
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
43 |
|
8081 | 44 |
|
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
45 |
Decoding (utf8 to unicode): |
8081 | 46 |
|t| |
47 |
||
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
48 |
t := ISO10646_to_UTF8 encodeString:'Helloś'. |
8411 | 49 |
ISO10646_to_UTF8 decodeString:t. |
8081 | 50 |
" |
51 |
! ! |
|
52 |
||
53 |
!ISO10646_to_UTF8 methodsFor:'encoding & decoding'! |
|
54 |
||
55 |
decode:aCode |
|
56 |
self shouldNotImplement "/ no single byte conversion possible |
|
57 |
! |
|
58 |
||
59 |
decodeString:aStringOrByteCollection |
|
60 |
"given a string in UTF8 encoding, |
|
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
61 |
return a new string containing the same characters, in Unicode encoding. |
17623
6fe31bc70e49
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
17489
diff
changeset
|
62 |
Returns either a normal String, a Unicode16String or a Unicode32String instance. |
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
63 |
This is only useful, when reading from external sources or communicating with |
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
64 |
other systems |
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
65 |
(ST/X never uses utf8 internally, but always uses strings of fully decoded unicode characters). |
8081 | 66 |
This only handles up-to 30bit characters. |
67 |
||
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
68 |
If you work a lot with utf-8 encoded textFiles, |
8081 | 69 |
this is a first-class candidate for a primitive." |
70 |
||
8411 | 71 |
|sz anyAbove7BitAscii nBitsRequired |
8081 | 72 |
ascii "{ Class: SmallInteger }" |
73 |
byte "{ Class: SmallInteger }" |
|
74 |
s newString idx next6Bits last6Bits |
|
75 |
errorReporter| |
|
76 |
||
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
77 |
"/ avoid creation of new strings |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
78 |
aStringOrByteCollection isString ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
79 |
aStringOrByteCollection contains8BitCharacters ifFalse:[^ aStringOrByteCollection]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
80 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
81 |
|
8773 | 82 |
errorReporter := [:msg | |
83 |
DecodingError newException |
|
84 |
defaultValue:aStringOrByteCollection; |
|
10531
7a799d53e932
changed #decodeString: - proceedable exception
Stefan Vogel <sv@exept.de>
parents:
9928
diff
changeset
|
85 |
raiseRequestWith:aStringOrByteCollection errorString:msg. |
8773 | 86 |
]. |
8081 | 87 |
|
88 |
next6Bits := [ |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
89 |
| byte | |
8411 | 90 |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
91 |
byte := s nextByte. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
92 |
byte isNil ifTrue:[^ errorReporter value:'short utf8 string']. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
93 |
ascii := (ascii bitShift:6) bitOr:(byte bitAnd:2r00111111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
94 |
(byte bitAnd:2r11000000) ~~ 2r10000000 ifTrue:[ |
14172 | 95 |
^ errorReporter value:'illegal followbyte (next)'. |
96 |
]. |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
97 |
]. |
8081 | 98 |
|
99 |
last6Bits := [ |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
100 |
| a byte | |
8081 | 101 |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
102 |
byte := s nextByte. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
103 |
byte isNil ifTrue:[^ errorReporter value:'short utf8 string']. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
104 |
a := (ascii bitShift:6) bitOr:(byte bitAnd:2r00111111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
105 |
(a > 16r3FFFFFFF) ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
106 |
"/ ST/X can only represent 30 bit unicode characters. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
107 |
errorReporter value:'unicode character out of range'. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
108 |
a := 16r3FFFFFFF. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
109 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
110 |
ascii := a. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
111 |
(byte bitAnd:2r11000000) ~~ 2r10000000 ifTrue:[ |
14172 | 112 |
^ errorReporter value:'illegal followbyte (last)'. |
113 |
]. |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
114 |
]. |
8081 | 115 |
|
116 |
nBitsRequired := 8. |
|
8411 | 117 |
anyAbove7BitAscii := false. |
8081 | 118 |
sz := 0. |
119 |
s := aStringOrByteCollection readStream. |
|
14172 | 120 |
|
121 |
"first determine the string size" |
|
8081 | 122 |
[s atEnd] whileFalse:[ |
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
123 |
byte := ascii := s nextByte. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
124 |
(byte bitAnd:16r80) ~~ 0 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
125 |
anyAbove7BitAscii := true. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
126 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
127 |
"/ 80 .. 7FF |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
128 |
ascii := (byte bitAnd:2r00011111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
129 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
130 |
ascii > 16rFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
131 |
nBitsRequired := nBitsRequired max:16 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
132 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
133 |
"/ a strict utf8 decoder does not allow overlong sequences |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
134 |
ascii < 16r80 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
135 |
errorReporter value:'overlong utf8 sequence' |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
136 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
137 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
138 |
(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
139 |
"/ 800 .. FFFF |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
140 |
ascii := (byte bitAnd:2r00001111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
141 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
142 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
143 |
ascii > 16rFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
144 |
nBitsRequired := nBitsRequired max:16 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
145 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
146 |
ascii < 16r800 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
147 |
errorReporter value:'overlong utf8 sequence' |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
148 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
149 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
150 |
(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
151 |
"/ 10000 .. 1FFFFF |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
152 |
ascii := (byte bitAnd:2r00000111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
153 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
154 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
155 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
156 |
ascii > 16rFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
157 |
ascii > 16rFFFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
158 |
nBitsRequired := nBitsRequired max:32 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
159 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
160 |
nBitsRequired := nBitsRequired max:16 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
161 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
162 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
163 |
ascii < 16r10000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
164 |
errorReporter value:'overlong utf8 sequence' |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
165 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
166 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
167 |
(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
168 |
"/ 200000 .. 3FFFFFF |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
169 |
ascii := (byte bitAnd:2r00000011). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
170 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
171 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
172 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
173 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
174 |
ascii > 16rFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
175 |
ascii > 16rFFFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
176 |
nBitsRequired := nBitsRequired max:32 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
177 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
178 |
nBitsRequired := nBitsRequired max:16 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
179 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
180 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
181 |
ascii < 200000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
182 |
errorReporter value:'overlong utf8 sequence' |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
183 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
184 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
185 |
(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
186 |
"/ 4000000 .. 7FFFFFFF |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
187 |
ascii := (byte bitAnd:2r00000001). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
188 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
189 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
190 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
191 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
192 |
last6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
193 |
ascii > 16rFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
194 |
ascii > 16rFFFF ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
195 |
nBitsRequired := nBitsRequired max:32 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
196 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
197 |
nBitsRequired := nBitsRequired max:16 |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
198 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
199 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
200 |
ascii < 16r4000000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
201 |
errorReporter value:'overlong utf8 sequence' |
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 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
204 |
errorReporter value:'invalid utf8 encoding' |
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 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
207 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
208 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
209 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
210 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
211 |
sz := sz + 1. |
8081 | 212 |
]. |
213 |
nBitsRequired == 8 ifTrue:[ |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
214 |
anyAbove7BitAscii ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
215 |
"/ can return the original string |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
216 |
aStringOrByteCollection isString ifTrue:[^ aStringOrByteCollection]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
217 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
218 |
newString := String uninitializedNew:sz |
8081 | 219 |
] ifFalse:[ |
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
220 |
nBitsRequired <= 16 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
221 |
newString := Unicode16String new:sz |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
222 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
223 |
newString := Unicode32String new:sz |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
224 |
] |
8081 | 225 |
]. |
226 |
||
227 |
next6Bits := [ |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
228 |
|byte| |
8081 | 229 |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
230 |
byte := s nextByte. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
231 |
ascii := (ascii bitShift:6) bitOr:(byte bitAnd:2r00111111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
232 |
]. |
8081 | 233 |
|
14172 | 234 |
s reset. |
8081 | 235 |
idx := 1. |
14172 | 236 |
|
237 |
"now fill the string" |
|
8081 | 238 |
[s atEnd] whileFalse:[ |
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
239 |
byte := ascii := s nextByte. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
240 |
(byte bitAnd:2r10000000) ~~ 0 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
241 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
242 |
ascii := (byte bitAnd:2r00011111). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
243 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
244 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
245 |
(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
246 |
ascii := (byte bitAnd:2r00001111). |
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 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
249 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
250 |
(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
251 |
ascii := (byte bitAnd:2r00000111). |
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:2r11111100) == 2r11111000 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
257 |
ascii := (byte bitAnd:2r00000011). |
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 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
263 |
(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
264 |
ascii := (byte bitAnd:2r00000001). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
265 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
266 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
267 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
268 |
next6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
269 |
last6Bits value. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
270 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
271 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
272 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
273 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
274 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
275 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
276 |
newString at:idx put:(Character value:ascii). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
277 |
idx := idx + 1. |
8081 | 278 |
]. |
279 |
^ newString |
|
280 |
||
281 |
" |
|
8411 | 282 |
CharacterArray fromUTF8Bytes:#[ 16r41 16r42 ] |
283 |
CharacterArray fromUTF8Bytes:#[ 16rC1 16r02 ] |
|
284 |
CharacterArray fromUTF8Bytes:#[ 16rE0 16r81 16r02 ] |
|
285 |
CharacterArray fromUTF8Bytes:#[ 16rEF 16rBF 16rBF ] |
|
8081 | 286 |
|
287 |
rfc2279 examples: |
|
8411 | 288 |
CharacterArray fromUTF8Bytes:#[ 16r41 16rE2 16r89 16rA2 16rCE 16r91 16r2E ] |
289 |
CharacterArray fromUTF8Bytes:#[ 16rED 16r95 16r9C 16rEA 16rB5 16rAD 16rEC 16r96 16rB4 ] |
|
290 |
CharacterArray fromUTF8Bytes:#[ 16rE6 16r97 16rA5 16rE6 16r9C 16rAC 16rE8 16rAA 16r9E ] |
|
8081 | 291 |
|
292 |
invalid: |
|
8411 | 293 |
CharacterArray fromUTF8Bytes:#[ 16rC0 16r80 ] |
294 |
CharacterArray fromUTF8Bytes:#[ 16rE0 16r80 16r80 ] |
|
8081 | 295 |
" |
9928 | 296 |
|
297 |
"Modified: / 18-09-2006 / 19:55:52 / cg" |
|
8081 | 298 |
! |
299 |
||
300 |
encode:aCode |
|
301 |
self shouldNotImplement "/ no single byte conversion possible |
|
302 |
! |
|
303 |
||
304 |
encodeString:aUnicodeString |
|
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
305 |
"return the UTF-8 representation of a Unicode string. |
8081 | 306 |
The resulting string is only useful to be stored on some external file, |
307 |
not for being used inside ST/X. |
|
308 |
||
8411 | 309 |
If you work a lot with utf8 encoded textFiles, |
8081 | 310 |
this is a first-class candidate for a primitive." |
311 |
||
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
312 |
|s| |
8081 | 313 |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
314 |
"/ avoid creation of new strings |
14172 | 315 |
aUnicodeString contains8BitCharacters ifFalse:[^ aUnicodeString asSingleByteString]. |
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
316 |
|
8221 | 317 |
s := WriteStream on:(String uninitializedNew:aUnicodeString size). |
8081 | 318 |
aUnicodeString do:[:eachCharacter | |
17623
6fe31bc70e49
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
17489
diff
changeset
|
319 |
|codePoint "{Class: SmallInteger }" b1 b2 b3 b4 b5 v "{Class: SmallInteger }"| |
8081 | 320 |
|
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
321 |
codePoint := eachCharacter codePoint. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
322 |
codePoint <= 16r7F ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
323 |
s nextPut:eachCharacter. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
324 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
325 |
b1 := Character value:((codePoint bitAnd:16r3F) bitOr:2r10000000). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
326 |
v := codePoint bitShift:-6. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
327 |
v <= 16r1F ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
328 |
s nextPut:(Character value:(v bitOr:2r11000000)). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
329 |
s nextPut:b1. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
330 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
331 |
b2 := Character value:((v bitAnd:16r3F) bitOr:2r10000000). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
332 |
v := v bitShift:-6. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
333 |
v <= 16r0F ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
334 |
s nextPut:(Character value:(v bitOr:2r11100000)). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
335 |
s nextPut:b2; nextPut:b1. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
336 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
337 |
b3 := Character value:((v bitAnd:16r3F) bitOr:2r10000000). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
338 |
v := v bitShift:-6. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
339 |
v <= 16r07 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
340 |
s nextPut:(Character value:(v bitOr:2r11110000)). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
341 |
s nextPut:b3; nextPut:b2; nextPut:b1. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
342 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
343 |
b4 := Character value:((v bitAnd:16r3F) bitOr:2r10000000). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
344 |
v := v bitShift:-6. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
345 |
v <= 16r03 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
346 |
s nextPut:(Character value:(v bitOr:2r11111000)). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
347 |
s nextPut:b4; nextPut:b3; nextPut:b2; nextPut:b1. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
348 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
349 |
b5 := Character value:((v bitAnd:16r3F) bitOr:2r10000000). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
350 |
v := v bitShift:-6. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
351 |
v <= 16r01 ifTrue:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
352 |
s nextPut:(Character value:(v bitOr:2r11111100)). |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
353 |
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
|
354 |
] ifFalse:[ |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
355 |
"/ cannot happen - we only support up to 30 bit characters |
17623
6fe31bc70e49
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
17489
diff
changeset
|
356 |
EncodingError raiseWith:eachCharacter errorString:'codePoint > 31bit in #utf8Encode'. |
8460
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
357 |
] |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
358 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
359 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
360 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
361 |
]. |
f4d333135e1d
tuned encoding/decoding (quick check for 8-bit chars)
penk
parents:
8411
diff
changeset
|
362 |
]. |
8081 | 363 |
]. |
364 |
||
365 |
^ s contents |
|
366 |
||
367 |
" |
|
368 |
(self encodeString:'hello') asByteArray #[104 101 108 108 111] |
|
369 |
(self encodeString:(Character value:16r40) asString) asByteArray #[64] |
|
370 |
(self encodeString:(Character value:16r7F) asString) asByteArray #[127] |
|
371 |
(self encodeString:(Character value:16r80) asString) asByteArray #[194 128] |
|
8411 | 372 |
(self encodeString:(Character value:16rFF) asString) asByteArray #[195 191] |
373 |
(self encodeString:(Character value:16r100) asString) asByteArray #[196 128] |
|
374 |
(self encodeString:(Character value:16r200) asString) asByteArray #[200 128] |
|
375 |
(self encodeString:(Character value:16r400) asString) asByteArray #[208 128] |
|
376 |
(self encodeString:(Character value:16r800) asString) asByteArray #[224 160 128] |
|
377 |
(self encodeString:(Character value:16r1000) asString) asByteArray #[225 128 128] |
|
378 |
(self encodeString:(Character value:16r2000) asString) asByteArray #[226 128 128] |
|
379 |
(self encodeString:(Character value:16r4000) asString) asByteArray #[228 128 128] |
|
380 |
(self encodeString:(Character value:16r8000) asString) asByteArray #[232 128 128] |
|
381 |
(self encodeString:(Character value:16rFFFF) asString) asByteArray #[239 191 191] |
|
8081 | 382 |
" |
383 |
! ! |
|
384 |
||
11974 | 385 |
!ISO10646_to_UTF8 privateMethodsFor:'queries'! |
386 |
||
387 |
bytesToReadFor:firstByte |
|
388 |
|bytesToRead| |
|
389 |
||
390 |
bytesToRead := 1. |
|
391 |
(firstByte isBitSet:8) ifFalse:[^1]. |
|
392 |
7 downTo:3 |
|
393 |
do:[:idx | |
|
394 |
(firstByte isBitSet:idx) ifTrue:[ |
|
395 |
bytesToRead := bytesToRead + 1 |
|
396 |
] ifFalse:[ |
|
397 |
^bytesToRead |
|
398 |
] |
|
399 |
]. |
|
400 |
^bytesToRead |
|
401 |
||
402 |
"Created: / 14-06-2005 / 17:17:24 / janfrog" |
|
403 |
! ! |
|
404 |
||
8163 | 405 |
!ISO10646_to_UTF8 methodsFor:'queries'! |
406 |
||
11974 | 407 |
characterSize:charOrcodePoint |
408 |
"return the number of bytes required to encode codePoint" |
|
409 |
||
410 |
"Taken from RFC 3629" |
|
411 |
||
412 |
(charOrcodePoint asInteger between:16r00000000 and:16r0000007F) ifTrue:[^1]. |
|
413 |
(charOrcodePoint asInteger between:16r00000080 and:16r000007FF) ifTrue:[^2]. |
|
414 |
(charOrcodePoint asInteger between:16r00000800 and:16r0000FFFF) ifTrue:[^3]. |
|
415 |
(charOrcodePoint asInteger between:16r00010000 and:16r0010FFFF) ifTrue:[^4]. |
|
416 |
||
417 |
^self error:'Invalid codePoint' |
|
418 |
||
419 |
"Created: / 15-06-2005 / 15:16:22 / janfrog" |
|
420 |
! |
|
421 |
||
8163 | 422 |
nameOfEncoding |
14172 | 423 |
^ #utf8 |
8163 | 424 |
! ! |
425 |
||
11974 | 426 |
!ISO10646_to_UTF8 methodsFor:'stream support'! |
427 |
||
428 |
readNext:charactersToRead charactersFrom:stream |
|
11996 | 429 |
|
11974 | 430 |
| s | |
431 |
||
432 |
s := (String new:charactersToRead) writeStream. |
|
433 |
charactersToRead timesRepeat:[ |
|
434 |
| c | |
|
435 |
c := stream peek. |
|
436 |
s nextPutAll:(stream next:(self bytesToReadFor:c)) |
|
437 |
]. |
|
438 |
^ self decodeString:s contents |
|
439 |
||
440 |
"Created: / 16-06-2005 / 11:45:14 / masca" |
|
441 |
! |
|
442 |
||
443 |
readNextCharacterFrom:aStream |
|
11996 | 444 |
|
11974 | 445 |
|firstByte bytesToRead str| |
446 |
||
447 |
firstByte := aStream peek. |
|
448 |
firstByte ifNil:[^nil]. |
|
449 |
firstByte := firstByte asInteger. |
|
450 |
bytesToRead := self bytesToReadFor:firstByte. |
|
451 |
str := self decodeString:(aStream next:bytesToRead). |
|
452 |
str size > 1 ifTrue:[ |
|
453 |
self error:'Badly coded method' |
|
454 |
]. |
|
455 |
^ str first |
|
456 |
||
457 |
"Created: / 14-06-2005 / 17:03:59 / janfrog" |
|
458 |
! ! |
|
459 |
||
8081 | 460 |
!ISO10646_to_UTF8 class methodsFor:'documentation'! |
461 |
||
462 |
version |
|
17623
6fe31bc70e49
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
17489
diff
changeset
|
463 |
^ '$Header: /cvs/stx/stx/libbasic/CharacterEncoderImplementations__ISO10646_to_UTF8.st,v 1.19 2015-03-14 21:33:29 stefan Exp $' |
8081 | 464 |
! ! |
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
465 |