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