author | Patrik Svestka <patrik.svestka@gmail.com> |
Tue, 09 Apr 2019 11:34:04 +0200 | |
branch | jv |
changeset 24093 | 0f94f6c8c9d4 |
parent 23107 | 40173e082cbc |
child 25406 | eba3da836698 |
permissions | -rw-r--r-- |
8148 | 1 |
" |
2 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
23107 | 3 |
COPYRIGHT (c) 2015 Jan Vrany |
8411 | 4 |
All Rights Reserved |
8148 | 5 |
|
6 |
This software is furnished under a license and may be used |
|
7 |
only in accordance with the terms of that license and with the |
|
8 |
inclusion of the above copyright notice. This software may not |
|
9 |
be provided or otherwise made available to, or used by, any |
|
10 |
other person. No title to or ownership of the software is |
|
11 |
hereby transferred. |
|
12 |
" |
|
8081 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
15 |
"{ NameSpace: CharacterEncoderImplementations }" |
|
16 |
||
17 |
TwoByteEncoder subclass:#ISO10646_to_UTF8 |
|
18 |
instanceVariableNames:'' |
|
19 |
classVariableNames:'' |
|
20 |
poolDictionaries:'' |
|
21 |
category:'Collections-Text-Encodings' |
|
22 |
! |
|
23 |
||
18604
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
24 |
ISO10646_to_UTF8 class instanceVariableNames:'theOneAndOnlyInstance' |
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 |
" |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
27 |
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
|
28 |
" |
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 |
|
8081 | 31 |
!ISO10646_to_UTF8 class methodsFor:'documentation'! |
32 |
||
8148 | 33 |
copyright |
34 |
" |
|
35 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
23107 | 36 |
COPYRIGHT (c) 2015 Jan Vrany |
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 |
||
21387 | 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 |
|
8081 | 107 |
!ISO10646_to_UTF8 methodsFor:'encoding & decoding'! |
108 |
||
109 |
decode:aCode |
|
110 |
self shouldNotImplement "/ no single byte conversion possible |
|
111 |
! |
|
112 |
||
113 |
decodeString:aStringOrByteCollection |
|
114 |
"given a string in UTF8 encoding, |
|
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
115 |
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
|
116 |
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
|
117 |
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
|
118 |
other systems |
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
119 |
(ST/X never uses utf8 internally, but always uses strings of fully decoded unicode characters). |
19838 | 120 |
This only handles up-to 30bit characters." |
14172 | 121 |
|
19838 | 122 |
^ CharacterArray decodeFromUTF8:aStringOrByteCollection. |
8081 | 123 |
! |
124 |
||
125 |
encode:aCode |
|
126 |
self shouldNotImplement "/ no single byte conversion possible |
|
127 |
! |
|
128 |
||
129 |
encodeString:aUnicodeString |
|
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
130 |
"return the UTF-8 representation of a Unicode string. |
8081 | 131 |
The resulting string is only useful to be stored on some external file, |
19838 | 132 |
not for being used inside ST/X." |
8081 | 133 |
|
19838 | 134 |
^ aUnicodeString utf8Encoded. |
8081 | 135 |
! ! |
136 |
||
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
137 |
!ISO10646_to_UTF8 methodsFor:'queries'! |
11974 | 138 |
|
139 |
bytesToReadFor:firstByte |
|
140 |
|bytesToRead| |
|
141 |
||
142 |
bytesToRead := 1. |
|
143 |
(firstByte isBitSet:8) ifFalse:[^1]. |
|
144 |
7 downTo:3 |
|
145 |
do:[:idx | |
|
146 |
(firstByte isBitSet:idx) ifTrue:[ |
|
147 |
bytesToRead := bytesToRead + 1 |
|
148 |
] ifFalse:[ |
|
149 |
^bytesToRead |
|
150 |
] |
|
151 |
]. |
|
152 |
^bytesToRead |
|
153 |
||
154 |
"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
|
155 |
! |
8163 | 156 |
|
11974 | 157 |
characterSize:charOrcodePoint |
158 |
"return the number of bytes required to encode codePoint" |
|
159 |
||
160 |
"Taken from RFC 3629" |
|
161 |
||
162 |
(charOrcodePoint asInteger between:16r00000000 and:16r0000007F) ifTrue:[^1]. |
|
163 |
(charOrcodePoint asInteger between:16r00000080 and:16r000007FF) ifTrue:[^2]. |
|
164 |
(charOrcodePoint asInteger between:16r00000800 and:16r0000FFFF) ifTrue:[^3]. |
|
165 |
(charOrcodePoint asInteger between:16r00010000 and:16r0010FFFF) ifTrue:[^4]. |
|
166 |
||
167 |
^self error:'Invalid codePoint' |
|
168 |
||
169 |
"Created: / 15-06-2005 / 15:16:22 / janfrog" |
|
170 |
! |
|
171 |
||
8163 | 172 |
nameOfEncoding |
14172 | 173 |
^ #utf8 |
8163 | 174 |
! ! |
175 |
||
11974 | 176 |
!ISO10646_to_UTF8 methodsFor:'stream support'! |
177 |
||
178 |
readNext:charactersToRead charactersFrom:stream |
|
11996 | 179 |
|
11974 | 180 |
| s | |
181 |
||
182 |
s := (String new:charactersToRead) writeStream. |
|
183 |
charactersToRead timesRepeat:[ |
|
184 |
| c | |
|
185 |
c := stream peek. |
|
186 |
s nextPutAll:(stream next:(self bytesToReadFor:c)) |
|
187 |
]. |
|
188 |
^ self decodeString:s contents |
|
189 |
||
190 |
"Created: / 16-06-2005 / 11:45:14 / masca" |
|
191 |
! |
|
192 |
||
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
193 |
readNextCharacterFrom:stream |
11974 | 194 |
|
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
195 |
| c bytesYetToRead s | |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
196 |
c := stream peek. |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
197 |
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
|
198 |
bytesYetToRead == 1 ifTrue:[ |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
199 |
stream next. |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
200 |
^ c. |
11974 | 201 |
]. |
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
202 |
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
|
203 |
s nextPutAll:(stream next: bytesYetToRead). |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
204 |
^ self decodeString:s contents |
11974 | 205 |
|
206 |
"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
|
207 |
"Modified: / 03-10-2015 / 08:49:09 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
11974 | 208 |
! ! |
209 |
||
8081 | 210 |
!ISO10646_to_UTF8 class methodsFor:'documentation'! |
211 |
||
212 |
version |
|
18601
00dc53dfe54d
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
17623
diff
changeset
|
213 |
^ '$Header$' |
21299 | 214 |
! |
215 |
||
216 |
version_CVS |
|
217 |
^ '$Header$' |
|
21387 | 218 |
! |
219 |
||
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
220 |
version_HG |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
221 |
|
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
222 |
^ '$Changeset: <not expanded> $' |
8081 | 223 |
! ! |
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
224 |