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