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