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