author | Claus Gittinger <cg@exept.de> |
Fri, 17 Feb 2017 10:25:31 +0100 | |
changeset 21480 | 20b4ddb4ba7a |
parent 21477 | 3a69148be78e |
child 22408 | 09eceae5d786 |
permissions | -rw-r--r-- |
21477 | 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 |
||
21477 | 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 |
||
21477 | 129 |
encodeCharacter:aUnicodeCharacter on:aStream |
130 |
"given a character in unicode, encode it onto aStream." |
|
131 |
||
132 |
aStream nextPutUtf8:aUnicodeCharacter. |
|
133 |
||
134 |
"Created: / 16-02-2017 / 16:20:57 / stefan" |
|
135 |
! |
|
136 |
||
8081 | 137 |
encodeString:aUnicodeString |
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
138 |
"return the UTF-8 representation of a Unicode string. |
8081 | 139 |
The resulting string is only useful to be stored on some external file, |
19838 | 140 |
not for being used inside ST/X." |
8081 | 141 |
|
19838 | 142 |
^ aUnicodeString utf8Encoded. |
21477 | 143 |
! |
144 |
||
145 |
encodeString:aUnicodeString on:aStream |
|
146 |
"given a string in unicode, encode it onto aStream." |
|
147 |
||
148 |
aStream nextPutAllUtf8:aUnicodeString. |
|
149 |
||
150 |
"Created: / 16-02-2017 / 16:27:31 / stefan" |
|
8081 | 151 |
! ! |
152 |
||
11974 | 153 |
!ISO10646_to_UTF8 privateMethodsFor:'queries'! |
154 |
||
155 |
bytesToReadFor:firstByte |
|
156 |
|bytesToRead| |
|
157 |
||
158 |
bytesToRead := 1. |
|
159 |
(firstByte isBitSet:8) ifFalse:[^1]. |
|
160 |
7 downTo:3 |
|
161 |
do:[:idx | |
|
162 |
(firstByte isBitSet:idx) ifTrue:[ |
|
163 |
bytesToRead := bytesToRead + 1 |
|
164 |
] ifFalse:[ |
|
165 |
^bytesToRead |
|
166 |
] |
|
167 |
]. |
|
168 |
^bytesToRead |
|
169 |
||
170 |
"Created: / 14-06-2005 / 17:17:24 / janfrog" |
|
171 |
! ! |
|
172 |
||
8163 | 173 |
!ISO10646_to_UTF8 methodsFor:'queries'! |
174 |
||
11974 | 175 |
characterSize:charOrcodePoint |
176 |
"return the number of bytes required to encode codePoint" |
|
177 |
||
178 |
"Taken from RFC 3629" |
|
179 |
||
180 |
(charOrcodePoint asInteger between:16r00000000 and:16r0000007F) ifTrue:[^1]. |
|
181 |
(charOrcodePoint asInteger between:16r00000080 and:16r000007FF) ifTrue:[^2]. |
|
182 |
(charOrcodePoint asInteger between:16r00000800 and:16r0000FFFF) ifTrue:[^3]. |
|
183 |
(charOrcodePoint asInteger between:16r00010000 and:16r0010FFFF) ifTrue:[^4]. |
|
184 |
||
185 |
^self error:'Invalid codePoint' |
|
186 |
||
187 |
"Created: / 15-06-2005 / 15:16:22 / janfrog" |
|
188 |
! |
|
189 |
||
8163 | 190 |
nameOfEncoding |
14172 | 191 |
^ #utf8 |
8163 | 192 |
! ! |
193 |
||
11974 | 194 |
!ISO10646_to_UTF8 methodsFor:'stream support'! |
195 |
||
196 |
readNext:charactersToRead charactersFrom:stream |
|
11996 | 197 |
|
11974 | 198 |
| s | |
199 |
||
200 |
s := (String new:charactersToRead) writeStream. |
|
201 |
charactersToRead timesRepeat:[ |
|
202 |
| c | |
|
203 |
c := stream peek. |
|
204 |
s nextPutAll:(stream next:(self bytesToReadFor:c)) |
|
205 |
]. |
|
206 |
^ self decodeString:s contents |
|
207 |
||
208 |
"Created: / 16-06-2005 / 11:45:14 / masca" |
|
209 |
! |
|
210 |
||
211 |
readNextCharacterFrom:aStream |
|
11996 | 212 |
|
11974 | 213 |
|firstByte bytesToRead str| |
214 |
||
215 |
firstByte := aStream peek. |
|
216 |
firstByte ifNil:[^nil]. |
|
217 |
firstByte := firstByte asInteger. |
|
218 |
bytesToRead := self bytesToReadFor:firstByte. |
|
219 |
str := self decodeString:(aStream next:bytesToRead). |
|
220 |
str size > 1 ifTrue:[ |
|
221 |
self error:'Badly coded method' |
|
222 |
]. |
|
223 |
^ str first |
|
224 |
||
225 |
"Created: / 14-06-2005 / 17:03:59 / janfrog" |
|
226 |
! ! |
|
227 |
||
8081 | 228 |
!ISO10646_to_UTF8 class methodsFor:'documentation'! |
229 |
||
230 |
version |
|
18601
00dc53dfe54d
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
17623
diff
changeset
|
231 |
^ '$Header$' |
21299 | 232 |
! |
233 |
||
234 |
version_CVS |
|
235 |
^ '$Header$' |
|
8081 | 236 |
! ! |
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
237 |