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