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