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