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