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