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