author | Claus Gittinger <cg@exept.de> |
Mon, 22 Feb 1999 22:06:24 +0100 | |
changeset 3996 | 3183ccccabaa |
parent 3989 | 109b4e016f51 |
child 4120 | 5ad76c8d8e77 |
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 |
||
3989 | 13 |
CharacterArray variableByteSubclass:#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 |
%{ |
2894 | 22 |
|
3809
dd1df62365ee
use threadsafe fprintf (win32)
Claus Gittinger <cg@exept.de>
parents:
3751
diff
changeset
|
23 |
#include <stdio.h> |
dd1df62365ee
use threadsafe fprintf (win32)
Claus Gittinger <cg@exept.de>
parents:
3751
diff
changeset
|
24 |
#define _STDIO_H_INCLUDED_ |
437 | 25 |
|
88 | 26 |
#include <ctype.h> |
359 | 27 |
|
273 | 28 |
#ifdef LINUX |
359 | 29 |
# define __STRINGDEFS__ |
30 |
# include <linuxIntern.h> |
|
302 | 31 |
#else |
32 |
extern char *strchr(); |
|
273 | 33 |
#endif |
95 | 34 |
|
35 |
/* |
|
36 |
* old st/x creates strings with spaces in it; |
|
37 |
* new st/x will fill it with zeros (for st-80 compatibility) |
|
38 |
* the define below sets old behavior. |
|
39 |
*/ |
|
40 |
#define INITIALIZE_WITH_SPACE |
|
88 | 41 |
%} |
216 | 42 |
! ! |
88 | 43 |
|
608 | 44 |
!String class methodsFor:'documentation'! |
45 |
||
46 |
copyright |
|
47 |
" |
|
48 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
49 |
All Rights Reserved |
|
50 |
||
51 |
This software is furnished under a license and may be used |
|
52 |
only in accordance with the terms of that license and with the |
|
53 |
inclusion of the above copyright notice. This software may not |
|
54 |
be provided or otherwise made available to, or used by, any |
|
55 |
other person. No title to or ownership of the software is |
|
56 |
hereby transferred. |
|
57 |
" |
|
58 |
! |
|
3 | 59 |
|
608 | 60 |
documentation |
61 |
" |
|
62 |
Strings are ByteArrays storing Characters. |
|
63 |
||
64 |
Strings are kind of kludgy: to allow for easy handling by c-functions, |
|
65 |
there is always one 0-byte added at the end, which is not counted |
|
3809
dd1df62365ee
use threadsafe fprintf (win32)
Claus Gittinger <cg@exept.de>
parents:
3751
diff
changeset
|
66 |
in the strings size. |
dd1df62365ee
use threadsafe fprintf (win32)
Claus Gittinger <cg@exept.de>
parents:
3751
diff
changeset
|
67 |
Also, the at:put: method does not allow for storing 0-bytes. |
608 | 68 |
(to do this, the basicAt:put: and basicNew: methods are redefined) |
3 | 69 |
|
608 | 70 |
You cannot add any instvars to String, since the the run time system & compiler |
71 |
creates literal strings and knows that strings have no named instvars. |
|
3809
dd1df62365ee
use threadsafe fprintf (win32)
Claus Gittinger <cg@exept.de>
parents:
3751
diff
changeset
|
72 |
If you really need strings with instVars, you have to create a subclass |
608 | 73 |
of String (the access functions defined here can handle this). |
74 |
A little warning though: not all smalltalk systems allow subclassing String, |
|
75 |
so your program may become unportable if you do so. |
|
1290 | 76 |
|
77 |
[author:] |
|
1486 | 78 |
Claus Gittinger |
1310 | 79 |
|
80 |
[see also:] |
|
1486 | 81 |
Text StringCollection TwoByteString JISEncodedString |
82 |
Symbol |
|
608 | 83 |
" |
3 | 84 |
! ! |
85 |
||
1 | 86 |
!String class methodsFor:'instance creation'! |
87 |
||
88 |
basicNew:anInteger |
|
89 |
"return a new empty string with anInteger characters" |
|
90 |
||
91 |
%{ /* NOCONTEXT */ |
|
92 |
||
93 |
OBJ newString; |
|
94 |
REGISTER int len; |
|
95 |
REGISTER unsigned char *cp; |
|
96 |
REGISTER OBJ *op; |
|
97 |
int nInstVars, instsize; |
|
98 |
||
252 | 99 |
if (__isSmallInteger(anInteger)) { |
3451 | 100 |
len = __intVal(anInteger); |
101 |
if (len >= 0) { |
|
102 |
instsize = OHDR_SIZE + len + 1; |
|
103 |
if (self == String) { |
|
104 |
if (__CanDoQuickNew(instsize)) { /* OBJECT ALLOCATION */ |
|
105 |
/* |
|
106 |
* the most common case |
|
107 |
*/ |
|
108 |
__qCheckedNew(newString, instsize); |
|
109 |
__InstPtr(newString)->o_class = self; /* no need for PROTECT - there was no GC */ |
|
110 |
__qSTORE(newString, self); |
|
111 |
||
112 |
cp = __stringVal(newString); |
|
2862 | 113 |
|
357 | 114 |
#if defined(memset4) && !defined(NON_ASCII) |
3451 | 115 |
{ |
116 |
/* |
|
117 |
* no sizeof(int) here please - |
|
118 |
* - memset4 (if defined) fills 4-bytes on ALL machines |
|
119 |
*/ |
|
120 |
int l4 = len >> 2; |
|
121 |
||
122 |
if (len & 3) l4++; |
|
123 |
memset4(cp, 0x20202020, l4); |
|
124 |
cp[len] = '\0'; |
|
125 |
} |
|
95 | 126 |
#else |
357 | 127 |
# ifdef FAST_MEMSET |
3451 | 128 |
memset(cp, ' ', len); |
129 |
cp[len] = '\0'; |
|
357 | 130 |
# else |
3451 | 131 |
while (len >= 8) { |
3449
84160615c630
oops - basicNew was bad for subclasses of String.
Claus Gittinger <cg@exept.de>
parents:
3403
diff
changeset
|
132 |
# ifndef NON_ASCII /* i.e. EBCDIC */ |
2954 | 133 |
# ifdef INT64 |
3451 | 134 |
((INT64 *)cp)[0] = 0x2020202020202020L; |
2862 | 135 |
# else |
3451 | 136 |
((int *)cp)[0] = 0x20202020; |
137 |
((int *)cp)[1] = 0x20202020; |
|
2862 | 138 |
# endif |
357 | 139 |
# else |
3451 | 140 |
cp[0] = cp[1] = cp[2] = cp[3] = ' '; |
141 |
cp[4] = cp[5] = cp[6] = cp[7] = ' '; |
|
357 | 142 |
# endif |
3451 | 143 |
cp += 8; |
144 |
len -= 8; |
|
145 |
} |
|
146 |
while (len--) |
|
147 |
*cp++ = ' '; |
|
148 |
*cp = '\0'; |
|
2867
015574e235fd
tuned replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2862
diff
changeset
|
149 |
# endif /* not FAST_MEMSET */ |
015574e235fd
tuned replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2862
diff
changeset
|
150 |
#endif /* not memset4 */ |
015574e235fd
tuned replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2862
diff
changeset
|
151 |
|
3451 | 152 |
RETURN (newString); |
153 |
} |
|
154 |
nInstVars = 0; |
|
155 |
} else { |
|
156 |
nInstVars = __intVal(__ClassInstPtr(self)->c_ninstvars); |
|
157 |
instsize += __OBJS2BYTES__(nInstVars); |
|
158 |
} |
|
159 |
||
160 |
__PROTECT_CONTEXT__; |
|
161 |
__qNew(newString, instsize); /* OBJECT ALLOCATION */ |
|
162 |
__UNPROTECT_CONTEXT__; |
|
163 |
||
164 |
if (newString == nil) goto fail; |
|
165 |
||
166 |
__InstPtr(newString)->o_class = self; |
|
167 |
__qSTORE(newString, self); |
|
168 |
||
169 |
if (nInstVars) { |
|
170 |
/* |
|
171 |
* nil-out instvars |
|
172 |
*/ |
|
2867
015574e235fd
tuned replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2862
diff
changeset
|
173 |
#if defined(memset4) |
3451 | 174 |
memset4(__InstPtr(newString)->i_instvars, nil, nInstVars); |
2867
015574e235fd
tuned replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2862
diff
changeset
|
175 |
#else |
015574e235fd
tuned replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2862
diff
changeset
|
176 |
# if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES) |
3451 | 177 |
/* |
178 |
* knowing that nil is 0 |
|
179 |
*/ |
|
180 |
memset(__InstPtr(newString)->i_instvars, 0, __OBJS2BYTES__(nInstVars)); |
|
2867
015574e235fd
tuned replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2862
diff
changeset
|
181 |
# else |
3451 | 182 |
op = __InstPtr(newString)->i_instvars; |
183 |
do { |
|
184 |
*op++ = nil; |
|
185 |
} while (--nInstVars); |
|
357 | 186 |
# endif |
95 | 187 |
#endif |
3451 | 188 |
cp = __stringVal(newString) + __OBJS2BYTES__(nInstVars); |
189 |
} else { |
|
190 |
cp = __stringVal(newString); |
|
191 |
} |
|
192 |
||
193 |
/* |
|
194 |
* fill with spaces |
|
195 |
*/ |
|
2867
015574e235fd
tuned replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2862
diff
changeset
|
196 |
#ifdef FAST_MEMSET |
3451 | 197 |
memset(cp, ' ', len); |
198 |
*(cp + len) = '\0'; |
|
1 | 199 |
#else |
3451 | 200 |
while (len >= 8) { |
357 | 201 |
# ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
2954 | 202 |
# ifdef INT64 |
3451 | 203 |
((INT64 *)cp)[0] = 0x2020202020202020L; |
2862 | 204 |
# else |
3451 | 205 |
((int *)cp)[0] = 0x20202020; |
206 |
((int *)cp)[1] = 0x20202020; |
|
2862 | 207 |
# endif |
357 | 208 |
# else |
3451 | 209 |
cp[0] = cp[1] = cp[2] = cp[3] = ' '; |
210 |
cp[4] = cp[5] = cp[6] = cp[7] = ' '; |
|
357 | 211 |
# endif |
3451 | 212 |
cp += 8; |
213 |
len -= 8; |
|
214 |
} |
|
215 |
while (len--) |
|
216 |
*cp++ = ' '; |
|
217 |
*cp = '\0'; |
|
1 | 218 |
#endif |
3451 | 219 |
RETURN (newString); |
220 |
} |
|
1 | 221 |
} |
95 | 222 |
fail: ;; |
1019 | 223 |
%}. |
95 | 224 |
" |
225 |
invalid argument, or out-of-memory: |
|
226 |
use error handling in superclass |
|
227 |
" |
|
1 | 228 |
^ (super basicNew:anInteger) atAllPut:(Character space) |
229 |
! |
|
230 |
||
608 | 231 |
new:n |
232 |
^ self basicNew:n |
|
347 | 233 |
! |
234 |
||
235 |
readFrom:aStream onError:exceptionBlock |
|
236 |
"read & return the next String from the (character-)stream aStream; |
|
237 |
skipping all whitespace first; return the value of exceptionBlock, |
|
238 |
if no string can be read. The sequence of characters as read from the |
|
239 |
stream must be one as stored via storeOn: or storeString." |
|
240 |
||
241 |
|collected char| |
|
242 |
||
243 |
" |
|
244 |
this method is not to be inherited |
|
245 |
(i.e. not ok for subclasses; Symbol, for example) |
|
246 |
" |
|
247 |
self ~~ String ifTrue:[ |
|
3451 | 248 |
^ super readFrom:aStream onError:exceptionBlock |
347 | 249 |
]. |
250 |
||
251 |
"skip whiteSpace" |
|
252 |
aStream skipSeparators. |
|
253 |
(aStream next == $') ifTrue:[ |
|
3451 | 254 |
collected := WriteStream on:(String new). |
255 |
[true] whileTrue:[ |
|
256 |
aStream atEnd ifTrue:[ |
|
257 |
"/ mhmh - reached the end without a closing quote |
|
258 |
"/ looks like an error to me ... |
|
259 |
^ exceptionBlock value |
|
260 |
]. |
|
261 |
char := aStream next. |
|
262 |
char == $' ifTrue:[ |
|
263 |
"/ look for another quote |
|
264 |
aStream peekOrNil == $' ifFalse:[ |
|
265 |
^ collected contents |
|
266 |
]. |
|
267 |
aStream next. |
|
268 |
]. |
|
269 |
collected nextPut:char |
|
270 |
] |
|
347 | 271 |
]. |
272 |
^ exceptionBlock value |
|
273 |
||
274 |
" |
|
275 |
String readFrom:('''hello world''' readStream) |
|
276 |
String readFrom:('''hello '''' world''' readStream) |
|
277 |
String readFrom:('1 ''hello'' ' readStream) |
|
278 |
String readFrom:('1 ''hello'' ' readStream) onError:['foobar'] |
|
3376 | 279 |
" |
280 |
||
281 |
"Modified: / 14.4.1998 / 18:46:26 / cg" |
|
608 | 282 |
! |
283 |
||
284 |
unititializedNew:anInteger |
|
285 |
"redefine it back - strings must have a 0-byte at the end" |
|
286 |
||
287 |
^ self basicNew:anInteger |
|
288 |
! ! |
|
289 |
||
290 |
!String class methodsFor:'binary storage'! |
|
291 |
||
292 |
binaryDefinitionFrom:stream manager:manager |
|
3085 | 293 |
"read a binary representation from stream. This is only invoked |
294 |
for long strings. Short strings are stored with 1byte length." |
|
295 |
||
296 |
|s len| |
|
608 | 297 |
|
3085 | 298 |
"take care of subclasses ..." |
299 |
((self == String) or:[self == Symbol]) ifTrue:[ |
|
3111 | 300 |
len := stream nextNumber:4. |
301 |
s := String basicNew:len. |
|
302 |
stream nextBytes:len into:s startingAt:1. |
|
303 |
^ s |
|
3085 | 304 |
]. |
305 |
^ super binaryDefinitionFrom:stream manager:manager |
|
306 |
||
307 |
"Modified: / 2.11.1997 / 16:18:37 / cg" |
|
608 | 308 |
! ! |
309 |
||
310 |
!String class methodsFor:'queries'! |
|
311 |
||
2680
18c85df5d930
#defaultPlatformClass for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2670
diff
changeset
|
312 |
defaultPlatformClass |
18c85df5d930
#defaultPlatformClass for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2670
diff
changeset
|
313 |
"dummy for ST-80 compatibility" |
18c85df5d930
#defaultPlatformClass for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2670
diff
changeset
|
314 |
|
18c85df5d930
#defaultPlatformClass for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2670
diff
changeset
|
315 |
^ self |
18c85df5d930
#defaultPlatformClass for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2670
diff
changeset
|
316 |
|
18c85df5d930
#defaultPlatformClass for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2670
diff
changeset
|
317 |
"Created: 6.6.1997 / 18:25:56 / cg" |
18c85df5d930
#defaultPlatformClass for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2670
diff
changeset
|
318 |
! |
18c85df5d930
#defaultPlatformClass for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2670
diff
changeset
|
319 |
|
608 | 320 |
isBuiltInClass |
1264 | 321 |
"return true if this class is known by the run-time-system. |
322 |
Here, true is returned for myself, false for subclasses." |
|
608 | 323 |
|
324 |
^ self == String |
|
1264 | 325 |
|
326 |
"Modified: 23.4.1996 / 16:00:38 / cg" |
|
1 | 327 |
! ! |
328 |
||
329 |
!String methodsFor:'accessing'! |
|
330 |
||
329 | 331 |
at:index |
332 |
"return the character at position index, an Integer. |
|
345 | 333 |
Reimplemented here to avoid the additional at:->basicAt: send |
334 |
(which we can do here, since at: is obviously not redefined in a subclass). |
|
329 | 335 |
This method is the same as at:." |
336 |
||
337 |
%{ /* NOCONTEXT */ |
|
338 |
||
339 |
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
|
340 |
REGISTER OBJ slf, cls; |
329 | 341 |
|
342 |
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
|
343 |
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
|
344 |
slf = self; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
345 |
|
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
346 |
cls = __qClass(slf); |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
347 |
if (cls != String) |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
348 |
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
|
349 |
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
|
350 |
RETURN ( __MKCHARACTER(__stringVal(slf)[indx] & 0xFF) ); |
329 | 351 |
} |
352 |
} |
|
353 |
%}. |
|
3451 | 354 |
index isInteger ifFalse:[ |
355 |
^ self indexNotInteger:index |
|
356 |
]. |
|
329 | 357 |
^ self subscriptBoundsError:index |
1 | 358 |
! |
359 |
||
329 | 360 |
at:index put:aCharacter |
361 |
"store the argument, aCharacter at position index, an Integer. |
|
1218
5ed62714c8d1
commentary; ignoredMethods removed
Claus Gittinger <cg@exept.de>
parents:
1143
diff
changeset
|
362 |
Return aCharacter (sigh). |
345 | 363 |
Reimplemented here to avoid the additional at:put:->basicAt:put: send |
364 |
(but only for Strings, since subclasses may redefine basicAt:put:). |
|
329 | 365 |
This method is the same as basicAt:put:." |
366 |
||
367 |
%{ /* NOCONTEXT */ |
|
368 |
||
369 |
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
|
370 |
REGISTER OBJ slf; |
329 | 371 |
|
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
372 |
slf = self; |
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 |
if (__isString(slf)) { |
1486 | 375 |
if (__isCharacter(aCharacter)) { |
376 |
value = __intVal(_characterVal(aCharacter)); |
|
377 |
if (((unsigned)value <= 0xFF) |
|
378 |
&& __isSmallInteger(index)) { |
|
379 |
indx = __intVal(index) - 1; |
|
380 |
if ((unsigned)indx < (unsigned)(__stringSize(slf))) { |
|
381 |
__stringVal(slf)[indx] = value; |
|
382 |
RETURN ( aCharacter ); |
|
383 |
} |
|
384 |
} |
|
385 |
} |
|
329 | 386 |
} |
387 |
%}. |
|
345 | 388 |
(self isMemberOf:String) ifFalse:[ |
1486 | 389 |
^ super at:index put:aCharacter |
345 | 390 |
]. |
391 |
||
329 | 392 |
(aCharacter isMemberOf:Character) ifFalse:[ |
1486 | 393 |
" |
394 |
tried to store something which is not a character |
|
395 |
" |
|
396 |
^ self elementNotCharacter |
|
329 | 397 |
]. |
398 |
(aCharacter asciiValue between:1 and:255) ifFalse:[ |
|
1486 | 399 |
" |
400 |
tried to store a multibyte character |
|
401 |
" |
|
402 |
^ self elementBoundsError |
|
329 | 403 |
]. |
404 |
" |
|
405 |
invalid index |
|
406 |
" |
|
3451 | 407 |
index isInteger ifFalse:[ |
408 |
^ self indexNotInteger:index |
|
409 |
]. |
|
329 | 410 |
^ self subscriptBoundsError:index |
1218
5ed62714c8d1
commentary; ignoredMethods removed
Claus Gittinger <cg@exept.de>
parents:
1143
diff
changeset
|
411 |
|
5ed62714c8d1
commentary; ignoredMethods removed
Claus Gittinger <cg@exept.de>
parents:
1143
diff
changeset
|
412 |
"Modified: 19.4.1996 / 11:40:27 / cg" |
329 | 413 |
! |
414 |
||
608 | 415 |
basicAt:index |
416 |
"return the character at position index, an Integer |
|
417 |
- reimplemented here since we return characters" |
|
418 |
||
419 |
%{ /* NOCONTEXT */ |
|
420 |
||
421 |
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
|
422 |
REGISTER OBJ slf, cls; |
608 | 423 |
|
424 |
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
|
425 |
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
|
426 |
|
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
427 |
slf = self; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
428 |
cls = __qClass(slf); |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
429 |
if (cls != String) |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
430 |
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
|
431 |
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
|
432 |
RETURN ( __MKCHARACTER(__stringVal(slf)[indx] & 0xFF) ); |
608 | 433 |
} |
434 |
} |
|
435 |
%}. |
|
3451 | 436 |
index isInteger ifFalse:[ |
437 |
^ self indexNotInteger:index |
|
438 |
]. |
|
608 | 439 |
^ self subscriptBoundsError:index |
440 |
! |
|
441 |
||
1 | 442 |
basicAt:index put:aCharacter |
1218
5ed62714c8d1
commentary; ignoredMethods removed
Claus Gittinger <cg@exept.de>
parents:
1143
diff
changeset
|
443 |
"store the argument, aCharacter at position index, an Integer. |
5ed62714c8d1
commentary; ignoredMethods removed
Claus Gittinger <cg@exept.de>
parents:
1143
diff
changeset
|
444 |
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
|
445 |
- reimplemented here since we store characters" |
1 | 446 |
|
447 |
%{ /* NOCONTEXT */ |
|
448 |
||
449 |
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
|
450 |
REGISTER OBJ slf; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
451 |
REGISTER OBJ cls; |
1 | 452 |
|
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
453 |
slf = self; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
454 |
|
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
455 |
if (__isCharacter(aCharacter)) { |
1486 | 456 |
value = __intVal(_characterVal(aCharacter)); |
457 |
if (((unsigned)value <= 0xFF) |
|
458 |
&& __isSmallInteger(index)) { |
|
459 |
indx = __intVal(index) - 1; |
|
460 |
cls = __qClass(slf); |
|
461 |
if (cls != String) |
|
462 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
463 |
if ((unsigned)indx < (unsigned)(__stringSize(slf))) { |
|
464 |
__stringVal(slf)[indx] = value; |
|
465 |
RETURN ( aCharacter ); |
|
466 |
} |
|
467 |
} |
|
1 | 468 |
} |
159 | 469 |
%}. |
1 | 470 |
(aCharacter isMemberOf:Character) ifFalse:[ |
1486 | 471 |
" |
472 |
tried to store something which is not a character |
|
473 |
" |
|
474 |
^ self elementNotCharacter |
|
299 | 475 |
]. |
476 |
(aCharacter asciiValue between:1 and:255) ifFalse:[ |
|
1486 | 477 |
" |
478 |
tried to store a multibyte character |
|
479 |
" |
|
480 |
^ self elementBoundsError |
|
299 | 481 |
]. |
482 |
" |
|
483 |
invalid index |
|
484 |
" |
|
3451 | 485 |
index isInteger ifFalse:[ |
486 |
^ self indexNotInteger:index |
|
487 |
]. |
|
299 | 488 |
^ self subscriptBoundsError:index |
1218
5ed62714c8d1
commentary; ignoredMethods removed
Claus Gittinger <cg@exept.de>
parents:
1143
diff
changeset
|
489 |
|
5ed62714c8d1
commentary; ignoredMethods removed
Claus Gittinger <cg@exept.de>
parents:
1143
diff
changeset
|
490 |
"Modified: 19.4.1996 / 11:15:48 / cg" |
429 | 491 |
! |
492 |
||
608 | 493 |
basicSize |
494 |
"return the number of characters in myself. |
|
495 |
Redefined here to exclude the 0-byte at the end." |
|
496 |
||
497 |
%{ /* NOCONTEXT */ |
|
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
498 |
REGISTER OBJ slf, cls; |
608 | 499 |
|
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
500 |
slf = self; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
501 |
|
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
502 |
cls = __qClass(slf); |
608 | 503 |
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
|
504 |
RETURN ( __MKSMALLINT(__stringSize(slf)) ); |
608 | 505 |
} |
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
506 |
RETURN ( __MKSMALLINT(__stringSize(slf) |
1486 | 507 |
- __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)))); |
608 | 508 |
%} |
509 |
! |
|
429 | 510 |
|
608 | 511 |
size |
512 |
"return the number of characters in myself. |
|
513 |
Reimplemented here to avoid the additional size->basicSize send |
|
514 |
(which we can do here, since size is obviously not redefined in a subclass). |
|
515 |
This method is the same as basicSize." |
|
429 | 516 |
|
608 | 517 |
%{ /* NOCONTEXT */ |
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
518 |
REGISTER OBJ cls, slf; |
608 | 519 |
|
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
520 |
slf = self; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
521 |
cls = __qClass(slf); |
608 | 522 |
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
|
523 |
RETURN ( __MKSMALLINT(__stringSize(slf)) ); |
429 | 524 |
} |
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
525 |
RETURN ( __MKSMALLINT(__stringSize(slf) |
1134 | 526 |
- __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)))); |
608 | 527 |
%} |
2 | 528 |
! ! |
529 |
||
3085 | 530 |
!String methodsFor:'binary storage'! |
531 |
||
532 |
storeBinaryDefinitionOn:stream manager:manager |
|
533 |
"append a binary representation of the receiver onto stream. |
|
534 |
Redefined since short Strings can be stored with a special type code |
|
535 |
in a more compact way. |
|
536 |
This is an internal interface for the binary storage mechanism." |
|
537 |
||
538 |
|myClass myBasicSize| |
|
539 |
||
540 |
"/ not for subclasses with named instVars. |
|
541 |
(myClass := self class) instSize ~~ 0 ifTrue:[ |
|
3111 | 542 |
^ super storeBinaryDefinitionOn:stream manager:manager |
3085 | 543 |
]. |
544 |
||
545 |
myBasicSize := self basicSize. |
|
546 |
||
547 |
"/ can use a more compact representation; |
|
548 |
"/ but not for subclasses ... |
|
549 |
||
550 |
((myClass == String) |
|
551 |
and:[myBasicSize <= 255]) ifTrue:[ |
|
3111 | 552 |
"/ special encoding: <codeForString> <len> <bytes> ... |
553 |
stream nextPut:(manager codeForString); nextPut:myBasicSize. |
|
3085 | 554 |
] ifFalse:[ |
3111 | 555 |
manager putIdOfClass:myClass on:stream. |
556 |
stream nextNumber:4 put:myBasicSize. |
|
3085 | 557 |
]. |
558 |
stream nextPutBytes:myBasicSize from:self startingAt:1. |
|
559 |
||
560 |
"Modified: / 2.11.1997 / 15:28:56 / cg" |
|
561 |
! ! |
|
562 |
||
53 | 563 |
!String methodsFor:'character searching'! |
1 | 564 |
|
1950
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
565 |
identityIndexOf:aCharacter |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
566 |
"return the index of the first occurrences of the argument, aCharacter |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
567 |
in the receiver or 0 if not found - reimplemented here for speed." |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
568 |
|
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
569 |
%{ /* NOCONTEXT */ |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
570 |
|
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
571 |
REGISTER unsigned char *cp; |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
572 |
#ifdef FAST_STRCHR |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
573 |
char *strchr(); |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
574 |
#else |
2858
866ffc52ac8a
oops - need unsigned character compares
Claus Gittinger <cg@exept.de>
parents:
2856
diff
changeset
|
575 |
REGISTER unsigned byteValue; |
1950
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
576 |
REGISTER int index; |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
577 |
char c; |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
578 |
#endif |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
579 |
OBJ cls; |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
580 |
|
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
581 |
if (__isCharacter(aCharacter)) { |
3111 | 582 |
cp = __stringVal(self); |
583 |
if ((cls = __qClass(self)) != String) |
|
584 |
cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1950
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
585 |
#ifdef FAST_STRCHR |
3111 | 586 |
cp = (unsigned char *) strchr(cp, __intVal(_characterVal(aCharacter))); |
587 |
if (cp) { |
|
588 |
RETURN ( __MKSMALLINT(cp - __stringVal(self) + 1) ); |
|
589 |
} |
|
1950
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
590 |
#else |
3111 | 591 |
byteValue = __intVal(_characterVal(aCharacter)); |
592 |
index = 1; |
|
593 |
while (c = *cp++) { |
|
594 |
if (c == byteValue) { RETURN ( __MKSMALLINT(index) ); } |
|
595 |
index++; |
|
596 |
} |
|
1950
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
597 |
#endif |
3111 | 598 |
RETURN ( __MKSMALLINT(0)); |
1950
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
599 |
} |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
600 |
%}. |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
601 |
"/ with identity compares, only characters can be in myself |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
602 |
^ 0 |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
603 |
|
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
604 |
" |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
605 |
'hello world' indexOf:(Character space) |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
606 |
'hello world' indexOf:$A |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
607 |
" |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
608 |
! |
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
609 |
|
1 | 610 |
includes:aCharacter |
611 |
"return true if the argument, aCharacter is included in the receiver |
|
612 |
- reimplemented here for speed" |
|
613 |
||
614 |
%{ /* NOCONTEXT */ |
|
615 |
||
616 |
REGISTER unsigned char *cp; |
|
617 |
REGISTER int byteValue; |
|
53 | 618 |
OBJ cls; |
1 | 619 |
|
53 | 620 |
if (__isCharacter(aCharacter)) { |
3111 | 621 |
byteValue = __intVal(_characterVal(aCharacter)); |
622 |
cp = __stringVal(self); |
|
623 |
if ((cls = __qClass(self)) != String) |
|
624 |
cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1 | 625 |
#ifdef FAST_STRCHR |
3111 | 626 |
cp = (unsigned char *) strchr(cp, __intVal(_characterVal(aCharacter))); |
627 |
if (cp) { |
|
628 |
RETURN ( true ); |
|
629 |
} |
|
1 | 630 |
#else |
3111 | 631 |
while (*cp) { |
632 |
if (*cp == byteValue) { |
|
633 |
RETURN ( true ); |
|
634 |
} |
|
635 |
cp++; |
|
636 |
} |
|
1 | 637 |
#endif |
3111 | 638 |
RETURN (false); |
1 | 639 |
} |
299 | 640 |
%}. |
1950
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
641 |
^ super includes:aCharacter |
53 | 642 |
|
95 | 643 |
" |
644 |
'hello world' includes:$a |
|
645 |
'hello world' includes:$o |
|
646 |
'hello world' includes:$x |
|
647 |
'hello world' includes:1 |
|
299 | 648 |
'hello world' asTwoByteString includes:$o |
95 | 649 |
" |
1 | 650 |
! |
651 |
||
10 | 652 |
includesAny:aCollection |
653 |
"return true, if the receiver includes any of the characters in the |
|
53 | 654 |
argument, aCollection. |
655 |
- redefined for speed if the argument is a String." |
|
10 | 656 |
|
657 |
%{ /* NOCONTEXT */ |
|
658 |
||
659 |
REGISTER unsigned char *cp; |
|
660 |
REGISTER unsigned char *matchP; |
|
53 | 661 |
OBJ cls; |
10 | 662 |
|
53 | 663 |
if (__isString(aCollection)) { |
1134 | 664 |
matchP = __stringVal(aCollection); |
665 |
cp = __stringVal(self); |
|
329 | 666 |
if ((cls = __qClass(self)) != String) |
1134 | 667 |
cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
53 | 668 |
|
159 | 669 |
while (*cp) { |
329 | 670 |
if (strchr(matchP, *cp)) { |
159 | 671 |
RETURN ( true ); |
672 |
} |
|
673 |
cp++; |
|
674 |
} |
|
675 |
RETURN ( false ); |
|
10 | 676 |
} |
677 |
%} |
|
678 |
. |
|
679 |
^ super includesAny:aCollection |
|
95 | 680 |
|
681 |
" |
|
682 |
'hello world' includesAny:'abcd' |
|
683 |
'hello world' includesAny:'xyz' |
|
684 |
'hello world' includesAny:(Array with:$a with:$b with:$d) |
|
685 |
'hello world' includesAny:(Array with:$x with:$y) |
|
686 |
'hello world' includesAny:(Array with:1 with:2) |
|
687 |
" |
|
10 | 688 |
! |
689 |
||
1 | 690 |
indexOf:aCharacter |
691 |
"return the index of the first occurrences of the argument, aCharacter |
|
692 |
in the receiver or 0 if not found - reimplemented here for speed." |
|
693 |
||
694 |
%{ /* NOCONTEXT */ |
|
695 |
||
696 |
REGISTER unsigned char *cp; |
|
2858
866ffc52ac8a
oops - need unsigned character compares
Claus Gittinger <cg@exept.de>
parents:
2856
diff
changeset
|
697 |
REGISTER unsigned byteValue; |
2530 | 698 |
REGISTER int index; |
1 | 699 |
#ifdef FAST_STRCHR |
700 |
char *strchr(); |
|
2529
f33167bc7b23
Define vars for #ifdef FAST_STRCHR.
Stefan Vogel <sv@exept.de>
parents:
2528
diff
changeset
|
701 |
#else |
2858
866ffc52ac8a
oops - need unsigned character compares
Claus Gittinger <cg@exept.de>
parents:
2856
diff
changeset
|
702 |
unsigned char c; |
1 | 703 |
#endif |
53 | 704 |
OBJ cls; |
1 | 705 |
|
53 | 706 |
if (__isCharacter(aCharacter)) { |
3111 | 707 |
cp = __stringVal(self); |
708 |
if ((cls = __qClass(self)) != String) |
|
709 |
cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
2528
a2a7639ab47c
allow searching for 0-byte with indexOf and indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2416
diff
changeset
|
710 |
|
3111 | 711 |
byteValue = __intVal(_characterVal(aCharacter)); |
712 |
if (byteValue == 0) { |
|
713 |
index = strlen(cp); |
|
714 |
if (index >= __stringSize(self)) { |
|
715 |
index = 0; |
|
716 |
} else { |
|
717 |
index++; |
|
718 |
} |
|
719 |
RETURN ( __MKSMALLINT(index) ); |
|
720 |
} |
|
2528
a2a7639ab47c
allow searching for 0-byte with indexOf and indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2416
diff
changeset
|
721 |
|
1 | 722 |
#ifdef FAST_STRCHR |
3111 | 723 |
cp = (unsigned char *) strchr(cp, byteValue); |
724 |
if (cp) { |
|
725 |
RETURN ( __MKSMALLINT(cp - __stringVal(self) + 1) ); |
|
726 |
} |
|
1 | 727 |
#else |
3111 | 728 |
index = 1; |
729 |
while (c = *cp++) { |
|
730 |
if (c == byteValue) { RETURN ( __MKSMALLINT(index) ); } |
|
731 |
index++; |
|
732 |
} |
|
1 | 733 |
#endif |
3111 | 734 |
RETURN ( __MKSMALLINT(0)); |
1 | 735 |
} |
370 | 736 |
%}. |
1950
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
737 |
^ super indexOf:aCharacter |
95 | 738 |
|
739 |
" |
|
740 |
'hello world' indexOf:(Character space) |
|
741 |
'hello world' indexOf:$A |
|
742 |
" |
|
1 | 743 |
! |
744 |
||
745 |
indexOf:aCharacter startingAt:start |
|
746 |
"return the index of the first occurrence of the argument, aCharacter |
|
747 |
in myself starting at start, anInteger or 0 if not found; |
|
748 |
- reimplemented here for speed" |
|
749 |
||
750 |
%{ /* NOCONTEXT */ |
|
751 |
||
752 |
REGISTER unsigned char *cp; |
|
2858
866ffc52ac8a
oops - need unsigned character compares
Claus Gittinger <cg@exept.de>
parents:
2856
diff
changeset
|
753 |
REGISTER int index; |
866ffc52ac8a
oops - need unsigned character compares
Claus Gittinger <cg@exept.de>
parents:
2856
diff
changeset
|
754 |
REGISTER unsigned byteValue; |
1 | 755 |
#ifdef FAST_STRCHR |
756 |
char *strchr(); |
|
370 | 757 |
#else |
2858
866ffc52ac8a
oops - need unsigned character compares
Claus Gittinger <cg@exept.de>
parents:
2856
diff
changeset
|
758 |
unsigned char c; |
1 | 759 |
#endif |
760 |
int len; |
|
53 | 761 |
OBJ cls; |
1 | 762 |
|
252 | 763 |
if (__isSmallInteger(start)) { |
3111 | 764 |
if (__isCharacter(aCharacter)) { |
765 |
byteValue = __intVal(_characterVal(aCharacter)); |
|
766 |
index = __intVal(start); |
|
767 |
if (index <= 0) |
|
768 |
index = 1; |
|
769 |
if ((cls = __qClass(self)) != String) |
|
770 |
index += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
771 |
len = __stringSize(self); |
|
772 |
if (index <= len) { |
|
773 |
cp = __stringVal(self) + index - 1; |
|
2528
a2a7639ab47c
allow searching for 0-byte with indexOf and indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2416
diff
changeset
|
774 |
|
3111 | 775 |
if (byteValue == 0) { |
776 |
index += strlen(cp); |
|
777 |
if (index > len) { |
|
778 |
index = 0; |
|
779 |
} |
|
780 |
RETURN ( __MKSMALLINT(index) ); |
|
781 |
} |
|
2528
a2a7639ab47c
allow searching for 0-byte with indexOf and indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2416
diff
changeset
|
782 |
|
1 | 783 |
#ifdef FAST_STRCHR |
3111 | 784 |
cp = (unsigned char *) strchr(cp, byteValue); |
785 |
if (cp) { |
|
786 |
RETURN ( __MKSMALLINT(cp - __stringVal(self) + 1) ); |
|
787 |
} |
|
1 | 788 |
#else |
3111 | 789 |
while (c = *cp++) { |
2858
866ffc52ac8a
oops - need unsigned character compares
Claus Gittinger <cg@exept.de>
parents:
2856
diff
changeset
|
790 |
|
3111 | 791 |
if (c == byteValue) { |
792 |
RETURN ( __MKSMALLINT(index) ); |
|
793 |
} |
|
794 |
index++; |
|
795 |
} |
|
1 | 796 |
#endif |
3111 | 797 |
} |
798 |
} |
|
799 |
RETURN ( __MKSMALLINT(0) ); |
|
1 | 800 |
} |
370 | 801 |
%}. |
1 | 802 |
^ super indexOf:aCharacter startingAt:start |
53 | 803 |
|
95 | 804 |
" |
805 |
'hello world' indexOf:$l startingAt:1 |
|
806 |
'hello world' indexOf:$l startingAt:5 |
|
807 |
" |
|
1 | 808 |
! |
809 |
||
516
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
810 |
indexOfAny:aCollectionOfCharacters startingAt:start |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
811 |
"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
|
812 |
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
|
813 |
- 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
|
814 |
|
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
815 |
%{ /* NOCONTEXT */ |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
816 |
|
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
817 |
REGISTER unsigned char *ccp; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
818 |
REGISTER unsigned char *cp; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
819 |
REGISTER int index; |
2894 | 820 |
unsigned char *matchP; |
2858
866ffc52ac8a
oops - need unsigned character compares
Claus Gittinger <cg@exept.de>
parents:
2856
diff
changeset
|
821 |
unsigned char c; |
516
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
822 |
int len; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
823 |
OBJ cls; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
824 |
|
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
825 |
if (__isSmallInteger(start) |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
826 |
&& __isString(aCollectionOfCharacters)) { |
3111 | 827 |
matchP = __stringVal(aCollectionOfCharacters); |
828 |
index = __intVal(start); |
|
829 |
if (index <= 0) |
|
830 |
index = 1; |
|
831 |
if ((cls = __qClass(self)) != String) |
|
832 |
index += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
833 |
len = __stringSize(self); |
|
834 |
if (index <= len) { |
|
835 |
cp = __stringVal(self) + index - 1; |
|
836 |
while (c = *cp++) { |
|
837 |
for (ccp = matchP; *ccp ; ccp++) { |
|
838 |
if (*ccp == c) { |
|
839 |
RETURN ( __MKSMALLINT(index) ); |
|
840 |
} |
|
841 |
} |
|
842 |
index++; |
|
843 |
} |
|
844 |
} |
|
845 |
RETURN ( __MKSMALLINT(0) ); |
|
516
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
846 |
} |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
847 |
%}. |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
848 |
"/ |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
849 |
"/ 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
|
850 |
"/ |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
851 |
^ super indexOfAny:aCollectionOfCharacters startingAt:start |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
852 |
|
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
853 |
" |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
854 |
'hello world' indexOfAny:'eoa' startingAt:1 |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
855 |
'hello world' indexOfAny:'eoa' startingAt:6 |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
856 |
'hello world' indexOfAny:'AOE' startingAt:1 |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
857 |
" |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
858 |
! |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
859 |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
860 |
indexOfControlCharacterStartingAt:start |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
861 |
"return the index of the next control character; |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
862 |
that is a character with asciiValue < 32. |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
863 |
Return 0 if none is found." |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
864 |
|
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
865 |
%{ /* NOCONTEXT */ |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
866 |
|
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
867 |
#ifndef NON_ASCII /* i.e. not EBCDIC ;-) */ |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
868 |
REGISTER unsigned char *cp; |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
869 |
REGISTER unsigned char c; |
3734
5d4e741062bd
may not finish search with 0-byte
Claus Gittinger <cg@exept.de>
parents:
3712
diff
changeset
|
870 |
REGISTER unsigned char *cpEnd; |
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
871 |
int len, index; |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
872 |
OBJ cls; |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
873 |
|
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
874 |
index = __intVal(start); |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
875 |
if (index <= 0) { |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
876 |
index = 1; |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
877 |
} |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
878 |
if ((cls = __qClass(self)) != String) |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
879 |
index += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
880 |
len = __stringSize(self); |
3734
5d4e741062bd
may not finish search with 0-byte
Claus Gittinger <cg@exept.de>
parents:
3712
diff
changeset
|
881 |
cpEnd = __stringVal(self) + len; |
5d4e741062bd
may not finish search with 0-byte
Claus Gittinger <cg@exept.de>
parents:
3712
diff
changeset
|
882 |
cp = __stringVal(self) + index - 1; |
5d4e741062bd
may not finish search with 0-byte
Claus Gittinger <cg@exept.de>
parents:
3712
diff
changeset
|
883 |
if (cp < cpEnd) { |
5d4e741062bd
may not finish search with 0-byte
Claus Gittinger <cg@exept.de>
parents:
3712
diff
changeset
|
884 |
while (cp < cpEnd) { |
5d4e741062bd
may not finish search with 0-byte
Claus Gittinger <cg@exept.de>
parents:
3712
diff
changeset
|
885 |
if (*cp++ < ' ') { |
5d4e741062bd
may not finish search with 0-byte
Claus Gittinger <cg@exept.de>
parents:
3712
diff
changeset
|
886 |
RETURN ( __MKSMALLINT(cp - __stringVal(self)) ); |
5d4e741062bd
may not finish search with 0-byte
Claus Gittinger <cg@exept.de>
parents:
3712
diff
changeset
|
887 |
} |
5d4e741062bd
may not finish search with 0-byte
Claus Gittinger <cg@exept.de>
parents:
3712
diff
changeset
|
888 |
} |
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
889 |
RETURN ( __MKSMALLINT(0) ); |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
890 |
} |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
891 |
#endif |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
892 |
%}. |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
893 |
^ super indexOfControlCharacterStartingAt:start |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
894 |
|
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
895 |
" |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
896 |
'hello world' indexOfControlCharacterStartingAt:1 |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
897 |
'hello world\foo' withCRs indexOfControlCharacterStartingAt:1 |
3734
5d4e741062bd
may not finish search with 0-byte
Claus Gittinger <cg@exept.de>
parents:
3712
diff
changeset
|
898 |
'1\' withCRs indexOfControlCharacterStartingAt:1 |
5d4e741062bd
may not finish search with 0-byte
Claus Gittinger <cg@exept.de>
parents:
3712
diff
changeset
|
899 |
'1\' withCRs indexOfControlCharacterStartingAt:2 |
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
900 |
" |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
901 |
! |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3651
diff
changeset
|
902 |
|
3403 | 903 |
indexOfNonSeparatorStartingAt:start |
904 |
"return the index of the next non-whiteSpace character" |
|
905 |
||
906 |
%{ /* NOCONTEXT */ |
|
907 |
||
908 |
REGISTER unsigned char *cp; |
|
909 |
REGISTER unsigned char c; |
|
910 |
int len, index; |
|
911 |
OBJ cls; |
|
912 |
||
913 |
index = __intVal(start); |
|
914 |
if (index <= 0) { |
|
3451 | 915 |
index = 1; |
3403 | 916 |
} |
917 |
if ((cls = __qClass(self)) != String) |
|
3451 | 918 |
index += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
3403 | 919 |
len = __stringSize(self); |
920 |
if (index > len) { |
|
3451 | 921 |
RETURN ( __MKSMALLINT(0) ); |
3403 | 922 |
} |
923 |
cp = __stringVal(self) + index - 1; |
|
924 |
while (c = *cp++) { |
|
925 |
#ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
|
3451 | 926 |
if (c > ' ') |
3403 | 927 |
#endif |
3451 | 928 |
if ((c != ' ') && (c != '\t') && (c != '\n') |
929 |
&& (c != '\r') && (c != '\f')) { |
|
930 |
RETURN ( __MKSMALLINT(cp - __stringVal(self)) ); |
|
931 |
} |
|
3403 | 932 |
} |
933 |
%}. |
|
934 |
^ 0 |
|
935 |
||
936 |
" |
|
937 |
'hello world' indexOfNonWhiteSpaceStartingAt:3 |
|
938 |
'hello world' indexOfNonWhiteSpaceStartingAt:7 |
|
939 |
" |
|
940 |
! |
|
941 |
||
1 | 942 |
indexOfSeparatorStartingAt:start |
943 |
"return the index of the next separator character" |
|
944 |
||
945 |
%{ /* NOCONTEXT */ |
|
946 |
||
947 |
REGISTER unsigned char *cp; |
|
2858
866ffc52ac8a
oops - need unsigned character compares
Claus Gittinger <cg@exept.de>
parents:
2856
diff
changeset
|
948 |
REGISTER unsigned char c; |
1 | 949 |
int len, index; |
53 | 950 |
OBJ cls; |
1 | 951 |
|
1134 | 952 |
index = __intVal(start); |
1 | 953 |
if (index <= 0) { |
3111 | 954 |
index = 1; |
1 | 955 |
} |
329 | 956 |
if ((cls = __qClass(self)) != String) |
3111 | 957 |
index += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1134 | 958 |
len = __stringSize(self); |
1 | 959 |
if (index > len) { |
3111 | 960 |
RETURN ( __MKSMALLINT(0) ); |
1 | 961 |
} |
1134 | 962 |
cp = __stringVal(self) + index - 1; |
1 | 963 |
while (c = *cp++) { |
329 | 964 |
#ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
3111 | 965 |
if (c <= ' ') |
1 | 966 |
#endif |
3111 | 967 |
if ((c == ' ') || (c == '\t') || (c == '\n') |
968 |
|| (c == '\r') || (c == '\f')) { |
|
969 |
RETURN ( __MKSMALLINT(cp - __stringVal(self)) ); |
|
970 |
} |
|
1 | 971 |
} |
972 |
%} |
|
973 |
. |
|
974 |
^ 0 |
|
53 | 975 |
|
95 | 976 |
" |
977 |
'hello world' indexOfSeparatorStartingAt:3 |
|
978 |
'hello world' indexOfSeparatorStartingAt:7 |
|
979 |
" |
|
608 | 980 |
! |
981 |
||
982 |
occurrencesOf:aCharacter |
|
983 |
"count the occurrences of the argument, aCharacter in myself |
|
984 |
- reimplemented here for speed" |
|
985 |
||
986 |
%{ /* NOCONTEXT */ |
|
987 |
||
988 |
REGISTER unsigned char *cp; |
|
2858
866ffc52ac8a
oops - need unsigned character compares
Claus Gittinger <cg@exept.de>
parents:
2856
diff
changeset
|
989 |
REGISTER unsigned byteValue; |
608 | 990 |
REGISTER int count; |
991 |
OBJ cls; |
|
992 |
||
993 |
if (__isCharacter(aCharacter)) { |
|
3111 | 994 |
count = 0; |
995 |
byteValue = __intVal(_characterVal(aCharacter)); |
|
996 |
cp = __stringVal(self); |
|
997 |
if ((cls = __qClass(self)) != String) |
|
998 |
cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
999 |
while (*cp) { |
|
1000 |
if (*cp++ == byteValue) count++; |
|
1001 |
} |
|
1002 |
RETURN ( __MKSMALLINT(count) ); |
|
608 | 1003 |
} |
1004 |
%}. |
|
1950
2378ed1ae467
must fall back to super searchers, when searching
Claus Gittinger <cg@exept.de>
parents:
1804
diff
changeset
|
1005 |
^ super occurrencesOf:aCharacter |
608 | 1006 |
|
1007 |
" |
|
1008 |
'hello world' occurrencesOf:$a |
|
1009 |
'hello world' occurrencesOf:$w |
|
1010 |
'hello world' occurrencesOf:$l |
|
1011 |
'hello world' occurrencesOf:$x |
|
1012 |
'hello world' occurrencesOf:1 |
|
1013 |
" |
|
53 | 1014 |
! ! |
1015 |
||
608 | 1016 |
!String methodsFor:'comparing'! |
2 | 1017 |
|
608 | 1018 |
= aString |
1019 |
"Compare the receiver with the argument and return true if the |
|
1020 |
receiver is equal to the argument. Otherwise return false. |
|
1021 |
This compare is case-sensitive (i.e. 'Foo' is NOT = 'foo'). |
|
1022 |
Use sameAs: to compare with case ignored." |
|
1 | 1023 |
|
375 | 1024 |
%{ /* NOCONTEXT */ |
1025 |
||
608 | 1026 |
int l1, l2; |
1027 |
REGISTER OBJ s = aString; |
|
3314
94046261fba0
tuned #~= , #= and #startsWith:
Claus Gittinger <cg@exept.de>
parents:
3281
diff
changeset
|
1028 |
unsigned char *cp1, *cp2; |
608 | 1029 |
OBJ cls; |
3111 | 1030 |
OBJ myCls; |
3335 | 1031 |
INT addrDelta; |
608 | 1032 |
|
1033 |
if (s == self) { |
|
3989 | 1034 |
RETURN ( true ); |
608 | 1035 |
} |
1036 |
if (! __isNonNilObject(s)) { |
|
3989 | 1037 |
RETURN ( false ); |
608 | 1038 |
} |
1 | 1039 |
|
3111 | 1040 |
cls = __qClass(s); |
1041 |
myCls = __qClass(self); |
|
608 | 1042 |
|
3111 | 1043 |
if ((cls == String) || (cls == Symbol) || (cls == myCls)) { |
3989 | 1044 |
cp2 = __stringVal(s); |
1045 |
l2 = __stringSize(s); |
|
1046 |
/* |
|
1047 |
* care for instances of subclasses ... |
|
1048 |
*/ |
|
1049 |
if (cls != String) { |
|
1050 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1051 |
||
1052 |
cp2 += n; |
|
1053 |
l2 -= n; |
|
1054 |
} |
|
1055 |
||
1056 |
cp1 = __stringVal(self); |
|
1057 |
l1 = __stringSize(self); |
|
1058 |
/* |
|
1059 |
* care for instances of subclasses ... |
|
1060 |
*/ |
|
1061 |
if (myCls != String) { |
|
1062 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(myCls)->c_ninstvars)); |
|
1063 |
||
1064 |
cp1 += n; |
|
1065 |
l1 -= n; |
|
1066 |
} |
|
1067 |
||
1068 |
if (l1 != l2) { |
|
1069 |
RETURN ( false ); |
|
1070 |
} |
|
3314
94046261fba0
tuned #~= , #= and #startsWith:
Claus Gittinger <cg@exept.de>
parents:
3281
diff
changeset
|
1071 |
#undef FAST_STRNCMP |
3111 | 1072 |
#ifdef FAST_STRNCMP |
3989 | 1073 |
RETURN ( (strncmp(cp1, cp2, l1) == 0) ? true : false ); |
3111 | 1074 |
#else |
3989 | 1075 |
addrDelta = cp2 - cp1; |
1076 |
# ifdef UNROLL_LOOPS |
|
1077 |
while (l1 >= (sizeof(unsigned INT)*4)) { |
|
1078 |
if (((unsigned INT *)cp1)[0] != ((unsigned INT *)(cp1+addrDelta))[0]) { |
|
1079 |
RETURN (false); |
|
1080 |
} |
|
1081 |
if (((unsigned INT *)cp1)[1] != ((unsigned INT *)(cp1+addrDelta))[1]) { |
|
1082 |
RETURN (false); |
|
1083 |
} |
|
1084 |
if (((unsigned INT *)cp1)[2] != ((unsigned INT *)(cp1+addrDelta))[2]) { |
|
1085 |
RETURN (false); |
|
1086 |
} |
|
1087 |
if (((unsigned INT *)cp1)[3] != ((unsigned INT *)(cp1+addrDelta))[3]) { |
|
1088 |
RETURN (false); |
|
1089 |
} |
|
1090 |
l1 -= (sizeof(unsigned INT) * 4); |
|
1091 |
cp1 += (sizeof(unsigned INT) * 4); |
|
1092 |
} |
|
1093 |
# endif /* UNROLL_LOOPS */ |
|
1094 |
while (l1 >= sizeof(unsigned INT)) { |
|
1095 |
if (*((unsigned INT *)cp1) != *((unsigned INT *)(cp1+addrDelta))) { |
|
1096 |
RETURN (false); |
|
1097 |
} |
|
1098 |
l1 -= sizeof(unsigned INT); |
|
1099 |
cp1 += sizeof(unsigned INT); |
|
1100 |
} |
|
1101 |
if (l1 >= sizeof(unsigned short)) { |
|
1102 |
if (*((unsigned short *)cp1) != *((unsigned short *)(cp1+addrDelta))) { |
|
1103 |
RETURN (false); |
|
1104 |
} |
|
1105 |
l1 -= sizeof(unsigned short); |
|
1106 |
cp1 += sizeof(unsigned short); |
|
1107 |
} |
|
1108 |
while (l1) { |
|
1109 |
if (*cp1 != *(cp1+addrDelta)) { |
|
1110 |
RETURN (false); |
|
1111 |
} |
|
1112 |
l1--; |
|
1113 |
cp1++; |
|
1114 |
} |
|
1115 |
||
1116 |
RETURN (true); |
|
3111 | 1117 |
#endif |
1 | 1118 |
} |
3111 | 1119 |
%}. |
608 | 1120 |
^ super = aString |
1 | 1121 |
|
95 | 1122 |
" |
608 | 1123 |
'foo' = 'Foo' |
1124 |
'foo' sameAs: 'Foo' |
|
95 | 1125 |
" |
1 | 1126 |
! |
1127 |
||
608 | 1128 |
> aString |
1129 |
"Compare the receiver with the argument and return true if the |
|
1130 |
receiver is greater than the argument. Otherwise return false. |
|
1131 |
No national variants are honored; use after: for this. |
|
1132 |
In contrast to ST-80, case differences are NOT ignored, thus |
|
1133 |
'foo' > 'Foo' will return true. |
|
1134 |
This may change." |
|
1 | 1135 |
|
375 | 1136 |
%{ /* NOCONTEXT */ |
1137 |
||
608 | 1138 |
int len1, len2, cmp; |
1139 |
REGISTER OBJ s = aString; |
|
2858
866ffc52ac8a
oops - need unsigned character compares
Claus Gittinger <cg@exept.de>
parents:
2856
diff
changeset
|
1140 |
unsigned char *cp1, *cp2; |
608 | 1141 |
OBJ cls; |
3111 | 1142 |
OBJ myCls; |
1 | 1143 |
|
3111 | 1144 |
if (__isNonNilObject(s)) { |
1145 |
cls = __qClass(s); |
|
1146 |
myCls = __qClass(self); |
|
608 | 1147 |
|
3111 | 1148 |
if ((cls == String) || (cls == Symbol) || (cls == myCls)) { |
1149 |
cp2 = __stringVal(s); |
|
1150 |
len2 = __stringSize(s); |
|
1151 |
/* |
|
1152 |
* care for instances of subclasses ... |
|
1153 |
*/ |
|
1154 |
if (cls != String) { |
|
1155 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
608 | 1156 |
|
3111 | 1157 |
cp2 += n; |
1158 |
len2 -= n; |
|
1159 |
} |
|
375 | 1160 |
|
3111 | 1161 |
cp1 = __stringVal(self); |
1162 |
len1 = __stringSize(self); |
|
1163 |
/* |
|
1164 |
* care for instances of subclasses ... |
|
1165 |
*/ |
|
1166 |
if (myCls != String) { |
|
1167 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(myCls)->c_ninstvars)); |
|
608 | 1168 |
|
3111 | 1169 |
cp1 += n; |
1170 |
len1 -= n; |
|
1171 |
} |
|
608 | 1172 |
|
3111 | 1173 |
if (len1 <= len2) |
1174 |
cmp = strncmp(cp1, cp2, len1); |
|
1175 |
else |
|
1176 |
cmp = strncmp(cp1, cp2, len2); |
|
608 | 1177 |
|
3111 | 1178 |
if (cmp > 0) { |
1179 |
RETURN ( true ); |
|
1180 |
} |
|
1181 |
if ((cmp == 0) && (len1 > len2)) { |
|
1182 |
RETURN ( true ); |
|
1183 |
} |
|
1184 |
RETURN ( false ); |
|
1185 |
} |
|
1 | 1186 |
} |
370 | 1187 |
%}. |
608 | 1188 |
^ super > aString |
1189 |
! |
|
1 | 1190 |
|
608 | 1191 |
after:aString |
1192 |
"Compare the receiver with the argument and return true if the |
|
1193 |
receiver should come after the argument in a sorted list. |
|
1194 |
Otherwise return false. |
|
1195 |
The comparison is language specific, depending on the value of |
|
1368 | 1196 |
LC_COLLATE, which is initialized from the environment. |
1197 |
||
1198 |
STUPID: |
|
1486 | 1199 |
#after has a completely different meaning in SeqColl ..." |
1 | 1200 |
|
1251 | 1201 |
^ (self compareCollatingWith:aString) > 0 |
1202 |
||
1203 |
" |
|
1204 |
'max' > 'mäx' |
|
1205 |
'man' > 'mäx' |
|
1206 |
'max' after:'mäx' |
|
1207 |
'man' after:'mäx' |
|
1208 |
" |
|
1209 |
||
1368 | 1210 |
"Modified: 10.5.1996 / 14:02:45 / cg" |
1251 | 1211 |
! |
1212 |
||
1213 |
compareCollatingWith:aString |
|
1214 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
1215 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
1216 |
The comparison is language specific, depending on the value of |
|
1217 |
LC_COLLATE, which is in the shell environment." |
|
1218 |
||
1 | 1219 |
%{ /* NOCONTEXT */ |
1220 |
||
608 | 1221 |
int cmp; |
1222 |
REGISTER OBJ s = aString; |
|
2858
866ffc52ac8a
oops - need unsigned character compares
Claus Gittinger <cg@exept.de>
parents:
2856
diff
changeset
|
1223 |
unsigned char *cp1, *cp2; |
53 | 1224 |
OBJ cls; |
3111 | 1225 |
OBJ myCls; |
608 | 1226 |
|
3111 | 1227 |
if (__isNonNilObject(s)) { |
1228 |
cls = __qClass(s); |
|
1229 |
myCls = __qClass(self); |
|
1230 |
||
1231 |
if ((cls == String) || (cls == Symbol) || (cls == myCls)) { |
|
1232 |
cp1 = __stringVal(self); |
|
608 | 1233 |
|
3111 | 1234 |
/* |
1235 |
* care for instances of subclasses ... |
|
1236 |
*/ |
|
1237 |
if (myCls != String) { |
|
1238 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(myCls)->c_ninstvars)); |
|
1239 |
||
1240 |
cp1 += n; |
|
1241 |
} |
|
1 | 1242 |
|
3111 | 1243 |
cp2 = __stringVal(s); |
1244 |
/* |
|
1245 |
* care for instances of subclasses ... |
|
1246 |
*/ |
|
1247 |
if (cls != String) { |
|
1248 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
608 | 1249 |
|
3111 | 1250 |
cp2 += n; |
1251 |
} |
|
608 | 1252 |
|
1253 |
#ifdef HAS_STRCOLL |
|
3111 | 1254 |
cmp = strcoll(cp1, cp2); |
608 | 1255 |
#else |
3111 | 1256 |
cmp = strcmp(cp1, cp2); |
608 | 1257 |
#endif |
1258 |
||
3111 | 1259 |
if (cmp > 0) { |
1260 |
RETURN ( __MKSMALLINT(1) ); |
|
1261 |
} |
|
1262 |
if (cmp < 0) { |
|
1263 |
RETURN ( __MKSMALLINT(-1) ); |
|
1264 |
} |
|
1265 |
RETURN ( __MKSMALLINT(0) ); |
|
1266 |
} |
|
370 | 1267 |
} |
329 | 1268 |
%}. |
608 | 1269 |
" |
1270 |
currently, this operation is only defined for strings, symbols |
|
1271 |
and subclasses. |
|
1272 |
" |
|
1251 | 1273 |
^ self primitiveFailed |
1 | 1274 |
! |
1275 |
||
608 | 1276 |
~= aString |
1277 |
"Compare the receiver with the argument and return true if the |
|
1278 |
receiver is not equal to the argument. Otherwise return false. |
|
3111 | 1279 |
This compare is case-sensitive (i.e. 'Foo' is NOT = 'foo'). |
1280 |
Actually, there is no need to redefine that method here, |
|
1281 |
the default (= not as inherited) works ok. |
|
1282 |
However, this may be heavily used and the redefinition saves an |
|
1283 |
extra message send." |
|
1 | 1284 |
|
608 | 1285 |
%{ /* NOCONTEXT */ |
1286 |
||
1 | 1287 |
int l1, l2; |
608 | 1288 |
REGISTER OBJ s = aString; |
3314
94046261fba0
tuned #~= , #= and #startsWith:
Claus Gittinger <cg@exept.de>
parents:
3281
diff
changeset
|
1289 |
unsigned char *cp1, *cp2; |
3111 | 1290 |
OBJ cls, myCls; |
3335 | 1291 |
INT addrDelta; |
1 | 1292 |
|
608 | 1293 |
if (s == self) { |
3989 | 1294 |
RETURN ( false ); |
608 | 1295 |
} |
1296 |
if (! __isNonNilObject(s)) { |
|
3989 | 1297 |
RETURN ( true ); |
608 | 1298 |
} |
3111 | 1299 |
|
1300 |
cls = __qClass(s); |
|
1301 |
myCls = __qClass(self); |
|
1302 |
||
1303 |
if ((cls == String) || (cls == Symbol) || (cls == myCls)) { |
|
3989 | 1304 |
cp1 = __stringVal(self); |
1305 |
l1 = __stringSize(self); |
|
1306 |
/* |
|
1307 |
* care for instances of subclasses ... |
|
1308 |
*/ |
|
1309 |
if (myCls != String) { |
|
1310 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(myCls)->c_ninstvars)); |
|
1311 |
||
1312 |
cp1 += n; |
|
1313 |
l1 -= n; |
|
1314 |
} |
|
1315 |
||
1316 |
cp2 = __stringVal(s); |
|
1317 |
l2 = __stringSize(s); |
|
1318 |
/* |
|
1319 |
* care for instances of subclasses ... |
|
1320 |
*/ |
|
1321 |
if (cls != String) { |
|
1322 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1323 |
||
1324 |
cp2 += n; |
|
1325 |
l2 -= n; |
|
1326 |
} |
|
1327 |
||
1328 |
if (l1 != l2) { |
|
1329 |
RETURN ( true ); |
|
1330 |
} |
|
3314
94046261fba0
tuned #~= , #= and #startsWith:
Claus Gittinger <cg@exept.de>
parents:
3281
diff
changeset
|
1331 |
|
94046261fba0
tuned #~= , #= and #startsWith:
Claus Gittinger <cg@exept.de>
parents:
3281
diff
changeset
|
1332 |
#undef FAST_STRNCMP |
3111 | 1333 |
#ifdef FAST_STRNCMP |
3989 | 1334 |
RETURN ( (strncmp(cp1, cp2, l1) == 0) ? false : true ); |
3111 | 1335 |
#else |
3989 | 1336 |
addrDelta = cp2 - cp1; |
1337 |
# ifdef UNROLL_LOOPS |
|
1338 |
while (l1 >= (sizeof(unsigned INT)*4)) { |
|
1339 |
if (((unsigned INT *)cp1)[0] != ((unsigned INT *)(cp1+addrDelta))[0]) { |
|
1340 |
RETURN (true); |
|
1341 |
} |
|
1342 |
if (((unsigned INT *)cp1)[1] != ((unsigned INT *)(cp1+addrDelta))[1]) { |
|
1343 |
RETURN (true); |
|
1344 |
} |
|
1345 |
if (((unsigned INT *)cp1)[2] != ((unsigned INT *)(cp1+addrDelta))[2]) { |
|
1346 |
RETURN (true); |
|
1347 |
} |
|
1348 |
if (((unsigned INT *)cp1)[3] != ((unsigned INT *)(cp1+addrDelta))[3]) { |
|
1349 |
RETURN (true); |
|
1350 |
} |
|
1351 |
l1 -= (sizeof(unsigned INT) * 4); |
|
1352 |
cp1 += (sizeof(unsigned INT) * 4); |
|
1353 |
} |
|
1354 |
# endif /* UNROLL_LOOPS */ |
|
1355 |
while (l1 >= sizeof(unsigned INT)) { |
|
1356 |
if (*((unsigned INT *)cp1) != *((unsigned INT *)(cp1+addrDelta))) { |
|
1357 |
RETURN (true); |
|
1358 |
} |
|
1359 |
l1 -= sizeof(unsigned INT); |
|
1360 |
cp1 += sizeof(unsigned INT); |
|
1361 |
} |
|
1362 |
if (l1 >= sizeof(unsigned short)) { |
|
1363 |
if (*((unsigned short *)cp1) != *((unsigned short *)(cp1+addrDelta))) { |
|
1364 |
RETURN (true); |
|
1365 |
} |
|
1366 |
l1 -= sizeof(unsigned short); |
|
1367 |
cp1 += sizeof(unsigned short); |
|
1368 |
} |
|
1369 |
while (l1) { |
|
1370 |
if (*cp1 != *(cp1+addrDelta)) { |
|
1371 |
RETURN (true); |
|
1372 |
} |
|
1373 |
l1--; |
|
1374 |
cp1++; |
|
1375 |
} |
|
1376 |
RETURN (false); |
|
3111 | 1377 |
#endif |
608 | 1378 |
} |
3111 | 1379 |
%}. |
608 | 1380 |
^ super ~= aString |
1381 |
! ! |
|
1382 |
||
1383 |
!String methodsFor:'converting'! |
|
1384 |
||
3281
d108dee000f7
#asBoldText added for often used bold texts in dialogs, etc.
tz
parents:
3234
diff
changeset
|
1385 |
asBoldText |
d108dee000f7
#asBoldText added for often used bold texts in dialogs, etc.
tz
parents:
3234
diff
changeset
|
1386 |
"return self as a bold text" |
d108dee000f7
#asBoldText added for often used bold texts in dialogs, etc.
tz
parents:
3234
diff
changeset
|
1387 |
|
d108dee000f7
#asBoldText added for often used bold texts in dialogs, etc.
tz
parents:
3234
diff
changeset
|
1388 |
^Text string: self emphasis: #bold |
d108dee000f7
#asBoldText added for often used bold texts in dialogs, etc.
tz
parents:
3234
diff
changeset
|
1389 |
! |
d108dee000f7
#asBoldText added for often used bold texts in dialogs, etc.
tz
parents:
3234
diff
changeset
|
1390 |
|
2856 | 1391 |
asString |
1392 |
"I am a string" |
|
1393 |
||
1394 |
^ self |
|
1395 |
||
1396 |
"Created: 14.8.1997 / 11:37:03 / stefan" |
|
1397 |
! |
|
1398 |
||
608 | 1399 |
asSymbol |
3710
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1400 |
"return a unique symbol with the name taken from my characters." |
608 | 1401 |
%{ |
1402 |
OBJ newSymbol; |
|
3710
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1403 |
OBJ cls; |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1404 |
int indx; |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1405 |
|
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1406 |
cls = __qClass(self); |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1407 |
if (cls != String) { |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1408 |
indx = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1409 |
} else { |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1410 |
indx = 0; |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1411 |
} |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1412 |
newSymbol = __MKSYMBOL(__stringVal(self) + indx, (OBJ *)0); |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1413 |
if (newSymbol) { |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1414 |
RETURN ( newSymbol); |
608 | 1415 |
} |
1416 |
%}. |
|
1417 |
^ ObjectMemory allocationFailureSignal raise. |
|
3710
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1418 |
|
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1419 |
" |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1420 |
'hello' asSymbol |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1421 |
" |
608 | 1422 |
! |
1423 |
||
1424 |
asSymbolIfInterned |
|
1425 |
"if a symbol with the receivers characters is already known, return it. |
|
1426 |
Otherwise, return nil. This can be used to query for an existing |
|
1427 |
symbol and is the same as |
|
3710
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1428 |
self knownAsSymbol ifTrue:[self asSymbol] ifFalse:[nil] |
608 | 1429 |
but slightly faster, since the symbol lookup operation is only |
3710
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1430 |
performed once." |
1 | 1431 |
|
608 | 1432 |
%{ /* NOCONTEXT */ |
3710
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1433 |
OBJ cls; |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1434 |
int indx; |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1435 |
|
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1436 |
cls = __qClass(self); |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1437 |
if (cls != String) { |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1438 |
indx = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1439 |
} else { |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1440 |
indx = 0; |
608 | 1441 |
} |
3710
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1442 |
RETURN ( __SYMBOL_OR_NIL(__stringVal(self) + indx)); |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1443 |
%} |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1444 |
|
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1445 |
" |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1446 |
'hello' asSymbolIfInterned |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1447 |
'fooBarBaz' asSymbolIfInterned |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
1448 |
" |
608 | 1449 |
! |
1450 |
||
2416 | 1451 |
decodeAsLiteralArray |
1452 |
"given a literalEncoding in the receiver, |
|
1453 |
create & return the corresponding object. |
|
1454 |
The inverse operation to #literalArrayEncoding." |
|
1455 |
||
1456 |
^ self |
|
1457 |
||
1458 |
"Created: 25.2.1997 / 19:18:19 / cg" |
|
1459 |
! |
|
1460 |
||
608 | 1461 |
literalArrayEncoding |
1246 | 1462 |
"encode myself as an array literal, from which a copy of the receiver |
1463 |
can be reconstructed with #decodeAsLiteralArray." |
|
608 | 1464 |
|
1465 |
^ self |
|
1466 |
||
1467 |
"Modified: 1.9.1995 / 02:25:45 / claus" |
|
1246 | 1468 |
"Modified: 22.4.1996 / 13:00:50 / cg" |
608 | 1469 |
! |
1470 |
||
1471 |
withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1472 |
"return a string with the characters of the receiver where all tabulator characters |
608 | 1473 |
are expanded into spaces (assuming 8-col tabs). |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1474 |
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
|
1475 |
otherwise a new string is returned. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1476 |
This does handle multiline strings. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1477 |
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
|
1478 |
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
|
1479 |
|
993e6ffdbf51
removed external decls for VM functions (should look for more)
Claus Gittinger <cg@exept.de>
parents:
1134
diff
changeset
|
1480 |
%{ /* STACK:700 */ |
2894 | 1481 |
unsigned char buffer[80*8 + 10]; |
1482 |
unsigned char *srcP, *dstP, *cp0; |
|
608 | 1483 |
int idx, sz; |
1484 |
int any = 0; |
|
1485 |
OBJ newString; |
|
1486 |
char c; |
|
1487 |
||
1488 |
if (__qClass(self) == String) { |
|
1489 |
/* |
|
1490 |
* 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
|
1491 |
* the buffer is large enough to even convert a |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1492 |
* receiver consisting fully of tabs. |
608 | 1493 |
*/ |
1494 |
if (__stringSize(self) < 80) { |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1495 |
idx = 1; |
608 | 1496 |
for (srcP = __stringVal(self), dstP = buffer; (c = *srcP); srcP++) { |
1497 |
if (c == '\t') { |
|
1498 |
any = 1; |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1499 |
while (idx % 8) { |
608 | 1500 |
idx++; |
1501 |
*dstP++ = ' '; |
|
159 | 1502 |
} |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1503 |
idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1504 |
*dstP++ = ' '; |
608 | 1505 |
} else { |
1506 |
*dstP++ = c; |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1507 |
idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1508 |
if (c == '\n') { |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1509 |
idx = 1; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1510 |
} |
608 | 1511 |
} |
1512 |
} |
|
1513 |
if (! any) RETURN(self); |
|
1514 |
*dstP = '\0'; |
|
1515 |
RETURN (__MKSTRING_L(buffer, (dstP-buffer))); |
|
1516 |
} |
|
1517 |
/* |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1518 |
* 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
|
1519 |
* and expand it. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1520 |
* |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1521 |
* first, scan for size ... |
608 | 1522 |
*/ |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1523 |
idx = 1; |
608 | 1524 |
for (srcP = __stringVal(self), sz = 0; (c = *srcP); srcP++) { |
1525 |
if (c == '\t') { |
|
1526 |
any = 1; |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1527 |
while (idx % 8) { |
608 | 1528 |
idx++; |
1529 |
sz++; |
|
1530 |
} |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1531 |
idx++; sz ++; |
608 | 1532 |
} else { |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1533 |
sz++; idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1534 |
if (c == '\n') { |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1535 |
idx = 1; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1536 |
} |
159 | 1537 |
} |
1538 |
} |
|
608 | 1539 |
if (! any) RETURN(self); |
1 | 1540 |
|
608 | 1541 |
/* |
1542 |
* get the string |
|
1543 |
*/ |
|
1544 |
sz = OHDR_SIZE + sz + 1; |
|
3111 | 1545 |
__qNew(newString, sz); /* OBJECT ALLOCATION */ |
608 | 1546 |
if (newString != nil) { |
1134 | 1547 |
__InstPtr(newString)->o_class = String; |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1548 |
|
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1549 |
/* |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1550 |
* expand |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1551 |
*/ |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1552 |
idx = 1; |
608 | 1553 |
for (srcP = __stringVal(self), dstP = cp0 = __stringVal(newString); (c = *srcP); srcP++) { |
1554 |
if (c == '\t') { |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1555 |
while (idx % 8) { |
608 | 1556 |
idx++; |
1557 |
*dstP++ = ' '; |
|
1558 |
} |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1559 |
idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1560 |
*dstP++ = ' '; |
608 | 1561 |
} else { |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1562 |
*dstP++ = c; idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1563 |
if (c == '\n') { |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1564 |
idx = 1; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1565 |
} |
608 | 1566 |
} |
1567 |
} |
|
1568 |
*dstP++ = '\0'; |
|
1569 |
RETURN (newString); |
|
1570 |
} |
|
1571 |
} |
|
1572 |
%}. |
|
1573 |
^ super withTabsExpanded |
|
1 | 1574 |
! ! |
1575 |
||
1576 |
!String methodsFor:'copying'! |
|
1577 |
||
1578 |
, aString |
|
2 | 1579 |
"return the concatenation of myself and the argument, aString as |
1580 |
a String. |
|
1 | 1581 |
- reimplemented here for speed" |
1582 |
||
1583 |
%{ |
|
95 | 1584 |
int l1, l2, sz; |
1 | 1585 |
REGISTER OBJ s = aString; |
329 | 1586 |
REGISTER OBJ _string = String; |
13 | 1587 |
OBJ myClass, argClass, newString; |
1 | 1588 |
|
329 | 1589 |
if (__isNonNilObject(s)) { |
3651
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1590 |
myClass = __qClass(self); |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1591 |
argClass = __qClass(s); |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1592 |
/* |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1593 |
* can do it here if both are Strings/Symbols: |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1594 |
*/ |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1595 |
if (((myClass == _string) || (myClass == Symbol)) |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1596 |
&& ((argClass == _string) || (argClass == Symbol))) { |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1597 |
l1 = __stringSize(self); |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1598 |
l2 = __stringSize(s); |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1599 |
|
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1600 |
sz = OHDR_SIZE + l1 + l2 + 1; |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1601 |
__qNew(newString, sz); /* OBJECT ALLOCATION */ |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1602 |
if (newString != nil) { |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1603 |
char *cp1, *cp2; |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1604 |
REGISTER unsigned char *dstp; |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1605 |
|
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1606 |
__InstPtr(newString)->o_class = String; |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1607 |
dstp = __stringVal(newString); |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1608 |
cp1 = (char *) __stringVal(self); |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1609 |
cp2 = (char *) __stringVal(aString); |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1610 |
|
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1611 |
#ifdef bcopy4 |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1612 |
/* knowing that allocation is 4-byte aligned and |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1613 |
* size rounded up to next 4-byte, the first copy |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1614 |
* can be done word-wise. |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1615 |
* that speeds up size-10-string , size-10-string |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1616 |
* by 10% on a P5/200. |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1617 |
*/ |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1618 |
{ |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1619 |
int nw = l1 >> 2; |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1620 |
|
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1621 |
if (l1 & 3) nw++; |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1622 |
bcopy4(cp1, dstp, nw); |
3673 | 1623 |
dstp += l1; |
3651
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1624 |
} |
3673 | 1625 |
#else |
1626 |
# ifdef FAST_MEMCPY |
|
1627 |
bcopy(cp1, dstp, l1); |
|
1628 |
dstp += l1; |
|
1629 |
# else |
|
1630 |
# ifdef FAST_STRCPY |
|
1631 |
strncpy(dstp, cp1, l1); |
|
1632 |
dstp += l1; |
|
1633 |
# else |
|
3975 | 1634 |
while (l1 >= 4) { |
1635 |
*(int *)dstp = *(int *)cp1; |
|
1636 |
dstp += 4; cp1 += 4; |
|
1637 |
l1 -= 4; |
|
1638 |
} |
|
3673 | 1639 |
while (l1--) *dstp++ = *cp1++; |
1640 |
# endif |
|
1641 |
# endif |
|
3651
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1642 |
#endif |
2862 | 1643 |
|
3673 | 1644 |
#ifdef bcopy4 |
1645 |
if (((INT)dstp & 3) == 0) { |
|
1646 |
int nw = l2 >> 2; |
|
1647 |
||
1648 |
if (l2 & 3) nw++; |
|
1649 |
bcopy4(cp2, dstp, nw); |
|
1650 |
*(dstp + l2) = '\0'; |
|
1651 |
RETURN ( newString ); |
|
1652 |
} |
|
1653 |
#endif |
|
1654 |
||
1 | 1655 |
#ifdef FAST_MEMCPY |
3673 | 1656 |
bcopy(cp2, dstp, l2+1); |
3975 | 1657 |
dstp[l2] = '\0'; |
1 | 1658 |
#else |
1659 |
# ifdef FAST_STRCPY |
|
3673 | 1660 |
strncpy(dstp, cp2, l2); |
3975 | 1661 |
dstp[l2] = '\0'; |
1 | 1662 |
# else |
3651
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1663 |
while (l2--) *dstp++ = *cp2++; |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1664 |
*dstp = '\0'; |
1 | 1665 |
# endif |
1666 |
#endif |
|
3651
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1667 |
RETURN ( newString ); |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1668 |
} |
265d1c30eff5
tuned comma (concatenation) for i386.
Claus Gittinger <cg@exept.de>
parents:
3579
diff
changeset
|
1669 |
} |
1 | 1670 |
} |
519
0ab6612e060b
expand tabs slightly rewritten (no real change)
Claus Gittinger <cg@exept.de>
parents:
516
diff
changeset
|
1671 |
%}. |
1 | 1672 |
^ super , aString |
1673 |
! |
|
1674 |
||
1675 |
concatenate:string1 and:string2 |
|
1676 |
"return the concatenation of myself and the arguments, string1 and string2. |
|
1677 |
This is equivalent to self , string1 , string2 |
|
1678 |
- generated by compiler when such a construct is detected" |
|
1679 |
||
1680 |
|newString| |
|
2862 | 1681 |
|
1 | 1682 |
%{ |
95 | 1683 |
int len1, len2, len3, sz; |
1 | 1684 |
#if !defined(FAST_MEMCPY) && !defined(FAST_STRCPY) |
1685 |
REGISTER unsigned char *srcp; |
|
1686 |
#endif |
|
1687 |
REGISTER unsigned char *dstp; |
|
1688 |
||
53 | 1689 |
if ((__isString(self) || __isSymbol(self)) |
1690 |
&& (__isString(string1) || __isSymbol(string1)) |
|
1691 |
&& (__isString(string2) || __isSymbol(string2))) { |
|
1134 | 1692 |
len1 = __stringSize(self); |
1693 |
len2 = __stringSize(string1); |
|
1694 |
len3 = __stringSize(string2); |
|
95 | 1695 |
sz = OHDR_SIZE + len1 + len2 + len3 + 1; |
3111 | 1696 |
__qNew(newString, sz); /* OBJECT ALLOCATION */ |
159 | 1697 |
if (newString != nil) { |
1134 | 1698 |
__InstPtr(newString)->o_class = String; |
1699 |
dstp = __stringVal(newString); |
|
1 | 1700 |
#ifdef FAST_MEMCPY |
1134 | 1701 |
bcopy(__stringVal(self), dstp, len1); |
1702 |
bcopy(__stringVal(string1), dstp + len1, len2); |
|
1703 |
bcopy(__stringVal(string2), dstp + len1 + len2, len3+1); |
|
3153
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1704 |
*(dstp + len1 + len2 + len3) = '\0'; |
1 | 1705 |
#else |
1706 |
# ifdef FAST_STRCPY |
|
3153
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1707 |
strncpy(dstp, __stringVal(self), len1); |
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1708 |
strncpy(dstp + len1, __stringVal(string1), len2); |
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1709 |
strncpy(dstp + len1 + len2, __stringVal(string2), len3); |
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1710 |
*(dstp + len1 + len2 + len3) = '\0'; |
1 | 1711 |
# else |
1134 | 1712 |
srcp = __stringVal(self); |
3153
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1713 |
while (len1--) *dstp++ = *srcp++; |
1134 | 1714 |
srcp = __stringVal(string1); |
3153
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1715 |
while (len2--) *dstp++ = *srcp++; |
1134 | 1716 |
srcp = __stringVal(string2); |
3153
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1717 |
while (len3--) *dstp++ = *srcp++; |
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1718 |
*dstp = '\0'; |
1 | 1719 |
# endif |
1720 |
#endif |
|
159 | 1721 |
RETURN ( newString ); |
1722 |
} |
|
1 | 1723 |
} |
519
0ab6612e060b
expand tabs slightly rewritten (no real change)
Claus Gittinger <cg@exept.de>
parents:
516
diff
changeset
|
1724 |
%}. |
1 | 1725 |
^ super , string1 , string2 |
1726 |
! |
|
1727 |
||
1728 |
concatenate:string1 and:string2 and:string3 |
|
1729 |
"return the concatenation of myself and the string arguments. |
|
1730 |
This is equivalent to self , string1 , string2 , string3 |
|
1731 |
- generated by compiler when such a construct is detected" |
|
1732 |
||
1733 |
|newString| |
|
2862 | 1734 |
|
1 | 1735 |
%{ |
95 | 1736 |
int len1, len2, len3, len4, sz; |
1 | 1737 |
#if !defined(FAST_MEMCPY) && !defined(FAST_STRCPY) |
1738 |
REGISTER unsigned char *srcp; |
|
1739 |
#endif |
|
1740 |
REGISTER unsigned char *dstp; |
|
1741 |
||
53 | 1742 |
if ((__isString(self) || __isSymbol(self)) |
1743 |
&& (__isString(string1) || __isSymbol(string1)) |
|
1744 |
&& (__isString(string2) || __isSymbol(string2)) |
|
1745 |
&& (__isString(string3) || __isSymbol(string3))) { |
|
1134 | 1746 |
len1 = __stringSize(self); |
1747 |
len2 = __stringSize(string1); |
|
1748 |
len3 = __stringSize(string2); |
|
1749 |
len4 = __stringSize(string3); |
|
95 | 1750 |
sz = OHDR_SIZE + len1 + len2 + len3 + len4 + 1; |
3111 | 1751 |
__qNew(newString, sz); /* OBJECT ALLOCATION */ |
159 | 1752 |
if (newString != nil) { |
1134 | 1753 |
__InstPtr(newString)->o_class = String; |
1754 |
dstp = __stringVal(newString); |
|
1 | 1755 |
#ifdef FAST_MEMCPY |
1134 | 1756 |
bcopy(__stringVal(self), dstp, len1); |
1757 |
bcopy(__stringVal(string1), dstp + len1, len2); |
|
1758 |
bcopy(__stringVal(string2), dstp + len1 + len2, len3); |
|
1759 |
bcopy(__stringVal(string3), dstp + len1 + len2 + len3, len4+1); |
|
3153
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1760 |
*(dstp + len1 + len2 + len3 + len4) = '\0'; |
1 | 1761 |
#else |
1762 |
# ifdef FAST_STRCPY |
|
3153
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1763 |
strncpy(dstp, __stringVal(self), len1); |
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1764 |
strncpy(dstp + len1, __stringVal(string1), len2); |
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1765 |
strncpy(dstp + len1 + len2, __stringVal(string2), len3); |
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1766 |
strncpy(dstp + len1 + len2 + len3, __stringVal(string3), len4); |
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1767 |
*(dstp + len1 + len2 + len3 + len4) = '\0'; |
1 | 1768 |
# else |
1134 | 1769 |
srcp = __stringVal(self); |
3153
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1770 |
while (len1--) *dstp++ = *srcp++; |
1134 | 1771 |
srcp = __stringVal(string1); |
3153
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1772 |
while (len2--) *dstp++ = *srcp++; |
1134 | 1773 |
srcp = __stringVal(string2); |
3153
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1774 |
while (len3--) *dstp++ = *srcp++; |
1134 | 1775 |
srcp = __stringVal(string3); |
3153
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1776 |
while (len4--) *dstp++ = *srcp++; |
d8eed4e5c60f
use strncpy when concatenating strings.
Claus Gittinger <cg@exept.de>
parents:
3111
diff
changeset
|
1777 |
*dstp = '\0'; |
1 | 1778 |
# endif |
1779 |
#endif |
|
159 | 1780 |
RETURN ( newString ); |
1781 |
} |
|
1 | 1782 |
} |
519
0ab6612e060b
expand tabs slightly rewritten (no real change)
Claus Gittinger <cg@exept.de>
parents:
516
diff
changeset
|
1783 |
%}. |
1 | 1784 |
^ super , string1 , string2 , string3 |
1785 |
! |
|
1786 |
||
608 | 1787 |
copyFrom:start |
1788 |
"return the substring from start, anInteger to the end. |
|
1789 |
This method will always return a string, even if the receiver |
|
1790 |
is a subclass-instance. This might change if there is a need. |
|
1791 |
- reimplemented here for speed" |
|
1 | 1792 |
|
13 | 1793 |
%{ /* NOCONTEXT */ |
1 | 1794 |
|
608 | 1795 |
#if !defined(FAST_MEMCPY) |
1796 |
REGISTER unsigned char *srcp; |
|
1797 |
#endif |
|
1 | 1798 |
REGISTER unsigned char *dstp; |
608 | 1799 |
REGISTER int count; |
1800 |
int len, index1, sz; |
|
1801 |
OBJ newString; |
|
2862 | 1802 |
OBJ myClass; |
608 | 1803 |
|
2862 | 1804 |
myClass = __qClass(self); |
1805 |
||
2894 | 1806 |
#ifndef NO_PRIM_STRING |
2862 | 1807 |
if (__isSmallInteger(start) |
1808 |
&& ((myClass==String) || (myClass==Symbol))) { |
|
1134 | 1809 |
len = __stringSize(self); |
1810 |
index1 = __intVal(start); |
|
608 | 1811 |
if (index1 > 0) { |
1812 |
if (index1 <= len) { |
|
1813 |
count = len - index1 + 1; |
|
2862 | 1814 |
sz = OHDR_SIZE + count + 1; |
1815 |
||
834 | 1816 |
__PROTECT_CONTEXT__ |
3111 | 1817 |
__qNew(newString, sz); /* OBJECT ALLOCATION */ |
834 | 1818 |
__UNPROTECT_CONTEXT__ |
2862 | 1819 |
|
608 | 1820 |
if (newString != nil) { |
1134 | 1821 |
__InstPtr(newString)->o_class = String; |
1822 |
dstp = __stringVal(newString); |
|
1 | 1823 |
#ifdef FAST_MEMCPY |
1134 | 1824 |
bcopy(__stringVal(self) + index1 - 1, dstp, count); |
608 | 1825 |
dstp[count] = '\0'; |
1 | 1826 |
#else |
1827 |
# ifdef FAST_STRCPY |
|
1134 | 1828 |
strncpy(dstp, __stringVal(self) + index1 - 1, count); |
608 | 1829 |
dstp[count] = '\0'; |
1 | 1830 |
# else |
1134 | 1831 |
srcp = __stringVal(self) + index1 - 1; |
608 | 1832 |
while (count--) { |
1833 |
*dstp++ = *srcp++; |
|
1834 |
} |
|
1835 |
*dstp = '\0'; |
|
1 | 1836 |
# endif |
1837 |
#endif |
|
608 | 1838 |
RETURN ( newString ); |
1839 |
} |
|
1840 |
} |
|
159 | 1841 |
} |
13 | 1842 |
} |
2894 | 1843 |
#endif |
51 | 1844 |
%}. |
608 | 1845 |
"fall back in case of non-integer index or out-of-bound index; |
1846 |
will eventually lead to an out-of-bound signal raise" |
|
51 | 1847 |
|
608 | 1848 |
^ super copyFrom:start |
1 | 1849 |
! |
1850 |
||
1851 |
copyFrom:start to:stop |
|
1852 |
"return the substring starting at index start, anInteger and ending |
|
53 | 1853 |
at stop, anInteger. This method will always return a string, even |
1854 |
if the receiver is a subclass-instance. This might change if there is a need. |
|
1 | 1855 |
- reimplemented here for speed" |
1856 |
||
13 | 1857 |
%{ /* NOCONTEXT */ |
1858 |
||
1 | 1859 |
REGISTER unsigned char *srcp; |
1860 |
REGISTER unsigned char *dstp; |
|
1861 |
REGISTER int count; |
|
95 | 1862 |
int len, sz, index1, index2; |
13 | 1863 |
OBJ newString; |
2862 | 1864 |
OBJ myClass; |
1 | 1865 |
|
2862 | 1866 |
myClass = __qClass(self); |
1867 |
||
2894 | 1868 |
#ifndef NO_PRIM_STRING |
2862 | 1869 |
if (__bothSmallInteger(start, stop) |
1870 |
&& ((myClass==String) || (myClass==Symbol))) { |
|
1134 | 1871 |
len = __stringSize(self); |
1872 |
index1 = __intVal(start); |
|
1873 |
index2 = __intVal(stop); |
|
1 | 1874 |
|
159 | 1875 |
if ((index1 <= index2) && (index1 > 0)) { |
1876 |
if (index2 <= len) { |
|
1877 |
count = index2 - index1 + 1; |
|
1399 | 1878 |
sz = OHDR_SIZE + count + 1; |
2862 | 1879 |
|
834 | 1880 |
__PROTECT_CONTEXT__ |
3111 | 1881 |
__qNew(newString, sz); /* OBJECT ALLOCATION */ |
834 | 1882 |
__UNPROTECT_CONTEXT__ |
1399 | 1883 |
|
159 | 1884 |
if (newString != nil) { |
1134 | 1885 |
__InstPtr(newString)->o_class = String; |
1886 |
dstp = __stringVal(newString); |
|
2894 | 1887 |
srcp = __stringVal(self) + index1 - 1; |
1888 |
#ifdef bcopy4 |
|
3111 | 1889 |
{ |
1890 |
int nw = count >> 2; |
|
2894 | 1891 |
|
3111 | 1892 |
if (count & 3) { |
1893 |
nw++; |
|
1894 |
} |
|
1895 |
bcopy4(srcp, dstp, nw); |
|
1896 |
dstp[count] = '\0'; |
|
1897 |
} |
|
1 | 1898 |
#else |
2894 | 1899 |
# ifdef FAST_MEMCPY |
1900 |
bcopy(srcp, dstp, count); |
|
209 | 1901 |
dstp[count] = '\0'; |
1902 |
# else |
|
2894 | 1903 |
# ifdef FAST_STRCPY |
1904 |
strncpy(dstp, srcp, count); |
|
1905 |
dstp[count] = '\0'; |
|
1906 |
# else |
|
159 | 1907 |
while (count--) { |
1908 |
*dstp++ = *srcp++; |
|
1909 |
} |
|
1910 |
*dstp = '\0'; |
|
2894 | 1911 |
# endif |
209 | 1912 |
# endif |
13 | 1913 |
#endif |
159 | 1914 |
RETURN ( newString ); |
1915 |
} |
|
1916 |
} |
|
1917 |
} |
|
375 | 1918 |
/* |
1919 |
* allow empty copy |
|
1920 |
*/ |
|
1921 |
if (index1 > index2) { |
|
834 | 1922 |
__PROTECT_CONTEXT__ |
3111 | 1923 |
__qNew(newString, OHDR_SIZE+1); /* OBJECT ALLOCATION */ |
834 | 1924 |
__UNPROTECT_CONTEXT__ |
375 | 1925 |
if (newString != nil) { |
1134 | 1926 |
__InstPtr(newString)->o_class = String; |
2894 | 1927 |
(__stringVal(newString))[0] = '\0'; |
375 | 1928 |
RETURN ( newString ); |
1929 |
} |
|
1930 |
} |
|
1 | 1931 |
} |
2894 | 1932 |
#endif |
51 | 1933 |
%}. |
1934 |
"fall back in case of non-integer index or out-of-bound index; |
|
1935 |
will eventually lead to an out-of-bound signal raise" |
|
1936 |
||
1 | 1937 |
^ super copyFrom:start to:stop |
1938 |
! |
|
1939 |
||
608 | 1940 |
copyWith:aCharacter |
1941 |
"return a new string containing the receivers characters |
|
1942 |
and the single new character, aCharacter. |
|
1943 |
This is different from concatentation, which expects another string |
|
1944 |
as argument, but equivalent to copy-and-addLast. |
|
1945 |
Reimplemented here for more speed" |
|
1946 |
||
1947 |
%{ /* NOCONTEXT */ |
|
1948 |
||
2887 | 1949 |
int count; |
608 | 1950 |
int sz; |
1951 |
REGISTER unsigned char *dstp; |
|
1952 |
OBJ cls, newString; |
|
2862 | 1953 |
OBJ myClass; |
608 | 1954 |
|
2862 | 1955 |
myClass = __qClass(self); |
1956 |
||
2894 | 1957 |
#ifndef NO_PRIM_STRING |
2862 | 1958 |
if (__isCharacter(aCharacter) |
1959 |
&& ((myClass==String) || (myClass==Symbol))) { |
|
2887 | 1960 |
count = __stringSize(self); |
1961 |
sz = OHDR_SIZE + count + 1 + 1; |
|
608 | 1962 |
|
834 | 1963 |
__PROTECT_CONTEXT__ |
3111 | 1964 |
__qNew(newString, sz); /* OBJECT ALLOCATION */ |
834 | 1965 |
__UNPROTECT_CONTEXT__ |
2862 | 1966 |
|
608 | 1967 |
if (newString) { |
1134 | 1968 |
__InstPtr(newString)->o_class = String; |
1969 |
dstp = __stringVal(newString); |
|
2862 | 1970 |
|
2887 | 1971 |
#ifdef bcopy4 |
1972 |
{ |
|
1973 |
int nw = count >> 2; |
|
1974 |
char *srcp = (char *)__stringVal(self); |
|
1975 |
||
1976 |
if (count & 3) { |
|
1977 |
nw++; |
|
1978 |
} |
|
1979 |
bcopy4(srcp, dstp, nw); |
|
1980 |
dstp += count; |
|
1981 |
} |
|
608 | 1982 |
#else |
2887 | 1983 |
# ifdef FAST_MEMCPY |
1984 |
bcopy(__stringVal(self), dstp, count); |
|
1985 |
dstp += count; |
|
608 | 1986 |
# else |
2887 | 1987 |
# ifdef FAST_STRCPY |
1988 |
strncpy(dstp, __stringVal(self), count); |
|
1989 |
dstp += count; |
|
1990 |
# else |
|
608 | 1991 |
{ |
1992 |
REGISTER unsigned char *srcp; |
|
1993 |
||
2862 | 1994 |
srcp = __stringVal(self); |
608 | 1995 |
while ((*dstp = *srcp++) != '\0') |
1996 |
dstp++; |
|
1997 |
} |
|
2887 | 1998 |
# endif |
608 | 1999 |
# endif |
2887 | 2000 |
# endif |
1134 | 2001 |
*dstp++ = __intVal(_characterVal(aCharacter)); |
608 | 2002 |
*dstp = '\0'; |
2003 |
RETURN (newString ); |
|
2004 |
} |
|
2005 |
} |
|
2894 | 2006 |
#endif |
608 | 2007 |
%}. |
2008 |
"fall back in case of non-character arg; |
|
2009 |
will eventually lead to an bad element signal raise" |
|
2010 |
||
2011 |
^ super copyWith:aCharacter |
|
2012 |
! |
|
2013 |
||
2886 | 2014 |
deepCopy |
2015 |
"return a copy of the receiver" |
|
608 | 2016 |
|
2017 |
" |
|
2018 |
could be an instance of a subclass which needs deepCopy |
|
2019 |
of its named instvars ... |
|
2020 |
" |
|
2021 |
(self isMemberOf:String) ifTrue:[ |
|
2022 |
^ self copyFrom:1 |
|
2023 |
]. |
|
2024 |
^ super deepCopy |
|
2025 |
! |
|
2026 |
||
2886 | 2027 |
deepCopyUsing:aDictionary |
608 | 2028 |
"return a copy of the receiver - reimplemented to be a bit faster" |
2029 |
||
2030 |
" |
|
2031 |
could be an instance of a subclass which needs deepCopy |
|
2032 |
of its named instvars ... |
|
2033 |
" |
|
2034 |
(self isMemberOf:String) ifTrue:[ |
|
2035 |
^ self copyFrom:1 |
|
2036 |
]. |
|
2037 |
^ super deepCopy |
|
2038 |
! |
|
2039 |
||
2886 | 2040 |
shallowCopy |
2041 |
"return a copy of the receiver" |
|
608 | 2042 |
|
2043 |
(self isMemberOf:String) ifTrue:[ |
|
2044 |
^ self copyFrom:1 |
|
2045 |
]. |
|
2046 |
^ super shallowCopy |
|
2047 |
! |
|
2048 |
||
2886 | 2049 |
simpleDeepCopy |
2050 |
"return a copy of the receiver" |
|
608 | 2051 |
|
2052 |
" |
|
2053 |
could be an instance of a subclass which needs deepCopy |
|
2054 |
of its named instvars ... |
|
2055 |
" |
|
2056 |
(self isMemberOf:String) ifTrue:[ |
|
2057 |
^ self copyFrom:1 |
|
2058 |
]. |
|
2059 |
^ super deepCopy |
|
2060 |
! ! |
|
2061 |
||
2062 |
!String methodsFor:'filling and replacing'! |
|
2063 |
||
2886 | 2064 |
atAllPut:aCharacter |
608 | 2065 |
"replace all elements with aCharacter |
1 | 2066 |
- reimplemented here for speed" |
2067 |
||
13 | 2068 |
%{ /* NOCONTEXT */ |
2069 |
||
608 | 2070 |
#ifndef FAST_MEMSET |
2071 |
REGISTER unsigned char *dst; |
|
2072 |
REGISTER int l; |
|
1 | 2073 |
#endif |
608 | 2074 |
REGISTER int byteValue; |
1 | 2075 |
|
608 | 2076 |
if (__isCharacter(aCharacter) && __isString(self)) { |
3711
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2077 |
byteValue = __intVal(_characterVal(aCharacter)); |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2078 |
|
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2079 |
#ifdef FAST_MEMSET |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2080 |
memset(__stringVal(self), byteValue, __stringSize(self)); |
608 | 2081 |
#else |
3712 | 2082 |
{ |
3711
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2083 |
INT v; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2084 |
|
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2085 |
v = (byteValue << 8) | byteValue; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2086 |
v = (v << 16) | v; |
3712 | 2087 |
|
2088 |
dst = __stringVal(self); |
|
2089 |
l = __stringSize(self); |
|
2090 |
||
2091 |
# ifdef FAST_MEMSET4 /* sorry intel: your stosd instruction is slower ... */ |
|
2092 |
memset4(dst, v, l>>2); |
|
2093 |
l = l & 3; |
|
2094 |
# else |
|
2095 |
# ifdef UINT64 |
|
3711
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2096 |
v = (v << 32) | v; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2097 |
while (l >= 8) { |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2098 |
((UINT64 *)dst)[0] = v; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2099 |
dst += 8; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2100 |
l -= 8; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2101 |
} |
3712 | 2102 |
# else /* no UINT64 */ |
3711
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2103 |
while (l >= 16) { |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2104 |
((int *)dst)[0] = v; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2105 |
((int *)dst)[1] = v; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2106 |
((int *)dst)[2] = v; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2107 |
((int *)dst)[3] = v; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2108 |
dst += 16; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2109 |
l -= 16; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2110 |
} |
3712 | 2111 |
if (l >= 8) { |
2112 |
((int *)dst)[0] = v; |
|
2113 |
((int *)dst)[1] = v; |
|
2114 |
dst += 8; |
|
2115 |
l -= 8; |
|
2116 |
} |
|
3711
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2117 |
if (l >= 4) { |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2118 |
((int *)dst)[0] = v; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2119 |
dst += 4; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2120 |
l -= 4; |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2121 |
} |
3712 | 2122 |
# if 0 |
2123 |
if (l >= 2) { |
|
2124 |
((short *)dst)[0] = v; |
|
2125 |
dst += 2; |
|
2126 |
l -= 2; |
|
2127 |
} |
|
2128 |
# endif |
|
2129 |
||
2130 |
# endif |
|
3711
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2131 |
# endif |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2132 |
} |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2133 |
|
3712 | 2134 |
/* |
2135 |
* remaining bytes |
|
2136 |
*/ |
|
3711
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2137 |
while (l--) |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2138 |
*dst++ = byteValue; |
3712 | 2139 |
|
3711
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2140 |
#endif /* no FAST_MEMSET */ |
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2141 |
|
bdb2e9d06d33
20% speedup (p6/266) in #atAllPut:
Claus Gittinger <cg@exept.de>
parents:
3710
diff
changeset
|
2142 |
RETURN ( self ); |
608 | 2143 |
} |
2862 | 2144 |
%}. |
608 | 2145 |
^ super atAllPut:aCharacter |
1 | 2146 |
|
608 | 2147 |
" |
3712 | 2148 |
(String new:10) atAllPut:$* |
608 | 2149 |
String new:10 withAll:$* |
2150 |
" |
|
2151 |
! |
|
2152 |
||
2929 | 2153 |
replaceAll:oldCharacter with:newCharacter |
608 | 2154 |
"replace all oldCharacters by newCharacter in the receiver" |
2155 |
||
2156 |
%{ /* NOCONTEXT */ |
|
2157 |
||
2158 |
REGISTER unsigned char *srcp; |
|
2159 |
REGISTER unsigned oldVal, newVal; |
|
2867
015574e235fd
tuned replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2862
diff
changeset
|
2160 |
unsigned char c; |
608 | 2161 |
|
2162 |
if (__isCharacter(oldCharacter) |
|
2163 |
&& __isCharacter(newCharacter) |
|
2164 |
&& __isString(self)) { |
|
3111 | 2165 |
srcp = (unsigned char *)__stringVal(self); |
2166 |
oldVal = __intVal(_characterVal(oldCharacter)); |
|
2167 |
newVal = __intVal(_characterVal(newCharacter)); |
|
2168 |
while ((c = *srcp) != '\0') { |
|
2169 |
if (c == oldVal) |
|
2170 |
*srcp = newVal; |
|
2171 |
srcp++; |
|
2172 |
} |
|
2173 |
RETURN ( self ); |
|
1 | 2174 |
} |
2862 | 2175 |
%}. |
2929 | 2176 |
^ super replaceAll:oldCharacter with:newCharacter |
51 | 2177 |
|
608 | 2178 |
" |
2929 | 2179 |
'helloWorld' copy replaceAll:$o with:$O |
608 | 2180 |
" |
2181 |
! |
|
1 | 2182 |
|
2886 | 2183 |
replaceFrom:start to:stop with:aString startingAt:repStart |
1 | 2184 |
"replace the characters starting at index start, anInteger and ending |
2185 |
at stop, anInteger with characters from aString starting at repStart. |
|
3234 | 2186 |
Return the receiver. |
1 | 2187 |
|
2188 |
- reimplemented here for speed" |
|
2189 |
||
2190 |
%{ /* NOCONTEXT */ |
|
2191 |
||
2192 |
REGISTER unsigned char *srcp, *dstp; |
|
2193 |
REGISTER int count; |
|
2194 |
int len, index1, index2; |
|
2195 |
int repLen, repIndex; |
|
2196 |
||
2894 | 2197 |
#ifndef NO_PRIM_STRING |
53 | 2198 |
if ((__isString(aString) || __isSymbol(aString)) |
2199 |
&& __isString(self) |
|
252 | 2200 |
&& __bothSmallInteger(start, stop)) { |
3451 | 2201 |
len = __stringSize(self); |
2202 |
index1 = __intVal(start); |
|
2203 |
index2 = __intVal(stop); |
|
2204 |
count = index2 - index1 + 1; |
|
2205 |
if (count <= 0) { |
|
2206 |
RETURN (self); |
|
2207 |
} |
|
2208 |
if ((index2 <= len) && (index1 > 0)) { |
|
2209 |
repLen = __stringSize(aString); |
|
2210 |
repIndex = __intVal(repStart); |
|
2211 |
if ((repIndex > 0) && ((repIndex + count - 1) <= repLen)) { |
|
2212 |
srcp = __stringVal(aString) + repIndex - 1; |
|
2213 |
dstp = __stringVal(self) + index1 - 1; |
|
2214 |
if (aString == self) { |
|
2215 |
/* take care of overlapping copy */ |
|
2216 |
if (srcp < dstp) { |
|
2217 |
/* must do a reverse copy */ |
|
2218 |
srcp += count; |
|
2219 |
dstp += count; |
|
2220 |
while (count-- > 0) { |
|
2221 |
*--dstp = *--srcp; |
|
2222 |
} |
|
2223 |
RETURN (self); |
|
2224 |
} |
|
2225 |
} |
|
2867
015574e235fd
tuned replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2862
diff
changeset
|
2226 |
#ifdef bcopy4 |
3451 | 2227 |
/* copy quadbytes if pointers are aligned */ |
2228 |
/* |
|
2229 |
* no sizeof(int) here please - |
|
2230 |
* - bcopy4 (if defined) copies 4-bytes on ALL machines |
|
2231 |
*/ |
|
2232 |
if ((count > 12) |
|
2233 |
&& (((unsigned INT)srcp & 3) == 0) |
|
2234 |
&& (((unsigned INT)dstp & 3) == 0)) { |
|
2235 |
int n; |
|
2236 |
||
2237 |
n = count >> 2; /* make it quads */ |
|
2238 |
bcopy4(srcp, dstp, n); |
|
2239 |
n <<= 2; /* back to chars */ |
|
2240 |
dstp += n; |
|
2241 |
srcp += n; |
|
2242 |
count -= n; |
|
2243 |
} |
|
2244 |
while (count-- > 0) { |
|
2245 |
*dstp++ = *srcp++; |
|
2246 |
} |
|
1 | 2247 |
#else |
2867
015574e235fd
tuned replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2862
diff
changeset
|
2248 |
# ifdef FAST_MEMCPY |
3451 | 2249 |
bcopy(srcp, dstp, count); |
2867
015574e235fd
tuned replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2862
diff
changeset
|
2250 |
# else |
3451 | 2251 |
/* copy longs if pointers are aligned */ |
2252 |
if ((((unsigned INT)srcp & (sizeof(INT)-1)) == 0) |
|
2253 |
&& (((unsigned INT)dstp & (sizeof(INT)-1)) == 0)) { |
|
2254 |
while (count >= sizeof(INT)) { |
|
2255 |
*((unsigned INT *)dstp) = *((unsigned INT *)srcp); |
|
2256 |
dstp += sizeof(INT); |
|
2257 |
srcp += sizeof(INT); |
|
2258 |
count -= sizeof(INT); |
|
2259 |
} |
|
2260 |
} |
|
2261 |
while (count-- > 0) { |
|
2262 |
*dstp++ = *srcp++; |
|
2263 |
} |
|
2867
015574e235fd
tuned replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2862
diff
changeset
|
2264 |
# endif |
1 | 2265 |
#endif |
3451 | 2266 |
RETURN (self); |
2267 |
} |
|
2268 |
} |
|
1 | 2269 |
} |
2894 | 2270 |
#endif |
2862 | 2271 |
%}. |
1 | 2272 |
^ super replaceFrom:start to:stop with:aString startingAt:repStart |
2867
015574e235fd
tuned replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2862
diff
changeset
|
2273 |
|
1 | 2274 |
! |
2275 |
||
2886 | 2276 |
reverse |
2277 |
"in-place reverse the characters of the string." |
|
53 | 2278 |
|
2279 |
"Q: is there a need to redefine it here ?" |
|
1 | 2280 |
|
2281 |
%{ /* NOCONTEXT */ |
|
2282 |
||
2283 |
REGISTER char c; |
|
2284 |
REGISTER unsigned char *hip, *lowp; |
|
2285 |
||
53 | 2286 |
if (__isString(self)) { |
1134 | 2287 |
lowp = __stringVal(self); |
2288 |
hip = lowp + __stringSize(self) - 1; |
|
159 | 2289 |
while (lowp < hip) { |
2290 |
c = *lowp; |
|
2291 |
*lowp = *hip; |
|
2292 |
*hip = c; |
|
2293 |
lowp++; |
|
2294 |
hip--; |
|
2295 |
} |
|
2296 |
RETURN ( self ); |
|
1 | 2297 |
} |
2862 | 2298 |
%}. |
1 | 2299 |
^ super reverse |
2300 |
! |
|
2301 |
||
2302 |
withoutSeparators |
|
2862 | 2303 |
"return a string containing the chars of myself |
2304 |
without leading and trailing whitespace. |
|
2305 |
If there is no whitespace, the receiver is returned. |
|
95 | 2306 |
Notice, this is different from String>>withoutSpaces." |
1 | 2307 |
|
42 | 2308 |
|startIndex "{ Class: SmallInteger }" |
51 | 2309 |
endIndex "{ Class: SmallInteger }" |
2310 |
sz| |
|
2311 |
||
53 | 2312 |
startIndex := 0. |
2862 | 2313 |
|
51 | 2314 |
%{ |
2884 | 2315 |
REGISTER unsigned char *cp; |
2316 |
REGISTER unsigned char *ep; |
|
2317 |
REGISTER unsigned char c; |
|
51 | 2318 |
REGISTER unsigned char *cp0; |
2884 | 2319 |
REGISTER unsigned char *ep0; |
1 | 2320 |
|
51 | 2321 |
/* ignore instances of subclasses ... */ |
329 | 2322 |
if (__qClass(self) == String) { |
3111 | 2323 |
cp = cp0 = __stringVal(self); |
2862 | 2324 |
|
3111 | 2325 |
/* |
2326 |
* find first non-whiteSpace from beginning |
|
2327 |
*/ |
|
2862 | 2328 |
#ifndef NON_ASCII |
2954 | 2329 |
# ifdef UINT64 |
3111 | 2330 |
while (*((UINT64 *)cp) == 0x2020202020202020L) { |
2331 |
cp += 8; |
|
2332 |
} |
|
2862 | 2333 |
# endif |
3111 | 2334 |
while (*((unsigned *)cp) == 0x20202020) { |
2335 |
cp += 4; |
|
2336 |
} |
|
2862 | 2337 |
#endif |
3111 | 2338 |
while ((c = *cp) |
329 | 2339 |
#ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
3111 | 2340 |
&& (c <= ' ') |
329 | 2341 |
#endif |
3111 | 2342 |
&& ((c == ' ') || (c == '\n') || (c == '\t') |
2343 |
|| (c == '\r') || (c == '\f')) |
|
2344 |
) { |
|
2345 |
cp++; |
|
2346 |
} |
|
2884 | 2347 |
|
3111 | 2348 |
/* |
2349 |
* find first non-whiteSpace from end |
|
2350 |
*/ |
|
2351 |
ep = ep0 = cp0 + __stringSize(self) - 1; |
|
2352 |
while ((ep >= cp) && (*ep == ' ')) ep--; |
|
2353 |
c = *ep; |
|
2354 |
while ((ep >= cp) && |
|
2862 | 2355 |
#ifndef NON_ASCII |
3111 | 2356 |
(c <= ' ') && |
329 | 2357 |
#endif |
3111 | 2358 |
((c == ' ') || (c == '\n') || (c == '\t') |
2359 |
|| (c == '\r') || (c == '\f'))) { |
|
2360 |
ep--; |
|
2361 |
c = *ep; |
|
2362 |
} |
|
2884 | 2363 |
|
3111 | 2364 |
/* |
2365 |
* no whiteSpace ? |
|
2366 |
*/ |
|
2367 |
if ((cp == cp0) && (ep == ep0)) { |
|
2368 |
RETURN(self); |
|
2369 |
} |
|
2884 | 2370 |
|
3111 | 2371 |
startIndex = __MKSMALLINT(cp - cp0 + 1); |
2372 |
endIndex = __MKSMALLINT(ep - cp0 + 1); |
|
51 | 2373 |
} |
202 | 2374 |
%}. |
2375 |
startIndex == 0 ifTrue:[^ super withoutSeparators]. |
|
2376 |
||
2377 |
startIndex > endIndex ifTrue:[^ '']. |
|
1 | 2378 |
^ self copyFrom:startIndex to:endIndex |
2884 | 2379 |
|
2380 |
" |
|
2381 |
'hello' withoutSeparators |
|
2382 |
' hello' withoutSeparators |
|
2383 |
' hello ' withoutSeparators |
|
2384 |
' hello ' withoutSeparators |
|
2385 |
' hello ' withoutSeparators |
|
2386 |
' hello ' withoutSeparators |
|
2387 |
' ' withoutSeparators |
|
2388 |
" |
|
2389 |
||
608 | 2390 |
! |
2391 |
||
2392 |
withoutSpaces |
|
2862 | 2393 |
"return a string containing the characters of myself |
2394 |
without leading and trailing spaces. |
|
2884 | 2395 |
If there are no spaces, the receiver is returned unchanged. |
608 | 2396 |
Notice, this is different from String>>withoutSeparators." |
2397 |
||
2398 |
|startIndex "{ Class: SmallInteger }" |
|
2399 |
endIndex "{ Class: SmallInteger }" |
|
2400 |
sz blank| |
|
2401 |
||
2402 |
startIndex := 0. |
|
2403 |
%{ |
|
2404 |
REGISTER unsigned char *cp; |
|
2884 | 2405 |
REGISTER unsigned char *ep; |
2406 |
unsigned char *cp0; |
|
2407 |
unsigned char *ep0; |
|
608 | 2408 |
|
2409 |
/* ignore instances of subclasses ... */ |
|
2410 |
if (__qClass(self) == String) { |
|
3111 | 2411 |
cp = cp0 = __stringVal(self); |
2862 | 2412 |
|
3111 | 2413 |
/* |
2414 |
* find first non-blank from beginning |
|
2415 |
*/ |
|
2862 | 2416 |
#ifndef NON_ASCII |
2954 | 2417 |
# ifdef UINT64 |
3111 | 2418 |
while (*((UINT64 *)cp) == 0x2020202020202020L) { |
2419 |
cp += 8; |
|
2420 |
} |
|
2954 | 2421 |
# endif /* UINT64 */ |
3111 | 2422 |
while (*((unsigned *)cp) == 0x20202020) { |
2423 |
cp += 4; |
|
2424 |
} |
|
2862 | 2425 |
#endif |
3111 | 2426 |
while (*cp == ' ') cp++; |
2884 | 2427 |
|
3111 | 2428 |
/* |
2429 |
* find first non-blank from end |
|
2430 |
*/ |
|
2431 |
ep = ep0 = cp0 + __stringSize(self) - 1; |
|
2432 |
while ((ep >= cp) && (*ep == ' ')) ep--; |
|
2862 | 2433 |
|
3111 | 2434 |
/* |
2435 |
* no blanks ? |
|
2436 |
*/ |
|
2437 |
if ((cp == cp0) && (ep == ep0)) { |
|
2438 |
RETURN(self); |
|
2439 |
} |
|
2884 | 2440 |
|
3111 | 2441 |
startIndex = __MKSMALLINT(cp - cp0 + 1); |
2442 |
endIndex = __MKSMALLINT(ep - cp0 + 1); |
|
608 | 2443 |
} |
2444 |
%}. |
|
2445 |
startIndex == 0 ifTrue:[^ super withoutSpaces]. |
|
2446 |
||
2447 |
startIndex > endIndex ifTrue:[^ '']. |
|
2886 | 2448 |
((startIndex == 1) and:[endIndex == self size]) ifTrue:[^ self]. |
608 | 2449 |
^ self copyFrom:startIndex to:endIndex |
2884 | 2450 |
|
2451 |
" |
|
2452 |
' hello' withoutSpaces |
|
2453 |
' hello ' withoutSpaces |
|
2454 |
' hello ' withoutSpaces |
|
2455 |
' hello ' withoutSpaces |
|
2456 |
' hello ' withoutSpaces |
|
2457 |
' ' withoutSpaces |
|
2458 |
" |
|
608 | 2459 |
! ! |
2460 |
||
2461 |
!String methodsFor:'printing & storing'! |
|
2462 |
||
2886 | 2463 |
isLiteral |
2464 |
"return true, if the receiver can be used as a literal |
|
608 | 2465 |
(i.e. can be used in constant arrays)" |
2466 |
||
2467 |
^ true |
|
2468 |
! |
|
2469 |
||
2886 | 2470 |
|
2471 |
"print the receiver on standard output. |
|
608 | 2472 |
This method does NOT use the stream classes and will therefore work |
2473 |
even in case of emergency." |
|
2474 |
||
2475 |
%{ /* NOCONTEXT */ |
|
2476 |
||
2477 |
if (__qClass(self) == String) { |
|
1486 | 2478 |
#ifdef WIN32 |
3809
dd1df62365ee
use threadsafe fprintf (win32)
Claus Gittinger <cg@exept.de>
parents:
3751
diff
changeset
|
2479 |
__win32_fprintf(stdout, "%s" , __stringVal(self)); |
1486 | 2480 |
#else |
3751 | 2481 |
fputs(__stringVal(self), stdout); fflush(stdout); |
1486 | 2482 |
#endif |
3751 | 2483 |
RETURN (self); |
608 | 2484 |
} |
3751 | 2485 |
%}. |
608 | 2486 |
^ super print |
2487 |
! |
|
2488 |
||
3751 | 2489 |
printCR |
2490 |
"print the receiver on standard output, followed by a cr. |
|
2491 |
This method does NOT use the stream classes and will therefore work |
|
2492 |
even in case of emergency." |
|
2493 |
||
2494 |
%{ /* NOCONTEXT */ |
|
2495 |
||
2496 |
if (__qClass(self) == String) { |
|
2497 |
#ifdef WIN32 |
|
3809
dd1df62365ee
use threadsafe fprintf (win32)
Claus Gittinger <cg@exept.de>
parents:
3751
diff
changeset
|
2498 |
__win32_fprintf(stderr, "%s\n" , __stringVal(self)); |
3751 | 2499 |
#else |
2500 |
fputs(__stringVal(self), stdout); fputs("\n", stdout); fflush(stdout); |
|
2501 |
#endif |
|
2502 |
RETURN (self); |
|
2503 |
} |
|
2504 |
%}. |
|
2505 |
^ super printCR |
|
2506 |
! |
|
2507 |
||
2886 | 2508 |
printfPrintString:formatString |
608 | 2509 |
"non-portable but sometimes useful. |
2510 |
Return a printed representation of the receiver as specified by formatString, |
|
2511 |
which is defined by printf. |
|
2512 |
No checking on buffer overrun is done; the resulting string may not |
|
2513 |
be larger than 799 characters. |
|
2514 |
This method is NONSTANDARD and may be removed without notice." |
|
2515 |
||
2516 |
%{ /* STACK: 1000 */ |
|
2517 |
||
2518 |
char buffer[800]; |
|
2519 |
char *cp; |
|
2520 |
OBJ s; |
|
2521 |
||
2522 |
if (__isString(formatString)) { |
|
2523 |
/* |
|
2524 |
* actually only needed on sparc: since thisContext is |
|
2525 |
* in a global register, which gets destroyed by printf, |
|
2526 |
* manually save it here - very stupid ... |
|
2527 |
*/ |
|
2528 |
||
1134 | 2529 |
cp = (char *)__stringVal(self); |
608 | 2530 |
if (__qClass(self) != String) |
1134 | 2531 |
cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
608 | 2532 |
|
809
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
735
diff
changeset
|
2533 |
__BEGIN_PROTECT_REGISTERS__ |
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
735
diff
changeset
|
2534 |
|
1134 | 2535 |
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
|
2536 |
|
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
735
diff
changeset
|
2537 |
__END_PROTECT_REGISTERS__ |
608 | 2538 |
|
833 | 2539 |
s = __MKSTRING(buffer COMMA_SND); |
608 | 2540 |
if (s != nil) { |
2541 |
RETURN (s); |
|
2542 |
} |
|
2543 |
} |
|
2544 |
%}. |
|
2545 |
self primitiveFailed |
|
2546 |
||
2547 |
" |
|
2548 |
'hello' printfPrintString:'%%s -> %s' |
|
2549 |
'hello' printfPrintString:'%%10s -> %10s' |
|
2550 |
'hello' printfPrintString:'%%-10s -> %-10s' |
|
2551 |
" |
|
2552 |
! |
|
2553 |
||
2886 | 2554 |
storeOn:aStream |
608 | 2555 |
"put the storeString of myself on aStream" |
2556 |
||
2557 |
aStream nextPut: $'. |
|
2558 |
(self includes:$') ifTrue:[ |
|
3579 | 2559 |
self printWithQuotesDoubledOn:aStream |
608 | 2560 |
] ifFalse:[ |
3579 | 2561 |
aStream nextPutAll:self |
608 | 2562 |
]. |
2563 |
aStream nextPut:$' |
|
3579 | 2564 |
|
2565 |
"Modified: / 15.6.1998 / 17:21:51 / cg" |
|
608 | 2566 |
! |
2567 |
||
2886 | 2568 |
storeString |
2569 |
"return a String for storing myself" |
|
608 | 2570 |
|
2571 |
|s n index| |
|
2572 |
||
2573 |
n := self occurrencesOf:$'. |
|
2574 |
n == 0 ifFalse:[ |
|
2575 |
s := String new:(n + 2 + self size). |
|
2576 |
s at:1 put:$'. |
|
2577 |
index := 2. |
|
2578 |
self do:[:thisChar | |
|
2579 |
(thisChar == $') ifTrue:[ |
|
2580 |
s at:index put:thisChar. |
|
2581 |
index := index + 1. |
|
2582 |
]. |
|
2583 |
s at:index put:thisChar. |
|
2584 |
index := index + 1. |
|
2585 |
]. |
|
2586 |
s at:index put:$'. |
|
2587 |
^ s |
|
2588 |
]. |
|
2589 |
^ '''' , self , '''' |
|
1 | 2590 |
! ! |
2591 |
||
2592 |
!String methodsFor:'queries'! |
|
2593 |
||
2886 | 2594 |
bitsPerCharacter |
1239 | 2595 |
"return the number of bits each character has. |
2596 |
Here, 8 is returned (storing single byte characters)." |
|
2597 |
||
1218
5ed62714c8d1
commentary; ignoredMethods removed
Claus Gittinger <cg@exept.de>
parents:
1143
diff
changeset
|
2598 |
^ 8 |
1239 | 2599 |
|
2600 |
"Modified: 20.4.1996 / 23:08:42 / cg" |
|
1218
5ed62714c8d1
commentary; ignoredMethods removed
Claus Gittinger <cg@exept.de>
parents:
1143
diff
changeset
|
2601 |
! |
5ed62714c8d1
commentary; ignoredMethods removed
Claus Gittinger <cg@exept.de>
parents:
1143
diff
changeset
|
2602 |
|
2886 | 2603 |
isEmpty |
2604 |
"return true if the receiver is empty (i.e. if size == 0) |
|
68 | 2605 |
Redefined here for performance" |
2606 |
||
2607 |
%{ /* NOCONTEXT */ |
|
2608 |
OBJ cls; |
|
2609 |
||
329 | 2610 |
cls = __qClass(self); |
68 | 2611 |
if ((cls == String) || (cls == Symbol)) { |
3451 | 2612 |
RETURN ( (__stringSize(self) == 0) ? true : false); |
68 | 2613 |
} |
2862 | 2614 |
%}. |
3227
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2615 |
^ self size == 0 |
608 | 2616 |
! |
2617 |
||
2886 | 2618 |
knownAsSymbol |
2619 |
"return true, if there is a symbol with same characters in the |
|
2862 | 2620 |
system. |
2621 |
Can be used to check for existance of a symbol without creating one" |
|
608 | 2622 |
|
2623 |
%{ /* NOCONTEXT */ |
|
3710
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2624 |
OBJ cls; |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2625 |
int indx; |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2626 |
|
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2627 |
cls = __qClass(self); |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2628 |
if (cls != String) { |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2629 |
indx = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2630 |
} else { |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2631 |
indx = 0; |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2632 |
} |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2633 |
RETURN ( __KNOWNASSYMBOL(__stringVal(self) + indx) ); |
608 | 2634 |
%} |
3710
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2635 |
|
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2636 |
" |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2637 |
'hello' knownAsSymbol |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2638 |
'fooBarBaz' knownAsSymbol |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2639 |
" |
3227
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2640 |
! |
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2641 |
|
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2642 |
notEmpty |
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2643 |
"return true if the receiver is not empty (i.e. if size ~~ 0) |
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2644 |
Redefined here for performance" |
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2645 |
|
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2646 |
%{ /* NOCONTEXT */ |
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2647 |
OBJ cls; |
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2648 |
|
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2649 |
cls = __qClass(self); |
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2650 |
if ((cls == String) || (cls == Symbol)) { |
3451 | 2651 |
RETURN ( (__stringSize(self) != 0) ? true : false); |
3227
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2652 |
} |
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2653 |
%}. |
b75df9db0435
added tuned version notEmpty
Claus Gittinger <cg@exept.de>
parents:
3153
diff
changeset
|
2654 |
^ self size ~~ 0 |
1 | 2655 |
! ! |
608 | 2656 |
|
2670 | 2657 |
!String methodsFor:'regular expression matching'! |
2658 |
||
2886 | 2659 |
asRegex |
2660 |
"Compile the receiver as a regex matcher. May raise RxParser>>syntaxErrorSignal |
|
2670 | 2661 |
or RxParser>>compilationErrorSignal. |
2662 |
||| This is a part of the Regular Expression Matcher package, (c) Vassili Bykov, 1996. |
|
2663 |
||| Refer to `documentation' protocol of RxParser class for details." |
|
2664 |
||
2665 |
^ Regex::RxParser preferredMatcherClass for: (Regex::RxParser new parse: self) |
|
2666 |
||
2667 |
"Modified: 3.6.1997 / 11:25:25 / cg" |
|
2668 |
! |
|
2669 |
||
2886 | 2670 |
matchesRegex: regexString |
2670 | 2671 |
"Test if the receiver matches a regex. May raise RxParser>>regexErrorSignal or |
2672 |
child signals. |
|
2673 |
||| This is a part of the Regular Expression Matcher package, (c) Vassili Bykov, 1996. |
|
2674 |
||| Refer to `documentation' protocol of RxParser class for details." |
|
2675 |
||
2676 |
^regexString asRegex matches: self |
|
2677 |
||
2678 |
"Modified: 3.6.1997 / 11:25:30 / cg" |
|
2679 |
! |
|
2680 |
||
2886 | 2681 |
prefixMatchesRegex: regexString |
2670 | 2682 |
"Test if the receiver's prefix matches a regex. |
2683 |
May raise RxParser class>>regexErrorSignal or child signals. |
|
2684 |
||| This is a part of the Regular Expression Matcher package, (c) Vassili Bykov, 1996. |
|
2685 |
||| Refer to `documentation' protocol of RxParser class for details." |
|
2686 |
||
2687 |
^regexString asRegex matchesPrefix: self |
|
2688 |
||
2689 |
"Modified: 3.6.1997 / 11:25:34 / cg" |
|
2690 |
! ! |
|
2691 |
||
608 | 2692 |
!String methodsFor:'testing'! |
2693 |
||
2886 | 2694 |
endsWith:aStringOrChar |
2862 | 2695 |
"return true, if the receiver ends with something, aStringOrChar." |
1380 | 2696 |
|
2697 |
%{ /* NOCONTEXT */ |
|
2698 |
||
2699 |
int len1, len2; |
|
2700 |
REGISTER unsigned char *src1, *src2; |
|
2858
866ffc52ac8a
oops - need unsigned character compares
Claus Gittinger <cg@exept.de>
parents:
2856
diff
changeset
|
2701 |
unsigned char c; |
3545
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2702 |
REGISTER OBJ slf = self; |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2703 |
|
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2704 |
if ((__isString(slf) || __isSymbol(slf)) |
2862 | 2705 |
&& (__isString(aStringOrChar) || __isSymbol(aStringOrChar))) { |
3545
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2706 |
len1 = __qSize(slf); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2707 |
len2 = __qSize(aStringOrChar); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2708 |
if (len1 < len2) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2709 |
RETURN ( false ); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2710 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2711 |
|
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2712 |
src1 = __stringVal(slf) + (len1 - len2); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2713 |
src2 = __stringVal(aStringOrChar); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2714 |
while (c = *src2++) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2715 |
if (c != *src1++) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2716 |
RETURN ( false ); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2717 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2718 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2719 |
RETURN (true); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2720 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2721 |
if (__isCharacter(aStringOrChar)) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2722 |
int val; |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2723 |
|
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2724 |
val = __intVal(_characterVal(aStringOrChar)); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2725 |
if ((unsigned)val <= 255) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2726 |
len1 = __stringSize(slf); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2727 |
if (len1 > 0) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2728 |
RETURN ( (__stringVal(slf)[len1-1] == val) ? true : false); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2729 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2730 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2731 |
RETURN ( false ); |
1380 | 2732 |
} |
2733 |
%}. |
|
2862 | 2734 |
^ super endsWith:aStringOrChar |
1380 | 2735 |
|
2736 |
" |
|
2737 |
'hello world' endsWith:'world' |
|
2738 |
'hello world' endsWith:'earth' |
|
3545
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2739 |
'hello world' endsWith:$d |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2740 |
'hello world' endsWith:$e |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2741 |
'' endsWith:$d |
1380 | 2742 |
'hello world' endsWith:#($r $l $d) |
2743 |
" |
|
2744 |
! |
|
2745 |
||
2886 | 2746 |
isBlank |
2747 |
"return true, if the receivers size is 0 or if it contains only spaces. |
|
608 | 2748 |
Q: should we care for whiteSpace in general here ?" |
2749 |
||
2750 |
%{ /* NOCONTEXT */ |
|
2751 |
||
2752 |
REGISTER unsigned char *src; |
|
2753 |
REGISTER unsigned char c; |
|
2754 |
OBJ cls; |
|
2755 |
||
1134 | 2756 |
src = __stringVal(self); |
608 | 2757 |
if ((cls = __qClass(self)) != String) |
3710
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2758 |
src += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
608 | 2759 |
|
2760 |
#ifndef NON_ASCII |
|
2954 | 2761 |
# ifdef UINT64 |
2762 |
while (*((UINT64 *)src) == 0x2020202020202020L) { |
|
3710
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2763 |
src += 8; |
2862 | 2764 |
} |
2954 | 2765 |
# endif /* UINT64 */ |
3710
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2766 |
|
608 | 2767 |
while (*((unsigned *)src) == 0x20202020) { |
3710
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2768 |
src += 4; |
608 | 2769 |
} |
2862 | 2770 |
#endif /* ascii */ |
2771 |
||
2772 |
while (c = *src++) { |
|
3710
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2773 |
if (c != ' ') { |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2774 |
RETURN ( false ); |
6ee8b3c37d09
allow subclass-insts of String
Claus Gittinger <cg@exept.de>
parents:
3673
diff
changeset
|
2775 |
} |
2862 | 2776 |
} |
608 | 2777 |
%}. |
2778 |
^ true |
|
2779 |
! |
|
2780 |
||
2886 | 2781 |
levenshteinTo:aString s:substWeight c:caseWeight i:insrtWeight d:deleteWeight |
608 | 2782 |
"parametrized levenshtein. arguments are the costs for |
2783 |
substitution, case-change, insertion and deletion of a character." |
|
2784 |
||
1138
993e6ffdbf51
removed external decls for VM functions (should look for more)
Claus Gittinger <cg@exept.de>
parents:
1134
diff
changeset
|
2785 |
%{ /* STACK: 2000 */ |
608 | 2786 |
|
2787 |
/* |
|
2788 |
* this is very heavy used when correcting errors |
|
2789 |
* (all symbols are searched for best match) - therefore it must be fast |
|
2790 |
*/ |
|
2791 |
{ |
|
2792 |
unsigned short *data; |
|
2793 |
int l1, l2; |
|
2794 |
REGISTER int sz; |
|
2795 |
unsigned char *s1, *s2; |
|
2796 |
int v1, v2, v3, m; |
|
2797 |
REGISTER unsigned short *dp; |
|
2798 |
REGISTER int delta; |
|
2799 |
REGISTER int j; |
|
2800 |
int i; |
|
2801 |
int iW, cW, sW, dW; |
|
1486 | 2802 |
# define FASTSIZE 30 /* increase STACK if you increase this ... */ |
608 | 2803 |
unsigned short fastData[(FASTSIZE + 1) * (FASTSIZE + 1)]; |
2804 |
||
2805 |
if ((__isString(self) || __isSymbol(self)) |
|
2806 |
&& (__isString(aString) || __isSymbol(aString)) |
|
2807 |
&& __bothSmallInteger(insrtWeight, caseWeight) |
|
2808 |
&& __bothSmallInteger(substWeight, deleteWeight)) { |
|
1134 | 2809 |
iW = __intVal(insrtWeight); |
2810 |
cW = __intVal(caseWeight); |
|
2811 |
sW = __intVal(substWeight); |
|
2812 |
dW = __intVal(deleteWeight); |
|
2813 |
s1 = __stringVal(self); |
|
2814 |
s2 = __stringVal(aString); |
|
608 | 2815 |
l1 = strlen(s1); |
2816 |
l2 = strlen(s2); |
|
2817 |
||
2818 |
sz = (l1 < l2) ? l2 : l1; |
|
2819 |
delta = sz + 1; |
|
2820 |
if (sz <= FASTSIZE) { |
|
2821 |
data = fastData; |
|
2822 |
} else { |
|
2823 |
/* add ifdef ALLOCA here ... */ |
|
2824 |
data = (unsigned short *)malloc(delta * delta * sizeof(short)); |
|
2825 |
if (! data) goto mallocFailed; |
|
2826 |
} |
|
2827 |
||
2828 |
data[0] = 0; |
|
2829 |
dp = data+1; |
|
2830 |
for (j=1, dp=data+1; j<=sz; j++, dp++) |
|
2831 |
*dp = *(dp-1) + iW; |
|
2832 |
||
2833 |
for (i=1, dp=data+delta; i<=sz; i++, dp+=delta) |
|
2834 |
*dp = *(dp-delta) + dW; |
|
2835 |
||
2836 |
for (i=1; i<=l1; i++) { |
|
2837 |
for (j=1; j<=l2; j++) { |
|
2838 |
dp = data + (i*delta) + j; |
|
2839 |
if (s1[i] != s2[j]) { |
|
2840 |
if (tolower(s1[i]) == tolower(s2[j])) { |
|
2841 |
m = cW; |
|
2842 |
} else { |
|
2843 |
m = sW; |
|
2844 |
} |
|
2845 |
} else |
|
2846 |
m = 0; |
|
2847 |
||
2848 |
v2 = *(dp - 1) + iW; |
|
2849 |
v3 = *(dp - delta) + dW; |
|
2850 |
v1 = *(dp - delta - 1) + m; |
|
2851 |
if (v1 < v2) |
|
2852 |
if (v1 < v3) |
|
2853 |
m = v1; |
|
2854 |
else |
|
2855 |
m = v3; |
|
2856 |
else |
|
2857 |
if (v2 < v3) |
|
2858 |
m = v2; |
|
2859 |
else |
|
2860 |
m = v3; |
|
2861 |
*dp = m; |
|
2862 |
} |
|
2863 |
} |
|
2864 |
m = data[l1 * delta + l2]; |
|
2865 |
if (sz > FASTSIZE) |
|
2866 |
free(data); |
|
1134 | 2867 |
RETURN ( __MKSMALLINT(m) ); |
608 | 2868 |
} |
2869 |
mallocFailed: ; |
|
2870 |
} |
|
2871 |
%}. |
|
2872 |
||
2873 |
^ super levenshteinTo:aString |
|
2874 |
s:substWeight c:caseWeight |
|
2875 |
i:insrtWeight d:deleteWeight |
|
2876 |
||
2877 |
"'ocmprt' levenshteinTo:'computer' |
|
2878 |
'computer' levenshteinTo:'computer' |
|
2879 |
'ocmputer' levenshteinTo:'computer' |
|
2880 |
'cmputer' levenshteinTo:'computer' |
|
2881 |
'Computer' levenshteinTo:'computer'" |
|
1380 | 2882 |
! |
2883 |
||
2886 | 2884 |
startsWith:aStringOrChar |
2862 | 2885 |
"return true, if the receiver starts with something, aStringOrChar." |
1380 | 2886 |
|
2887 |
%{ /* NOCONTEXT */ |
|
2888 |
||
2889 |
int len1, len2; |
|
2890 |
REGISTER unsigned char *src1, *src2; |
|
2858
866ffc52ac8a
oops - need unsigned character compares
Claus Gittinger <cg@exept.de>
parents:
2856
diff
changeset
|
2891 |
unsigned char c; |
3545
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2892 |
REGISTER OBJ slf = self; |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2893 |
|
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2894 |
if (((__qClass(slf)==String) || (__qClass(slf)==Symbol)) |
3314
94046261fba0
tuned #~= , #= and #startsWith:
Claus Gittinger <cg@exept.de>
parents:
3281
diff
changeset
|
2895 |
&& __isNonNilObject(aStringOrChar) |
94046261fba0
tuned #~= , #= and #startsWith:
Claus Gittinger <cg@exept.de>
parents:
3281
diff
changeset
|
2896 |
&& ((__qClass(aStringOrChar)==String) || (__qClass(aStringOrChar)==Symbol))) { |
3545
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2897 |
src1 = __stringVal(slf); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2898 |
src2 = __stringVal(aStringOrChar); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2899 |
if (src1[0] != src2[0]) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2900 |
RETURN ( false ); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2901 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2902 |
|
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2903 |
len1 = __qSize(slf); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2904 |
len2 = __qSize(aStringOrChar); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2905 |
if (len1 < len2) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2906 |
RETURN ( false ); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2907 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2908 |
|
3546
00984e13534a
8-byte compares (if possible) in startsWith:
Claus Gittinger <cg@exept.de>
parents:
3545
diff
changeset
|
2909 |
# ifdef UINT64 |
00984e13534a
8-byte compares (if possible) in startsWith:
Claus Gittinger <cg@exept.de>
parents:
3545
diff
changeset
|
2910 |
while (len2 > (OHDR_SIZE+sizeof(UINT64))) { |
00984e13534a
8-byte compares (if possible) in startsWith:
Claus Gittinger <cg@exept.de>
parents:
3545
diff
changeset
|
2911 |
if ( *((unsigned UINT64 *)src1) != *((unsigned UINT64 *)src2) ) { |
00984e13534a
8-byte compares (if possible) in startsWith:
Claus Gittinger <cg@exept.de>
parents:
3545
diff
changeset
|
2912 |
RETURN (false); |
00984e13534a
8-byte compares (if possible) in startsWith:
Claus Gittinger <cg@exept.de>
parents:
3545
diff
changeset
|
2913 |
} |
00984e13534a
8-byte compares (if possible) in startsWith:
Claus Gittinger <cg@exept.de>
parents:
3545
diff
changeset
|
2914 |
len2 -= sizeof(UINT64); |
00984e13534a
8-byte compares (if possible) in startsWith:
Claus Gittinger <cg@exept.de>
parents:
3545
diff
changeset
|
2915 |
src1 += sizeof(UINT64); |
00984e13534a
8-byte compares (if possible) in startsWith:
Claus Gittinger <cg@exept.de>
parents:
3545
diff
changeset
|
2916 |
src2 += sizeof(UINT64); |
00984e13534a
8-byte compares (if possible) in startsWith:
Claus Gittinger <cg@exept.de>
parents:
3545
diff
changeset
|
2917 |
} |
00984e13534a
8-byte compares (if possible) in startsWith:
Claus Gittinger <cg@exept.de>
parents:
3545
diff
changeset
|
2918 |
# endif /* UINT64 */ |
00984e13534a
8-byte compares (if possible) in startsWith:
Claus Gittinger <cg@exept.de>
parents:
3545
diff
changeset
|
2919 |
|
3545
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2920 |
while (len2 > (OHDR_SIZE+sizeof(INT))) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2921 |
if ( *((unsigned INT *)src1) != *((unsigned INT *)src2) ) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2922 |
RETURN (false); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2923 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2924 |
len2 -= sizeof(INT); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2925 |
src1 += sizeof(INT); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2926 |
src2 += sizeof(INT); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2927 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2928 |
|
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2929 |
while (c = *src2++) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2930 |
if (c != *src1) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2931 |
RETURN ( false ); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2932 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2933 |
src1++; |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2934 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2935 |
RETURN (true); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2936 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2937 |
if (__isCharacter(aStringOrChar)) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2938 |
int val; |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2939 |
|
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2940 |
val = __intVal(_characterVal(aStringOrChar)); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2941 |
if ((unsigned)val <= 255) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2942 |
len1 = __stringSize(slf); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2943 |
if (len1 > 0) { |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2944 |
RETURN ( (__stringVal(slf)[0] == val) ? true : false); |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2945 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2946 |
} |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2947 |
RETURN ( false ); |
1380 | 2948 |
} |
2949 |
%}. |
|
2862 | 2950 |
^ super startsWith:aStringOrChar |
1380 | 2951 |
|
2952 |
" |
|
3314
94046261fba0
tuned #~= , #= and #startsWith:
Claus Gittinger <cg@exept.de>
parents:
3281
diff
changeset
|
2953 |
'hello world' startsWith:'hello' |
94046261fba0
tuned #~= , #= and #startsWith:
Claus Gittinger <cg@exept.de>
parents:
3281
diff
changeset
|
2954 |
'hello world' startsWith:'hella' |
1380 | 2955 |
'hello world' startsWith:'hi' |
3545
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2956 |
'hello world' startsWith:$h |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2957 |
'hello world' startsWith:$H |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2958 |
'hello world' startsWith:(Character value:16rFF00) |
5238e4d15d64
better code for endsWith: / startsWith: for single-character arg.
Claus Gittinger <cg@exept.de>
parents:
3451
diff
changeset
|
2959 |
'hello world' startsWith:60 |
1380 | 2960 |
'hello world' startsWith:#($h $e $l) |
2961 |
" |
|
608 | 2962 |
! ! |
2963 |
||
633 | 2964 |
!String class methodsFor:'documentation'! |
2965 |
||
2886 | 2966 |
version |
3989 | 2967 |
^ '$Header: /cvs/stx/stx/libbasic/String.st,v 1.133 1999-02-15 14:46:48 cg Exp $' |
633 | 2968 |
! ! |