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