author | Claus Gittinger <cg@exept.de> |
Thu, 04 Jan 1996 18:02:20 +0100 | |
changeset 834 | c68ed1088b42 |
parent 833 | b5485d985d65 |
child 835 | 8bd6f4aa8130 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
159 | 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 |
" |
|
12 |
||
255 | 13 |
CharacterArray subclass:#String |
608 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Text' |
|
88 | 18 |
! |
19 |
||
216 | 20 |
!String primitiveDefinitions! |
88 | 21 |
%{ |
22 |
#include <stdio.h> |
|
437 | 23 |
#define _STDIO_H_INCLUDED_ |
24 |
||
88 | 25 |
#include <ctype.h> |
359 | 26 |
|
273 | 27 |
#ifdef LINUX |
359 | 28 |
# define __STRINGDEFS__ |
29 |
# include <linuxIntern.h> |
|
302 | 30 |
#else |
31 |
extern char *strchr(); |
|
273 | 32 |
#endif |
95 | 33 |
|
34 |
/* |
|
35 |
* old st/x creates strings with spaces in it; |
|
36 |
* new st/x will fill it with zeros (for st-80 compatibility) |
|
37 |
* the define below sets old behavior. |
|
38 |
*/ |
|
39 |
#define INITIALIZE_WITH_SPACE |
|
88 | 40 |
%} |
216 | 41 |
! ! |
88 | 42 |
|
608 | 43 |
!String class methodsFor:'documentation'! |
44 |
||
45 |
copyright |
|
46 |
" |
|
47 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
48 |
All Rights Reserved |
|
49 |
||
50 |
This software is furnished under a license and may be used |
|
51 |
only in accordance with the terms of that license and with the |
|
52 |
inclusion of the above copyright notice. This software may not |
|
53 |
be provided or otherwise made available to, or used by, any |
|
54 |
other person. No title to or ownership of the software is |
|
55 |
hereby transferred. |
|
56 |
" |
|
57 |
! |
|
3 | 58 |
|
608 | 59 |
documentation |
60 |
" |
|
61 |
Strings are ByteArrays storing Characters. |
|
62 |
||
63 |
Strings are kind of kludgy: to allow for easy handling by c-functions, |
|
64 |
there is always one 0-byte added at the end, which is not counted |
|
65 |
in size. also, the at:put: method does not allow for storing 0-bytes. |
|
66 |
(to do this, the basicAt:put: and basicNew: methods are redefined) |
|
3 | 67 |
|
608 | 68 |
You cannot add any instvars to String, since the the run time system & compiler |
69 |
creates literal strings and knows that strings have no named instvars. |
|
70 |
||
71 |
If you really need strings with instVars, you have to create a subclass |
|
72 |
of String (the access functions defined here can handle this). |
|
73 |
A little warning though: not all smalltalk systems allow subclassing String, |
|
74 |
so your program may become unportable if you do so. |
|
75 |
" |
|
3 | 76 |
! ! |
77 |
||
1 | 78 |
!String class methodsFor:'instance creation'! |
79 |
||
80 |
basicNew:anInteger |
|
81 |
"return a new empty string with anInteger characters" |
|
82 |
||
83 |
%{ /* NOCONTEXT */ |
|
84 |
||
85 |
OBJ newString; |
|
86 |
REGISTER int len; |
|
87 |
REGISTER unsigned char *cp; |
|
88 |
REGISTER OBJ *op; |
|
89 |
int nInstVars, instsize; |
|
90 |
||
252 | 91 |
if (__isSmallInteger(anInteger)) { |
159 | 92 |
len = _intVal(anInteger); |
93 |
if (len >= 0) { |
|
95 | 94 |
if (self == String) { |
95 |
instsize = OHDR_SIZE + len + 1; |
|
96 |
if (_CanDoQuickNew(instsize)) { |
|
97 |
/* |
|
98 |
* the most common case |
|
99 |
*/ |
|
159 | 100 |
_qCheckedNew(newString, instsize); |
101 |
_InstPtr(newString)->o_class = self; |
|
368 | 102 |
__qSTORE(newString, self); |
103 |
||
159 | 104 |
cp = _stringVal(newString); |
357 | 105 |
#if defined(memset4) && !defined(NON_ASCII) |
106 |
instsize = len >> 2; |
|
107 |
if (len & 3) instsize++; |
|
108 |
memset4(cp, 0x20202020, instsize); |
|
159 | 109 |
*(cp + len) = '\0'; |
95 | 110 |
#else |
357 | 111 |
# ifdef FAST_MEMSET |
112 |
memset(cp, ' ', len); |
|
113 |
*(cp + len) = '\0'; |
|
114 |
# else |
|
159 | 115 |
while (len >= 8) { |
357 | 116 |
# ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
117 |
((int *)cp)[0] = 0x20202020; |
|
118 |
((int *)cp)[1] = 0x20202020; |
|
119 |
# else |
|
159 | 120 |
cp[0] = cp[1] = cp[2] = cp[3] = ' '; |
121 |
cp[4] = cp[5] = cp[6] = cp[7] = ' '; |
|
357 | 122 |
# endif |
159 | 123 |
cp += 8; |
95 | 124 |
len -= 8; |
159 | 125 |
} |
126 |
while (len--) |
|
127 |
*cp++ = ' '; |
|
128 |
*cp = '\0'; |
|
357 | 129 |
# endif |
95 | 130 |
#endif |
159 | 131 |
RETURN (newString); |
95 | 132 |
} |
133 |
nInstVars = 0; |
|
134 |
} else { |
|
135 |
nInstVars = _intVal(_ClassInstPtr(self)->c_ninstvars); |
|
159 | 136 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars) + len + 1; |
95 | 137 |
} |
138 |
||
834 | 139 |
__PROTECT_CONTEXT__ |
159 | 140 |
_qNew(newString, instsize, SENDER); |
834 | 141 |
__UNPROTECT_CONTEXT__ |
159 | 142 |
if (newString == nil) goto fail; |
143 |
_InstPtr(newString)->o_class = self; |
|
368 | 144 |
__qSTORE(newString, self); |
1 | 145 |
|
95 | 146 |
if (nInstVars) { |
1 | 147 |
#if defined(memset4) |
159 | 148 |
memset4(_InstPtr(newString)->i_instvars, nil, nInstVars); |
1 | 149 |
#else |
150 |
# if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES) |
|
159 | 151 |
/* |
152 |
* knowing that nil is 0 |
|
153 |
*/ |
|
154 |
memset(_InstPtr(newString)->i_instvars, 0, __OBJS2BYTES__(nInstVars)); |
|
1 | 155 |
# else |
159 | 156 |
op = _InstPtr(newString)->i_instvars; |
157 |
do { |
|
158 |
*op++ = nil; |
|
159 |
} while (--nInstVars); |
|
1 | 160 |
# endif |
161 |
#endif |
|
159 | 162 |
cp = _stringVal(newString) + __OBJS2BYTES__(nInstVars); |
163 |
} else { |
|
95 | 164 |
cp = _stringVal(newString); |
165 |
} |
|
1 | 166 |
|
167 |
#ifdef FAST_MEMSET |
|
159 | 168 |
memset(cp, ' ', len); |
169 |
*(cp + len) = '\0'; |
|
1 | 170 |
#else |
159 | 171 |
while (len >= 8) { |
357 | 172 |
# ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
173 |
((int *)cp)[0] = 0x20202020; |
|
174 |
((int *)cp)[1] = 0x20202020; |
|
175 |
# else |
|
159 | 176 |
cp[0] = cp[1] = cp[2] = cp[3] = ' '; |
177 |
cp[4] = cp[5] = cp[6] = cp[7] = ' '; |
|
357 | 178 |
# endif |
159 | 179 |
cp += 8; |
180 |
len -= 8; |
|
181 |
} |
|
182 |
while (len--) |
|
183 |
*cp++ = ' '; |
|
184 |
*cp = '\0'; |
|
1 | 185 |
#endif |
159 | 186 |
RETURN (newString); |
187 |
} |
|
1 | 188 |
} |
95 | 189 |
fail: ;; |
1 | 190 |
%} |
191 |
. |
|
95 | 192 |
" |
193 |
invalid argument, or out-of-memory: |
|
194 |
use error handling in superclass |
|
195 |
" |
|
1 | 196 |
^ (super basicNew:anInteger) atAllPut:(Character space) |
197 |
! |
|
198 |
||
608 | 199 |
new:n |
200 |
^ self basicNew:n |
|
347 | 201 |
! |
202 |
||
203 |
readFrom:aStream onError:exceptionBlock |
|
204 |
"read & return the next String from the (character-)stream aStream; |
|
205 |
skipping all whitespace first; return the value of exceptionBlock, |
|
206 |
if no string can be read. The sequence of characters as read from the |
|
207 |
stream must be one as stored via storeOn: or storeString." |
|
208 |
||
209 |
|collected char| |
|
210 |
||
211 |
" |
|
212 |
this method is not to be inherited |
|
213 |
(i.e. not ok for subclasses; Symbol, for example) |
|
214 |
" |
|
215 |
self ~~ String ifTrue:[ |
|
216 |
^ super readFrom:aStream onError:exceptionBlock |
|
217 |
]. |
|
218 |
||
219 |
"skip whiteSpace" |
|
220 |
aStream skipSeparators. |
|
221 |
(aStream next == $') ifTrue:[ |
|
222 |
collected := WriteStream on:(String new). |
|
223 |
[true] whileTrue:[ |
|
224 |
aStream atEnd ifTrue:[ |
|
225 |
"/ mhmh - reached the end without a closing quote |
|
226 |
"/ looks like an error to me ... |
|
227 |
^ exceptionBlock value |
|
228 |
]. |
|
229 |
char := aStream next. |
|
230 |
char == $' ifTrue:[ |
|
231 |
"/ look for another quote |
|
232 |
aStream peek == $' ifFalse:[ |
|
233 |
^ collected contents |
|
234 |
]. |
|
235 |
aStream next. |
|
236 |
]. |
|
237 |
collected nextPut:char |
|
238 |
] |
|
239 |
]. |
|
240 |
^ exceptionBlock value |
|
241 |
||
242 |
" |
|
243 |
String readFrom:('''hello world''' readStream) |
|
244 |
String readFrom:('''hello '''' world''' readStream) |
|
245 |
String readFrom:('1 ''hello'' ' readStream) |
|
246 |
String readFrom:('1 ''hello'' ' readStream) onError:['foobar'] |
|
247 |
" |
|
608 | 248 |
! |
249 |
||
250 |
unititializedNew:anInteger |
|
251 |
"redefine it back - strings must have a 0-byte at the end" |
|
252 |
||
253 |
^ self basicNew:anInteger |
|
254 |
! ! |
|
255 |
||
256 |
!String class methodsFor:'binary storage'! |
|
257 |
||
258 |
binaryDefinitionFrom:stream manager:manager |
|
259 |
"read a binary representation from stream." |
|
260 |
||
261 |
^ (stream next:(stream nextNumber: 4)) asString |
|
262 |
! ! |
|
263 |
||
264 |
!String class methodsFor:'queries'! |
|
265 |
||
266 |
isBuiltInClass |
|
267 |
"this class is known by the run-time-system" |
|
268 |
||
269 |
^ self == String |
|
1 | 270 |
! ! |
271 |
||
272 |
!String methodsFor:'accessing'! |
|
273 |
||
329 | 274 |
at:index |
275 |
"return the character at position index, an Integer. |
|
345 | 276 |
Reimplemented here to avoid the additional at:->basicAt: send |
277 |
(which we can do here, since at: is obviously not redefined in a subclass). |
|
329 | 278 |
This method is the same as at:." |
279 |
||
280 |
%{ /* NOCONTEXT */ |
|
281 |
||
282 |
REGISTER int indx; |
|
283 |
REGISTER OBJ cls; |
|
284 |
||
285 |
if (__isSmallInteger(index)) { |
|
286 |
indx = _intVal(index); |
|
287 |
if (indx > 0) { |
|
288 |
cls = __qClass(self); |
|
289 |
if (cls != String) |
|
345 | 290 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
329 | 291 |
if (indx <= (_stringSize(self))) { |
345 | 292 |
RETURN ( _MKCHARACTER(_stringVal(self)[indx-1] & 0xFF) ); |
329 | 293 |
} |
294 |
} |
|
295 |
} |
|
296 |
%}. |
|
297 |
^ self subscriptBoundsError:index |
|
1 | 298 |
! |
299 |
||
329 | 300 |
at:index put:aCharacter |
301 |
"store the argument, aCharacter at position index, an Integer. |
|
345 | 302 |
Reimplemented here to avoid the additional at:put:->basicAt:put: send |
303 |
(but only for Strings, since subclasses may redefine basicAt:put:). |
|
329 | 304 |
This method is the same as basicAt:put:." |
305 |
||
306 |
%{ /* NOCONTEXT */ |
|
307 |
||
308 |
REGISTER int value, indx; |
|
309 |
||
345 | 310 |
if (__isString(self)) { |
311 |
if (__isCharacter(aCharacter)) { |
|
312 |
value = _intVal(_characterVal(aCharacter)); |
|
422 | 313 |
#ifdef OLD |
345 | 314 |
if ((value > 0) |
315 |
&& (value <= 255) |
|
422 | 316 |
#else |
317 |
if (((value & ~0xFF) == 0) |
|
318 |
#endif |
|
345 | 319 |
&& __isSmallInteger(index)) { |
320 |
indx = _intVal(index); |
|
321 |
if (indx > 0) { |
|
322 |
if (indx <= (_stringSize(self))) { |
|
323 |
_stringVal(self)[indx-1] = value; |
|
324 |
RETURN ( aCharacter ); |
|
325 |
} |
|
329 | 326 |
} |
327 |
} |
|
328 |
} |
|
329 |
} |
|
330 |
%}. |
|
345 | 331 |
(self isMemberOf:String) ifFalse:[ |
332 |
^ super at:index put:aCharacter |
|
333 |
]. |
|
334 |
||
329 | 335 |
(aCharacter isMemberOf:Character) ifFalse:[ |
336 |
" |
|
337 |
tried to store something which is not a character |
|
338 |
" |
|
339 |
^ self elementNotCharacter |
|
340 |
]. |
|
341 |
(aCharacter asciiValue between:1 and:255) ifFalse:[ |
|
342 |
" |
|
343 |
tried to store a multibyte character |
|
344 |
" |
|
345 |
^ self elementBoundsError |
|
346 |
]. |
|
347 |
" |
|
348 |
invalid index |
|
349 |
" |
|
350 |
^ self subscriptBoundsError:index |
|
351 |
! |
|
352 |
||
608 | 353 |
basicAt:index |
354 |
"return the character at position index, an Integer |
|
355 |
- reimplemented here since we return characters" |
|
356 |
||
357 |
%{ /* NOCONTEXT */ |
|
358 |
||
359 |
REGISTER int indx; |
|
360 |
REGISTER OBJ cls; |
|
361 |
||
362 |
if (__isSmallInteger(index)) { |
|
363 |
indx = _intVal(index); |
|
364 |
if (indx > 0) { |
|
365 |
cls = __qClass(self); |
|
366 |
if (cls != String) |
|
367 |
indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
368 |
if (indx <= (_stringSize(self))) { |
|
369 |
RETURN ( _MKCHARACTER(_stringVal(self)[indx-1] & 0xFF) ); |
|
370 |
} |
|
371 |
} |
|
372 |
} |
|
373 |
%}. |
|
374 |
^ self subscriptBoundsError:index |
|
375 |
! |
|
376 |
||
1 | 377 |
basicAt:index put:aCharacter |
378 |
"store the argument, aCharacter at position index, an Integer |
|
345 | 379 |
- reimplemented here since we store characters |
380 |
(but only for Strings, since subclasses may redefine basicAt:put:)." |
|
1 | 381 |
|
382 |
%{ /* NOCONTEXT */ |
|
383 |
||
384 |
REGISTER int value, indx; |
|
385 |
||
345 | 386 |
if (__isString(self)) { |
387 |
if (__isCharacter(aCharacter)) { |
|
388 |
value = _intVal(_characterVal(aCharacter)); |
|
389 |
if ((value > 0) |
|
390 |
&& (value <= 255) |
|
391 |
&& __isSmallInteger(index)) { |
|
392 |
indx = _intVal(index); |
|
393 |
if (indx > 0) { |
|
394 |
if (indx <= (_stringSize(self))) { |
|
395 |
_stringVal(self)[indx-1] = value; |
|
396 |
RETURN ( aCharacter ); |
|
397 |
} |
|
329 | 398 |
} |
159 | 399 |
} |
400 |
} |
|
1 | 401 |
} |
159 | 402 |
%}. |
345 | 403 |
(self isMemberOf:String) ifFalse:[ |
404 |
^ super basicAt:index put:aCharacter |
|
405 |
]. |
|
406 |
||
1 | 407 |
(aCharacter isMemberOf:Character) ifFalse:[ |
299 | 408 |
" |
409 |
tried to store something which is not a character |
|
410 |
" |
|
159 | 411 |
^ self elementNotCharacter |
299 | 412 |
]. |
413 |
(aCharacter asciiValue between:1 and:255) ifFalse:[ |
|
414 |
" |
|
415 |
tried to store a multibyte character |
|
416 |
" |
|
417 |
^ self elementBoundsError |
|
418 |
]. |
|
419 |
" |
|
420 |
invalid index |
|
421 |
" |
|
422 |
^ self subscriptBoundsError:index |
|
429 | 423 |
! |
424 |
||
608 | 425 |
basicSize |
426 |
"return the number of characters in myself. |
|
427 |
Redefined here to exclude the 0-byte at the end." |
|
428 |
||
429 |
%{ /* NOCONTEXT */ |
|
430 |
REGISTER OBJ cls; |
|
431 |
||
432 |
cls = __qClass(self); |
|
433 |
if (cls == String) { |
|
434 |
RETURN ( _MKSMALLINT(_stringSize(self)) ); |
|
435 |
} |
|
436 |
RETURN ( _MKSMALLINT(_stringSize(self) |
|
437 |
- __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)))); |
|
438 |
%} |
|
439 |
! |
|
429 | 440 |
|
608 | 441 |
size |
442 |
"return the number of characters in myself. |
|
443 |
Reimplemented here to avoid the additional size->basicSize send |
|
444 |
(which we can do here, since size is obviously not redefined in a subclass). |
|
445 |
This method is the same as basicSize." |
|
429 | 446 |
|
608 | 447 |
%{ /* NOCONTEXT */ |
448 |
REGISTER OBJ cls; |
|
449 |
||
450 |
cls = __qClass(self); |
|
451 |
if (cls == String) { |
|
452 |
RETURN ( _MKSMALLINT(_stringSize(self)) ); |
|
429 | 453 |
} |
608 | 454 |
RETURN ( _MKSMALLINT(_stringSize(self) |
455 |
- __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)))); |
|
456 |
%} |
|
2 | 457 |
! ! |
458 |
||
276 | 459 |
!String methodsFor:'binary storage'! |
460 |
||
461 |
storeBinaryOn:stream manager:manager |
|
462 |
"append a binary representation of the receiver onto stream. |
|
463 |
Redefined to store short Strings in a more compact way with a |
|
464 |
special type code." |
|
465 |
||
286 | 466 |
|sz "{ Class: SmallInteger }"| |
467 |
||
468 |
sz := self size. |
|
276 | 469 |
"not, if I am a thingy of a subclass" |
286 | 470 |
((self class ~~ String) or:[sz > 255]) ifTrue:[ |
276 | 471 |
^ super storeBinaryOn:stream manager:manager |
472 |
]. |
|
473 |
stream nextPut:manager codeForString. |
|
286 | 474 |
stream nextPut:sz. |
475 |
1 to:sz do:[:index | |
|
276 | 476 |
stream nextPut:(self basicAt:index) asciiValue |
477 |
] |
|
1 | 478 |
! ! |
479 |
||
53 | 480 |
!String methodsFor:'character searching'! |
1 | 481 |
|
482 |
includes:aCharacter |
|
483 |
"return true if the argument, aCharacter is included in the receiver |
|
484 |
- reimplemented here for speed" |
|
485 |
||
486 |
%{ /* NOCONTEXT */ |
|
487 |
||
488 |
REGISTER unsigned char *cp; |
|
489 |
REGISTER int byteValue; |
|
53 | 490 |
OBJ cls; |
1 | 491 |
|
53 | 492 |
if (__isCharacter(aCharacter)) { |
159 | 493 |
byteValue = _intVal(_characterVal(aCharacter)); |
494 |
cp = _stringVal(self); |
|
329 | 495 |
if ((cls = __qClass(self)) != String) |
159 | 496 |
cp += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
1 | 497 |
#ifdef FAST_STRCHR |
159 | 498 |
cp = (unsigned char *) strchr(cp, _intVal(_characterVal(aCharacter))); |
499 |
if (cp) { |
|
500 |
RETURN ( true ); |
|
501 |
} |
|
1 | 502 |
#else |
159 | 503 |
while (*cp) { |
504 |
if (*cp == byteValue) { |
|
505 |
RETURN ( true ); |
|
506 |
} |
|
507 |
cp++; |
|
508 |
} |
|
1 | 509 |
#endif |
510 |
} |
|
299 | 511 |
%}. |
1 | 512 |
^ false |
53 | 513 |
|
95 | 514 |
" |
515 |
'hello world' includes:$a |
|
516 |
'hello world' includes:$o |
|
517 |
'hello world' includes:$x |
|
518 |
'hello world' includes:1 |
|
299 | 519 |
'hello world' asTwoByteString includes:$o |
95 | 520 |
" |
1 | 521 |
! |
522 |
||
10 | 523 |
includesAny:aCollection |
524 |
"return true, if the receiver includes any of the characters in the |
|
53 | 525 |
argument, aCollection. |
526 |
- redefined for speed if the argument is a String." |
|
10 | 527 |
|
528 |
%{ /* NOCONTEXT */ |
|
529 |
||
530 |
REGISTER unsigned char *cp; |
|
531 |
REGISTER unsigned char *matchP; |
|
53 | 532 |
OBJ cls; |
10 | 533 |
|
53 | 534 |
if (__isString(aCollection)) { |
159 | 535 |
matchP = _stringVal(aCollection); |
536 |
cp = _stringVal(self); |
|
329 | 537 |
if ((cls = __qClass(self)) != String) |
159 | 538 |
cp += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
53 | 539 |
|
159 | 540 |
while (*cp) { |
329 | 541 |
if (strchr(matchP, *cp)) { |
159 | 542 |
RETURN ( true ); |
543 |
} |
|
544 |
cp++; |
|
545 |
} |
|
546 |
RETURN ( false ); |
|
10 | 547 |
} |
548 |
%} |
|
549 |
. |
|
550 |
^ super includesAny:aCollection |
|
95 | 551 |
|
552 |
" |
|
553 |
'hello world' includesAny:'abcd' |
|
554 |
'hello world' includesAny:'xyz' |
|
555 |
'hello world' includesAny:(Array with:$a with:$b with:$d) |
|
556 |
'hello world' includesAny:(Array with:$x with:$y) |
|
557 |
'hello world' includesAny:(Array with:1 with:2) |
|
558 |
" |
|
10 | 559 |
! |
560 |
||
1 | 561 |
indexOf:aCharacter |
562 |
"return the index of the first occurrences of the argument, aCharacter |
|
563 |
in the receiver or 0 if not found - reimplemented here for speed." |
|
564 |
||
565 |
%{ /* NOCONTEXT */ |
|
566 |
||
567 |
REGISTER unsigned char *cp; |
|
568 |
#ifdef FAST_STRCHR |
|
569 |
char *strchr(); |
|
570 |
#else |
|
571 |
REGISTER int byteValue; |
|
572 |
REGISTER int index; |
|
370 | 573 |
char c; |
1 | 574 |
#endif |
53 | 575 |
OBJ cls; |
1 | 576 |
|
53 | 577 |
if (__isCharacter(aCharacter)) { |
159 | 578 |
cp = _stringVal(self); |
329 | 579 |
if ((cls = __qClass(self)) != String) |
159 | 580 |
cp += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
1 | 581 |
#ifdef FAST_STRCHR |
159 | 582 |
cp = (unsigned char *) strchr(cp, _intVal(_characterVal(aCharacter))); |
583 |
if (cp) { |
|
584 |
RETURN ( _MKSMALLINT(cp - _stringVal(self) + 1) ); |
|
585 |
} |
|
1 | 586 |
#else |
159 | 587 |
byteValue = _intVal(_characterVal(aCharacter)); |
588 |
index = 1; |
|
370 | 589 |
while (c = *cp++) { |
590 |
if (c == byteValue) { RETURN ( _MKSMALLINT(index) ); } |
|
159 | 591 |
index++; |
592 |
} |
|
1 | 593 |
#endif |
594 |
} |
|
370 | 595 |
%}. |
596 |
"/ cannot include anything but characters ... |
|
1 | 597 |
^ 0 |
95 | 598 |
|
599 |
" |
|
600 |
'hello world' indexOf:(Character space) |
|
601 |
'hello world' indexOf:$A |
|
602 |
" |
|
1 | 603 |
! |
604 |
||
605 |
indexOf:aCharacter startingAt:start |
|
606 |
"return the index of the first occurrence of the argument, aCharacter |
|
607 |
in myself starting at start, anInteger or 0 if not found; |
|
608 |
- reimplemented here for speed" |
|
609 |
||
610 |
%{ /* NOCONTEXT */ |
|
611 |
||
612 |
REGISTER unsigned char *cp; |
|
613 |
REGISTER int index, byteValue; |
|
614 |
#ifdef FAST_STRCHR |
|
615 |
char *strchr(); |
|
370 | 616 |
#else |
617 |
char c; |
|
1 | 618 |
#endif |
619 |
int len; |
|
53 | 620 |
OBJ cls; |
1 | 621 |
|
252 | 622 |
if (__isSmallInteger(start)) { |
159 | 623 |
if (__isCharacter(aCharacter)) { |
624 |
byteValue = _intVal(_characterVal(aCharacter)); |
|
625 |
index = _intVal(start); |
|
626 |
if (index <= 0) |
|
627 |
index = 1; |
|
329 | 628 |
if ((cls = __qClass(self)) != String) |
159 | 629 |
index += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
630 |
len = _stringSize(self); |
|
631 |
if (index <= len) { |
|
632 |
cp = _stringVal(self) + index - 1; |
|
1 | 633 |
#ifdef FAST_STRCHR |
159 | 634 |
cp = (unsigned char *) strchr(cp, byteValue); |
635 |
if (cp) { |
|
636 |
RETURN ( _MKSMALLINT(cp - _stringVal(self) + 1) ); |
|
637 |
} |
|
1 | 638 |
#else |
370 | 639 |
while (c = *cp++) { |
640 |
if (c == byteValue) { |
|
159 | 641 |
RETURN ( _MKSMALLINT(index) ); |
642 |
} |
|
643 |
index++; |
|
644 |
} |
|
1 | 645 |
#endif |
159 | 646 |
} |
647 |
} |
|
648 |
RETURN ( _MKSMALLINT(0) ); |
|
1 | 649 |
} |
370 | 650 |
%}. |
1 | 651 |
^ super indexOf:aCharacter startingAt:start |
53 | 652 |
|
95 | 653 |
" |
654 |
'hello world' indexOf:$l startingAt:1 |
|
655 |
'hello world' indexOf:$l startingAt:5 |
|
656 |
" |
|
1 | 657 |
! |
658 |
||
516
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
659 |
indexOfAny:aCollectionOfCharacters startingAt:start |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
660 |
"return the index of the first occurrence of any character in aCollectionOfCharacters, |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
661 |
in myself starting at start, anInteger or 0 if not found; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
662 |
- reimplemented here for speed if aCollectionOfCharacters is a string." |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
663 |
|
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
664 |
%{ /* NOCONTEXT */ |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
665 |
|
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
666 |
REGISTER unsigned char *ccp; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
667 |
REGISTER unsigned char *cp; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
668 |
REGISTER int index; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
669 |
char *matchP; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
670 |
char c; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
671 |
int len; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
672 |
OBJ cls; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
673 |
|
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
674 |
if (__isSmallInteger(start) |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
675 |
&& __isString(aCollectionOfCharacters)) { |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
676 |
matchP = __stringVal(aCollectionOfCharacters); |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
677 |
index = _intVal(start); |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
678 |
if (index <= 0) |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
679 |
index = 1; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
680 |
if ((cls = __qClass(self)) != String) |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
681 |
index += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
682 |
len = _stringSize(self); |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
683 |
if (index <= len) { |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
684 |
cp = _stringVal(self) + index - 1; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
685 |
while (c = *cp++) { |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
686 |
for (ccp = matchP; *ccp ; ccp++) { |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
687 |
if (*ccp == c) { |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
688 |
RETURN ( _MKSMALLINT(index) ); |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
689 |
} |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
690 |
} |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
691 |
index++; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
692 |
} |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
693 |
} |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
694 |
RETURN ( _MKSMALLINT(0) ); |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
695 |
} |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
696 |
%}. |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
697 |
"/ |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
698 |
"/ fallback: 1st argument not a string or error |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
699 |
"/ |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
700 |
^ super indexOfAny:aCollectionOfCharacters startingAt:start |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
701 |
|
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
702 |
" |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
703 |
'hello world' indexOfAny:'eoa' startingAt:1 |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
704 |
'hello world' indexOfAny:'eoa' startingAt:6 |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
705 |
'hello world' indexOfAny:'AOE' startingAt:1 |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
706 |
" |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
707 |
! |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
708 |
|
1 | 709 |
indexOfSeparatorStartingAt:start |
710 |
"return the index of the next separator character" |
|
711 |
||
712 |
%{ /* NOCONTEXT */ |
|
713 |
||
714 |
REGISTER unsigned char *cp; |
|
715 |
REGISTER char c; |
|
716 |
int len, index; |
|
53 | 717 |
OBJ cls; |
1 | 718 |
|
719 |
index = _intVal(start); |
|
720 |
if (index <= 0) { |
|
159 | 721 |
index = 1; |
1 | 722 |
} |
329 | 723 |
if ((cls = __qClass(self)) != String) |
159 | 724 |
index += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
1 | 725 |
len = _stringSize(self); |
726 |
if (index > len) { |
|
159 | 727 |
RETURN ( _MKSMALLINT(0) ); |
1 | 728 |
} |
729 |
cp = _stringVal(self) + index - 1; |
|
730 |
while (c = *cp++) { |
|
329 | 731 |
#ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
159 | 732 |
if (c <= ' ') |
1 | 733 |
#endif |
159 | 734 |
if ((c == ' ') || (c == '\t') || (c == '\n') |
735 |
|| (c == '\r') || (c == '\f')) { |
|
736 |
RETURN ( _MKSMALLINT(cp - _stringVal(self)) ); |
|
737 |
} |
|
1 | 738 |
} |
739 |
%} |
|
740 |
. |
|
741 |
^ 0 |
|
53 | 742 |
|
95 | 743 |
" |
744 |
'hello world' indexOfSeparatorStartingAt:3 |
|
745 |
'hello world' indexOfSeparatorStartingAt:7 |
|
746 |
" |
|
608 | 747 |
! |
748 |
||
749 |
occurrencesOf:aCharacter |
|
750 |
"count the occurrences of the argument, aCharacter in myself |
|
751 |
- reimplemented here for speed" |
|
752 |
||
753 |
%{ /* NOCONTEXT */ |
|
754 |
||
755 |
REGISTER unsigned char *cp; |
|
756 |
REGISTER int byteValue; |
|
757 |
REGISTER int count; |
|
758 |
OBJ cls; |
|
759 |
||
760 |
if (__isCharacter(aCharacter)) { |
|
761 |
count = 0; |
|
762 |
byteValue = _intVal(_characterVal(aCharacter)); |
|
763 |
cp = _stringVal(self); |
|
764 |
if ((cls = __qClass(self)) != String) |
|
765 |
cp += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
766 |
while (*cp) { |
|
767 |
if (*cp++ == byteValue) count++; |
|
768 |
} |
|
769 |
RETURN ( _MKSMALLINT(count) ); |
|
770 |
} |
|
771 |
%}. |
|
772 |
^ 0 |
|
773 |
||
774 |
" |
|
775 |
'hello world' occurrencesOf:$a |
|
776 |
'hello world' occurrencesOf:$w |
|
777 |
'hello world' occurrencesOf:$l |
|
778 |
'hello world' occurrencesOf:$x |
|
779 |
'hello world' occurrencesOf:1 |
|
780 |
" |
|
53 | 781 |
! ! |
782 |
||
608 | 783 |
!String methodsFor:'comparing'! |
2 | 784 |
|
608 | 785 |
= aString |
786 |
"Compare the receiver with the argument and return true if the |
|
787 |
receiver is equal to the argument. Otherwise return false. |
|
788 |
This compare is case-sensitive (i.e. 'Foo' is NOT = 'foo'). |
|
789 |
Use sameAs: to compare with case ignored." |
|
1 | 790 |
|
375 | 791 |
%{ /* NOCONTEXT */ |
792 |
||
608 | 793 |
int l1, l2; |
794 |
REGISTER OBJ s = aString; |
|
795 |
char *cp1, *cp2; |
|
796 |
OBJ cls; |
|
797 |
||
798 |
if (s == self) { |
|
799 |
RETURN ( true ); |
|
800 |
} |
|
801 |
if (! __isNonNilObject(s)) { |
|
802 |
RETURN ( false ); |
|
803 |
} |
|
1 | 804 |
|
608 | 805 |
if (((cls = __qClass(s)) == String) || (cls == Symbol) || (cls == __qClass(self))) { |
806 |
cp1 = (char *) _stringVal(self); |
|
807 |
l1 = _stringSize(self); |
|
808 |
/* |
|
809 |
* care for instances of subclasses ... |
|
810 |
*/ |
|
811 |
if (__qClass(self) != String) { |
|
812 |
int n = __OBJS2BYTES__(_intVal(_ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
813 |
||
814 |
cp1 += n; |
|
815 |
l1 -= n; |
|
379 | 816 |
} |
375 | 817 |
|
608 | 818 |
cp2 = (char *) _stringVal(s); |
819 |
l2 = _stringSize(s); |
|
820 |
/* |
|
821 |
* care for instances of subclasses ... |
|
822 |
*/ |
|
823 |
if (cls != String) { |
|
824 |
int n = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
825 |
||
826 |
cp2 += n; |
|
827 |
l2 -= n; |
|
375 | 828 |
} |
608 | 829 |
|
830 |
if (l1 != l2) { |
|
831 |
RETURN ( false ); |
|
832 |
} |
|
833 |
RETURN ( (strncmp(cp1, cp2, l1) == 0) ? true : false ); |
|
1 | 834 |
} |
608 | 835 |
%} |
836 |
. |
|
837 |
^ super = aString |
|
1 | 838 |
|
95 | 839 |
" |
608 | 840 |
'foo' = 'Foo' |
841 |
'foo' sameAs: 'Foo' |
|
95 | 842 |
" |
1 | 843 |
! |
844 |
||
608 | 845 |
> aString |
846 |
"Compare the receiver with the argument and return true if the |
|
847 |
receiver is greater than the argument. Otherwise return false. |
|
848 |
No national variants are honored; use after: for this. |
|
849 |
In contrast to ST-80, case differences are NOT ignored, thus |
|
850 |
'foo' > 'Foo' will return true. |
|
851 |
This may change." |
|
1 | 852 |
|
375 | 853 |
%{ /* NOCONTEXT */ |
854 |
||
608 | 855 |
int len1, len2, cmp; |
856 |
REGISTER OBJ s = aString; |
|
857 |
char *cp1, *cp2; |
|
858 |
OBJ cls; |
|
1 | 859 |
|
608 | 860 |
if (__isNonNilObject(s) |
861 |
&& (((cls = __qClass(s)) == String) || (cls == Symbol) || (cls == __qClass(self)))) { |
|
862 |
cp1 = (char *) _stringVal(self); |
|
863 |
len1 = _stringSize(self); |
|
864 |
||
865 |
/* |
|
866 |
* care for instances of subclasses ... |
|
867 |
*/ |
|
868 |
if (__qClass(self) != String) { |
|
869 |
int n = __OBJS2BYTES__(_intVal(_ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
870 |
||
871 |
cp1 += n; |
|
872 |
len1 -= n; |
|
379 | 873 |
} |
375 | 874 |
|
608 | 875 |
cp2 = (char *) _stringVal(s); |
876 |
len2 = _stringSize(s); |
|
877 |
/* |
|
878 |
* care for instances of subclasses ... |
|
879 |
*/ |
|
880 |
if (cls != String) { |
|
881 |
int n = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
882 |
||
883 |
cp2 += n; |
|
884 |
len2 -= n; |
|
375 | 885 |
} |
608 | 886 |
|
887 |
if (len1 <= len2) |
|
888 |
cmp = strncmp(cp1, cp2, len1); |
|
889 |
else |
|
890 |
cmp = strncmp(cp1, cp2, len2); |
|
891 |
||
892 |
if (cmp > 0) { |
|
893 |
RETURN ( true ); |
|
894 |
} |
|
895 |
if ((cmp == 0) && (len1 > len2)) { |
|
896 |
RETURN ( true ); |
|
897 |
} |
|
898 |
RETURN ( false ); |
|
1 | 899 |
} |
370 | 900 |
%}. |
608 | 901 |
^ super > aString |
902 |
! |
|
1 | 903 |
|
608 | 904 |
after:aString |
905 |
"Compare the receiver with the argument and return true if the |
|
906 |
receiver should come after the argument in a sorted list. |
|
907 |
Otherwise return false. |
|
908 |
The comparison is language specific, depending on the value of |
|
909 |
LC_COLLATE, which is initialized from the environment." |
|
1 | 910 |
|
911 |
%{ /* NOCONTEXT */ |
|
912 |
||
608 | 913 |
int cmp; |
914 |
REGISTER OBJ s = aString; |
|
915 |
char *cp1, *cp2; |
|
53 | 916 |
OBJ cls; |
1 | 917 |
|
608 | 918 |
if (__isNonNilObject(s) |
919 |
&& (((cls = __qClass(s)) == String) || (cls == Symbol) || (cls == __qClass(self)))) { |
|
920 |
cp1 = (char *) _stringVal(self); |
|
921 |
||
922 |
/* |
|
923 |
* care for instances of subclasses ... |
|
924 |
*/ |
|
925 |
if (__qClass(self) != String) { |
|
926 |
int n = __OBJS2BYTES__(_intVal(_ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
927 |
||
928 |
cp1 += n; |
|
929 |
} |
|
1 | 930 |
|
608 | 931 |
cp2 = (char *) _stringVal(s); |
932 |
/* |
|
933 |
* care for instances of subclasses ... |
|
934 |
*/ |
|
935 |
if (cls != String) { |
|
936 |
int n = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
937 |
||
938 |
cp2 += n; |
|
939 |
} |
|
940 |
||
941 |
#ifdef HAS_STRCOLL |
|
942 |
cmp = strcoll(cp1, cp2); |
|
943 |
#else |
|
944 |
cmp = strcmp(cp1, cp2); |
|
945 |
#endif |
|
946 |
||
947 |
if (cmp > 0) { |
|
948 |
RETURN ( true ); |
|
949 |
} |
|
950 |
RETURN ( false ); |
|
370 | 951 |
} |
329 | 952 |
%}. |
608 | 953 |
" |
954 |
currently, this operation is only defined for strings, symbols |
|
955 |
and subclasses. |
|
956 |
" |
|
957 |
self primitiveFailed |
|
1 | 958 |
! |
959 |
||
608 | 960 |
~= aString |
961 |
"Compare the receiver with the argument and return true if the |
|
962 |
receiver is not equal to the argument. Otherwise return false. |
|
963 |
This compare is case-sensitive (i.e. 'Foo' is NOT = 'foo')" |
|
1 | 964 |
|
608 | 965 |
%{ /* NOCONTEXT */ |
966 |
||
1 | 967 |
int l1, l2; |
608 | 968 |
REGISTER OBJ s = aString; |
969 |
char *cp1, *cp2; |
|
970 |
OBJ cls; |
|
1 | 971 |
|
608 | 972 |
if (s == self) { |
973 |
RETURN ( false ); |
|
974 |
} |
|
975 |
if (! __isNonNilObject(s)) { |
|
976 |
RETURN ( true ); |
|
977 |
} |
|
978 |
if (((cls = __qClass(s)) == String) || (cls == Symbol) || (cls == __qClass(self))) { |
|
979 |
cp1 = (char *) _stringVal(self); |
|
980 |
l1 = _stringSize(self); |
|
981 |
/* |
|
982 |
* care for instances of subclasses ... |
|
983 |
*/ |
|
984 |
if (__qClass(self) != String) { |
|
985 |
int n = __OBJS2BYTES__(_intVal(_ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
1 | 986 |
|
608 | 987 |
cp1 += n; |
988 |
l1 -= n; |
|
989 |
} |
|
990 |
||
991 |
cp2 = (char *) _stringVal(s); |
|
992 |
l2 = _stringSize(s); |
|
993 |
/* |
|
994 |
* care for instances of subclasses ... |
|
995 |
*/ |
|
996 |
if (cls != String) { |
|
997 |
int n = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
998 |
||
999 |
cp2 += n; |
|
1000 |
l2 -= n; |
|
159 | 1001 |
} |
62 | 1002 |
|
608 | 1003 |
if (l1 != l2) { |
1004 |
RETURN ( true ); |
|
1005 |
} |
|
1006 |
RETURN ( (strncmp(cp1, cp2, l1) == 0) ? false : true ); |
|
1007 |
} |
|
1008 |
%} |
|
1009 |
. |
|
1010 |
^ super ~= aString |
|
1011 |
! ! |
|
1012 |
||
1013 |
!String methodsFor:'converting'! |
|
1014 |
||
1015 |
asSymbol |
|
1016 |
"return a unique symbol with the name taken from myself. |
|
1017 |
The receiver must be a String, subclass instances are (currently) |
|
1018 |
not allowed." |
|
1019 |
%{ |
|
1020 |
OBJ newSymbol; |
|
62 | 1021 |
|
608 | 1022 |
if (__qClass(self) == String) { |
1023 |
newSymbol = _MKSYMBOL(_stringVal(self), (OBJ *)0, __context); |
|
1024 |
if (newSymbol) { |
|
1025 |
RETURN ( newSymbol); |
|
1026 |
} |
|
1027 |
} |
|
1028 |
%}. |
|
1029 |
self class ~~ String ifTrue:[ |
|
1030 |
"/ |
|
1031 |
"/ it is only allowed to as for string symbols - no subclass instances |
|
1032 |
"/ |
|
1033 |
^ self primitiveFailed |
|
1034 |
]. |
|
1035 |
^ ObjectMemory allocationFailureSignal raise. |
|
1036 |
! |
|
1037 |
||
1038 |
asSymbolIfInterned |
|
1039 |
"if a symbol with the receivers characters is already known, return it. |
|
1040 |
Otherwise, return nil. This can be used to query for an existing |
|
1041 |
symbol and is the same as |
|
1042 |
self knownAsSymbol ifTrue:[self asSymbol] ifFalse:[nil] |
|
1043 |
but slightly faster, since the symbol lookup operation is only |
|
1044 |
performed once. |
|
1045 |
The receiver must be a String, subclass instances are (currently) |
|
1046 |
not allowed." |
|
1 | 1047 |
|
608 | 1048 |
%{ /* NOCONTEXT */ |
1049 |
OBJ __SYMBOL_OR_NIL(); |
|
1050 |
||
1051 |
if (__qClass(self) == String) { |
|
1052 |
RETURN ( __SYMBOL_OR_NIL(_stringVal(self))); |
|
1053 |
} |
|
1054 |
%}. |
|
1055 |
self primitiveFailed |
|
1056 |
! |
|
1057 |
||
1058 |
literalArrayEncoding |
|
1059 |
"encode myself as an array literal." |
|
1060 |
||
1061 |
^ self |
|
1062 |
||
1063 |
"Modified: 1.9.1995 / 02:25:45 / claus" |
|
1064 |
! |
|
1065 |
||
1066 |
withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1067 |
"return a string with the characters of the receiver where all tabulator characters |
608 | 1068 |
are expanded into spaces (assuming 8-col tabs). |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1069 |
Notice: if the receiver does not contain any tabs, it is returned unchanged; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1070 |
otherwise a new string is returned. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1071 |
This does handle multiline strings. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1072 |
Rewritten for speed - because this is very heavily used when reading |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1073 |
big files in the FileBrowser (and therefore speeds up fileReading considerably)." |
608 | 1074 |
%{ |
1075 |
char buffer[80*8 + 10]; |
|
1076 |
char *srcP, *dstP, *cp0; |
|
1077 |
int idx, sz; |
|
1078 |
int any = 0; |
|
1079 |
OBJ newString; |
|
1080 |
OBJ __MKSTRING_L(); |
|
1081 |
char c; |
|
1082 |
||
1083 |
if (__qClass(self) == String) { |
|
1084 |
/* |
|
1085 |
* for small strings (< 80), do it without a prescan ... |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1086 |
* the buffer is large enough to even convert a |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1087 |
* receiver consisting fully of tabs. |
608 | 1088 |
*/ |
1089 |
if (__stringSize(self) < 80) { |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1090 |
idx = 1; |
608 | 1091 |
for (srcP = __stringVal(self), dstP = buffer; (c = *srcP); srcP++) { |
1092 |
if (c == '\t') { |
|
1093 |
any = 1; |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1094 |
while (idx % 8) { |
608 | 1095 |
idx++; |
1096 |
*dstP++ = ' '; |
|
159 | 1097 |
} |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1098 |
idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1099 |
*dstP++ = ' '; |
608 | 1100 |
} else { |
1101 |
*dstP++ = c; |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1102 |
idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1103 |
if (c == '\n') { |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1104 |
idx = 1; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1105 |
} |
608 | 1106 |
} |
1107 |
} |
|
1108 |
if (! any) RETURN(self); |
|
1109 |
*dstP = '\0'; |
|
1110 |
RETURN (__MKSTRING_L(buffer, (dstP-buffer))); |
|
1111 |
} |
|
1112 |
/* |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1113 |
* for large strings, we compute the new size, allocate a new string |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1114 |
* and expand it. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1115 |
* |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1116 |
* first, scan for size ... |
608 | 1117 |
*/ |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1118 |
idx = 1; |
608 | 1119 |
for (srcP = __stringVal(self), sz = 0; (c = *srcP); srcP++) { |
1120 |
if (c == '\t') { |
|
1121 |
any = 1; |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1122 |
while (idx % 8) { |
608 | 1123 |
idx++; |
1124 |
sz++; |
|
1125 |
} |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1126 |
idx++; sz ++; |
608 | 1127 |
} else { |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1128 |
sz++; idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1129 |
if (c == '\n') { |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1130 |
idx = 1; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1131 |
} |
159 | 1132 |
} |
1133 |
} |
|
608 | 1134 |
if (! any) RETURN(self); |
1 | 1135 |
|
608 | 1136 |
/* |
1137 |
* get the string |
|
1138 |
*/ |
|
1139 |
sz = OHDR_SIZE + sz + 1; |
|
1140 |
_qNew(newString, sz, __context); |
|
1141 |
if (newString != nil) { |
|
1142 |
_InstPtr(newString)->o_class = String; |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1143 |
|
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1144 |
/* |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1145 |
* expand |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1146 |
*/ |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1147 |
idx = 1; |
608 | 1148 |
for (srcP = __stringVal(self), dstP = cp0 = __stringVal(newString); (c = *srcP); srcP++) { |
1149 |
if (c == '\t') { |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1150 |
while (idx % 8) { |
608 | 1151 |
idx++; |
1152 |
*dstP++ = ' '; |
|
1153 |
} |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1154 |
idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1155 |
*dstP++ = ' '; |
608 | 1156 |
} else { |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1157 |
*dstP++ = c; idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1158 |
if (c == '\n') { |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1159 |
idx = 1; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1160 |
} |
608 | 1161 |
} |
1162 |
} |
|
1163 |
*dstP++ = '\0'; |
|
1164 |
RETURN (newString); |
|
1165 |
} |
|
1166 |
} |
|
1167 |
%}. |
|
1168 |
^ super withTabsExpanded |
|
1 | 1169 |
! ! |
1170 |
||
1171 |
!String methodsFor:'copying'! |
|
1172 |
||
1173 |
, aString |
|
2 | 1174 |
"return the concatenation of myself and the argument, aString as |
1175 |
a String. |
|
1 | 1176 |
- reimplemented here for speed" |
1177 |
||
1178 |
%{ |
|
95 | 1179 |
int l1, l2, sz; |
1 | 1180 |
char *cp1, *cp2; |
1181 |
REGISTER unsigned char *dstp; |
|
1182 |
REGISTER OBJ s = aString; |
|
329 | 1183 |
REGISTER OBJ _string = String; |
13 | 1184 |
OBJ myClass, argClass, newString; |
1 | 1185 |
|
329 | 1186 |
if (__isNonNilObject(s)) { |
1187 |
myClass = __qClass(self); |
|
1188 |
argClass = __qClass(s); |
|
95 | 1189 |
/* |
368 | 1190 |
* can do it here if |
95 | 1191 |
* either same class or both Strings/Symbols: |
1192 |
*/ |
|
159 | 1193 |
if ((myClass == argClass) |
329 | 1194 |
|| (((myClass == _string) || (myClass == Symbol)) |
1195 |
&& ((argClass == _string) || (argClass == Symbol)))) { |
|
159 | 1196 |
cp1 = (char *) _stringVal(self); |
1197 |
l1 = _stringSize(self); |
|
329 | 1198 |
if (myClass != _string) { |
1199 |
int n = __OBJS2BYTES__(_intVal(_ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
1 | 1200 |
|
159 | 1201 |
cp1 += n; |
1202 |
l1 -= n; |
|
1203 |
} |
|
1 | 1204 |
|
159 | 1205 |
cp2 = (char *) _stringVal(s); |
1206 |
l2 = _stringSize(s); |
|
329 | 1207 |
if (argClass != _string) { |
1208 |
int n = __OBJS2BYTES__(_intVal(_ClassInstPtr(__qClass(s))->c_ninstvars)); |
|
1 | 1209 |
|
159 | 1210 |
cp2 += n; |
1211 |
l2 -= n; |
|
1212 |
} |
|
1 | 1213 |
|
95 | 1214 |
sz = OHDR_SIZE + l1 + l2 + 1; |
159 | 1215 |
_qNew(newString, sz, __context); |
1216 |
if (newString != nil) { |
|
1217 |
_InstPtr(newString)->o_class = String; |
|
1218 |
dstp = _stringVal(newString); |
|
95 | 1219 |
/* |
1220 |
* refetch in case of a GC |
|
1221 |
*/ |
|
1222 |
cp1 = (char *) _stringVal(self); |
|
209 | 1223 |
cp2 = (char *) _stringVal(aString); |
1 | 1224 |
#ifdef FAST_MEMCPY |
159 | 1225 |
bcopy(cp1, dstp, l1); |
1226 |
bcopy(cp2, dstp + l1, l2+1); |
|
1 | 1227 |
#else |
1228 |
# ifdef FAST_STRCPY |
|
159 | 1229 |
strcpy(dstp, cp1); |
1230 |
strcpy(dstp + l1, cp2); |
|
1 | 1231 |
# else |
159 | 1232 |
while ((*dstp++ = *cp1++) != '\0') ; |
1233 |
dstp--; |
|
1234 |
while ((*dstp++ = *cp2++) != '\0') ; |
|
1 | 1235 |
# endif |
1236 |
#endif |
|
159 | 1237 |
RETURN ( newString ); |
1238 |
} |
|
1239 |
} |
|
1 | 1240 |
} |
519
0ab6612e060b
expand tabs slightly rewritten (no real change)
Claus Gittinger <cg@exept.de>
parents:
516
diff
changeset
|
1241 |
%}. |
1 | 1242 |
^ super , aString |
1243 |
! |
|
1244 |
||
1245 |
concatenate:string1 and:string2 |
|
1246 |
"return the concatenation of myself and the arguments, string1 and string2. |
|
1247 |
This is equivalent to self , string1 , string2 |
|
1248 |
- generated by compiler when such a construct is detected" |
|
1249 |
||
1250 |
|newString| |
|
1251 |
%{ |
|
95 | 1252 |
int len1, len2, len3, sz; |
1 | 1253 |
#if !defined(FAST_MEMCPY) && !defined(FAST_STRCPY) |
1254 |
REGISTER unsigned char *srcp; |
|
1255 |
#endif |
|
1256 |
REGISTER unsigned char *dstp; |
|
1257 |
||
53 | 1258 |
if ((__isString(self) || __isSymbol(self)) |
1259 |
&& (__isString(string1) || __isSymbol(string1)) |
|
1260 |
&& (__isString(string2) || __isSymbol(string2))) { |
|
159 | 1261 |
len1 = _stringSize(self); |
1262 |
len2 = _stringSize(string1); |
|
1263 |
len3 = _stringSize(string2); |
|
95 | 1264 |
sz = OHDR_SIZE + len1 + len2 + len3 + 1; |
159 | 1265 |
_qNew(newString, sz, __context); |
1266 |
if (newString != nil) { |
|
1267 |
_InstPtr(newString)->o_class = String; |
|
1268 |
dstp = _stringVal(newString); |
|
1 | 1269 |
#ifdef FAST_MEMCPY |
159 | 1270 |
bcopy(_stringVal(self), dstp, len1); |
1271 |
bcopy(_stringVal(string1), dstp + len1, len2); |
|
1272 |
bcopy(_stringVal(string2), dstp + len1 + len2, len3+1); |
|
1 | 1273 |
#else |
1274 |
# ifdef FAST_STRCPY |
|
159 | 1275 |
strcpy(dstp, _stringVal(self)); |
1276 |
strcpy(dstp + len1, _stringVal(string1)); |
|
1277 |
strcpy(dstp + len1 + len2, _stringVal(string2)); |
|
1 | 1278 |
# else |
159 | 1279 |
srcp = _stringVal(self); |
1280 |
while ((*dstp++ = *srcp++) != '\0') ; |
|
1281 |
dstp--; |
|
1282 |
srcp = _stringVal(string1); |
|
1283 |
while ((*dstp++ = *srcp++) != '\0') ; |
|
1284 |
dstp--; |
|
1285 |
srcp = _stringVal(string2); |
|
1286 |
while ((*dstp++ = *srcp++) != '\0') ; |
|
1 | 1287 |
# endif |
1288 |
#endif |
|
159 | 1289 |
RETURN ( newString ); |
1290 |
} |
|
1 | 1291 |
} |
519
0ab6612e060b
expand tabs slightly rewritten (no real change)
Claus Gittinger <cg@exept.de>
parents:
516
diff
changeset
|
1292 |
%}. |
1 | 1293 |
^ super , string1 , string2 |
1294 |
! |
|
1295 |
||
1296 |
concatenate:string1 and:string2 and:string3 |
|
1297 |
"return the concatenation of myself and the string arguments. |
|
1298 |
This is equivalent to self , string1 , string2 , string3 |
|
1299 |
- generated by compiler when such a construct is detected" |
|
1300 |
||
1301 |
|newString| |
|
1302 |
%{ |
|
95 | 1303 |
int len1, len2, len3, len4, sz; |
1 | 1304 |
#if !defined(FAST_MEMCPY) && !defined(FAST_STRCPY) |
1305 |
REGISTER unsigned char *srcp; |
|
1306 |
#endif |
|
1307 |
REGISTER unsigned char *dstp; |
|
1308 |
||
53 | 1309 |
if ((__isString(self) || __isSymbol(self)) |
1310 |
&& (__isString(string1) || __isSymbol(string1)) |
|
1311 |
&& (__isString(string2) || __isSymbol(string2)) |
|
1312 |
&& (__isString(string3) || __isSymbol(string3))) { |
|
159 | 1313 |
len1 = _stringSize(self); |
1314 |
len2 = _stringSize(string1); |
|
1315 |
len3 = _stringSize(string2); |
|
1316 |
len4 = _stringSize(string3); |
|
95 | 1317 |
sz = OHDR_SIZE + len1 + len2 + len3 + len4 + 1; |
159 | 1318 |
_qNew(newString, sz, __context); |
1319 |
if (newString != nil) { |
|
1320 |
_InstPtr(newString)->o_class = String; |
|
1321 |
dstp = _stringVal(newString); |
|
1 | 1322 |
#ifdef FAST_MEMCPY |
159 | 1323 |
bcopy(_stringVal(self), dstp, len1); |
1324 |
bcopy(_stringVal(string1), dstp + len1, len2); |
|
1325 |
bcopy(_stringVal(string2), dstp + len1 + len2, len3); |
|
1326 |
bcopy(_stringVal(string3), dstp + len1 + len2 + len3, len4+1); |
|
1 | 1327 |
#else |
1328 |
# ifdef FAST_STRCPY |
|
159 | 1329 |
strcpy(dstp, _stringVal(self)); |
1330 |
strcpy(dstp + len1, _stringVal(string1)); |
|
1331 |
strcpy(dstp + len1 + len2, _stringVal(string2)); |
|
1332 |
strcpy(dstp + len1 + len2 + len3, _stringVal(string3)); |
|
1 | 1333 |
# else |
159 | 1334 |
srcp = _stringVal(self); |
1335 |
while ((*dstp++ = *srcp++) != '\0') ; |
|
1336 |
dstp--; |
|
1337 |
srcp = _stringVal(string1); |
|
1338 |
while ((*dstp++ = *srcp++) != '\0') ; |
|
1339 |
dstp--; |
|
1340 |
srcp = _stringVal(string2); |
|
1341 |
while ((*dstp++ = *srcp++) != '\0') ; |
|
1342 |
dstp--; |
|
1343 |
srcp = _stringVal(string3); |
|
1344 |
while ((*dstp++ = *srcp++) != '\0') ; |
|
1 | 1345 |
# endif |
1346 |
#endif |
|
159 | 1347 |
RETURN ( newString ); |
1348 |
} |
|
1 | 1349 |
} |
519
0ab6612e060b
expand tabs slightly rewritten (no real change)
Claus Gittinger <cg@exept.de>
parents:
516
diff
changeset
|
1350 |
%}. |
1 | 1351 |
^ super , string1 , string2 , string3 |
1352 |
! |
|
1353 |
||
608 | 1354 |
copyFrom:start |
1355 |
"return the substring from start, anInteger to the end. |
|
1356 |
This method will always return a string, even if the receiver |
|
1357 |
is a subclass-instance. This might change if there is a need. |
|
1358 |
- reimplemented here for speed" |
|
1 | 1359 |
|
13 | 1360 |
%{ /* NOCONTEXT */ |
1 | 1361 |
|
608 | 1362 |
#if !defined(FAST_MEMCPY) |
1363 |
REGISTER unsigned char *srcp; |
|
1364 |
#endif |
|
1 | 1365 |
REGISTER unsigned char *dstp; |
608 | 1366 |
REGISTER int count; |
1367 |
int len, index1, sz; |
|
1368 |
OBJ newString; |
|
1369 |
||
1370 |
if (__isSmallInteger(start)) { |
|
1371 |
len = _stringSize(self); |
|
1372 |
index1 = _intVal(start); |
|
1373 |
if (index1 > 0) { |
|
1374 |
if (__qClass(self) != String) { |
|
1375 |
int n = __OBJS2BYTES__(_intVal(_ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
1 | 1376 |
|
608 | 1377 |
index1 += n; |
1378 |
} |
|
1379 |
if (index1 <= len) { |
|
1380 |
count = len - index1 + 1; |
|
834 | 1381 |
__PROTECT_CONTEXT__ |
608 | 1382 |
sz = OHDR_SIZE + count + 1; |
1383 |
_qNew(newString, sz, SENDER); |
|
834 | 1384 |
__UNPROTECT_CONTEXT__ |
608 | 1385 |
if (newString != nil) { |
1386 |
_InstPtr(newString)->o_class = String; |
|
1387 |
dstp = _stringVal(newString); |
|
1 | 1388 |
#ifdef FAST_MEMCPY |
608 | 1389 |
bcopy(_stringVal(self) + index1 - 1, dstp, count); |
1390 |
dstp[count] = '\0'; |
|
1 | 1391 |
#else |
1392 |
# ifdef FAST_STRCPY |
|
608 | 1393 |
strncpy(dstp, _stringVal(self) + index1 - 1, count); |
1394 |
dstp[count] = '\0'; |
|
1 | 1395 |
# else |
608 | 1396 |
srcp = _stringVal(self) + index1 - 1; |
1397 |
while (count--) { |
|
1398 |
*dstp++ = *srcp++; |
|
1399 |
} |
|
1400 |
*dstp = '\0'; |
|
1 | 1401 |
# endif |
1402 |
#endif |
|
608 | 1403 |
RETURN ( newString ); |
1404 |
} |
|
1405 |
} |
|
159 | 1406 |
} |
13 | 1407 |
} |
51 | 1408 |
%}. |
608 | 1409 |
"fall back in case of non-integer index or out-of-bound index; |
1410 |
will eventually lead to an out-of-bound signal raise" |
|
51 | 1411 |
|
608 | 1412 |
^ super copyFrom:start |
1 | 1413 |
! |
1414 |
||
1415 |
copyFrom:start to:stop |
|
1416 |
"return the substring starting at index start, anInteger and ending |
|
53 | 1417 |
at stop, anInteger. This method will always return a string, even |
1418 |
if the receiver is a subclass-instance. This might change if there is a need. |
|
1 | 1419 |
- reimplemented here for speed" |
1420 |
||
13 | 1421 |
%{ /* NOCONTEXT */ |
1422 |
||
1 | 1423 |
#if !defined(FAST_MEMCPY) |
1424 |
REGISTER unsigned char *srcp; |
|
1425 |
#endif |
|
1426 |
REGISTER unsigned char *dstp; |
|
1427 |
REGISTER int count; |
|
95 | 1428 |
int len, sz, index1, index2; |
13 | 1429 |
OBJ newString; |
1 | 1430 |
|
252 | 1431 |
if (__bothSmallInteger(start, stop)) { |
159 | 1432 |
len = _stringSize(self); |
1433 |
index1 = _intVal(start); |
|
1434 |
index2 = _intVal(stop); |
|
1 | 1435 |
|
159 | 1436 |
if ((index1 <= index2) && (index1 > 0)) { |
329 | 1437 |
if (__qClass(self) != String) { |
1438 |
int n = __OBJS2BYTES__(_intVal(_ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
1 | 1439 |
|
159 | 1440 |
index1 += n; |
1441 |
index2 += n; |
|
1442 |
} |
|
1443 |
if (index2 <= len) { |
|
1444 |
count = index2 - index1 + 1; |
|
834 | 1445 |
__PROTECT_CONTEXT__ |
95 | 1446 |
sz = OHDR_SIZE + count + 1; |
159 | 1447 |
_qNew(newString, sz, SENDER); |
834 | 1448 |
__UNPROTECT_CONTEXT__ |
159 | 1449 |
if (newString != nil) { |
1450 |
_InstPtr(newString)->o_class = String; |
|
1451 |
dstp = _stringVal(newString); |
|
1 | 1452 |
#ifdef FAST_MEMCPY |
159 | 1453 |
bcopy(_stringVal(self) + index1 - 1, dstp, count); |
1454 |
dstp[count] = '\0'; |
|
1 | 1455 |
#else |
209 | 1456 |
# ifdef FAST_STRCPY |
1457 |
strncpy(dstp, _stringVal(self) + index1 - 1, count); |
|
1458 |
dstp[count] = '\0'; |
|
1459 |
# else |
|
159 | 1460 |
srcp = _stringVal(self) + index1 - 1; |
1461 |
while (count--) { |
|
1462 |
*dstp++ = *srcp++; |
|
1463 |
} |
|
1464 |
*dstp = '\0'; |
|
209 | 1465 |
# endif |
13 | 1466 |
#endif |
159 | 1467 |
RETURN ( newString ); |
1468 |
} |
|
1469 |
} |
|
1470 |
} |
|
375 | 1471 |
/* |
1472 |
* allow empty copy |
|
1473 |
*/ |
|
1474 |
if (index1 > index2) { |
|
834 | 1475 |
__PROTECT_CONTEXT__ |
375 | 1476 |
_qNew(newString, OHDR_SIZE+1, SENDER); |
834 | 1477 |
__UNPROTECT_CONTEXT__ |
375 | 1478 |
if (newString != nil) { |
1479 |
_InstPtr(newString)->o_class = String; |
|
1480 |
__stringVal(newString)[0] = '\0'; |
|
1481 |
RETURN ( newString ); |
|
1482 |
} |
|
1483 |
} |
|
1 | 1484 |
} |
51 | 1485 |
%}. |
1486 |
"fall back in case of non-integer index or out-of-bound index; |
|
1487 |
will eventually lead to an out-of-bound signal raise" |
|
1488 |
||
1 | 1489 |
^ super copyFrom:start to:stop |
1490 |
! |
|
1491 |
||
608 | 1492 |
copyWith:aCharacter |
1493 |
"return a new string containing the receivers characters |
|
1494 |
and the single new character, aCharacter. |
|
1495 |
This is different from concatentation, which expects another string |
|
1496 |
as argument, but equivalent to copy-and-addLast. |
|
1497 |
Reimplemented here for more speed" |
|
1498 |
||
1499 |
%{ /* NOCONTEXT */ |
|
1500 |
||
1501 |
int sz; |
|
1502 |
REGISTER unsigned char *dstp; |
|
1503 |
int offs; |
|
1504 |
OBJ cls, newString; |
|
1505 |
||
1506 |
if (__isCharacter(aCharacter)) { |
|
1507 |
sz = __qSize(self) + 1; |
|
1508 |
if ((cls = __qClass(self)) != String) { |
|
1509 |
offs = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
1510 |
sz -= offs; |
|
1511 |
} else |
|
1512 |
offs = 0; |
|
1513 |
||
834 | 1514 |
__PROTECT_CONTEXT__ |
608 | 1515 |
_qNew(newString, sz, SENDER); |
834 | 1516 |
__UNPROTECT_CONTEXT__ |
608 | 1517 |
if (newString) { |
1518 |
_InstPtr(newString)->o_class = String; |
|
1519 |
dstp = _stringVal(newString); |
|
1520 |
#ifdef FAST_MEMCPY |
|
1521 |
sz = sz - OHDR_SIZE - 1 - 1; |
|
1522 |
bcopy(_stringVal(self) + offs, dstp, sz); |
|
1523 |
dstp += sz; |
|
1524 |
#else |
|
1525 |
# ifdef FAST_STRCPY |
|
1526 |
strcpy(dstp, _stringVal(self) + offs); |
|
1527 |
dstp += sz - OHDR_SIZE - 1 - 1; |
|
1528 |
# else |
|
1529 |
{ |
|
1530 |
REGISTER unsigned char *srcp; |
|
1531 |
||
1532 |
srcp = _stringVal(self) + offs; |
|
1533 |
while ((*dstp = *srcp++) != '\0') |
|
1534 |
dstp++; |
|
1535 |
} |
|
1536 |
# endif |
|
1537 |
#endif |
|
1538 |
*dstp++ = _intVal(_characterVal(aCharacter)); |
|
1539 |
*dstp = '\0'; |
|
1540 |
RETURN (newString ); |
|
1541 |
} |
|
1542 |
} |
|
1543 |
%}. |
|
1544 |
"fall back in case of non-character arg; |
|
1545 |
will eventually lead to an bad element signal raise" |
|
1546 |
||
1547 |
^ super copyWith:aCharacter |
|
1548 |
! |
|
1549 |
||
1550 |
deepCopy |
|
1551 |
"return a copy of the receiver" |
|
1552 |
||
1553 |
" |
|
1554 |
could be an instance of a subclass which needs deepCopy |
|
1555 |
of its named instvars ... |
|
1556 |
" |
|
1557 |
(self isMemberOf:String) ifTrue:[ |
|
1558 |
^ self copyFrom:1 |
|
1559 |
]. |
|
1560 |
^ super deepCopy |
|
1561 |
! |
|
1562 |
||
1563 |
deepCopyUsing:aDictionary |
|
1564 |
"return a copy of the receiver - reimplemented to be a bit faster" |
|
1565 |
||
1566 |
" |
|
1567 |
could be an instance of a subclass which needs deepCopy |
|
1568 |
of its named instvars ... |
|
1569 |
" |
|
1570 |
(self isMemberOf:String) ifTrue:[ |
|
1571 |
^ self copyFrom:1 |
|
1572 |
]. |
|
1573 |
^ super deepCopy |
|
1574 |
! |
|
1575 |
||
1576 |
shallowCopy |
|
1577 |
"return a copy of the receiver" |
|
1578 |
||
1579 |
(self isMemberOf:String) ifTrue:[ |
|
1580 |
^ self copyFrom:1 |
|
1581 |
]. |
|
1582 |
^ super shallowCopy |
|
1583 |
! |
|
1584 |
||
1585 |
simpleDeepCopy |
|
1586 |
"return a copy of the receiver" |
|
1587 |
||
1588 |
" |
|
1589 |
could be an instance of a subclass which needs deepCopy |
|
1590 |
of its named instvars ... |
|
1591 |
" |
|
1592 |
(self isMemberOf:String) ifTrue:[ |
|
1593 |
^ self copyFrom:1 |
|
1594 |
]. |
|
1595 |
^ super deepCopy |
|
1596 |
! ! |
|
1597 |
||
1598 |
!String methodsFor:'filling and replacing'! |
|
1599 |
||
1600 |
atAllPut:aCharacter |
|
1601 |
"replace all elements with aCharacter |
|
1 | 1602 |
- reimplemented here for speed" |
1603 |
||
13 | 1604 |
%{ /* NOCONTEXT */ |
1605 |
||
608 | 1606 |
#ifndef FAST_MEMSET |
1607 |
REGISTER unsigned char *dst; |
|
1608 |
REGISTER int l; |
|
1 | 1609 |
#endif |
608 | 1610 |
REGISTER int byteValue; |
1 | 1611 |
|
608 | 1612 |
if (__isCharacter(aCharacter) && __isString(self)) { |
1613 |
byteValue = _intVal(_characterVal(aCharacter)); |
|
1614 |
#ifdef FAST_MEMSET |
|
1615 |
memset(_stringVal(self), byteValue, __qSize(self) - OHDR_SIZE - 1); |
|
1616 |
#else |
|
1617 |
dst = _stringVal(self); |
|
1618 |
l = __qSize(self) - OHDR_SIZE - 1; |
|
1619 |
while (l >= 8) { |
|
1620 |
dst[0] = dst[1] = dst[2] = dst[3] = byteValue; |
|
1621 |
dst[4] = dst[5] = dst[6] = dst[7] = byteValue; |
|
1622 |
dst += 8; |
|
1623 |
l -= 8; |
|
1624 |
} |
|
1625 |
while (l--) |
|
1626 |
*dst++ = byteValue; |
|
1627 |
#endif |
|
1628 |
RETURN ( self ); |
|
1629 |
} |
|
1630 |
%} |
|
1631 |
. |
|
1632 |
^ super atAllPut:aCharacter |
|
1 | 1633 |
|
608 | 1634 |
" |
1635 |
(String new:10) atAllPut:$* |
|
1636 |
String new:10 withAll:$* |
|
1637 |
" |
|
1638 |
! |
|
1639 |
||
1640 |
replaceAll:oldCharacter by:newCharacter |
|
1641 |
"replace all oldCharacters by newCharacter in the receiver" |
|
1642 |
||
1643 |
%{ /* NOCONTEXT */ |
|
1644 |
||
1645 |
REGISTER unsigned char *srcp; |
|
1646 |
REGISTER unsigned oldVal, newVal; |
|
1647 |
||
1648 |
if (__isCharacter(oldCharacter) |
|
1649 |
&& __isCharacter(newCharacter) |
|
1650 |
&& __isString(self)) { |
|
1651 |
srcp = (unsigned char *)_stringVal(self); |
|
1652 |
oldVal = _intVal(_characterVal(oldCharacter)); |
|
1653 |
newVal = _intVal(_characterVal(newCharacter)); |
|
1654 |
while (*srcp) { |
|
1655 |
if (*srcp == oldVal) |
|
1656 |
*srcp = newVal; |
|
1657 |
srcp++; |
|
159 | 1658 |
} |
608 | 1659 |
RETURN ( self ); |
1 | 1660 |
} |
608 | 1661 |
%} |
1662 |
. |
|
1663 |
^ super replaceAll:oldCharacter by:newCharacter |
|
51 | 1664 |
|
608 | 1665 |
" |
1666 |
'helloWorld' copy replaceAll:$o by:$O |
|
1667 |
" |
|
1668 |
! |
|
1 | 1669 |
|
1670 |
replaceFrom:start to:stop with:aString startingAt:repStart |
|
1671 |
"replace the characters starting at index start, anInteger and ending |
|
1672 |
at stop, anInteger with characters from aString starting at repStart. |
|
1673 |
||
1674 |
- reimplemented here for speed" |
|
1675 |
||
1676 |
%{ /* NOCONTEXT */ |
|
1677 |
||
1678 |
REGISTER unsigned char *srcp, *dstp; |
|
1679 |
REGISTER int count; |
|
1680 |
int len, index1, index2; |
|
1681 |
int repLen, repIndex; |
|
1682 |
||
53 | 1683 |
if ((__isString(aString) || __isSymbol(aString)) |
1684 |
&& __isString(self) |
|
252 | 1685 |
&& __bothSmallInteger(start, stop)) { |
159 | 1686 |
len = _stringSize(self); |
1687 |
index1 = _intVal(start); |
|
1688 |
index2 = _intVal(stop); |
|
1689 |
count = index2 - index1 + 1; |
|
1690 |
if (count <= 0) { |
|
1691 |
RETURN (self); |
|
1692 |
} |
|
1693 |
if ((index2 <= len) && (index1 > 0)) { |
|
1694 |
repLen = _stringSize(aString); |
|
1695 |
repIndex = _intVal(repStart); |
|
1696 |
if ((repIndex > 0) && ((repIndex + count - 1) <= repLen)) { |
|
1697 |
srcp = _stringVal(aString) + repIndex - 1; |
|
1698 |
dstp = _stringVal(self) + index1 - 1; |
|
1699 |
if (aString == self) { |
|
1700 |
/* take care of overlapping copy */ |
|
1701 |
if (srcp < dstp) { |
|
1702 |
/* must do a reverse copy */ |
|
1703 |
srcp += count; |
|
1704 |
dstp += count; |
|
1705 |
while (count-- > 0) { |
|
1706 |
*--dstp = *--srcp; |
|
1707 |
} |
|
1708 |
RETURN (self); |
|
1709 |
} |
|
1710 |
} |
|
1 | 1711 |
#ifdef FAST_MEMCPY |
159 | 1712 |
bcopy(srcp, dstp, count); |
1 | 1713 |
#else |
159 | 1714 |
while (count-- > 0) { |
1715 |
*dstp++ = *srcp++; |
|
1716 |
} |
|
1 | 1717 |
#endif |
159 | 1718 |
RETURN (self); |
1719 |
} |
|
1720 |
} |
|
1 | 1721 |
} |
1722 |
%} |
|
1723 |
. |
|
1724 |
^ super replaceFrom:start to:stop with:aString startingAt:repStart |
|
1725 |
! |
|
1726 |
||
1727 |
reverse |
|
53 | 1728 |
"in-place reverse the characters of the string." |
1729 |
||
1730 |
"Q: is there a need to redefine it here ?" |
|
1 | 1731 |
|
1732 |
%{ /* NOCONTEXT */ |
|
1733 |
||
1734 |
REGISTER char c; |
|
1735 |
REGISTER unsigned char *hip, *lowp; |
|
1736 |
||
53 | 1737 |
if (__isString(self)) { |
159 | 1738 |
lowp = _stringVal(self); |
1739 |
hip = lowp + _stringSize(self) - 1; |
|
1740 |
while (lowp < hip) { |
|
1741 |
c = *lowp; |
|
1742 |
*lowp = *hip; |
|
1743 |
*hip = c; |
|
1744 |
lowp++; |
|
1745 |
hip--; |
|
1746 |
} |
|
1747 |
RETURN ( self ); |
|
1 | 1748 |
} |
1749 |
%} |
|
1750 |
. |
|
1751 |
^ super reverse |
|
1752 |
! |
|
1753 |
||
1754 |
withoutSeparators |
|
95 | 1755 |
"return a copy of myself without leading and trailing whitespace. |
1756 |
Notice, this is different from String>>withoutSpaces." |
|
1 | 1757 |
|
42 | 1758 |
|startIndex "{ Class: SmallInteger }" |
51 | 1759 |
endIndex "{ Class: SmallInteger }" |
1760 |
sz| |
|
1761 |
||
53 | 1762 |
startIndex := 0. |
51 | 1763 |
%{ |
1764 |
REGISTER unsigned char *cp0; |
|
1765 |
REGISTER unsigned char *cp; |
|
1766 |
REGISTER unsigned char c; |
|
1 | 1767 |
|
51 | 1768 |
/* ignore instances of subclasses ... */ |
329 | 1769 |
if (__qClass(self) == String) { |
159 | 1770 |
cp = cp0 = _stringVal(self); |
379 | 1771 |
while ((c = *cp) |
329 | 1772 |
#ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
379 | 1773 |
&& (c <= ' ') |
329 | 1774 |
#endif |
379 | 1775 |
&& ((c == ' ') || (c == '\n') || (c == '\t') |
329 | 1776 |
|| (c == '\r') || (c == '\f')) |
1777 |
) { |
|
159 | 1778 |
cp++; |
1779 |
} |
|
1780 |
startIndex = _MKSMALLINT(cp - cp0 + 1); |
|
1781 |
cp = cp + strlen(cp) - 1; |
|
1782 |
while ((cp >= cp0) && (*cp == ' ')) cp--; |
|
1783 |
c = *cp; |
|
1784 |
while ((cp >= cp0) && |
|
329 | 1785 |
#ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
1786 |
(c <= ' ') && |
|
1787 |
#endif |
|
159 | 1788 |
((c == ' ') || (c == '\n') || (c == '\t') |
1789 |
|| (c == '\r') || (c == '\f'))) { |
|
1790 |
cp--; |
|
1791 |
c = *cp; |
|
1792 |
} |
|
1793 |
endIndex = _MKSMALLINT(cp - cp0 + 1); |
|
51 | 1794 |
} |
202 | 1795 |
%}. |
1796 |
startIndex == 0 ifTrue:[^ super withoutSeparators]. |
|
1797 |
||
1798 |
startIndex > endIndex ifTrue:[^ '']. |
|
1799 |
((startIndex == 1) and:[endIndex == self size]) ifTrue:[^ self]. |
|
1 | 1800 |
^ self copyFrom:startIndex to:endIndex |
608 | 1801 |
! |
1802 |
||
1803 |
withoutSpaces |
|
1804 |
"return a copy of myself without leading and trailing spaces. |
|
1805 |
Notice, this is different from String>>withoutSeparators." |
|
1806 |
||
1807 |
|startIndex "{ Class: SmallInteger }" |
|
1808 |
endIndex "{ Class: SmallInteger }" |
|
1809 |
sz blank| |
|
1810 |
||
1811 |
startIndex := 0. |
|
1812 |
%{ |
|
1813 |
REGISTER unsigned char *cp0; |
|
1814 |
REGISTER unsigned char *cp; |
|
1815 |
||
1816 |
/* ignore instances of subclasses ... */ |
|
1817 |
if (__qClass(self) == String) { |
|
1818 |
cp = cp0 = _stringVal(self); |
|
1819 |
while (*cp == ' ') cp++; |
|
1820 |
startIndex = _MKSMALLINT(cp - cp0 + 1); |
|
1821 |
cp = cp + strlen(cp) - 1; |
|
1822 |
while ((cp >= cp0) && (*cp == ' ')) cp--; |
|
1823 |
endIndex = _MKSMALLINT(cp - cp0 + 1); |
|
1824 |
} |
|
1825 |
%}. |
|
1826 |
startIndex == 0 ifTrue:[^ super withoutSpaces]. |
|
1827 |
||
1828 |
startIndex > endIndex ifTrue:[^ '']. |
|
1829 |
((startIndex == 1) and:[endIndex == self size]) ifTrue:[^ self]. |
|
1830 |
^ self copyFrom:startIndex to:endIndex |
|
1831 |
! ! |
|
1832 |
||
1833 |
!String methodsFor:'pattern matching'! |
|
1834 |
||
1835 |
endsWith:aString |
|
1836 |
"return true, if the receiver end with something, aString." |
|
1837 |
||
1838 |
%{ /* NOCONTEXT */ |
|
1839 |
||
1840 |
int len1, len2; |
|
1841 |
REGISTER unsigned char *src1, *src2; |
|
1842 |
char c; |
|
1843 |
||
1844 |
if ((__isString(self) || __isSymbol(self)) |
|
1845 |
&& (__isString(aString) || __isSymbol(aString))) { |
|
1846 |
len1 = __qSize(self); |
|
1847 |
len2 = __qSize(aString); |
|
1848 |
if (len1 < len2) { |
|
1849 |
RETURN ( false ); |
|
1850 |
} |
|
1851 |
||
1852 |
src1 = _stringVal(self) + len1 - len2; |
|
1853 |
src2 = _stringVal(aString); |
|
1854 |
while (c = *src2++) { |
|
1855 |
if (c != *src1++) { |
|
1856 |
RETURN ( false ); |
|
1857 |
} |
|
1858 |
} |
|
1859 |
RETURN (true); |
|
1860 |
} |
|
1861 |
%}. |
|
1862 |
(aString isMemberOf:Character) ifTrue:[ |
|
1863 |
self size == 0 ifTrue:[^ false]. |
|
1864 |
^ (self at:(self size)) == aString |
|
1865 |
]. |
|
1866 |
^ super endsWith:aString |
|
1867 |
||
1868 |
" |
|
1869 |
'hello world' endsWith:'world' |
|
1870 |
'hello world' endsWith:'earth' |
|
1871 |
'hello world' endsWith:$d |
|
1872 |
'hello world' endsWith:#($r $l $d) |
|
1873 |
" |
|
1874 |
! |
|
1875 |
||
1876 |
startsWith:aString |
|
1877 |
"return true, if the receiver starts with something, aString." |
|
1878 |
||
1879 |
%{ /* NOCONTEXT */ |
|
1880 |
||
1881 |
int len1, len2; |
|
1882 |
REGISTER unsigned char *src1, *src2; |
|
1883 |
char c; |
|
1884 |
||
1885 |
if ((__isString(self) || __isSymbol(self)) |
|
1886 |
&& (__isString(aString) || __isSymbol(aString))) { |
|
1887 |
len1 = __qSize(self); |
|
1888 |
len2 = __qSize(aString); |
|
1889 |
if (len1 < len2) { |
|
1890 |
RETURN ( false ); |
|
1891 |
} |
|
1892 |
||
1893 |
src1 = _stringVal(self); |
|
1894 |
src2 = _stringVal(aString); |
|
1895 |
while (c = *src2++) { |
|
1896 |
if (c != *src1++) { |
|
1897 |
RETURN ( false ); |
|
1898 |
} |
|
1899 |
} |
|
1900 |
RETURN (true); |
|
1901 |
} |
|
1902 |
%}. |
|
1903 |
(aString isMemberOf:Character) ifTrue:[ |
|
1904 |
self size == 0 ifTrue:[^ false]. |
|
1905 |
^ (self at:1) == aString |
|
1906 |
]. |
|
1907 |
^ super startsWith:aString |
|
1908 |
||
1909 |
" |
|
1910 |
'hello world' startsWith:'hello' |
|
1911 |
'hello world' startsWith:'hi' |
|
1912 |
'hello world' startsWith:$h |
|
1913 |
'hello world' startsWith:#($h $e $l) |
|
1914 |
" |
|
1915 |
! ! |
|
1916 |
||
1917 |
!String methodsFor:'printing & storing'! |
|
1918 |
||
1919 |
isLiteral |
|
1920 |
"return true, if the receiver can be used as a literal |
|
1921 |
(i.e. can be used in constant arrays)" |
|
1922 |
||
1923 |
^ true |
|
1924 |
! |
|
1925 |
||
1926 |
||
1927 |
"print the receiver on standard output. |
|
1928 |
This method does NOT use the stream classes and will therefore work |
|
1929 |
even in case of emergency." |
|
1930 |
||
1931 |
%{ /* NOCONTEXT */ |
|
1932 |
||
1933 |
if (__qClass(self) == String) { |
|
1934 |
fputs(_stringVal(self), stdout); fflush(stdout); |
|
1935 |
RETURN (self); |
|
1936 |
} |
|
1937 |
%} |
|
1938 |
. |
|
1939 |
^ super print |
|
1940 |
! |
|
1941 |
||
1942 |
printfPrintString:formatString |
|
1943 |
"non-portable but sometimes useful. |
|
1944 |
Return a printed representation of the receiver as specified by formatString, |
|
1945 |
which is defined by printf. |
|
1946 |
No checking on buffer overrun is done; the resulting string may not |
|
1947 |
be larger than 799 characters. |
|
1948 |
This method is NONSTANDARD and may be removed without notice." |
|
1949 |
||
1950 |
%{ /* STACK: 1000 */ |
|
1951 |
||
1952 |
char buffer[800]; |
|
1953 |
char *cp; |
|
1954 |
OBJ s; |
|
1955 |
||
1956 |
if (__isString(formatString)) { |
|
1957 |
/* |
|
1958 |
* actually only needed on sparc: since thisContext is |
|
1959 |
* in a global register, which gets destroyed by printf, |
|
1960 |
* manually save it here - very stupid ... |
|
1961 |
*/ |
|
1962 |
||
1963 |
cp = (char *)_stringVal(self); |
|
1964 |
if (__qClass(self) != String) |
|
1965 |
cp += __OBJS2BYTES__(_intVal(_ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
1966 |
||
809
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
735
diff
changeset
|
1967 |
__BEGIN_PROTECT_REGISTERS__ |
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
735
diff
changeset
|
1968 |
|
608 | 1969 |
sprintf(buffer, (char *)_stringVal(formatString), cp); |
809
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
735
diff
changeset
|
1970 |
|
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
735
diff
changeset
|
1971 |
__END_PROTECT_REGISTERS__ |
608 | 1972 |
|
833 | 1973 |
s = __MKSTRING(buffer COMMA_SND); |
608 | 1974 |
if (s != nil) { |
1975 |
RETURN (s); |
|
1976 |
} |
|
1977 |
} |
|
1978 |
%}. |
|
1979 |
self primitiveFailed |
|
1980 |
||
1981 |
" |
|
1982 |
'hello' printfPrintString:'%%s -> %s' |
|
1983 |
'hello' printfPrintString:'%%10s -> %10s' |
|
1984 |
'hello' printfPrintString:'%%-10s -> %-10s' |
|
1985 |
" |
|
1986 |
! |
|
1987 |
||
1988 |
storeOn:aStream |
|
1989 |
"put the storeString of myself on aStream" |
|
1990 |
||
1991 |
aStream nextPut: $'. |
|
1992 |
(self includes:$') ifTrue:[ |
|
1993 |
self do:[:thisChar | |
|
1994 |
(thisChar == $') ifTrue:[aStream nextPut:thisChar]. |
|
1995 |
aStream nextPut:thisChar |
|
1996 |
] |
|
1997 |
] ifFalse:[ |
|
1998 |
aStream nextPutAll:self |
|
1999 |
]. |
|
2000 |
aStream nextPut:$' |
|
2001 |
! |
|
2002 |
||
2003 |
storeString |
|
2004 |
"return a String for storing myself" |
|
2005 |
||
2006 |
|s n index| |
|
2007 |
||
2008 |
n := self occurrencesOf:$'. |
|
2009 |
n == 0 ifFalse:[ |
|
2010 |
s := String new:(n + 2 + self size). |
|
2011 |
s at:1 put:$'. |
|
2012 |
index := 2. |
|
2013 |
self do:[:thisChar | |
|
2014 |
(thisChar == $') ifTrue:[ |
|
2015 |
s at:index put:thisChar. |
|
2016 |
index := index + 1. |
|
2017 |
]. |
|
2018 |
s at:index put:thisChar. |
|
2019 |
index := index + 1. |
|
2020 |
]. |
|
2021 |
s at:index put:$'. |
|
2022 |
^ s |
|
2023 |
]. |
|
2024 |
^ '''' , self , '''' |
|
1 | 2025 |
! ! |
2026 |
||
2027 |
!String methodsFor:'queries'! |
|
2028 |
||
2029 |
encoding |
|
2030 |
"assume iso8859 encoding" |
|
2031 |
||
2032 |
^ #iso8859 |
|
2033 |
! |
|
2034 |
||
68 | 2035 |
isEmpty |
2036 |
"return true if the receiver is empty (i.e. if size == 0) |
|
2037 |
Redefined here for performance" |
|
2038 |
||
2039 |
%{ /* NOCONTEXT */ |
|
2040 |
OBJ cls; |
|
2041 |
||
329 | 2042 |
cls = __qClass(self); |
68 | 2043 |
if ((cls == String) || (cls == Symbol)) { |
159 | 2044 |
RETURN ( (_stringSize(self) == 0) ? true : false); |
68 | 2045 |
} |
2046 |
%} |
|
2047 |
. |
|
2048 |
^ super isEmpty |
|
608 | 2049 |
! |
2050 |
||
2051 |
knownAsSymbol |
|
2052 |
"return true, if there is a symbol with same characters in the |
|
2053 |
system - use to check for existance of a symbol without creating one" |
|
2054 |
||
2055 |
%{ /* NOCONTEXT */ |
|
2056 |
extern OBJ __KNOWNASSYMBOL(); |
|
2057 |
||
2058 |
RETURN ( __KNOWNASSYMBOL(_stringVal(self)) ); |
|
2059 |
%} |
|
1 | 2060 |
! ! |
608 | 2061 |
|
2062 |
!String methodsFor:'testing'! |
|
2063 |
||
2064 |
isBlank |
|
2065 |
"return true, if the receivers size is 0 or if it contains only spaces. |
|
2066 |
Q: should we care for whiteSpace in general here ?" |
|
2067 |
||
2068 |
%{ /* NOCONTEXT */ |
|
2069 |
||
2070 |
REGISTER unsigned char *src; |
|
2071 |
REGISTER unsigned char c; |
|
2072 |
OBJ cls; |
|
2073 |
||
2074 |
src = _stringVal(self); |
|
2075 |
if ((cls = __qClass(self)) != String) |
|
2076 |
src += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars)); |
|
2077 |
||
2078 |
#ifndef NON_ASCII |
|
2079 |
while (*((unsigned *)src) == 0x20202020) { |
|
2080 |
src += 4; |
|
2081 |
} |
|
2082 |
#endif |
|
2083 |
while (c = *src++) |
|
2084 |
if (c != ' ') { |
|
2085 |
RETURN ( false ); |
|
2086 |
} |
|
2087 |
%}. |
|
2088 |
^ true |
|
2089 |
! |
|
2090 |
||
2091 |
levenshteinTo:aString s:substWeight c:caseWeight i:insrtWeight d:deleteWeight |
|
2092 |
"parametrized levenshtein. arguments are the costs for |
|
2093 |
substitution, case-change, insertion and deletion of a character." |
|
2094 |
||
2095 |
%{ /* STACK: 4000 */ |
|
2096 |
||
2097 |
/* |
|
2098 |
* this is very heavy used when correcting errors |
|
2099 |
* (all symbols are searched for best match) - therefore it must be fast |
|
2100 |
*/ |
|
2101 |
{ |
|
2102 |
unsigned short *data; |
|
2103 |
int l1, l2; |
|
2104 |
REGISTER int sz; |
|
2105 |
unsigned char *s1, *s2; |
|
2106 |
int v1, v2, v3, m; |
|
2107 |
REGISTER unsigned short *dp; |
|
2108 |
REGISTER int delta; |
|
2109 |
REGISTER int j; |
|
2110 |
int i; |
|
2111 |
int iW, cW, sW, dW; |
|
2112 |
# define FASTSIZE 30 |
|
2113 |
unsigned short fastData[(FASTSIZE + 1) * (FASTSIZE + 1)]; |
|
2114 |
||
2115 |
if ((__isString(self) || __isSymbol(self)) |
|
2116 |
&& (__isString(aString) || __isSymbol(aString)) |
|
2117 |
&& __bothSmallInteger(insrtWeight, caseWeight) |
|
2118 |
&& __bothSmallInteger(substWeight, deleteWeight)) { |
|
2119 |
iW = _intVal(insrtWeight); |
|
2120 |
cW = _intVal(caseWeight); |
|
2121 |
sW = _intVal(substWeight); |
|
2122 |
dW = _intVal(deleteWeight); |
|
2123 |
s1 = _stringVal(self); |
|
2124 |
s2 = _stringVal(aString); |
|
2125 |
l1 = strlen(s1); |
|
2126 |
l2 = strlen(s2); |
|
2127 |
||
2128 |
sz = (l1 < l2) ? l2 : l1; |
|
2129 |
delta = sz + 1; |
|
2130 |
if (sz <= FASTSIZE) { |
|
2131 |
data = fastData; |
|
2132 |
} else { |
|
2133 |
/* add ifdef ALLOCA here ... */ |
|
2134 |
data = (unsigned short *)malloc(delta * delta * sizeof(short)); |
|
2135 |
if (! data) goto mallocFailed; |
|
2136 |
} |
|
2137 |
||
2138 |
data[0] = 0; |
|
2139 |
dp = data+1; |
|
2140 |
for (j=1, dp=data+1; j<=sz; j++, dp++) |
|
2141 |
*dp = *(dp-1) + iW; |
|
2142 |
||
2143 |
for (i=1, dp=data+delta; i<=sz; i++, dp+=delta) |
|
2144 |
*dp = *(dp-delta) + dW; |
|
2145 |
||
2146 |
for (i=1; i<=l1; i++) { |
|
2147 |
for (j=1; j<=l2; j++) { |
|
2148 |
dp = data + (i*delta) + j; |
|
2149 |
if (s1[i] != s2[j]) { |
|
2150 |
if (tolower(s1[i]) == tolower(s2[j])) { |
|
2151 |
m = cW; |
|
2152 |
} else { |
|
2153 |
m = sW; |
|
2154 |
} |
|
2155 |
} else |
|
2156 |
m = 0; |
|
2157 |
||
2158 |
v2 = *(dp - 1) + iW; |
|
2159 |
v3 = *(dp - delta) + dW; |
|
2160 |
v1 = *(dp - delta - 1) + m; |
|
2161 |
if (v1 < v2) |
|
2162 |
if (v1 < v3) |
|
2163 |
m = v1; |
|
2164 |
else |
|
2165 |
m = v3; |
|
2166 |
else |
|
2167 |
if (v2 < v3) |
|
2168 |
m = v2; |
|
2169 |
else |
|
2170 |
m = v3; |
|
2171 |
*dp = m; |
|
2172 |
} |
|
2173 |
} |
|
2174 |
m = data[l1 * delta + l2]; |
|
2175 |
if (sz > FASTSIZE) |
|
2176 |
free(data); |
|
2177 |
RETURN ( _MKSMALLINT(m) ); |
|
2178 |
} |
|
2179 |
mallocFailed: ; |
|
2180 |
} |
|
2181 |
%}. |
|
2182 |
||
2183 |
^ super levenshteinTo:aString |
|
2184 |
s:substWeight c:caseWeight |
|
2185 |
i:insrtWeight d:deleteWeight |
|
2186 |
||
2187 |
"'ocmprt' levenshteinTo:'computer' |
|
2188 |
'computer' levenshteinTo:'computer' |
|
2189 |
'ocmputer' levenshteinTo:'computer' |
|
2190 |
'cmputer' levenshteinTo:'computer' |
|
2191 |
'Computer' levenshteinTo:'computer'" |
|
2192 |
! ! |
|
2193 |
||
633 | 2194 |
!String class methodsFor:'documentation'! |
2195 |
||
2196 |
version |
|
834 | 2197 |
^ '$Header: /cvs/stx/stx/libbasic/String.st,v 1.59 1996-01-04 17:02:20 cg Exp $' |
633 | 2198 |
! ! |