author | Claus Gittinger <cg@exept.de> |
Thu, 26 Jan 2017 12:48:09 +0100 | |
changeset 21299 | 819caa3926a2 |
parent 21298 | cb1ce1924d13 |
child 21301 | f33ff66e5fff |
permissions | -rw-r--r-- |
8148 | 1 |
" |
2 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
8411 | 3 |
All Rights Reserved |
8148 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
8081 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
14 |
"{ NameSpace: CharacterEncoderImplementations }" |
|
15 |
||
16 |
TwoByteEncoder subclass:#ISO10646_to_UTF8 |
|
17 |
instanceVariableNames:'' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Text-Encodings' |
|
21 |
! |
|
22 |
||
18604
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
23 |
ISO10646_to_UTF8 class instanceVariableNames:'theOneAndOnlyInstance' |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
24 |
|
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 |
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
|
27 |
" |
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 |
|
8081 | 30 |
!ISO10646_to_UTF8 class methodsFor:'documentation'! |
31 |
||
8148 | 32 |
copyright |
33 |
" |
|
34 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
8411 | 35 |
All Rights Reserved |
8148 | 36 |
|
37 |
This software is furnished under a license and may be used |
|
38 |
only in accordance with the terms of that license and with the |
|
39 |
inclusion of the above copyright notice. This software may not |
|
40 |
be provided or otherwise made available to, or used by, any |
|
41 |
other person. No title to or ownership of the software is |
|
42 |
hereby transferred. |
|
43 |
" |
|
44 |
! |
|
45 |
||
21298 | 46 |
documentation |
47 |
" |
|
21299 | 48 |
I can encode characters into/from UTF8 |
49 |
||
21298 | 50 |
Notice the naming (many are confused): |
51 |
Unicode is the set of number-to-glyph assignments |
|
52 |
whereas: |
|
53 |
UTF8 is a concrete way of xmitting Unicode codePoints (numbers). |
|
54 |
UTF16 is another concrete encoding, for example. |
|
55 |
||
56 |
ST/X NEVER uses UTF8 internally - all characters are full 24bit characters. |
|
57 |
Onley when exchanging data, are these converted into UTF8 (or other) byte sequences. |
|
58 |
" |
|
59 |
! |
|
60 |
||
8081 | 61 |
examples |
62 |
" |
|
63 |
Encoding (unicode to utf8) |
|
8411 | 64 |
ISO10646_to_UTF8 encodeString:'hello'. |
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
65 |
|
8081 | 66 |
|
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
67 |
Decoding (utf8 to unicode): |
8081 | 68 |
|t| |
69 |
||
21298 | 70 |
t := ISO10646_to_UTF8 encodeString:'Helloœ'. |
8411 | 71 |
ISO10646_to_UTF8 decodeString:t. |
8081 | 72 |
" |
73 |
! ! |
|
74 |
||
18604
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
75 |
!ISO10646_to_UTF8 class methodsFor:'instance creation'! |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
76 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
77 |
flushSingleton |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
78 |
"flushes the cached singleton" |
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 |
theOneAndOnlyInstance := nil |
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 |
" |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
83 |
self flushSingleton |
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 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
87 |
new |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
88 |
"returns a singleton" |
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 |
theOneAndOnlyInstance isNil ifTrue:[ |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
91 |
theOneAndOnlyInstance := self basicNew initialize. |
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. |
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 |
|
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 |
"returns a singleton" |
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 isNil ifTrue:[ |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
100 |
theOneAndOnlyInstance := self basicNew initialize. |
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. |
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 |
|
8081 | 105 |
!ISO10646_to_UTF8 methodsFor:'encoding & decoding'! |
106 |
||
107 |
decode:aCode |
|
108 |
self shouldNotImplement "/ no single byte conversion possible |
|
109 |
! |
|
110 |
||
111 |
decodeString:aStringOrByteCollection |
|
112 |
"given a string in UTF8 encoding, |
|
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
113 |
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
|
114 |
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
|
115 |
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
|
116 |
other systems |
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
117 |
(ST/X never uses utf8 internally, but always uses strings of fully decoded unicode characters). |
19838 | 118 |
This only handles up-to 30bit characters." |
14172 | 119 |
|
19838 | 120 |
^ CharacterArray decodeFromUTF8:aStringOrByteCollection. |
8081 | 121 |
! |
122 |
||
123 |
encode:aCode |
|
124 |
self shouldNotImplement "/ no single byte conversion possible |
|
125 |
! |
|
126 |
||
127 |
encodeString:aUnicodeString |
|
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
128 |
"return the UTF-8 representation of a Unicode string. |
8081 | 129 |
The resulting string is only useful to be stored on some external file, |
19838 | 130 |
not for being used inside ST/X." |
8081 | 131 |
|
19838 | 132 |
^ aUnicodeString utf8Encoded. |
8081 | 133 |
! ! |
134 |
||
11974 | 135 |
!ISO10646_to_UTF8 privateMethodsFor:'queries'! |
136 |
||
137 |
bytesToReadFor:firstByte |
|
138 |
|bytesToRead| |
|
139 |
||
140 |
bytesToRead := 1. |
|
141 |
(firstByte isBitSet:8) ifFalse:[^1]. |
|
142 |
7 downTo:3 |
|
143 |
do:[:idx | |
|
144 |
(firstByte isBitSet:idx) ifTrue:[ |
|
145 |
bytesToRead := bytesToRead + 1 |
|
146 |
] ifFalse:[ |
|
147 |
^bytesToRead |
|
148 |
] |
|
149 |
]. |
|
150 |
^bytesToRead |
|
151 |
||
152 |
"Created: / 14-06-2005 / 17:17:24 / janfrog" |
|
153 |
! ! |
|
154 |
||
8163 | 155 |
!ISO10646_to_UTF8 methodsFor:'queries'! |
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 |
||
193 |
readNextCharacterFrom:aStream |
|
11996 | 194 |
|
11974 | 195 |
|firstByte bytesToRead str| |
196 |
||
197 |
firstByte := aStream peek. |
|
198 |
firstByte ifNil:[^nil]. |
|
199 |
firstByte := firstByte asInteger. |
|
200 |
bytesToRead := self bytesToReadFor:firstByte. |
|
201 |
str := self decodeString:(aStream next:bytesToRead). |
|
202 |
str size > 1 ifTrue:[ |
|
203 |
self error:'Badly coded method' |
|
204 |
]. |
|
205 |
^ str first |
|
206 |
||
207 |
"Created: / 14-06-2005 / 17:03:59 / janfrog" |
|
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$' |
|
8081 | 218 |
! ! |
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
219 |