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