author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 20 Dec 2010 07:13:27 +0000 | |
branch | jv |
changeset 17814 | b75a7f0c346b |
parent 17807 | 06cc6c49e291 |
child 17834 | 04ff72c5039a |
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) |
|
42 |
" |
|
43 |
! |
|
44 |
||
45 |
examples |
|
46 |
" |
|
47 |
Encoding (unicode to utf16BE) |
|
48 |
ISO10646_to_UTF16BE encodeString:'hello'. |
|
49 |
||
50 |
||
51 |
Decoding (utf16BE to unicode): |
|
52 |
|t| |
|
53 |
||
54 |
t := ISO10646_to_UTF16BE encodeString:''. |
|
55 |
ISO10646_to_UTF16BE decodeString:t. |
|
9325 | 56 |
|
57 |
Decoding (utf16LE-Bytes to unicode): |
|
58 |
|bytes| |
|
59 |
||
60 |
bytes := #[ 16r40 0 16r41 0 16r42 0 16r43 0 16r44 0 ]. |
|
61 |
ISO10646_to_UTF16LE decodeString:bytes. |
|
8903 | 62 |
" |
63 |
! ! |
|
64 |
||
65 |
!ISO10646_to_UTF16BE methodsFor:'encoding & decoding'! |
|
66 |
||
67 |
decode:aCode |
|
17735 | 68 |
^ aCode |
8903 | 69 |
! |
70 |
||
71 |
decodeString:aStringOrByteCollection |
|
72 |
"given a byteArray (2-bytes per character) or unsignedShortArra in UTF16 encoding, |
|
73 |
return a new string containing the same characters, in 8, 16bit (or more) encoding. |
|
74 |
Returns either a normal String, a TwoByte- or a FourByte-String instance. |
|
75 |
Only useful, when reading from external sources. |
|
76 |
This only handles up-to 30bit characters." |
|
77 |
||
78 |
|sz nBitsRequired s newString idx bitsPerElementIn nextIn |
|
17735 | 79 |
codeIn codeIn1 codeIn2 estimatedSize out| |
8903 | 80 |
|
81 |
aStringOrByteCollection isByteArray ifTrue:[ |
|
82 |
bitsPerElementIn := 8. |
|
83 |
] ifFalse:[ |
|
84 |
aStringOrByteCollection isString ifTrue:[ |
|
85 |
bitsPerElementIn := aStringOrByteCollection bitsPerCharacter. |
|
86 |
] ifFalse:[ |
|
87 |
bitsPerElementIn := 16. |
|
88 |
]. |
|
89 |
]. |
|
17735 | 90 |
estimatedSize := aStringOrByteCollection size * bitsPerElementIn // 16. |
8903 | 91 |
|
92 |
bitsPerElementIn == 8 ifTrue:[ |
|
93 |
nextIn := [self nextTwoByteValueFrom:s]. |
|
94 |
] ifFalse:[ |
|
95 |
nextIn := [s next]. |
|
96 |
]. |
|
97 |
||
98 |
s := aStringOrByteCollection readStream. |
|
17735 | 99 |
out := CharacterWriteStream on:(String new:estimatedSize). |
8903 | 100 |
[s atEnd] whileFalse:[ |
101 |
codeIn := nextIn value. |
|
102 |
codeIn <= 16rFF ifTrue:[ |
|
103 |
] ifFalse:[ |
|
104 |
(codeIn between:16rD800 and:16rDBFF) ifTrue:[ |
|
105 |
codeIn1 := codeIn. |
|
106 |
codeIn2 := nextIn value. |
|
107 |
codeIn := ((codeIn1 - 16rD800) bitShift:10) |
|
108 |
+ |
|
109 |
(codeIn2 - 16rDC00) |
|
110 |
+ 16r00010000. |
|
111 |
]. |
|
112 |
]. |
|
17735 | 113 |
out nextPut:(Character value:codeIn). |
8903 | 114 |
]. |
17735 | 115 |
newString := out contents. |
116 |
||
117 |
"/ nBitsRequired := 8. |
|
118 |
"/ sz := 0. |
|
119 |
"/ [s atEnd] whileFalse:[ |
|
120 |
"/ codeIn := nextIn value. |
|
121 |
"/ sz := sz + 1. |
|
122 |
"/ |
|
123 |
"/ codeIn <= 16rFF ifTrue:[ |
|
124 |
"/ ] ifFalse:[ |
|
125 |
"/ nBitsRequired := nBitsRequired max:16. |
|
126 |
"/ (codeIn between:16rD800 and:16rDBFF) ifTrue:[ |
|
127 |
"/ nBitsRequired := 32. |
|
128 |
"/ codeIn2 := nextIn value. |
|
129 |
"/ ]. |
|
130 |
"/ ] |
|
131 |
"/ ]. |
|
132 |
"/ |
|
133 |
"/ nBitsRequired == 8 ifTrue:[ |
|
134 |
"/ newString := String uninitializedNew:sz |
|
135 |
"/ ] ifFalse:[ |
|
136 |
"/ nBitsRequired <= 16 ifTrue:[ |
|
137 |
"/ newString := Unicode16String new:sz |
|
138 |
"/ ] ifFalse:[ |
|
139 |
"/ newString := Unicode32String new:sz |
|
140 |
"/ ] |
|
141 |
"/ ]. |
|
142 |
"/ |
|
143 |
"/ s := aStringOrByteCollection readStream. |
|
144 |
"/ idx := 1. |
|
145 |
"/ [s atEnd] whileFalse:[ |
|
146 |
"/ codeIn := nextIn value. |
|
147 |
"/ codeIn <= 16rFF ifTrue:[ |
|
148 |
"/ ] ifFalse:[ |
|
149 |
"/ nBitsRequired := nBitsRequired max:16. |
|
150 |
"/ (codeIn between:16rD800 and:16rDBFF) ifTrue:[ |
|
151 |
"/ nBitsRequired := 32. |
|
152 |
"/ codeIn1 := codeIn. |
|
153 |
"/ codeIn2 := nextIn value. |
|
154 |
"/ codeIn := ((codeIn1 - 16rD800) bitShift:10) |
|
155 |
"/ + |
|
156 |
"/ (codeIn2 - 16rDC00) |
|
157 |
"/ + 16r00010000. |
|
158 |
"/ ]. |
|
159 |
"/ ]. |
|
160 |
"/ newString at:idx put:(Character value:codeIn). |
|
161 |
"/ idx := idx + 1. |
|
162 |
"/ ]. |
|
8903 | 163 |
^ newString |
164 |
||
165 |
" |
|
166 |
self new decodeString:#[ 16r00 16r42 ] |
|
167 |
self new decodeString:#[ 16r01 16r42 ] |
|
168 |
self new decodeString:#[ 16r00 16r48 |
|
169 |
16r00 16r69 |
|
170 |
16rD8 16r00 |
|
171 |
16rDC 16r00 |
|
172 |
16r00 16r21 |
|
173 |
16r00 16r21 |
|
174 |
] |
|
175 |
||
176 |
self new decodeString:#( 16r0048 |
|
177 |
16r0069 |
|
178 |
16rD800 |
|
179 |
16rDC00 |
|
180 |
16r0021 |
|
181 |
16r0021 |
|
182 |
) |
|
183 |
" |
|
184 |
! |
|
185 |
||
186 |
encode:aCode |
|
17735 | 187 |
^ aCode |
8903 | 188 |
! |
189 |
||
190 |
encodeString:aUnicodeString |
|
191 |
"return the UTF-16 representation of a aUnicodeString. |
|
192 |
The resulting string is only useful to be stored on some external file, |
|
193 |
not for being used inside ST/X." |
|
194 |
||
195 |
|s| |
|
196 |
||
197 |
s := WriteStream on:(ByteArray uninitializedNew:aUnicodeString size). |
|
198 |
aUnicodeString do:[:eachCharacter | |
|
199 |
|codePoint t hi low| |
|
200 |
||
201 |
codePoint := eachCharacter codePoint. |
|
202 |
(codePoint <= 16rFFFF) ifTrue:[ |
|
203 |
((codePoint <= 16rD7FF) or:[ codePoint between:16rE000 and:16rFFFF]) ifTrue:[ |
|
204 |
self nextPutTwoByteValue:codePoint to:s. |
|
205 |
] ifFalse:[ |
|
206 |
"/ unrepresentable: D800..DFFFF |
|
207 |
self error:'unrepresentable value (D800..DFFFF) in utf16Encode'. |
|
208 |
]. |
|
209 |
] ifFalse:[ |
|
210 |
t := codePoint - 16r00010000. |
|
211 |
hi := t bitShift:-10. |
|
212 |
low := t bitAnd:16r3FF. |
|
213 |
hi > 16r3FF ifTrue:[ |
|
214 |
"/ unrepresentable: above 110000 |
|
215 |
self error:'unrepresentable value (> 10FFFF) in utf16Encode'. |
|
216 |
]. |
|
217 |
self nextPutTwoByteValue:(hi + 16rD800) to:s. |
|
218 |
self nextPutTwoByteValue:(low + 16rDC00) to:s. |
|
219 |
]. |
|
220 |
]. |
|
221 |
||
222 |
^ s contents |
|
223 |
||
224 |
" |
|
225 |
(self encodeString:'hello') #[0 104 0 101 0 108 0 108 0 111] |
|
226 |
(self encodeString:(Character value:16r40) asString) #[0 64] |
|
227 |
(self encodeString:(Character value:16rFF) asString) #[0 255] |
|
228 |
(self encodeString:(Character value:16r100) asString) #[1 0] |
|
229 |
(self encodeString:(Character value:16r1000) asString) #[16 0] |
|
230 |
(self encodeString:(Character value:16r2000) asString) #[32 0] |
|
231 |
(self encodeString:(Character value:16r4000) asString) #[64 0] |
|
232 |
(self encodeString:(Character value:16r8000) asString) #[128 0] |
|
233 |
(self encodeString:(Character value:16rD7FF) asString) #[215 255] |
|
234 |
(self encodeString:(Character value:16rE000) asString) #[224 0] |
|
235 |
(self encodeString:(Character value:16rFFFF) asString) #[255 255] |
|
236 |
(self encodeString:(Character value:16r10000) asString) #[216 64 220 0] |
|
237 |
(self encodeString:(Character value:16r10FFF) asString) #[216 67 223 255] |
|
238 |
(self encodeString:(Character value:16r1FFFF) asString) #[216 127 223 255] |
|
239 |
(self encodeString:(Character value:16r10FFFF) asString) #[219 255 223 255] |
|
240 |
error cases: |
|
241 |
(self encodeString:(Character value:16rD800) asString) |
|
242 |
(self encodeString:(Character value:16rD801) asString) |
|
243 |
(self encodeString:(Character value:16rDFFF) asString) |
|
244 |
(self encodeString:(Character value:16r110000) asString) |
|
245 |
" |
|
246 |
! ! |
|
247 |
||
248 |
!ISO10646_to_UTF16BE methodsFor:'private'! |
|
249 |
||
250 |
nextPutTwoByteValue:anInteger to:aStream |
|
251 |
aStream nextPutShort:anInteger MSB:true |
|
252 |
! |
|
253 |
||
254 |
nextTwoByteValueFrom:aStream |
|
255 |
^ aStream nextUnsignedShortMSB:true |
|
256 |
! ! |
|
257 |
||
258 |
!ISO10646_to_UTF16BE methodsFor:'queries'! |
|
259 |
||
260 |
nameOfEncoding |
|
261 |
^ #'utf8be' |
|
262 |
! ! |
|
263 |
||
264 |
!ISO10646_to_UTF16BE class methodsFor:'documentation'! |
|
265 |
||
266 |
version |
|
17814 | 267 |
^ '$Id: CharacterEncoderImplementations__ISO10646_to_UTF16BE.st 10602 2010-12-20 07:13:27Z vranyj1 $' |
17735 | 268 |
! |
269 |
||
270 |
version_CVS |
|
17814 | 271 |
^ 'Header: /cvs/stx/stx/libbasic/Encoder_ISO10646_to_UTF16BE.st,v 1.3 2009/11/04 00:46:08 cg Exp ' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17735
diff
changeset
|
272 |
! |
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17735
diff
changeset
|
273 |
|
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17735
diff
changeset
|
274 |
version_SVN |
17814 | 275 |
^ '$Id: CharacterEncoderImplementations__ISO10646_to_UTF16BE.st 10602 2010-12-20 07:13:27Z vranyj1 $' |
8903 | 276 |
! ! |
17767 | 277 |
|
17772
6e0b3c06c364
Method reverted to r10532
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17767
diff
changeset
|
278 |
|
17780 | 279 |
|
17807 | 280 |
|
17814 | 281 |