author | Jan Vrany <jan.vrany@labware.com> |
Tue, 01 Jun 2021 20:19:13 +0100 | |
branch | jv |
changeset 25424 | 51bd8a6b196f |
parent 25406 | eba3da836698 |
permissions | -rw-r--r-- |
8148 | 1 |
" |
2 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
23107 | 3 |
COPYRIGHT (c) 2015 Jan Vrany |
25406
eba3da836698
Fix 16 years old bug in `EncodedStream >> #next` for UTF8 to Unicode decoder
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4 |
COPYRIGHT (c) 2021 LabWare |
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 |
|
23107 | 37 |
COPYRIGHT (c) 2015 Jan Vrany |
25406
eba3da836698
Fix 16 years old bug in `EncodedStream >> #next` for UTF8 to Unicode decoder
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
38 |
COPYRIGHT (c) 2021 LabWare |
8411 | 39 |
All Rights Reserved |
8148 | 40 |
|
41 |
This software is furnished under a license and may be used |
|
42 |
only in accordance with the terms of that license and with the |
|
43 |
inclusion of the above copyright notice. This software may not |
|
44 |
be provided or otherwise made available to, or used by, any |
|
45 |
other person. No title to or ownership of the software is |
|
46 |
hereby transferred. |
|
47 |
" |
|
48 |
! |
|
49 |
||
21298 | 50 |
documentation |
51 |
" |
|
21299 | 52 |
I can encode characters into/from UTF8 |
53 |
||
21298 | 54 |
Notice the naming (many are confused): |
55 |
Unicode is the set of number-to-glyph assignments |
|
56 |
whereas: |
|
57 |
UTF8 is a concrete way of xmitting Unicode codePoints (numbers). |
|
58 |
UTF16 is another concrete encoding, for example. |
|
59 |
||
60 |
ST/X NEVER uses UTF8 internally - all characters are full 24bit characters. |
|
21301 | 61 |
Only when exchanging data, are these converted into UTF8 (or other) byte sequences. |
21298 | 62 |
" |
63 |
! |
|
64 |
||
8081 | 65 |
examples |
66 |
" |
|
67 |
Encoding (unicode to utf8) |
|
8411 | 68 |
ISO10646_to_UTF8 encodeString:'hello'. |
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
69 |
|
8081 | 70 |
|
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
71 |
Decoding (utf8 to unicode): |
8081 | 72 |
|t| |
73 |
||
21387 | 74 |
t := ISO10646_to_UTF8 encodeString:'Hello'. |
8411 | 75 |
ISO10646_to_UTF8 decodeString:t. |
8081 | 76 |
" |
77 |
! ! |
|
78 |
||
18604
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
79 |
!ISO10646_to_UTF8 class methodsFor:'instance creation'! |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
80 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
81 |
flushSingleton |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
82 |
"flushes the cached singleton" |
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 |
theOneAndOnlyInstance := nil |
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 |
self flushSingleton |
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 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
91 |
new |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
92 |
"returns a singleton" |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
93 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
94 |
theOneAndOnlyInstance isNil ifTrue:[ |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
95 |
theOneAndOnlyInstance := self basicNew initialize. |
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 |
^ theOneAndOnlyInstance. |
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 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
100 |
theOneAndOnlyInstance |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
101 |
"returns a singleton" |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
102 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
103 |
theOneAndOnlyInstance isNil ifTrue:[ |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
104 |
theOneAndOnlyInstance := self basicNew initialize. |
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 |
^ theOneAndOnlyInstance. |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
107 |
! ! |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
108 |
|
8081 | 109 |
!ISO10646_to_UTF8 methodsFor:'encoding & decoding'! |
110 |
||
111 |
decode:aCode |
|
112 |
self shouldNotImplement "/ no single byte conversion possible |
|
113 |
! |
|
114 |
||
115 |
decodeString:aStringOrByteCollection |
|
116 |
"given a string in UTF8 encoding, |
|
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
117 |
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
|
118 |
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
|
119 |
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
|
120 |
other systems |
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
121 |
(ST/X never uses utf8 internally, but always uses strings of fully decoded unicode characters). |
19838 | 122 |
This only handles up-to 30bit characters." |
14172 | 123 |
|
19838 | 124 |
^ CharacterArray decodeFromUTF8:aStringOrByteCollection. |
8081 | 125 |
! |
126 |
||
127 |
encode:aCode |
|
128 |
self shouldNotImplement "/ no single byte conversion possible |
|
129 |
! |
|
130 |
||
131 |
encodeString:aUnicodeString |
|
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
132 |
"return the UTF-8 representation of a Unicode string. |
8081 | 133 |
The resulting string is only useful to be stored on some external file, |
19838 | 134 |
not for being used inside ST/X." |
8081 | 135 |
|
19838 | 136 |
^ aUnicodeString utf8Encoded. |
8081 | 137 |
! ! |
138 |
||
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
139 |
!ISO10646_to_UTF8 methodsFor:'queries'! |
11974 | 140 |
|
141 |
bytesToReadFor:firstByte |
|
142 |
|bytesToRead| |
|
143 |
||
144 |
bytesToRead := 1. |
|
145 |
(firstByte isBitSet:8) ifFalse:[^1]. |
|
146 |
7 downTo:3 |
|
147 |
do:[:idx | |
|
148 |
(firstByte isBitSet:idx) ifTrue:[ |
|
149 |
bytesToRead := bytesToRead + 1 |
|
150 |
] ifFalse:[ |
|
151 |
^bytesToRead |
|
152 |
] |
|
153 |
]. |
|
154 |
^bytesToRead |
|
155 |
||
156 |
"Created: / 14-06-2005 / 17:17:24 / janfrog" |
|
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
157 |
! |
8163 | 158 |
|
11974 | 159 |
characterSize:charOrcodePoint |
160 |
"return the number of bytes required to encode codePoint" |
|
161 |
||
162 |
"Taken from RFC 3629" |
|
163 |
||
164 |
(charOrcodePoint asInteger between:16r00000000 and:16r0000007F) ifTrue:[^1]. |
|
165 |
(charOrcodePoint asInteger between:16r00000080 and:16r000007FF) ifTrue:[^2]. |
|
166 |
(charOrcodePoint asInteger between:16r00000800 and:16r0000FFFF) ifTrue:[^3]. |
|
167 |
(charOrcodePoint asInteger between:16r00010000 and:16r0010FFFF) ifTrue:[^4]. |
|
168 |
||
169 |
^self error:'Invalid codePoint' |
|
170 |
||
171 |
"Created: / 15-06-2005 / 15:16:22 / janfrog" |
|
172 |
! |
|
173 |
||
8163 | 174 |
nameOfEncoding |
14172 | 175 |
^ #utf8 |
8163 | 176 |
! ! |
177 |
||
11974 | 178 |
!ISO10646_to_UTF8 methodsFor:'stream support'! |
179 |
||
180 |
readNext:charactersToRead charactersFrom:stream |
|
11996 | 181 |
|
11974 | 182 |
| s | |
183 |
||
184 |
s := (String new:charactersToRead) writeStream. |
|
185 |
charactersToRead timesRepeat:[ |
|
186 |
| c | |
|
187 |
c := stream peek. |
|
188 |
s nextPutAll:(stream next:(self bytesToReadFor:c)) |
|
189 |
]. |
|
190 |
^ self decodeString:s contents |
|
191 |
||
192 |
"Created: / 16-06-2005 / 11:45:14 / masca" |
|
193 |
! |
|
194 |
||
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
195 |
readNextCharacterFrom:stream |
11974 | 196 |
|
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
197 |
| c bytesYetToRead s | |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
198 |
c := stream peek. |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
199 |
bytesYetToRead := self bytesToReadFor:c codePoint. |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
200 |
bytesYetToRead == 1 ifTrue:[ |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
201 |
stream next. |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
202 |
^ c. |
11974 | 203 |
]. |
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
204 |
s := (String new:1 + bytesYetToRead) writeStream. |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
205 |
s nextPutAll:(stream next: bytesYetToRead). |
25406
eba3da836698
Fix 16 years old bug in `EncodedStream >> #next` for UTF8 to Unicode decoder
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
206 |
|
eba3da836698
Fix 16 years old bug in `EncodedStream >> #next` for UTF8 to Unicode decoder
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
207 |
s := self decodeString:s contents. |
eba3da836698
Fix 16 years old bug in `EncodedStream >> #next` for UTF8 to Unicode decoder
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
208 |
self assert: s size == 1. |
eba3da836698
Fix 16 years old bug in `EncodedStream >> #next` for UTF8 to Unicode decoder
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
209 |
^ s first |
11974 | 210 |
|
211 |
"Created: / 14-06-2005 / 17:03:59 / janfrog" |
|
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
212 |
"Modified: / 03-10-2015 / 08:49:09 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
25406
eba3da836698
Fix 16 years old bug in `EncodedStream >> #next` for UTF8 to Unicode decoder
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
213 |
"Modified: / 29-01-2021 / 09:21:26 / Jan Vrany <jan.vrany@labware.com>" |
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$' |
|
21387 | 224 |
! |
225 |
||
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
226 |
version_HG |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
227 |
|
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
228 |
^ '$Changeset: <not expanded> $' |
8081 | 229 |
! ! |
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
230 |