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