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