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