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