author | Stefan Vogel <sv@exept.de> |
Wed, 10 Jan 2018 23:00:16 +0100 | |
changeset 22413 | b40bbf08ddd8 |
parent 22408 | 09eceae5d786 |
child 22429 | 48389a135c35 |
permissions | -rw-r--r-- |
8148 | 1 |
" |
2 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
8411 | 3 |
All Rights Reserved |
8148 | 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 |
" |
|
8081 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
14 |
"{ NameSpace: CharacterEncoderImplementations }" |
|
15 |
||
16 |
TwoByteEncoder subclass:#ISO10646_to_UTF8 |
|
17 |
instanceVariableNames:'' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Text-Encodings' |
|
21 |
! |
|
22 |
||
18604
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
23 |
ISO10646_to_UTF8 class instanceVariableNames:'theOneAndOnlyInstance' |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
24 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
25 |
" |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
26 |
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
|
27 |
" |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
28 |
! |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
29 |
|
8081 | 30 |
!ISO10646_to_UTF8 class methodsFor:'documentation'! |
31 |
||
8148 | 32 |
copyright |
33 |
" |
|
34 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
8411 | 35 |
All Rights Reserved |
8148 | 36 |
|
37 |
This software is furnished under a license and may be used |
|
38 |
only in accordance with the terms of that license and with the |
|
39 |
inclusion of the above copyright notice. This software may not |
|
40 |
be provided or otherwise made available to, or used by, any |
|
41 |
other person. No title to or ownership of the software is |
|
42 |
hereby transferred. |
|
43 |
" |
|
44 |
! |
|
45 |
||
21298 | 46 |
documentation |
47 |
" |
|
21299 | 48 |
I can encode characters into/from UTF8 |
49 |
||
21298 | 50 |
Notice the naming (many are confused): |
51 |
Unicode is the set of number-to-glyph assignments |
|
52 |
whereas: |
|
53 |
UTF8 is a concrete way of xmitting Unicode codePoints (numbers). |
|
54 |
UTF16 is another concrete encoding, for example. |
|
55 |
||
56 |
ST/X NEVER uses UTF8 internally - all characters are full 24bit characters. |
|
21301 | 57 |
Only when exchanging data, are these converted into UTF8 (or other) byte sequences. |
21298 | 58 |
" |
59 |
! |
|
60 |
||
8081 | 61 |
examples |
62 |
" |
|
63 |
Encoding (unicode to utf8) |
|
8411 | 64 |
ISO10646_to_UTF8 encodeString:'hello'. |
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
65 |
|
8081 | 66 |
|
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
67 |
Decoding (utf8 to unicode): |
8081 | 68 |
|t| |
69 |
||
22408 | 70 |
t := ISO10646_to_UTF8 encodeString:'Helloœ'. |
8411 | 71 |
ISO10646_to_UTF8 decodeString:t. |
8081 | 72 |
" |
73 |
! ! |
|
74 |
||
18604
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
75 |
!ISO10646_to_UTF8 class methodsFor:'instance creation'! |
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 |
flushSingleton |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
78 |
"flushes the cached singleton" |
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 := nil |
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 |
self flushSingleton |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
84 |
" |
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 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
87 |
new |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
88 |
"returns a singleton" |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
89 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
90 |
theOneAndOnlyInstance isNil ifTrue:[ |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
91 |
theOneAndOnlyInstance := self basicNew initialize. |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
92 |
]. |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
93 |
^ theOneAndOnlyInstance. |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
94 |
! |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
95 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
96 |
theOneAndOnlyInstance |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
97 |
"returns a singleton" |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
98 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
99 |
theOneAndOnlyInstance isNil ifTrue:[ |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
100 |
theOneAndOnlyInstance := self basicNew initialize. |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
101 |
]. |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
102 |
^ theOneAndOnlyInstance. |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
103 |
! ! |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
104 |
|
22408 | 105 |
!ISO10646_to_UTF8 class methodsFor:'queries'! |
106 |
||
107 |
bytesToReadFor:firstByte |
|
22413 | 108 |
(firstByte bitAnd:2r10000000) == 0 ifTrue:[^ 1]. |
22408 | 109 |
(firstByte bitAnd:2r11000000) == 2r10000000 ifTrue:[^ 2]. |
110 |
(firstByte bitAnd:2r11100000) == 2r11000000 ifTrue:[^ 3]. |
|
111 |
(firstByte bitAnd:2r11110000) == 2r11100000 ifTrue:[^ 4]. |
|
112 |
(firstByte bitAnd:2r11111000) == 2r11110000 ifTrue:[^ 5]. |
|
113 |
(firstByte bitAnd:2r11111100) == 2r11111000 ifTrue:[^ 6]. |
|
114 |
||
115 |
InvalidEncodingError raiseWith:firstByte errorString:' - unsupported utf8 encoding (too large, only 31bit supported)' |
|
116 |
||
117 |
"Created: / 14-06-2005 / 17:17:24 / janfrog" |
|
22413 | 118 |
"Modified: / 10-01-2018 / 22:59:20 / stefan" |
22408 | 119 |
! ! |
120 |
||
8081 | 121 |
!ISO10646_to_UTF8 methodsFor:'encoding & decoding'! |
122 |
||
123 |
decode:aCode |
|
22408 | 124 |
"given an integer in my encoding, return a unicode codePoint for it" |
125 |
||
8081 | 126 |
self shouldNotImplement "/ no single byte conversion possible |
22408 | 127 |
|
128 |
"Modified (comment): / 03-01-2018 / 23:15:37 / stefan" |
|
8081 | 129 |
! |
130 |
||
131 |
decodeString:aStringOrByteCollection |
|
132 |
"given a string in UTF8 encoding, |
|
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
133 |
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
|
134 |
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
|
135 |
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
|
136 |
other systems |
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
137 |
(ST/X never uses utf8 internally, but always uses strings of fully decoded unicode characters). |
19838 | 138 |
This only handles up-to 30bit characters." |
14172 | 139 |
|
19838 | 140 |
^ CharacterArray decodeFromUTF8:aStringOrByteCollection. |
8081 | 141 |
! |
142 |
||
143 |
encode:aCode |
|
22408 | 144 |
"given a codePoint in unicode, return a byte in my encoding for it" |
145 |
||
22413 | 146 |
self shouldNotImplement "/ no vonversion to a single byte is possible |
22408 | 147 |
|
22413 | 148 |
"Modified (comment): / 03-01-2018 / 23:13:58 / stefan" |
8081 | 149 |
! |
150 |
||
21477 | 151 |
encodeCharacter:aUnicodeCharacter on:aStream |
152 |
"given a character in unicode, encode it onto aStream." |
|
153 |
||
154 |
aStream nextPutUtf8:aUnicodeCharacter. |
|
155 |
||
156 |
"Created: / 16-02-2017 / 16:20:57 / stefan" |
|
157 |
! |
|
158 |
||
8081 | 159 |
encodeString:aUnicodeString |
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
160 |
"return the UTF-8 representation of a Unicode string. |
8081 | 161 |
The resulting string is only useful to be stored on some external file, |
19838 | 162 |
not for being used inside ST/X." |
8081 | 163 |
|
19838 | 164 |
^ aUnicodeString utf8Encoded. |
21477 | 165 |
! |
166 |
||
167 |
encodeString:aUnicodeString on:aStream |
|
168 |
"given a string in unicode, encode it onto aStream." |
|
169 |
||
170 |
aStream nextPutAllUtf8:aUnicodeString. |
|
171 |
||
172 |
"Created: / 16-02-2017 / 16:27:31 / stefan" |
|
8081 | 173 |
! ! |
174 |
||
8163 | 175 |
!ISO10646_to_UTF8 methodsFor:'queries'! |
176 |
||
22413 | 177 |
characterSize:charOrcodePoint |
11974 | 178 |
"return the number of bytes required to encode codePoint" |
179 |
||
22413 | 180 |
^ charOrcodePoint asCharacter utf8BytesPerCharacter. |
11974 | 181 |
|
182 |
"Created: / 15-06-2005 / 15:16:22 / janfrog" |
|
22413 | 183 |
"Modified: / 03-01-2018 / 23:05:59 / stefan" |
11974 | 184 |
! |
185 |
||
8163 | 186 |
nameOfEncoding |
14172 | 187 |
^ #utf8 |
8163 | 188 |
! ! |
189 |
||
11974 | 190 |
!ISO10646_to_UTF8 methodsFor:'stream support'! |
191 |
||
192 |
readNext:charactersToRead charactersFrom:stream |
|
22408 | 193 |
| s c cp hasUtf8| |
11996 | 194 |
|
22408 | 195 |
hasUtf8 := false. |
196 |
"stream may be both text or bytes" |
|
197 |
s := (stream contentsSpecies new:charactersToRead) writeStream. |
|
11974 | 198 |
charactersToRead timesRepeat:[ |
22408 | 199 |
c := stream next. |
200 |
s nextPut:c. |
|
201 |
cp := c codePoint. |
|
202 |
(cp bitTest:16r80) ifTrue:[ |
|
203 |
hasUtf8 := true. |
|
204 |
s nextPutAll:(stream next:(self class bytesToReadFor:cp)-1). |
|
205 |
]. |
|
11974 | 206 |
]. |
22408 | 207 |
hasUtf8 ifTrue:[ |
208 |
^ self decodeString:s contents. |
|
209 |
]. |
|
210 |
^ s contents asString |
|
11974 | 211 |
|
212 |
"Created: / 16-06-2005 / 11:45:14 / masca" |
|
22413 | 213 |
"Modified: / 10-01-2018 / 22:28:39 / stefan" |
11974 | 214 |
! |
215 |
||
216 |
readNextCharacterFrom:aStream |
|
22408 | 217 |
^ Character utf8DecodeFrom:aStream. |
11974 | 218 |
|
219 |
"Created: / 14-06-2005 / 17:03:59 / janfrog" |
|
22413 | 220 |
"Modified: / 10-01-2018 / 17:35:40 / stefan" |
11974 | 221 |
! ! |
222 |
||
8081 | 223 |
!ISO10646_to_UTF8 class methodsFor:'documentation'! |
224 |
||
225 |
version |
|
18601
00dc53dfe54d
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
17623
diff
changeset
|
226 |
^ '$Header$' |
21299 | 227 |
! |
228 |
||
229 |
version_CVS |
|
230 |
^ '$Header$' |
|
8081 | 231 |
! ! |
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
232 |