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