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