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