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