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