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