author | Stefan Vogel <sv@exept.de> |
Wed, 08 May 2019 15:04:54 +0200 | |
changeset 24121 | d0148c842873 |
parent 22472 | 45940fc5e0ad |
child 24214 | 7853c5e56c17 |
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 |
||
22472 | 18 |
VariableBytesEncoder subclass:#ISO10646_to_UTF16BE |
8903 | 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 |
||
22472 | 64 |
t := ISO10646_to_UTF16BE encodeString:'ÄÖÜß'. |
8903 | 65 |
ISO10646_to_UTF16BE decodeString:t. |
9325 | 66 |
|
67 |
Decoding (utf16LE-Bytes to unicode): |
|
22472 | 68 |
ISO10646_to_UTF16LE decodeString:#[ 16r40 0 16r41 0 16r42 0 16r43 0 16r44 0 ]. |
69 |
ISO10646_to_UTF16BE decodeString:#[ 16r40 0 16r41 0 16r42 0 16r43 0 16r44 0 ] copy swapBytes. |
|
8903 | 70 |
" |
71 |
! ! |
|
72 |
||
73 |
!ISO10646_to_UTF16BE methodsFor:'encoding & decoding'! |
|
74 |
||
75 |
decodeString:aStringOrByteCollection |
|
14170
eed0dbcc471c
comment/format in: #decodeString:
Stefan Vogel <sv@exept.de>
parents:
12432
diff
changeset
|
76 |
"given a byteArray (2-bytes per character) or unsignedShortArray in UTF16 encoding, |
8903 | 77 |
return a new string containing the same characters, in 8, 16bit (or more) encoding. |
78 |
Returns either a normal String, a TwoByte- or a FourByte-String instance. |
|
79 |
Only useful, when reading from external sources. |
|
80 |
This only handles up-to 30bit characters." |
|
81 |
||
14170
eed0dbcc471c
comment/format in: #decodeString:
Stefan Vogel <sv@exept.de>
parents:
12432
diff
changeset
|
82 |
|s newString bitsPerElementIn nextIn |
12432 | 83 |
codeIn codeIn1 codeIn2 estimatedSize out| |
8903 | 84 |
|
85 |
aStringOrByteCollection isByteArray ifTrue:[ |
|
86 |
bitsPerElementIn := 8. |
|
87 |
] ifFalse:[ |
|
88 |
aStringOrByteCollection isString ifTrue:[ |
|
89 |
bitsPerElementIn := aStringOrByteCollection bitsPerCharacter. |
|
90 |
] ifFalse:[ |
|
14175
84e1adb65a5d
changed: #decodeString: detect odd number of bytes
Stefan Vogel <sv@exept.de>
parents:
14170
diff
changeset
|
91 |
"can be a ShortArray" |
8903 | 92 |
bitsPerElementIn := 16. |
93 |
]. |
|
94 |
]. |
|
95 |
||
14175
84e1adb65a5d
changed: #decodeString: detect odd number of bytes
Stefan Vogel <sv@exept.de>
parents:
14170
diff
changeset
|
96 |
s := aStringOrByteCollection readStream. |
8903 | 97 |
bitsPerElementIn == 8 ifTrue:[ |
14175
84e1adb65a5d
changed: #decodeString: detect odd number of bytes
Stefan Vogel <sv@exept.de>
parents:
14170
diff
changeset
|
98 |
s size odd ifTrue:[ |
14208 | 99 |
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
|
100 |
]. |
8903 | 101 |
nextIn := [self nextTwoByteValueFrom:s]. |
102 |
] ifFalse:[ |
|
103 |
nextIn := [s next]. |
|
104 |
]. |
|
105 |
||
14175
84e1adb65a5d
changed: #decodeString: detect odd number of bytes
Stefan Vogel <sv@exept.de>
parents:
14170
diff
changeset
|
106 |
estimatedSize := s size * bitsPerElementIn // 16. |
12432 | 107 |
out := CharacterWriteStream on:(String new:estimatedSize). |
8903 | 108 |
[s atEnd] whileFalse:[ |
109 |
codeIn := nextIn value. |
|
110 |
codeIn <= 16rFF ifTrue:[ |
|
111 |
] ifFalse:[ |
|
112 |
(codeIn between:16rD800 and:16rDBFF) ifTrue:[ |
|
113 |
codeIn1 := codeIn. |
|
114 |
codeIn2 := nextIn value. |
|
115 |
codeIn := ((codeIn1 - 16rD800) bitShift:10) |
|
116 |
+ |
|
117 |
(codeIn2 - 16rDC00) |
|
118 |
+ 16r00010000. |
|
119 |
]. |
|
120 |
]. |
|
12432 | 121 |
out nextPut:(Character value:codeIn). |
8903 | 122 |
]. |
12432 | 123 |
newString := out contents. |
124 |
||
125 |
"/ nBitsRequired := 8. |
|
126 |
"/ sz := 0. |
|
127 |
"/ [s atEnd] whileFalse:[ |
|
128 |
"/ codeIn := nextIn value. |
|
129 |
"/ sz := sz + 1. |
|
130 |
"/ |
|
131 |
"/ codeIn <= 16rFF ifTrue:[ |
|
132 |
"/ ] ifFalse:[ |
|
133 |
"/ nBitsRequired := nBitsRequired max:16. |
|
134 |
"/ (codeIn between:16rD800 and:16rDBFF) ifTrue:[ |
|
135 |
"/ nBitsRequired := 32. |
|
136 |
"/ codeIn2 := nextIn value. |
|
137 |
"/ ]. |
|
138 |
"/ ] |
|
139 |
"/ ]. |
|
140 |
"/ |
|
141 |
"/ nBitsRequired == 8 ifTrue:[ |
|
142 |
"/ newString := String uninitializedNew:sz |
|
143 |
"/ ] ifFalse:[ |
|
144 |
"/ nBitsRequired <= 16 ifTrue:[ |
|
145 |
"/ newString := Unicode16String new:sz |
|
146 |
"/ ] ifFalse:[ |
|
147 |
"/ newString := Unicode32String new:sz |
|
148 |
"/ ] |
|
149 |
"/ ]. |
|
150 |
"/ |
|
151 |
"/ s := aStringOrByteCollection readStream. |
|
152 |
"/ idx := 1. |
|
153 |
"/ [s atEnd] whileFalse:[ |
|
154 |
"/ codeIn := nextIn value. |
|
155 |
"/ codeIn <= 16rFF ifTrue:[ |
|
156 |
"/ ] ifFalse:[ |
|
157 |
"/ nBitsRequired := nBitsRequired max:16. |
|
158 |
"/ (codeIn between:16rD800 and:16rDBFF) ifTrue:[ |
|
159 |
"/ nBitsRequired := 32. |
|
160 |
"/ codeIn1 := codeIn. |
|
161 |
"/ codeIn2 := nextIn value. |
|
162 |
"/ codeIn := ((codeIn1 - 16rD800) bitShift:10) |
|
163 |
"/ + |
|
164 |
"/ (codeIn2 - 16rDC00) |
|
165 |
"/ + 16r00010000. |
|
166 |
"/ ]. |
|
167 |
"/ ]. |
|
168 |
"/ newString at:idx put:(Character value:codeIn). |
|
169 |
"/ idx := idx + 1. |
|
170 |
"/ ]. |
|
8903 | 171 |
^ newString |
172 |
||
173 |
" |
|
174 |
self new decodeString:#[ 16r00 16r42 ] |
|
175 |
self new decodeString:#[ 16r01 16r42 ] |
|
176 |
self new decodeString:#[ 16r00 16r48 |
|
177 |
16r00 16r69 |
|
178 |
16rD8 16r00 |
|
179 |
16rDC 16r00 |
|
180 |
16r00 16r21 |
|
181 |
16r00 16r21 |
|
182 |
] |
|
183 |
||
184 |
self new decodeString:#( 16r0048 |
|
185 |
16r0069 |
|
186 |
16rD800 |
|
187 |
16rDC00 |
|
188 |
16r0021 |
|
189 |
16r0021 |
|
190 |
) |
|
191 |
" |
|
14208 | 192 |
|
193 |
"Modified: / 12-07-2012 / 19:56:12 / cg" |
|
8903 | 194 |
! |
195 |
||
196 |
encode:aCode |
|
12432 | 197 |
^ aCode |
8903 | 198 |
! |
199 |
||
200 |
encodeString:aUnicodeString |
|
201 |
"return the UTF-16 representation of a aUnicodeString. |
|
202 |
The resulting string is only useful to be stored on some external file, |
|
203 |
not for being used inside ST/X." |
|
204 |
||
22472 | 205 |
|stream size "{ Class:SmallInteger }"| |
8903 | 206 |
|
22472 | 207 |
stream := WriteStream on:(ByteArray uninitializedNew:aUnicodeString size * 2). |
208 |
size := aUnicodeString size. |
|
209 |
||
210 |
1 to:size do:[:idx | |
|
211 |
stream nextPutUtf16Bytes:(aUnicodeString at:idx) MSB:true. |
|
8903 | 212 |
]. |
213 |
||
22472 | 214 |
^ stream contents |
8903 | 215 |
|
216 |
" |
|
217 |
(self encodeString:'hello') #[0 104 0 101 0 108 0 108 0 111] |
|
218 |
(self encodeString:(Character value:16r40) asString) #[0 64] |
|
219 |
(self encodeString:(Character value:16rFF) asString) #[0 255] |
|
220 |
(self encodeString:(Character value:16r100) asString) #[1 0] |
|
221 |
(self encodeString:(Character value:16r1000) asString) #[16 0] |
|
222 |
(self encodeString:(Character value:16r2000) asString) #[32 0] |
|
223 |
(self encodeString:(Character value:16r4000) asString) #[64 0] |
|
224 |
(self encodeString:(Character value:16r8000) asString) #[128 0] |
|
225 |
(self encodeString:(Character value:16rD7FF) asString) #[215 255] |
|
226 |
(self encodeString:(Character value:16rE000) asString) #[224 0] |
|
227 |
(self encodeString:(Character value:16rFFFF) asString) #[255 255] |
|
228 |
(self encodeString:(Character value:16r10000) asString) #[216 64 220 0] |
|
229 |
(self encodeString:(Character value:16r10FFF) asString) #[216 67 223 255] |
|
230 |
(self encodeString:(Character value:16r1FFFF) asString) #[216 127 223 255] |
|
231 |
(self encodeString:(Character value:16r10FFFF) asString) #[219 255 223 255] |
|
232 |
error cases: |
|
233 |
(self encodeString:(Character value:16rD800) asString) |
|
234 |
(self encodeString:(Character value:16rD801) asString) |
|
235 |
(self encodeString:(Character value:16rDFFF) asString) |
|
236 |
(self encodeString:(Character value:16r110000) asString) |
|
237 |
" |
|
22472 | 238 |
|
239 |
"Modified: / 16-01-2018 / 19:38:30 / stefan" |
|
240 |
! ! |
|
241 |
||
242 |
!ISO10646_to_UTF16BE methodsFor:'private'! |
|
243 |
||
244 |
nextTwoByteValueFrom:aStream |
|
245 |
^ aStream nextUnsignedInt16MSB:true |
|
246 |
! ! |
|
247 |
||
248 |
!ISO10646_to_UTF16BE methodsFor:'queries'! |
|
249 |
||
250 |
characterSize:charOrCodePoint |
|
251 |
"return the number of bytes required to encode codePoint" |
|
252 |
||
253 |
^ charOrCodePoint codePoint <= 16rFFFF ifTrue:[2] ifFalse:[4] |
|
254 |
||
255 |
"Created: / 16-01-2018 / 19:21:09 / stefan" |
|
256 |
! |
|
257 |
||
258 |
nameOfEncoding |
|
259 |
^ #utf16be |
|
260 |
! ! |
|
261 |
||
262 |
!ISO10646_to_UTF16BE methodsFor:'stream support'! |
|
263 |
||
264 |
encodeCharacter:aUnicodeCharacter on:aStream |
|
265 |
"given a string in unicode, encode it onto aStream." |
|
266 |
||
267 |
aStream nextPutUtf16Bytes:aUnicodeCharacter MSB:true. |
|
268 |
||
269 |
"Created: / 16-02-2017 / 16:41:25 / stefan" |
|
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 |
||
22472 | 280 |
readNextCharacterFrom:aStream |
281 |
|codeIn codeIn2| |
|
8903 | 282 |
|
22472 | 283 |
codeIn := self nextTwoByteValueFrom:aStream. |
284 |
codeIn isNil ifTrue:[ |
|
285 |
^ nil. |
|
286 |
]. |
|
287 |
(codeIn between:16rD800 and:16rDBFF) ifTrue:[ |
|
288 |
codeIn2 := self nextTwoByteValueFrom:aStream. |
|
289 |
codeIn2 isNil ifTrue:[ |
|
290 |
InvalidEncodingError raiseErrorString:' - UTF16 missing followBytes'. |
|
291 |
]. |
|
292 |
codeIn := ((codeIn - 16rD800) bitShift:10) |
|
293 |
+ (codeIn2 - 16rDC00) |
|
294 |
+ 16r00010000. |
|
295 |
]. |
|
8903 | 296 |
|
22472 | 297 |
^ Character codePoint:codeIn. |
298 |
||
299 |
"Created: / 16-01-2018 / 22:31:29 / stefan" |
|
300 |
"Modified: / 17-01-2018 / 14:41:31 / stefan" |
|
8903 | 301 |
! ! |
302 |
||
303 |
!ISO10646_to_UTF16BE class methodsFor:'documentation'! |
|
304 |
||
305 |
version |
|
19217 | 306 |
^ '$Header$' |
12432 | 307 |
! |
308 |
||
309 |
version_CVS |
|
19217 | 310 |
^ '$Header$' |
8903 | 311 |
! ! |
19217 | 312 |