author | Claus Gittinger <cg@exept.de> |
Sun, 05 Jan 2020 17:27:00 +0100 | |
changeset 25177 | 1c618f3d51f5 |
parent 24940 | 476e387608cf |
child 25203 | a3ba0cf0dd07 |
permissions | -rw-r--r-- |
24213 | 1 |
"{ Encoding: utf8 }" |
2 |
||
7946 | 3 |
" |
4 |
COPYRIGHT (c) 1997 by eXept Software AG |
|
5 |
All Rights Reserved |
|
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 |
" |
|
10223
761e2a050b69
twoByteString moved (req'd in VM)
Claus Gittinger <cg@exept.de>
parents:
9648
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
7946 | 15 |
|
17537 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
7946 | 18 |
TwoByteString variableWordSubclass:#Unicode16String |
19 |
instanceVariableNames:'' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Collections-Text' |
|
23 |
! |
|
24 |
||
25 |
!Unicode16String class methodsFor:'documentation'! |
|
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1997 by eXept Software AG |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
||
39 |
||
40 |
" |
|
41 |
! |
|
42 |
||
43 |
documentation |
|
44 |
" |
|
10223
761e2a050b69
twoByteString moved (req'd in VM)
Claus Gittinger <cg@exept.de>
parents:
9648
diff
changeset
|
45 |
Represents 16-bit (2-byte) Unicode strings. |
7946 | 46 |
" |
47 |
! ! |
|
48 |
||
49 |
!Unicode16String class methodsFor:'initialization'! |
|
50 |
||
51 |
initialize |
|
52 |
"initialize the class - private" |
|
53 |
||
7949 | 54 |
self flags:(Behavior flagWords). |
55 |
||
56 |
Smalltalk at:#UnicodeString put:Unicode16String. |
|
7946 | 57 |
|
58 |
" |
|
59 |
Unicode16String initialize |
|
60 |
" |
|
61 |
||
62 |
"Created: 30.6.1997 / 15:35:52 / cg" |
|
63 |
"Modified: 30.6.1997 / 15:39:21 / cg" |
|
64 |
! ! |
|
65 |
||
21544 | 66 |
|
67 |
||
11323 | 68 |
!Unicode16String class methodsFor:'reading'! |
69 |
||
70 |
readFrom:aStreamOrString onError:exceptionBlock |
|
71 |
"read & return the next String from the (character-)stream aStream; |
|
72 |
skipping all whitespace first; return the value of exceptionBlock, |
|
73 |
if no string can be read. The sequence of characters as read from the |
|
74 |
stream must be one as stored via storeOn: or storeString." |
|
75 |
||
76 |
" |
|
77 |
this method is not to be inherited |
|
78 |
(i.e. not ok for subclasses; Symbol, for example) |
|
79 |
" |
|
80 |
self ~~ Unicode16String ifTrue:[ |
|
81 |
^ super readFrom:aStreamOrString onError:exceptionBlock |
|
82 |
]. |
|
83 |
^ self readSmalltalkStringFrom:aStreamOrString onError:exceptionBlock |
|
84 |
||
85 |
" |
|
24213 | 86 |
self readFrom:'abcäöü' storeString |
11323 | 87 |
" |
88 |
! ! |
|
89 |
||
90 |
||
7946 | 91 |
!Unicode16String methodsFor:'conversion'! |
92 |
||
8294
ba5f4421848f
Define #asUnicode16String and #asUnicode32String
Stefan Vogel <sv@exept.de>
parents:
8049
diff
changeset
|
93 |
asUnicode16String |
8942 | 94 |
"as the receiver already is a unicode-16 string, return it" |
95 |
||
8294
ba5f4421848f
Define #asUnicode16String and #asUnicode32String
Stefan Vogel <sv@exept.de>
parents:
8049
diff
changeset
|
96 |
^ self |
ba5f4421848f
Define #asUnicode16String and #asUnicode32String
Stefan Vogel <sv@exept.de>
parents:
8049
diff
changeset
|
97 |
! |
ba5f4421848f
Define #asUnicode16String and #asUnicode32String
Stefan Vogel <sv@exept.de>
parents:
8049
diff
changeset
|
98 |
|
7946 | 99 |
asUnicodeString |
8942 | 100 |
"as the receiver already is a unicode string, return it" |
101 |
||
7946 | 102 |
^ self |
24213 | 103 |
! |
104 |
||
105 |
utf16Encoded |
|
106 |
"have to encode only if there are characters that are outside the UTF-16 range" |
|
107 |
||
108 |
|sz "{Class:SmallInteger}" codePoint "{Class:SmallInteger}"| |
|
109 |
||
110 |
sz := self size. |
|
111 |
1 to:sz do:[:eachIdx| |
|
112 |
codePoint := self basicAt:eachIdx. |
|
113 |
||
114 |
(codePoint >= 16rD800 and:[codePoint <= 16rDFFF]) ifTrue:[ |
|
115 |
^ super utf16Encoded. |
|
116 |
]. |
|
117 |
]. |
|
118 |
||
119 |
"all my characters are in the UTF-16 range" |
|
120 |
^ self |
|
121 |
||
122 |
" |
|
123 |
((Unicode16String new:1) at:1 put:16rD800; yourself) utf16Encoded |
|
124 |
((Unicode16String new:1) at:1 put:16r2A00; yourself) utf16Encoded |
|
125 |
" |
|
126 |
||
127 |
"Created: / 28-05-2019 / 13:14:32 / Stefan Vogel" |
|
128 |
"Modified (comment): / 28-05-2019 / 14:45:01 / Stefan Vogel" |
|
7946 | 129 |
! ! |
130 |
||
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
131 |
!Unicode16String methodsFor:'printing & storing'! |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
132 |
|
14121
fea92d19d1a4
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
14075
diff
changeset
|
133 |
displayOn:aGCOrStream |
fea92d19d1a4
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
14075
diff
changeset
|
134 |
"display myself as on aStream. |
fea92d19d1a4
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
14075
diff
changeset
|
135 |
You must use an ISO10646 unicode font to display this string" |
fea92d19d1a4
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
14075
diff
changeset
|
136 |
|
17537 | 137 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
21544 | 138 |
"/ old ST80 means: draw-yourself on a GC. |
139 |
aGCOrStream isStream ifFalse:[ |
|
140 |
^ super displayOn:aGCOrStream |
|
14121
fea92d19d1a4
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
14075
diff
changeset
|
141 |
]. |
21544 | 142 |
|
143 |
aGCOrStream |
|
144 |
nextPut:$'; |
|
145 |
nextPutAllUnicode:self; |
|
146 |
nextPut:$'. |
|
147 |
||
148 |
"Modified: / 22-02-2017 / 16:56:32 / cg" |
|
14121
fea92d19d1a4
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
14075
diff
changeset
|
149 |
! |
fea92d19d1a4
added: #displayOn: (instead of #displaySting)
Stefan Vogel <sv@exept.de>
parents:
14075
diff
changeset
|
150 |
|
11323 | 151 |
printOn:aStream |
152 |
"print the receiver on aStream. |
|
13986
c8b952d6139c
comment/format in: #printOn:
Claus Gittinger <cg@exept.de>
parents:
13737
diff
changeset
|
153 |
Let aStream decide how to represent this, whether utf8, ucs16, ..." |
11323 | 154 |
|
155 |
aStream nextPutAllUnicode:self |
|
13986
c8b952d6139c
comment/format in: #printOn:
Claus Gittinger <cg@exept.de>
parents:
13737
diff
changeset
|
156 |
|
c8b952d6139c
comment/format in: #printOn:
Claus Gittinger <cg@exept.de>
parents:
13737
diff
changeset
|
157 |
"Modified (comment): / 25-01-2012 / 10:28:34 / cg" |
11323 | 158 |
! |
159 |
||
160 |
storeOn:aStream |
|
161 |
"put the storeString of myself on aStream" |
|
162 |
||
18970 | 163 |
self containsNon7BitAscii ifTrue:[ |
164 |
"non-7bit string has been utf8Encoded" |
|
165 |
self utf8Encoded storeOn:aStream. |
|
166 |
aStream nextPutAll:' utf8Decoded'. |
|
167 |
] ifFalse:[ |
|
168 |
self asSingleByteString storeOn:aStream. |
|
169 |
]. |
|
170 |
||
171 |
" |
|
172 |
String streamContents:[:s| |
|
173 |
'hello' asUnicodeString storeOn:s |
|
174 |
]. |
|
175 |
||
176 |
String streamContents:[:s| |
|
24213 | 177 |
'hello -öäüß' asUnicodeString storeOn:s |
18970 | 178 |
]. |
179 |
" |
|
13737 | 180 |
|
181 |
"/ aStream nextPut:$'. |
|
182 |
"/ (self includes:$') ifTrue:[ |
|
183 |
"/ self do:[:thisChar | |
|
184 |
"/ (thisChar == $') ifTrue:[aStream nextPut:thisChar]. |
|
185 |
"/ aStream nextPutUnicode:thisChar |
|
186 |
"/ ] |
|
187 |
"/ ] ifFalse:[ |
|
188 |
"/ aStream nextPutAllUnicode:self |
|
189 |
"/ ]. |
|
190 |
"/ aStream nextPut:$' |
|
191 |
||
192 |
"Modified: / 28-09-2011 / 16:17:38 / cg" |
|
11323 | 193 |
! |
194 |
||
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
195 |
storeString |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
196 |
"return a String for storing myself" |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
197 |
|
18970 | 198 |
self containsNon7BitAscii ifTrue:[ |
199 |
"non-7bit string has been utf8Encoded" |
|
200 |
^ (self utf8Encoded storeString),' utf8Decoded'. |
|
201 |
] ifFalse:[ |
|
202 |
^ self asSingleByteString storeString. |
|
203 |
]. |
|
204 |
||
205 |
" |
|
206 |
'hello' asUnicodeString storeString |
|
24213 | 207 |
'hello -öäüß' storeString |
208 |
'hello -öäüß' asUnicodeString storeString |
|
18970 | 209 |
" |
13737 | 210 |
|
13989
cd15b3b6154a
backward compatibility storestring
Claus Gittinger <cg@exept.de>
parents:
13988
diff
changeset
|
211 |
"Modified: / 25-01-2012 / 11:59:34 / cg" |
14124 | 212 |
! |
213 |
||
214 |
unicodeStoreOn:aStream |
|
215 |
"put the storeString of myself on aStream" |
|
216 |
||
217 |
aStream nextPut:$'. |
|
218 |
(self includes:$') ifTrue:[ |
|
219 |
self do:[:thisChar | |
|
220 |
(thisChar == $') ifTrue:[aStream nextPut:thisChar]. |
|
221 |
aStream nextPutUnicode:thisChar |
|
222 |
] |
|
223 |
] ifFalse:[ |
|
224 |
aStream nextPutAllUnicode:self |
|
225 |
]. |
|
226 |
aStream nextPut:$' |
|
227 |
! |
|
228 |
||
229 |
unicodeStoreString |
|
230 |
"return a UnicodeString for storing myself. |
|
231 |
This method is a kind of kludge. |
|
232 |
Use it when you want to write a storeString to an encoded Stream" |
|
233 |
||
234 |
^ self basicStoreString. |
|
235 |
! ! |
|
236 |
||
237 |
!Unicode16String methodsFor:'testing'! |
|
238 |
||
239 |
isUnicode16String |
|
240 |
^ true |
|
241 |
! |
|
242 |
||
243 |
isUnicodeString |
|
19937 | 244 |
"true if this is a 2- or 4-byte unicode string |
245 |
(i.e. not a single byte string). |
|
246 |
Notice, that the name is misleading: |
|
24940 | 247 |
all strings use unicode encoding" |
19937 | 248 |
|
14124 | 249 |
^ true |
24940 | 250 |
|
251 |
"Modified (comment): / 21-11-2019 / 18:25:59 / Stefan Vogel" |
|
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
252 |
! ! |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
253 |
|
21544 | 254 |
|
7946 | 255 |
!Unicode16String class methodsFor:'documentation'! |
256 |
||
257 |
version |
|
18970 | 258 |
^ '$Header$' |
13988
81154d310967
use new c-escape string for stroreString
Claus Gittinger <cg@exept.de>
parents:
13986
diff
changeset
|
259 |
! |
81154d310967
use new c-escape string for stroreString
Claus Gittinger <cg@exept.de>
parents:
13986
diff
changeset
|
260 |
|
81154d310967
use new c-escape string for stroreString
Claus Gittinger <cg@exept.de>
parents:
13986
diff
changeset
|
261 |
version_CVS |
18970 | 262 |
^ '$Header$' |
7946 | 263 |
! ! |
264 |
||
14605 | 265 |
|
7946 | 266 |
Unicode16String initialize! |