author | Stefan Vogel <sv@exept.de> |
Sat, 18 Jul 2015 01:22:18 +0200 | |
changeset 18593 | e69a425e3823 |
parent 18586 | b8798de459f4 |
child 18606 | 5d9eaf7bf761 |
child 18608 | 7d521f25267c |
permissions | -rw-r--r-- |
18586 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
4 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
235 | 5 |
All Rights Reserved |
1 | 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:
8094
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
8094
d05f69bd0097
Use #codePoint instead of deprecated #asciiValue
Stefan Vogel <sv@exept.de>
parents:
5761
diff
changeset
|
15 |
|
17621 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
5761 | 18 |
CharacterArray variableWordSubclass:#TwoByteString |
992
f456f8f7d421
JIS decode now in CharacterArray
Claus Gittinger <cg@exept.de>
parents:
631
diff
changeset
|
19 |
instanceVariableNames:'' |
f456f8f7d421
JIS decode now in CharacterArray
Claus Gittinger <cg@exept.de>
parents:
631
diff
changeset
|
20 |
classVariableNames:'' |
f456f8f7d421
JIS decode now in CharacterArray
Claus Gittinger <cg@exept.de>
parents:
631
diff
changeset
|
21 |
poolDictionaries:'' |
f456f8f7d421
JIS decode now in CharacterArray
Claus Gittinger <cg@exept.de>
parents:
631
diff
changeset
|
22 |
category:'Collections-Text' |
1 | 23 |
! |
24 |
||
89 | 25 |
!TwoByteString class methodsFor:'documentation'! |
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
235 | 30 |
All Rights Reserved |
1 | 31 |
|
89 | 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 |
documentation |
|
42 |
" |
|
43 |
TwoByteStrings are like strings, but storing 16bits per character. |
|
44 |
The integration of them into the system is not completed .... |
|
1290 | 45 |
|
46 |
[author:] |
|
47 |
Claus Gittinger |
|
1309 | 48 |
|
49 |
[see also:] |
|
1382 | 50 |
Text JISEncodedString |
1309 | 51 |
StringCollection |
89 | 52 |
" |
1214 | 53 |
! ! |
54 |
||
55 |
!TwoByteString class methodsFor:'initialization'! |
|
996 | 56 |
|
57 |
initialize |
|
1253 | 58 |
"initialize the class - private" |
59 |
||
996 | 60 |
self flags:(Behavior flagWords) |
61 |
||
62 |
" |
|
63 |
TwoByteString initialize |
|
64 |
" |
|
1214 | 65 |
|
1253 | 66 |
"Modified: 22.4.1996 / 16:14:14 / cg" |
89 | 67 |
! ! |
1 | 68 |
|
69 |
!TwoByteString class methodsFor:'instance creation'! |
|
70 |
||
71 |
basicNew:anInteger |
|
72 |
"return a new empty string with anInteger characters" |
|
73 |
||
1024 | 74 |
^ (super basicNew:anInteger) atAllPut:(Character space) |
75 |
||
76 |
"Modified: 26.2.1996 / 14:38:47 / cg" |
|
17621 | 77 |
! |
78 |
||
79 |
uninitializedNew:anInteger |
|
80 |
"return a new empty string with anInteger characters" |
|
81 |
||
82 |
^ super basicNew:anInteger |
|
83 |
||
84 |
" |
|
85 |
self uninitializedNew:10 |
|
86 |
" |
|
1 | 87 |
! ! |
88 |
||
89 |
!TwoByteString methodsFor:'accessing'! |
|
90 |
||
91 |
basicAt:index |
|
92 |
"return the character at position index, an Integer |
|
73 | 93 |
- reimplemented here since we return 16-bit characters" |
1 | 94 |
|
1024 | 95 |
|val| |
63 | 96 |
|
1024 | 97 |
val := super basicAt:index. |
63 | 98 |
^ Character value:val |
1024 | 99 |
|
100 |
"Modified: 26.2.1996 / 17:02:16 / cg" |
|
1 | 101 |
! |
102 |
||
103 |
basicAt:index put:aCharacter |
|
1230 | 104 |
"store the argument, aCharacter at position index, an Integer. |
105 |
Returns aCharacter (sigh). |
|
73 | 106 |
- reimplemented here since we store 16-bit characters" |
1 | 107 |
|
8094
d05f69bd0097
Use #codePoint instead of deprecated #asciiValue
Stefan Vogel <sv@exept.de>
parents:
5761
diff
changeset
|
108 |
super basicAt:index put:aCharacter codePoint. |
63 | 109 |
^ aCharacter |
608 | 110 |
|
1230 | 111 |
"Modified: 19.4.1996 / 11:16:22 / cg" |
14123 | 112 |
! |
113 |
||
114 |
unsignedShortAt:index |
|
115 |
"return the short at position index, an Integer" |
|
116 |
||
117 |
^ super basicAt:index. |
|
1014 | 118 |
! ! |
119 |
||
17621 | 120 |
!TwoByteString methodsFor:'encoding'! |
121 |
||
122 |
utf8Encoded |
|
123 |
"Return my UTF-8 representation as a new String" |
|
124 |
||
18593 | 125 |
self containsNon7BitAscii ifTrue:[ |
17621 | 126 |
^ self basicUtf8Encoded. |
127 |
]. |
|
128 |
||
129 |
^ self asSingleByteString. |
|
130 |
||
131 |
||
132 |
" |
|
133 |
'abcdef' asUnicode16String utf8Encoded |
|
18586 | 134 |
'abcdefäöü' asUnicode16String utf8Encoded |
17621 | 135 |
" |
136 |
! |
|
137 |
||
138 |
utf8EncodedOn:aStream |
|
139 |
"write to aStream in utf8 encoding" |
|
140 |
||
18593 | 141 |
self containsNon7BitAscii ifTrue:[ |
17621 | 142 |
aStream nextPutAllUtf8:self. |
143 |
] ifFalse:[ |
|
144 |
|sz "{Class: SmallInteger}"| |
|
145 |
||
146 |
sz := self size. |
|
147 |
1 to:sz do:[:idx| |
|
148 |
aStream nextPut:(self basicAt:idx). |
|
149 |
]. |
|
150 |
]. |
|
151 |
||
152 |
" |
|
18593 | 153 |
String streamContents:[:w| |
154 |
'abcde1234' asUnicode16String utf8EncodedOn:w |
|
155 |
]. |
|
156 |
String streamContents:[:w| |
|
157 |
'abcdeäöüß' asUnicode16String utf8EncodedOn:w |
|
158 |
]. |
|
17621 | 159 |
" |
160 |
! ! |
|
161 |
||
16750 | 162 |
!TwoByteString methodsFor:'filling and replacing'! |
163 |
||
164 |
replaceFrom:start to:stop with:aString startingAt:repStart |
|
165 |
"replace the characters starting at index start, anInteger and ending |
|
166 |
at stop, anInteger with characters from aString starting at repStart. |
|
167 |
Return the receiver. |
|
168 |
||
169 |
- reimplemented here for speed" |
|
170 |
||
171 |
%{ /* NOCONTEXT */ |
|
172 |
||
173 |
#ifndef NO_PRIM_STRING |
|
174 |
if (__bothSmallInteger(start, stop)) { |
|
18576 | 175 |
int len; |
176 |
int index1 = __intVal(start); |
|
177 |
int index2 = __intVal(stop); |
|
178 |
int count = index2 - index1 + 1; |
|
16750 | 179 |
|
180 |
if (count <= 0) { |
|
181 |
RETURN (self); |
|
182 |
} |
|
183 |
len = __twoByteStringSize(self); |
|
184 |
if ((index2 <= len) && (index1 > 0)) { |
|
18576 | 185 |
int repIndex = __intVal(repStart); |
16750 | 186 |
|
187 |
if (__isStringLike(aString)) { |
|
18576 | 188 |
int repLen = __stringSize(aString); |
16750 | 189 |
if ((repIndex > 0) && ((repIndex + count - 1) <= repLen)) { |
18576 | 190 |
REGISTER unsigned char *srcp = __stringVal(aString) + repIndex - 1; |
191 |
REGISTER unsigned short *dstp = __twoByteStringVal(self) + index1 - 1; |
|
16750 | 192 |
|
193 |
while (count-- > 0) { |
|
194 |
*dstp++ = *srcp++; |
|
195 |
} |
|
196 |
RETURN (self); |
|
197 |
} |
|
198 |
} else if (__isTwoByteString(aString) || __isUnicode16String(aString)) { |
|
18576 | 199 |
int repLen = __twoByteStringSize(aString); |
16750 | 200 |
if ((repIndex > 0) && ((repIndex + count - 1) <= repLen)) { |
18576 | 201 |
REGISTER unsigned short *srcp = __twoByteStringVal(aString) + repIndex - 1; |
202 |
REGISTER unsigned short *dstp = __twoByteStringVal(self) + index1 - 1; |
|
16750 | 203 |
|
204 |
if (aString == self) { |
|
205 |
/* take care of overlapping copy */ |
|
18576 | 206 |
memmove(dstp, srcp, count*sizeof(short)); |
207 |
RETURN (self); |
|
16750 | 208 |
} |
209 |
if (count > 5) { |
|
210 |
memcpy(dstp, srcp, count*sizeof(short)); |
|
211 |
} else { |
|
212 |
while (count-- > 0) { |
|
213 |
*dstp++ = *srcp++; |
|
214 |
} |
|
215 |
} |
|
216 |
RETURN (self); |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
} |
|
221 |
#endif |
|
222 |
%}. |
|
223 |
"/ arrive here if any index arg is out o range, or the source is neither a string, |
|
224 |
"/ nor a two-byte string. |
|
225 |
^ super replaceFrom:start to:stop with:aString startingAt:repStart |
|
226 |
||
227 |
" |
|
228 |
'hello world' asUnicode16String replaceFrom:1 to:5 with:'123456' startingAt:2 |
|
229 |
'hello world' asUnicode16String replaceFrom:1 to:5 with:'123456' asUnicode16String startingAt:2 |
|
230 |
'hello world' asUnicode16String replaceFrom:1 to:0 with:'123456' startingAt:2 |
|
231 |
'hello' asUnicode16String replaceFrom:1 to:6 with:'123456' startingAt:2 |
|
232 |
'hello world' asUnicode16String replaceFrom:1 to:1 with:'123456' startingAt:2 |
|
233 |
" |
|
234 |
! ! |
|
235 |
||
608 | 236 |
!TwoByteString methodsFor:'queries'! |
237 |
||
1017 | 238 |
bitsPerCharacter |
1239 | 239 |
"return the number of bits each character has. |
240 |
Here, 16 is returned (storing double byte characters)." |
|
241 |
||
1017 | 242 |
^ 16 |
1239 | 243 |
|
244 |
"Modified: 20.4.1996 / 23:08:38 / cg" |
|
14557 | 245 |
! |
246 |
||
18593 | 247 |
characterSize |
248 |
"answer the size in bits of my largest character (actually only 7, 8 or 16)" |
|
249 |
||
250 |
%{ /* NOCONTEXT */ |
|
251 |
||
252 |
REGISTER unsigned short *sp = __twoByteStringVal(self); |
|
253 |
REGISTER unsigned short *last = sp + __twoByteStringSize(self); |
|
254 |
OBJ cls = __qClass(self); |
|
255 |
int has8BitChars = 0; |
|
256 |
||
257 |
if (cls != Unicode16String && cls != TwoByteString) { |
|
258 |
sp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)) / 2; |
|
259 |
} |
|
18586 | 260 |
|
18593 | 261 |
#if __POINTER_SIZE__ == 8 |
262 |
if (sizeof(unsigned INT) == 8) { |
|
263 |
if (!has8BitChars) { |
|
264 |
for ( ; (sp+4) <= last; sp += 4) { |
|
265 |
if (*(unsigned INT *)sp & 0xFF80FF80FF80FF80) { |
|
266 |
/* there are at least 8-bit chars - check for more */ |
|
267 |
has8BitChars = 1; |
|
268 |
break; |
|
269 |
} |
|
270 |
} |
|
271 |
} |
|
272 |
for ( ; (sp+4) <= last; sp += 4) { |
|
273 |
if (*(unsigned INT *)sp & 0xFF00FF00FF00FF00) { |
|
274 |
RETURN(__mkSmallInteger(16)); |
|
275 |
} |
|
276 |
} |
|
277 |
} |
|
278 |
#endif |
|
279 |
if (sizeof(unsigned int) == 4) { |
|
280 |
if (!has8BitChars) { |
|
281 |
for ( ; (sp+2) <= last; sp += 2) { |
|
282 |
if (*(unsigned int *)sp & 0xFF80FF80) { |
|
283 |
/* there are at least 8-bit chars - check for more */ |
|
284 |
has8BitChars = 1; |
|
285 |
break; |
|
286 |
} |
|
287 |
} |
|
288 |
} |
|
289 |
for ( ; (sp+2) <= last; sp += 2) { |
|
290 |
if (*(unsigned int *)sp & 0xFF00FF00) { |
|
291 |
RETURN(__mkSmallInteger(16)); |
|
292 |
} |
|
293 |
} |
|
294 |
} |
|
295 |
if (!has8BitChars) { |
|
296 |
for ( ; sp < last; sp++) { |
|
297 |
if (*sp & 0xFF80) { |
|
298 |
/* there are at least 8-bit chars - check for more */ |
|
299 |
has8BitChars = 1; |
|
300 |
break; |
|
301 |
} |
|
302 |
} |
|
303 |
} |
|
304 |
for ( ; sp < last; sp++) { |
|
305 |
if (*sp & 0xFF00) { |
|
306 |
RETURN(__mkSmallInteger(16)); |
|
307 |
} |
|
308 |
} |
|
309 |
RETURN (__mkSmallInteger(has8BitChars ? 8 : 7)); |
|
310 |
%}. |
|
311 |
||
312 |
" |
|
313 |
'hello world' asUnicode16String characterSize |
|
314 |
'hello worldüäö' asUnicode16String characterSize |
|
315 |
'a' asUnicode16String characterSize |
|
316 |
'ü' asUnicode16String characterSize |
|
317 |
'aa' asUnicode16String characterSize |
|
318 |
'aü' asUnicode16String characterSize |
|
319 |
'aaa' asUnicode16String characterSize |
|
320 |
'aaü' asUnicode16String characterSize |
|
321 |
'aaaü' asUnicode16String characterSize |
|
322 |
'aaaa' asUnicode16String characterSize |
|
323 |
'aaaaü' asUnicode16String characterSize |
|
324 |
" |
|
18586 | 325 |
! |
326 |
||
18593 | 327 |
containsNon7BitAscii |
17621 | 328 |
"return true, if the underlying string contains 8BitCharacters (or widers) |
329 |
(i.e. if it is non-ascii)" |
|
330 |
||
331 |
%{ /* NOCONTEXT */ |
|
332 |
||
18593 | 333 |
REGISTER unsigned short *sp = __twoByteStringVal(self); |
334 |
REGISTER unsigned short *last = sp + __twoByteStringSize(self); |
|
335 |
OBJ cls = __qClass(self); |
|
17621 | 336 |
|
18593 | 337 |
if ( cls != Unicode16String && cls != TwoByteString) { |
17621 | 338 |
sp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)) / 2; |
339 |
} |
|
340 |
#if __POINTER_SIZE__ == 8 |
|
18593 | 341 |
if (sizeof(unsigned INT) == 8) { |
342 |
for ( ; (sp+4) <= last; sp += 4) { |
|
343 |
if (*(unsigned INT *)sp & 0xFF80FF80FF80FF80) { |
|
17621 | 344 |
RETURN ( true ); |
345 |
} |
|
346 |
} |
|
347 |
} |
|
348 |
#endif |
|
18593 | 349 |
if (sizeof(unsigned int) == 4) { |
350 |
for ( ; (sp+2) <= last; sp += 2) { |
|
17621 | 351 |
if (*(unsigned int *)sp & 0xFF80FF80) { |
352 |
RETURN ( true ); |
|
353 |
} |
|
354 |
} |
|
355 |
} |
|
18593 | 356 |
for ( ; sp < last; sp++) { |
17621 | 357 |
if (*sp & 0xFF80) { |
358 |
RETURN ( true ); |
|
359 |
} |
|
360 |
} |
|
361 |
RETURN (false); |
|
362 |
%}. |
|
363 |
||
364 |
" |
|
18593 | 365 |
'hello world' asUnicode16String containsNon7BitAscii |
366 |
'hello worldüäö' asUnicode16String containsNon7BitAscii |
|
367 |
'ü' asUnicode16String containsNon7BitAscii |
|
368 |
'aü' asUnicode16String containsNon7BitAscii |
|
369 |
'aaü' asUnicode16String containsNon7BitAscii |
|
370 |
'aaaü' asUnicode16String containsNon7BitAscii |
|
371 |
'aaaaü' asUnicode16String containsNon7BitAscii |
|
372 |
'aaaaa' asUnicode16String containsNon7BitAscii |
|
17621 | 373 |
" |
374 |
! |
|
375 |
||
14557 | 376 |
isWideString |
377 |
^ true |
|
608 | 378 |
! ! |
379 |
||
631 | 380 |
!TwoByteString class methodsFor:'documentation'! |
381 |
||
382 |
version |
|
18576 | 383 |
^ '$Header$' |
631 | 384 |
! ! |
8094
d05f69bd0097
Use #codePoint instead of deprecated #asciiValue
Stefan Vogel <sv@exept.de>
parents:
5761
diff
changeset
|
385 |
|
16750 | 386 |
|
996 | 387 |
TwoByteString initialize! |