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