author | Claus Gittinger <cg@exept.de> |
Tue, 16 Jan 2018 17:16:22 +0100 | |
changeset 22431 | 1da6e2a6be6d |
parent 21475 | 6409725581b1 |
child 22472 | 45940fc5e0ad |
permissions | -rw-r--r-- |
21475 | 1 |
"{ Encoding: utf8 }" |
2 |
||
8903 | 3 |
" |
4 |
COPYRIGHT (c) 2005 by eXept Software AG |
|
5 |
All Rights Reserved |
|
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 |
" |
|
14 |
"{ Package: 'stx:libbasic' }" |
|
15 |
||
16 |
"{ NameSpace: CharacterEncoderImplementations }" |
|
17 |
||
18 |
TwoByteEncoder subclass:#ISO10646_to_UTF16BE |
|
19 |
instanceVariableNames:'' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Collections-Text-Encodings' |
|
23 |
! |
|
24 |
||
25 |
!ISO10646_to_UTF16BE class methodsFor:'documentation'! |
|
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 2005 by eXept Software AG |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
41 |
documentation |
|
42 |
" |
|
43 |
encodes/decodes UTF16 BigEndian (big-end-first) |
|
21300 | 44 |
|
45 |
Notice the naming (many are confused): |
|
46 |
Unicode is the set of number-to-glyph assignments |
|
47 |
whereas: |
|
48 |
UTF8, UTF16 etc. are a concrete way of xmitting Unicode codePoints (numbers). |
|
49 |
||
50 |
ST/X NEVER uses UTF8 or UTF16 internally - all characters are full 24bit characters. |
|
51 |
Only when exchanging data, are these converted into UTF8 (or other) byte sequences. |
|
8903 | 52 |
" |
53 |
! |
|
54 |
||
55 |
examples |
|
56 |
" |
|
57 |
Encoding (unicode to utf16BE) |
|
58 |
ISO10646_to_UTF16BE encodeString:'hello'. |
|
59 |
||
60 |
||
61 |
Decoding (utf16BE to unicode): |
|
62 |
|t| |
|
63 |
||
64 |
t := ISO10646_to_UTF16BE encodeString:''. |
|
65 |
ISO10646_to_UTF16BE decodeString:t. |
|
9325 | 66 |
|
67 |
Decoding (utf16LE-Bytes to unicode): |
|
68 |
|bytes| |
|
69 |
||
70 |
bytes := #[ 16r40 0 16r41 0 16r42 0 16r43 0 16r44 0 ]. |
|
71 |
ISO10646_to_UTF16LE decodeString:bytes. |
|
8903 | 72 |
" |
73 |
! ! |
|
74 |
||
75 |
!ISO10646_to_UTF16BE methodsFor:'encoding & decoding'! |
|
76 |
||
77 |
decode:aCode |
|
12432 | 78 |
^ aCode |
8903 | 79 |
! |
80 |
||
81 |
decodeString:aStringOrByteCollection |
|
14170
eed0dbcc471c
comment/format in: #decodeString:
Stefan Vogel <sv@exept.de>
parents:
12432
diff
changeset
|
82 |
"given a byteArray (2-bytes per character) or unsignedShortArray in UTF16 encoding, |
8903 | 83 |
return a new string containing the same characters, in 8, 16bit (or more) encoding. |
84 |
Returns either a normal String, a TwoByte- or a FourByte-String instance. |
|
85 |
Only useful, when reading from external sources. |
|
86 |
This only handles up-to 30bit characters." |
|
87 |
||
14170
eed0dbcc471c
comment/format in: #decodeString:
Stefan Vogel <sv@exept.de>
parents:
12432
diff
changeset
|
88 |
|s newString bitsPerElementIn nextIn |
12432 | 89 |
codeIn codeIn1 codeIn2 estimatedSize out| |
8903 | 90 |
|
91 |
aStringOrByteCollection isByteArray ifTrue:[ |
|
92 |
bitsPerElementIn := 8. |
|
93 |
] ifFalse:[ |
|
94 |
aStringOrByteCollection isString ifTrue:[ |
|
95 |
bitsPerElementIn := aStringOrByteCollection bitsPerCharacter. |
|
96 |
] ifFalse:[ |
|
14175
84e1adb65a5d
changed: #decodeString: detect odd number of bytes
Stefan Vogel <sv@exept.de>
parents:
14170
diff
changeset
|
97 |
"can be a ShortArray" |
8903 | 98 |
bitsPerElementIn := 16. |
99 |
]. |
|
100 |
]. |
|
101 |
||
14175
84e1adb65a5d
changed: #decodeString: detect odd number of bytes
Stefan Vogel <sv@exept.de>
parents:
14170
diff
changeset
|
102 |
s := aStringOrByteCollection readStream. |
8903 | 103 |
bitsPerElementIn == 8 ifTrue:[ |
14175
84e1adb65a5d
changed: #decodeString: detect odd number of bytes
Stefan Vogel <sv@exept.de>
parents:
14170
diff
changeset
|
104 |
s size odd ifTrue:[ |
14208 | 105 |
InvalidEncodingError raiseWith:aStringOrByteCollection errorString:' - size is not a multiple of 2 bytes'. |
14175
84e1adb65a5d
changed: #decodeString: detect odd number of bytes
Stefan Vogel <sv@exept.de>
parents:
14170
diff
changeset
|
106 |
]. |
8903 | 107 |
nextIn := [self nextTwoByteValueFrom:s]. |
108 |
] ifFalse:[ |
|
109 |
nextIn := [s next]. |
|
110 |
]. |
|
111 |
||
14175
84e1adb65a5d
changed: #decodeString: detect odd number of bytes
Stefan Vogel <sv@exept.de>
parents:
14170
diff
changeset
|
112 |
estimatedSize := s size * bitsPerElementIn // 16. |
12432 | 113 |
out := CharacterWriteStream on:(String new:estimatedSize). |
8903 | 114 |
[s atEnd] whileFalse:[ |
115 |
codeIn := nextIn value. |
|
116 |
codeIn <= 16rFF ifTrue:[ |
|
117 |
] ifFalse:[ |
|
118 |
(codeIn between:16rD800 and:16rDBFF) ifTrue:[ |
|
119 |
codeIn1 := codeIn. |
|
120 |
codeIn2 := nextIn value. |
|
121 |
codeIn := ((codeIn1 - 16rD800) bitShift:10) |
|
122 |
+ |
|
123 |
(codeIn2 - 16rDC00) |
|
124 |
+ 16r00010000. |
|
125 |
]. |
|
126 |
]. |
|
12432 | 127 |
out nextPut:(Character value:codeIn). |
8903 | 128 |
]. |
12432 | 129 |
newString := out contents. |
130 |
||
131 |
"/ nBitsRequired := 8. |
|
132 |
"/ sz := 0. |
|
133 |
"/ [s atEnd] whileFalse:[ |
|
134 |
"/ codeIn := nextIn value. |
|
135 |
"/ sz := sz + 1. |
|
136 |
"/ |
|
137 |
"/ codeIn <= 16rFF ifTrue:[ |
|
138 |
"/ ] ifFalse:[ |
|
139 |
"/ nBitsRequired := nBitsRequired max:16. |
|
140 |
"/ (codeIn between:16rD800 and:16rDBFF) ifTrue:[ |
|
141 |
"/ nBitsRequired := 32. |
|
142 |
"/ codeIn2 := nextIn value. |
|
143 |
"/ ]. |
|
144 |
"/ ] |
|
145 |
"/ ]. |
|
146 |
"/ |
|
147 |
"/ nBitsRequired == 8 ifTrue:[ |
|
148 |
"/ newString := String uninitializedNew:sz |
|
149 |
"/ ] ifFalse:[ |
|
150 |
"/ nBitsRequired <= 16 ifTrue:[ |
|
151 |
"/ newString := Unicode16String new:sz |
|
152 |
"/ ] ifFalse:[ |
|
153 |
"/ newString := Unicode32String new:sz |
|
154 |
"/ ] |
|
155 |
"/ ]. |
|
156 |
"/ |
|
157 |
"/ s := aStringOrByteCollection readStream. |
|
158 |
"/ idx := 1. |
|
159 |
"/ [s atEnd] whileFalse:[ |
|
160 |
"/ codeIn := nextIn value. |
|
161 |
"/ codeIn <= 16rFF ifTrue:[ |
|
162 |
"/ ] ifFalse:[ |
|
163 |
"/ nBitsRequired := nBitsRequired max:16. |
|
164 |
"/ (codeIn between:16rD800 and:16rDBFF) ifTrue:[ |
|
165 |
"/ nBitsRequired := 32. |
|
166 |
"/ codeIn1 := codeIn. |
|
167 |
"/ codeIn2 := nextIn value. |
|
168 |
"/ codeIn := ((codeIn1 - 16rD800) bitShift:10) |
|
169 |
"/ + |
|
170 |
"/ (codeIn2 - 16rDC00) |
|
171 |
"/ + 16r00010000. |
|
172 |
"/ ]. |
|
173 |
"/ ]. |
|
174 |
"/ newString at:idx put:(Character value:codeIn). |
|
175 |
"/ idx := idx + 1. |
|
176 |
"/ ]. |
|
8903 | 177 |
^ newString |
178 |
||
179 |
" |
|
180 |
self new decodeString:#[ 16r00 16r42 ] |
|
181 |
self new decodeString:#[ 16r01 16r42 ] |
|
182 |
self new decodeString:#[ 16r00 16r48 |
|
183 |
16r00 16r69 |
|
184 |
16rD8 16r00 |
|
185 |
16rDC 16r00 |
|
186 |
16r00 16r21 |
|
187 |
16r00 16r21 |
|
188 |
] |
|
189 |
||
190 |
self new decodeString:#( 16r0048 |
|
191 |
16r0069 |
|
192 |
16rD800 |
|
193 |
16rDC00 |
|
194 |
16r0021 |
|
195 |
16r0021 |
|
196 |
) |
|
197 |
" |
|
14208 | 198 |
|
199 |
"Modified: / 12-07-2012 / 19:56:12 / cg" |
|
8903 | 200 |
! |
201 |
||
202 |
encode:aCode |
|
12432 | 203 |
^ aCode |
8903 | 204 |
! |
205 |
||
21475 | 206 |
encodeCharacter:aUnicodeCharacter on:aStream |
207 |
"given a string in unicode, encode it onto aStream." |
|
208 |
||
209 |
aStream nextPutUtf16Bytes:aUnicodeCharacter MSB:true. |
|
210 |
||
211 |
"Created: / 16-02-2017 / 16:41:25 / stefan" |
|
212 |
! |
|
213 |
||
8903 | 214 |
encodeString:aUnicodeString |
215 |
"return the UTF-16 representation of a aUnicodeString. |
|
216 |
The resulting string is only useful to be stored on some external file, |
|
217 |
not for being used inside ST/X." |
|
218 |
||
219 |
|s| |
|
220 |
||
221 |
s := WriteStream on:(ByteArray uninitializedNew:aUnicodeString size). |
|
222 |
aUnicodeString do:[:eachCharacter | |
|
223 |
|codePoint t hi low| |
|
224 |
||
225 |
codePoint := eachCharacter codePoint. |
|
226 |
(codePoint <= 16rFFFF) ifTrue:[ |
|
227 |
((codePoint <= 16rD7FF) or:[ codePoint between:16rE000 and:16rFFFF]) ifTrue:[ |
|
228 |
self nextPutTwoByteValue:codePoint to:s. |
|
229 |
] ifFalse:[ |
|
230 |
"/ unrepresentable: D800..DFFFF |
|
231 |
self error:'unrepresentable value (D800..DFFFF) in utf16Encode'. |
|
232 |
]. |
|
233 |
] ifFalse:[ |
|
234 |
t := codePoint - 16r00010000. |
|
235 |
hi := t bitShift:-10. |
|
236 |
low := t bitAnd:16r3FF. |
|
237 |
hi > 16r3FF ifTrue:[ |
|
238 |
"/ unrepresentable: above 110000 |
|
239 |
self error:'unrepresentable value (> 10FFFF) in utf16Encode'. |
|
240 |
]. |
|
241 |
self nextPutTwoByteValue:(hi + 16rD800) to:s. |
|
242 |
self nextPutTwoByteValue:(low + 16rDC00) to:s. |
|
243 |
]. |
|
244 |
]. |
|
245 |
||
246 |
^ s contents |
|
247 |
||
248 |
" |
|
249 |
(self encodeString:'hello') #[0 104 0 101 0 108 0 108 0 111] |
|
250 |
(self encodeString:(Character value:16r40) asString) #[0 64] |
|
251 |
(self encodeString:(Character value:16rFF) asString) #[0 255] |
|
252 |
(self encodeString:(Character value:16r100) asString) #[1 0] |
|
253 |
(self encodeString:(Character value:16r1000) asString) #[16 0] |
|
254 |
(self encodeString:(Character value:16r2000) asString) #[32 0] |
|
255 |
(self encodeString:(Character value:16r4000) asString) #[64 0] |
|
256 |
(self encodeString:(Character value:16r8000) asString) #[128 0] |
|
257 |
(self encodeString:(Character value:16rD7FF) asString) #[215 255] |
|
258 |
(self encodeString:(Character value:16rE000) asString) #[224 0] |
|
259 |
(self encodeString:(Character value:16rFFFF) asString) #[255 255] |
|
260 |
(self encodeString:(Character value:16r10000) asString) #[216 64 220 0] |
|
261 |
(self encodeString:(Character value:16r10FFF) asString) #[216 67 223 255] |
|
262 |
(self encodeString:(Character value:16r1FFFF) asString) #[216 127 223 255] |
|
263 |
(self encodeString:(Character value:16r10FFFF) asString) #[219 255 223 255] |
|
264 |
error cases: |
|
265 |
(self encodeString:(Character value:16rD800) asString) |
|
266 |
(self encodeString:(Character value:16rD801) asString) |
|
267 |
(self encodeString:(Character value:16rDFFF) asString) |
|
268 |
(self encodeString:(Character value:16r110000) asString) |
|
269 |
" |
|
21475 | 270 |
! |
271 |
||
272 |
encodeString:aUnicodeString on:aStream |
|
273 |
"given a string in unicode, encode it onto aStream." |
|
274 |
||
275 |
aStream nextPutAllUtf16Bytes:aUnicodeString MSB:true. |
|
276 |
||
277 |
"Created: / 16-02-2017 / 16:40:32 / stefan" |
|
8903 | 278 |
! ! |
279 |
||
280 |
!ISO10646_to_UTF16BE methodsFor:'private'! |
|
281 |
||
282 |
nextPutTwoByteValue:anInteger to:aStream |
|
19621 | 283 |
aStream nextPutInt16MSB:anInteger |
8903 | 284 |
! |
285 |
||
286 |
nextTwoByteValueFrom:aStream |
|
19217 | 287 |
^ aStream nextUnsignedInt16MSB:true |
8903 | 288 |
! ! |
289 |
||
290 |
!ISO10646_to_UTF16BE methodsFor:'queries'! |
|
291 |
||
292 |
nameOfEncoding |
|
14170
eed0dbcc471c
comment/format in: #decodeString:
Stefan Vogel <sv@exept.de>
parents:
12432
diff
changeset
|
293 |
^ #utf16be |
8903 | 294 |
! ! |
295 |
||
296 |
!ISO10646_to_UTF16BE class methodsFor:'documentation'! |
|
297 |
||
298 |
version |
|
19217 | 299 |
^ '$Header$' |
12432 | 300 |
! |
301 |
||
302 |
version_CVS |
|
19217 | 303 |
^ '$Header$' |
8903 | 304 |
! ! |
19217 | 305 |