author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Tue, 17 May 2016 10:05:14 +0100 | |
branch | jv |
changeset 19863 | 513bd7237fe7 |
parent 18807 | d79ce9fb5198 |
parent 19838 | a6ca726d596c |
child 21387 | e3865533e6a6 |
permissions | -rw-r--r-- |
19838 | 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 |
||
8081 | 48 |
examples |
49 |
" |
|
50 |
Encoding (unicode to utf8) |
|
8411 | 51 |
ISO10646_to_UTF8 encodeString:'hello'. |
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
52 |
|
8081 | 53 |
|
8297
e7a05a86f280
removed iso8859-chars (for hpux)
Claus Gittinger <cg@exept.de>
parents:
8221
diff
changeset
|
54 |
Decoding (utf8 to unicode): |
8081 | 55 |
|t| |
56 |
||
19838 | 57 |
t := ISO10646_to_UTF8 encodeString:'Hello'. |
8411 | 58 |
ISO10646_to_UTF8 decodeString:t. |
8081 | 59 |
" |
60 |
! ! |
|
61 |
||
18604
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
62 |
!ISO10646_to_UTF8 class methodsFor:'instance creation'! |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
63 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
64 |
flushSingleton |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
65 |
"flushes the cached singleton" |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
66 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
67 |
theOneAndOnlyInstance := nil |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
68 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
69 |
" |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
70 |
self flushSingleton |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
71 |
" |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
72 |
! |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
73 |
|
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
74 |
new |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
75 |
"returns a singleton" |
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 |
theOneAndOnlyInstance isNil ifTrue:[ |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
78 |
theOneAndOnlyInstance := self basicNew initialize. |
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. |
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 |
theOneAndOnlyInstance |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
84 |
"returns a singleton" |
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 |
theOneAndOnlyInstance isNil ifTrue:[ |
54caf7b64994
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
18601
diff
changeset
|
87 |
theOneAndOnlyInstance := self basicNew initialize. |
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 |
^ theOneAndOnlyInstance. |
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 |
|
8081 | 92 |
!ISO10646_to_UTF8 methodsFor:'encoding & decoding'! |
93 |
||
94 |
decode:aCode |
|
95 |
self shouldNotImplement "/ no single byte conversion possible |
|
96 |
! |
|
97 |
||
98 |
decodeString:aStringOrByteCollection |
|
99 |
"given a string in UTF8 encoding, |
|
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
100 |
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
|
101 |
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
|
102 |
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
|
103 |
other systems |
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
104 |
(ST/X never uses utf8 internally, but always uses strings of fully decoded unicode characters). |
19838 | 105 |
This only handles up-to 30bit characters." |
14172 | 106 |
|
19838 | 107 |
^ CharacterArray decodeFromUTF8:aStringOrByteCollection. |
8081 | 108 |
! |
109 |
||
110 |
encode:aCode |
|
111 |
self shouldNotImplement "/ no single byte conversion possible |
|
112 |
! |
|
113 |
||
114 |
encodeString:aUnicodeString |
|
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
115 |
"return the UTF-8 representation of a Unicode string. |
8081 | 116 |
The resulting string is only useful to be stored on some external file, |
19838 | 117 |
not for being used inside ST/X." |
8081 | 118 |
|
19838 | 119 |
^ aUnicodeString utf8Encoded. |
8081 | 120 |
! ! |
121 |
||
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
122 |
!ISO10646_to_UTF8 methodsFor:'queries'! |
11974 | 123 |
|
124 |
bytesToReadFor:firstByte |
|
125 |
|bytesToRead| |
|
126 |
||
127 |
bytesToRead := 1. |
|
128 |
(firstByte isBitSet:8) ifFalse:[^1]. |
|
129 |
7 downTo:3 |
|
130 |
do:[:idx | |
|
131 |
(firstByte isBitSet:idx) ifTrue:[ |
|
132 |
bytesToRead := bytesToRead + 1 |
|
133 |
] ifFalse:[ |
|
134 |
^bytesToRead |
|
135 |
] |
|
136 |
]. |
|
137 |
^bytesToRead |
|
138 |
||
139 |
"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
|
140 |
! |
8163 | 141 |
|
11974 | 142 |
characterSize:charOrcodePoint |
143 |
"return the number of bytes required to encode codePoint" |
|
144 |
||
145 |
"Taken from RFC 3629" |
|
146 |
||
147 |
(charOrcodePoint asInteger between:16r00000000 and:16r0000007F) ifTrue:[^1]. |
|
148 |
(charOrcodePoint asInteger between:16r00000080 and:16r000007FF) ifTrue:[^2]. |
|
149 |
(charOrcodePoint asInteger between:16r00000800 and:16r0000FFFF) ifTrue:[^3]. |
|
150 |
(charOrcodePoint asInteger between:16r00010000 and:16r0010FFFF) ifTrue:[^4]. |
|
151 |
||
152 |
^self error:'Invalid codePoint' |
|
153 |
||
154 |
"Created: / 15-06-2005 / 15:16:22 / janfrog" |
|
155 |
! |
|
156 |
||
8163 | 157 |
nameOfEncoding |
14172 | 158 |
^ #utf8 |
8163 | 159 |
! ! |
160 |
||
11974 | 161 |
!ISO10646_to_UTF8 methodsFor:'stream support'! |
162 |
||
163 |
readNext:charactersToRead charactersFrom:stream |
|
11996 | 164 |
|
11974 | 165 |
| s | |
166 |
||
167 |
s := (String new:charactersToRead) writeStream. |
|
168 |
charactersToRead timesRepeat:[ |
|
169 |
| c | |
|
170 |
c := stream peek. |
|
171 |
s nextPutAll:(stream next:(self bytesToReadFor:c)) |
|
172 |
]. |
|
173 |
^ self decodeString:s contents |
|
174 |
||
175 |
"Created: / 16-06-2005 / 11:45:14 / masca" |
|
176 |
! |
|
177 |
||
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
178 |
readNextCharacterFrom:stream |
11974 | 179 |
|
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
180 |
| c bytesYetToRead s | |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
181 |
c := stream peek. |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
182 |
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
|
183 |
bytesYetToRead == 1 ifTrue:[ |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
184 |
stream next. |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
185 |
^ c. |
11974 | 186 |
]. |
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
187 |
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
|
188 |
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
|
189 |
^ self decodeString:s contents |
11974 | 190 |
|
191 |
"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
|
192 |
"Modified: / 03-10-2015 / 08:49:09 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
11974 | 193 |
! ! |
194 |
||
8081 | 195 |
!ISO10646_to_UTF8 class methodsFor:'documentation'! |
196 |
||
197 |
version |
|
18601
00dc53dfe54d
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Stefan Vogel <sv@exept.de>
parents:
17623
diff
changeset
|
198 |
^ '$Header$' |
18807
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
199 |
! |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
200 |
|
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
201 |
version_HG |
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
202 |
|
d79ce9fb5198
Fixed EncodedStream>>next for UTF8 to Unicode decoder.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18630
diff
changeset
|
203 |
^ '$Changeset: <not expanded> $' |
8081 | 204 |
! ! |
17489
22f6151b5135
class: CharacterEncoderImplementations::ISO10646_to_UTF8
Claus Gittinger <cg@exept.de>
parents:
14172
diff
changeset
|
205 |