author | Claus Gittinger <cg@exept.de> |
Mon, 16 Dec 2019 11:42:04 +0100 | |
changeset 25127 | 22ff3c08a076 |
parent 22474 | f42c97c037ed |
child 25271 | 3b763ce09c7e |
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 |
||
22474 | 18 |
VariableBytesEncoder subclass:#ISO10646_to_UTF8 |
8081 | 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 |
" |
|
22474 | 50 |
I can encode unicode characters into utf-8 and |
51 |
decode utf-8 characters into unicode. |
|
21299 | 52 |
|
21298 | 53 |
Notice the naming (many are confused): |
54 |
Unicode is the set of number-to-glyph assignments |
|
55 |
whereas: |
|
56 |
UTF8 is a concrete way of xmitting Unicode codePoints (numbers). |
|
57 |
UTF16 is another concrete encoding, for example. |
|
58 |
||
59 |
ST/X NEVER uses UTF8 internally - all characters are full 24bit characters. |
|
21301 | 60 |
Only when exchanging data, are these converted into UTF8 (or other) byte sequences. |
21298 | 61 |
" |
62 |
! |
|
63 |
||
8081 | 64 |
examples |
65 |
" |
|
66 |
Encoding (unicode to utf8) |
|
8411 | 67 |
ISO10646_to_UTF8 encodeString:'hello'. |
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
68 |
|
8081 | 69 |
|
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
70 |
Decoding (utf8 to unicode): |
8081 | 71 |
|t| |
72 |
||
22429 | 73 |
t := ISO10646_to_UTF8 encodeString:'Helloœ'. |
8411 | 74 |
ISO10646_to_UTF8 decodeString:t. |
8081 | 75 |
" |
76 |
! ! |
|
77 |
||
18604
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
78 |
!ISO10646_to_UTF8 class methodsFor:'instance creation'! |
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 |
flushSingleton |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
81 |
"flushes the cached singleton" |
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 |
theOneAndOnlyInstance := nil |
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 |
self flushSingleton |
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 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
90 |
new |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
91 |
"returns a singleton" |
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 isNil ifTrue:[ |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
94 |
theOneAndOnlyInstance := self basicNew initialize. |
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 |
! |
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 |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
100 |
"returns a singleton" |
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 isNil ifTrue:[ |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
103 |
theOneAndOnlyInstance := self basicNew initialize. |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
104 |
]. |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
105 |
^ theOneAndOnlyInstance. |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
106 |
! ! |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
107 |
|
22408 | 108 |
!ISO10646_to_UTF8 class methodsFor:'queries'! |
109 |
||
110 |
bytesToReadFor:firstByte |
|
22413 | 111 |
(firstByte bitAnd:2r10000000) == 0 ifTrue:[^ 1]. |
22408 | 112 |
(firstByte bitAnd:2r11000000) == 2r10000000 ifTrue:[^ 2]. |
113 |
(firstByte bitAnd:2r11100000) == 2r11000000 ifTrue:[^ 3]. |
|
114 |
(firstByte bitAnd:2r11110000) == 2r11100000 ifTrue:[^ 4]. |
|
115 |
(firstByte bitAnd:2r11111000) == 2r11110000 ifTrue:[^ 5]. |
|
116 |
(firstByte bitAnd:2r11111100) == 2r11111000 ifTrue:[^ 6]. |
|
117 |
||
118 |
InvalidEncodingError raiseWith:firstByte errorString:' - unsupported utf8 encoding (too large, only 31bit supported)' |
|
119 |
||
120 |
"Created: / 14-06-2005 / 17:17:24 / janfrog" |
|
22413 | 121 |
"Modified: / 10-01-2018 / 22:59:20 / stefan" |
22408 | 122 |
! ! |
123 |
||
8081 | 124 |
!ISO10646_to_UTF8 methodsFor:'encoding & decoding'! |
125 |
||
126 |
decodeString:aStringOrByteCollection |
|
127 |
"given a string in UTF8 encoding, |
|
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
128 |
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
|
129 |
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
|
130 |
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
|
131 |
other systems |
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
132 |
(ST/X never uses utf8 internally, but always uses strings of fully decoded unicode characters). |
19838 | 133 |
This only handles up-to 30bit characters." |
14172 | 134 |
|
19838 | 135 |
^ CharacterArray decodeFromUTF8:aStringOrByteCollection. |
8081 | 136 |
! |
137 |
||
138 |
encodeString:aUnicodeString |
|
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
139 |
"return the UTF-8 representation of a Unicode string. |
8081 | 140 |
The resulting string is only useful to be stored on some external file, |
19838 | 141 |
not for being used inside ST/X." |
8081 | 142 |
|
19838 | 143 |
^ aUnicodeString utf8Encoded. |
8081 | 144 |
! ! |
145 |
||
8163 | 146 |
!ISO10646_to_UTF8 methodsFor:'queries'! |
147 |
||
22429 | 148 |
characterSize:charOrCodePoint |
11974 | 149 |
"return the number of bytes required to encode codePoint" |
150 |
||
22429 | 151 |
^ charOrCodePoint asCharacter utf8BytesPerCharacter. |
11974 | 152 |
|
153 |
"Created: / 15-06-2005 / 15:16:22 / janfrog" |
|
22413 | 154 |
"Modified: / 03-01-2018 / 23:05:59 / stefan" |
11974 | 155 |
! |
156 |
||
8163 | 157 |
nameOfEncoding |
14172 | 158 |
^ #utf8 |
8163 | 159 |
! ! |
160 |
||
11974 | 161 |
!ISO10646_to_UTF8 methodsFor:'stream support'! |
162 |
||
22474 | 163 |
encodeCharacter:aUnicodeCharacter on:aStream |
164 |
"given a character in unicode, encode it onto aStream." |
|
165 |
||
166 |
aStream nextPutUtf8:aUnicodeCharacter. |
|
167 |
||
168 |
"Created: / 16-02-2017 / 16:20:57 / stefan" |
|
169 |
! |
|
170 |
||
171 |
encodeString:aUnicodeString on:aStream |
|
172 |
"given a string in unicode, encode it onto aStream." |
|
11996 | 173 |
|
22474 | 174 |
aStream nextPutAllUtf8:aUnicodeString. |
175 |
||
176 |
"Created: / 16-02-2017 / 16:27:31 / stefan" |
|
177 |
! |
|
178 |
||
179 |
readNext:charactersToReadArg charactersFrom:aStream |
|
180 |
"decode the next charactersToRead on aStream from utf-8 to unicode" |
|
181 |
||
182 |
|s c cp hasUtf8 charactersToRead "{ Class:SmallInteger }"| |
|
183 |
||
184 |
charactersToRead := charactersToReadArg. |
|
22408 | 185 |
hasUtf8 := false. |
186 |
"stream may be both text or bytes" |
|
22474 | 187 |
s := (aStream contentsSpecies new:charactersToRead) writeStream. |
11974 | 188 |
charactersToRead timesRepeat:[ |
22474 | 189 |
c := aStream next. |
22408 | 190 |
s nextPut:c. |
191 |
cp := c codePoint. |
|
192 |
(cp bitTest:16r80) ifTrue:[ |
|
193 |
hasUtf8 := true. |
|
22474 | 194 |
s nextPutAll:(aStream next:(self class bytesToReadFor:cp)-1). |
22408 | 195 |
]. |
11974 | 196 |
]. |
22408 | 197 |
hasUtf8 ifTrue:[ |
198 |
^ self decodeString:s contents. |
|
199 |
]. |
|
200 |
^ s contents asString |
|
11974 | 201 |
|
202 |
"Created: / 16-06-2005 / 11:45:14 / masca" |
|
22474 | 203 |
"Modified (comment): / 17-01-2018 / 13:24:42 / stefan" |
11974 | 204 |
! |
205 |
||
22474 | 206 |
readNextCharacterFrom:aStream |
207 |
"decode the next character or byte on aStream from utf-8 to unicode" |
|
208 |
||
22408 | 209 |
^ Character utf8DecodeFrom:aStream. |
11974 | 210 |
|
211 |
"Created: / 14-06-2005 / 17:03:59 / janfrog" |
|
22413 | 212 |
"Modified: / 10-01-2018 / 17:35:40 / stefan" |
22474 | 213 |
"Modified (comment): / 17-01-2018 / 13:24:08 / stefan" |
11974 | 214 |
! ! |
215 |
||
8081 | 216 |
!ISO10646_to_UTF8 class methodsFor:'documentation'! |
217 |
||
218 |
version |
|
18601
00dc53dfe54d
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
17623
diff
changeset
|
219 |
^ '$Header$' |
21299 | 220 |
! |
221 |
||
222 |
version_CVS |
|
223 |
^ '$Header$' |
|
8081 | 224 |
! ! |
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
225 |