author | Claus Gittinger <cg@exept.de> |
Tue, 09 Apr 1996 22:47:05 +0200 | |
changeset 1143 | 27325100bdf4 |
parent 1138 | 993e6ffdbf51 |
child 1218 | 5ed62714c8d1 |
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 |
||
255 | 13 |
CharacterArray subclass:#String |
608 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Text' |
|
88 | 18 |
! |
19 |
||
216 | 20 |
!String primitiveDefinitions! |
88 | 21 |
%{ |
22 |
#include <stdio.h> |
|
437 | 23 |
#define _STDIO_H_INCLUDED_ |
24 |
||
88 | 25 |
#include <ctype.h> |
359 | 26 |
|
273 | 27 |
#ifdef LINUX |
359 | 28 |
# define __STRINGDEFS__ |
29 |
# include <linuxIntern.h> |
|
302 | 30 |
#else |
31 |
extern char *strchr(); |
|
273 | 32 |
#endif |
95 | 33 |
|
34 |
/* |
|
35 |
* old st/x creates strings with spaces in it; |
|
36 |
* new st/x will fill it with zeros (for st-80 compatibility) |
|
37 |
* the define below sets old behavior. |
|
38 |
*/ |
|
39 |
#define INITIALIZE_WITH_SPACE |
|
88 | 40 |
%} |
216 | 41 |
! ! |
88 | 42 |
|
608 | 43 |
!String class methodsFor:'documentation'! |
44 |
||
45 |
copyright |
|
46 |
" |
|
47 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
48 |
All Rights Reserved |
|
49 |
||
50 |
This software is furnished under a license and may be used |
|
51 |
only in accordance with the terms of that license and with the |
|
52 |
inclusion of the above copyright notice. This software may not |
|
53 |
be provided or otherwise made available to, or used by, any |
|
54 |
other person. No title to or ownership of the software is |
|
55 |
hereby transferred. |
|
56 |
" |
|
57 |
! |
|
3 | 58 |
|
608 | 59 |
documentation |
60 |
" |
|
61 |
Strings are ByteArrays storing Characters. |
|
62 |
||
63 |
Strings are kind of kludgy: to allow for easy handling by c-functions, |
|
64 |
there is always one 0-byte added at the end, which is not counted |
|
65 |
in size. also, the at:put: method does not allow for storing 0-bytes. |
|
66 |
(to do this, the basicAt:put: and basicNew: methods are redefined) |
|
3 | 67 |
|
608 | 68 |
You cannot add any instvars to String, since the the run time system & compiler |
69 |
creates literal strings and knows that strings have no named instvars. |
|
70 |
||
71 |
If you really need strings with instVars, you have to create a subclass |
|
72 |
of String (the access functions defined here can handle this). |
|
73 |
A little warning though: not all smalltalk systems allow subclassing String, |
|
74 |
so your program may become unportable if you do so. |
|
75 |
" |
|
3 | 76 |
! ! |
77 |
||
1 | 78 |
!String class methodsFor:'instance creation'! |
79 |
||
80 |
basicNew:anInteger |
|
81 |
"return a new empty string with anInteger characters" |
|
82 |
||
83 |
%{ /* NOCONTEXT */ |
|
84 |
||
85 |
OBJ newString; |
|
86 |
REGISTER int len; |
|
87 |
REGISTER unsigned char *cp; |
|
88 |
REGISTER OBJ *op; |
|
89 |
int nInstVars, instsize; |
|
90 |
||
252 | 91 |
if (__isSmallInteger(anInteger)) { |
1134 | 92 |
len = __intVal(anInteger); |
159 | 93 |
if (len >= 0) { |
95 | 94 |
if (self == String) { |
95 |
instsize = OHDR_SIZE + len + 1; |
|
835 | 96 |
if (__CanDoQuickNew(instsize)) { |
95 | 97 |
/* |
98 |
* the most common case |
|
99 |
*/ |
|
835 | 100 |
__qCheckedNew(newString, instsize); |
1134 | 101 |
__InstPtr(newString)->o_class = self; |
368 | 102 |
__qSTORE(newString, self); |
103 |
||
1134 | 104 |
cp = __stringVal(newString); |
357 | 105 |
#if defined(memset4) && !defined(NON_ASCII) |
106 |
instsize = len >> 2; |
|
107 |
if (len & 3) instsize++; |
|
108 |
memset4(cp, 0x20202020, instsize); |
|
159 | 109 |
*(cp + len) = '\0'; |
95 | 110 |
#else |
357 | 111 |
# ifdef FAST_MEMSET |
112 |
memset(cp, ' ', len); |
|
113 |
*(cp + len) = '\0'; |
|
114 |
# else |
|
159 | 115 |
while (len >= 8) { |
357 | 116 |
# ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
117 |
((int *)cp)[0] = 0x20202020; |
|
118 |
((int *)cp)[1] = 0x20202020; |
|
119 |
# else |
|
159 | 120 |
cp[0] = cp[1] = cp[2] = cp[3] = ' '; |
121 |
cp[4] = cp[5] = cp[6] = cp[7] = ' '; |
|
357 | 122 |
# endif |
159 | 123 |
cp += 8; |
95 | 124 |
len -= 8; |
159 | 125 |
} |
126 |
while (len--) |
|
127 |
*cp++ = ' '; |
|
128 |
*cp = '\0'; |
|
357 | 129 |
# endif |
95 | 130 |
#endif |
159 | 131 |
RETURN (newString); |
95 | 132 |
} |
133 |
nInstVars = 0; |
|
134 |
} else { |
|
1134 | 135 |
nInstVars = __intVal(__ClassInstPtr(self)->c_ninstvars); |
159 | 136 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars) + len + 1; |
95 | 137 |
} |
138 |
||
834 | 139 |
__PROTECT_CONTEXT__ |
836 | 140 |
__qNew(newString, instsize, SENDER); |
834 | 141 |
__UNPROTECT_CONTEXT__ |
159 | 142 |
if (newString == nil) goto fail; |
1134 | 143 |
__InstPtr(newString)->o_class = self; |
368 | 144 |
__qSTORE(newString, self); |
1 | 145 |
|
95 | 146 |
if (nInstVars) { |
1 | 147 |
#if defined(memset4) |
1134 | 148 |
memset4(__InstPtr(newString)->i_instvars, nil, nInstVars); |
1 | 149 |
#else |
150 |
# if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES) |
|
159 | 151 |
/* |
152 |
* knowing that nil is 0 |
|
153 |
*/ |
|
1134 | 154 |
memset(__InstPtr(newString)->i_instvars, 0, __OBJS2BYTES__(nInstVars)); |
1 | 155 |
# else |
1134 | 156 |
op = __InstPtr(newString)->i_instvars; |
159 | 157 |
do { |
158 |
*op++ = nil; |
|
159 |
} while (--nInstVars); |
|
1 | 160 |
# endif |
161 |
#endif |
|
1134 | 162 |
cp = __stringVal(newString) + __OBJS2BYTES__(nInstVars); |
159 | 163 |
} else { |
1134 | 164 |
cp = __stringVal(newString); |
95 | 165 |
} |
1 | 166 |
|
167 |
#ifdef FAST_MEMSET |
|
159 | 168 |
memset(cp, ' ', len); |
169 |
*(cp + len) = '\0'; |
|
1 | 170 |
#else |
159 | 171 |
while (len >= 8) { |
357 | 172 |
# ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
173 |
((int *)cp)[0] = 0x20202020; |
|
174 |
((int *)cp)[1] = 0x20202020; |
|
175 |
# else |
|
159 | 176 |
cp[0] = cp[1] = cp[2] = cp[3] = ' '; |
177 |
cp[4] = cp[5] = cp[6] = cp[7] = ' '; |
|
357 | 178 |
# endif |
159 | 179 |
cp += 8; |
180 |
len -= 8; |
|
181 |
} |
|
182 |
while (len--) |
|
183 |
*cp++ = ' '; |
|
184 |
*cp = '\0'; |
|
1 | 185 |
#endif |
159 | 186 |
RETURN (newString); |
187 |
} |
|
1 | 188 |
} |
95 | 189 |
fail: ;; |
1019 | 190 |
%}. |
95 | 191 |
" |
192 |
invalid argument, or out-of-memory: |
|
193 |
use error handling in superclass |
|
194 |
" |
|
1 | 195 |
^ (super basicNew:anInteger) atAllPut:(Character space) |
196 |
! |
|
197 |
||
608 | 198 |
new:n |
199 |
^ self basicNew:n |
|
347 | 200 |
! |
201 |
||
202 |
readFrom:aStream onError:exceptionBlock |
|
203 |
"read & return the next String from the (character-)stream aStream; |
|
204 |
skipping all whitespace first; return the value of exceptionBlock, |
|
205 |
if no string can be read. The sequence of characters as read from the |
|
206 |
stream must be one as stored via storeOn: or storeString." |
|
207 |
||
208 |
|collected char| |
|
209 |
||
210 |
" |
|
211 |
this method is not to be inherited |
|
212 |
(i.e. not ok for subclasses; Symbol, for example) |
|
213 |
" |
|
214 |
self ~~ String ifTrue:[ |
|
215 |
^ super readFrom:aStream onError:exceptionBlock |
|
216 |
]. |
|
217 |
||
218 |
"skip whiteSpace" |
|
219 |
aStream skipSeparators. |
|
220 |
(aStream next == $') ifTrue:[ |
|
221 |
collected := WriteStream on:(String new). |
|
222 |
[true] whileTrue:[ |
|
223 |
aStream atEnd ifTrue:[ |
|
224 |
"/ mhmh - reached the end without a closing quote |
|
225 |
"/ looks like an error to me ... |
|
226 |
^ exceptionBlock value |
|
227 |
]. |
|
228 |
char := aStream next. |
|
229 |
char == $' ifTrue:[ |
|
230 |
"/ look for another quote |
|
231 |
aStream peek == $' ifFalse:[ |
|
232 |
^ collected contents |
|
233 |
]. |
|
234 |
aStream next. |
|
235 |
]. |
|
236 |
collected nextPut:char |
|
237 |
] |
|
238 |
]. |
|
239 |
^ exceptionBlock value |
|
240 |
||
241 |
" |
|
242 |
String readFrom:('''hello world''' readStream) |
|
243 |
String readFrom:('''hello '''' world''' readStream) |
|
244 |
String readFrom:('1 ''hello'' ' readStream) |
|
245 |
String readFrom:('1 ''hello'' ' readStream) onError:['foobar'] |
|
246 |
" |
|
608 | 247 |
! |
248 |
||
249 |
unititializedNew:anInteger |
|
250 |
"redefine it back - strings must have a 0-byte at the end" |
|
251 |
||
252 |
^ self basicNew:anInteger |
|
253 |
! ! |
|
254 |
||
255 |
!String class methodsFor:'binary storage'! |
|
256 |
||
257 |
binaryDefinitionFrom:stream manager:manager |
|
258 |
"read a binary representation from stream." |
|
259 |
||
260 |
^ (stream next:(stream nextNumber: 4)) asString |
|
261 |
! ! |
|
262 |
||
263 |
!String class methodsFor:'queries'! |
|
264 |
||
265 |
isBuiltInClass |
|
266 |
"this class is known by the run-time-system" |
|
267 |
||
268 |
^ self == String |
|
1 | 269 |
! ! |
270 |
||
271 |
!String methodsFor:'accessing'! |
|
272 |
||
329 | 273 |
at:index |
274 |
"return the character at position index, an Integer. |
|
345 | 275 |
Reimplemented here to avoid the additional at:->basicAt: send |
276 |
(which we can do here, since at: is obviously not redefined in a subclass). |
|
329 | 277 |
This method is the same as at:." |
278 |
||
279 |
%{ /* NOCONTEXT */ |
|
280 |
||
281 |
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
|
282 |
REGISTER OBJ slf, cls; |
329 | 283 |
|
284 |
if (__isSmallInteger(index)) { |
|
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
285 |
indx = __intVal(index) - 1; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
286 |
slf = self; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
287 |
|
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
288 |
cls = __qClass(slf); |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
289 |
if (cls != String) |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
290 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
291 |
if ((unsigned)indx < (unsigned)(__stringSize(slf))) { |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
292 |
RETURN ( __MKCHARACTER(__stringVal(slf)[indx] & 0xFF) ); |
329 | 293 |
} |
294 |
} |
|
295 |
%}. |
|
296 |
^ self subscriptBoundsError:index |
|
1 | 297 |
! |
298 |
||
329 | 299 |
at:index put:aCharacter |
300 |
"store the argument, aCharacter at position index, an Integer. |
|
345 | 301 |
Reimplemented here to avoid the additional at:put:->basicAt:put: send |
302 |
(but only for Strings, since subclasses may redefine basicAt:put:). |
|
329 | 303 |
This method is the same as basicAt:put:." |
304 |
||
305 |
%{ /* NOCONTEXT */ |
|
306 |
||
307 |
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
|
308 |
REGISTER OBJ slf; |
329 | 309 |
|
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
310 |
slf = self; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
311 |
|
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
312 |
if (__isString(slf)) { |
345 | 313 |
if (__isCharacter(aCharacter)) { |
1134 | 314 |
value = __intVal(_characterVal(aCharacter)); |
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
315 |
if (((unsigned)value <= 0xFF) |
345 | 316 |
&& __isSmallInteger(index)) { |
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
317 |
indx = __intVal(index) - 1; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
318 |
if ((unsigned)indx < (unsigned)(__stringSize(slf))) { |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
319 |
__stringVal(slf)[indx] = value; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
320 |
RETURN ( aCharacter ); |
329 | 321 |
} |
322 |
} |
|
323 |
} |
|
324 |
} |
|
325 |
%}. |
|
345 | 326 |
(self isMemberOf:String) ifFalse:[ |
327 |
^ super at:index put:aCharacter |
|
328 |
]. |
|
329 |
||
329 | 330 |
(aCharacter isMemberOf:Character) ifFalse:[ |
331 |
" |
|
332 |
tried to store something which is not a character |
|
333 |
" |
|
334 |
^ self elementNotCharacter |
|
335 |
]. |
|
336 |
(aCharacter asciiValue between:1 and:255) ifFalse:[ |
|
337 |
" |
|
338 |
tried to store a multibyte character |
|
339 |
" |
|
340 |
^ self elementBoundsError |
|
341 |
]. |
|
342 |
" |
|
343 |
invalid index |
|
344 |
" |
|
345 |
^ self subscriptBoundsError:index |
|
346 |
! |
|
347 |
||
608 | 348 |
basicAt:index |
349 |
"return the character at position index, an Integer |
|
350 |
- reimplemented here since we return characters" |
|
351 |
||
352 |
%{ /* NOCONTEXT */ |
|
353 |
||
354 |
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
|
355 |
REGISTER OBJ slf, cls; |
608 | 356 |
|
357 |
if (__isSmallInteger(index)) { |
|
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
358 |
indx = __intVal(index) - 1; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
359 |
|
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
360 |
slf = self; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
361 |
cls = __qClass(slf); |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
362 |
if (cls != String) |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
363 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
364 |
if ((unsigned)indx < (unsigned)(__stringSize(slf))) { |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
365 |
RETURN ( __MKCHARACTER(__stringVal(slf)[indx] & 0xFF) ); |
608 | 366 |
} |
367 |
} |
|
368 |
%}. |
|
369 |
^ self subscriptBoundsError:index |
|
370 |
! |
|
371 |
||
1 | 372 |
basicAt:index put:aCharacter |
373 |
"store the argument, aCharacter at position index, an Integer |
|
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
374 |
- reimplemented here since we store characters" |
1 | 375 |
|
376 |
%{ /* NOCONTEXT */ |
|
377 |
||
378 |
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
|
379 |
REGISTER OBJ slf; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
380 |
REGISTER OBJ cls; |
1 | 381 |
|
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
382 |
slf = self; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
383 |
|
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
384 |
if (__isCharacter(aCharacter)) { |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
385 |
value = __intVal(_characterVal(aCharacter)); |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
386 |
if (((unsigned)value <= 0xFF) |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
387 |
&& __isSmallInteger(index)) { |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
388 |
indx = __intVal(index) - 1; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
389 |
cls = __qClass(slf); |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
390 |
if (cls != String) |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
391 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
392 |
if ((unsigned)indx < (unsigned)(__stringSize(slf))) { |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
393 |
__stringVal(slf)[indx] = value; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
394 |
RETURN ( aCharacter ); |
159 | 395 |
} |
396 |
} |
|
1 | 397 |
} |
159 | 398 |
%}. |
1 | 399 |
(aCharacter isMemberOf:Character) ifFalse:[ |
299 | 400 |
" |
401 |
tried to store something which is not a character |
|
402 |
" |
|
159 | 403 |
^ self elementNotCharacter |
299 | 404 |
]. |
405 |
(aCharacter asciiValue between:1 and:255) ifFalse:[ |
|
406 |
" |
|
407 |
tried to store a multibyte character |
|
408 |
" |
|
409 |
^ self elementBoundsError |
|
410 |
]. |
|
411 |
" |
|
412 |
invalid index |
|
413 |
" |
|
414 |
^ self subscriptBoundsError:index |
|
429 | 415 |
! |
416 |
||
608 | 417 |
basicSize |
418 |
"return the number of characters in myself. |
|
419 |
Redefined here to exclude the 0-byte at the end." |
|
420 |
||
421 |
%{ /* NOCONTEXT */ |
|
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
422 |
REGISTER OBJ slf, cls; |
608 | 423 |
|
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
424 |
slf = self; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
425 |
|
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
426 |
cls = __qClass(slf); |
608 | 427 |
if (cls == String) { |
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
428 |
RETURN ( __MKSMALLINT(__stringSize(slf)) ); |
608 | 429 |
} |
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
430 |
RETURN ( __MKSMALLINT(__stringSize(slf) |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
431 |
- __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)))); |
608 | 432 |
%} |
433 |
! |
|
429 | 434 |
|
608 | 435 |
size |
436 |
"return the number of characters in myself. |
|
437 |
Reimplemented here to avoid the additional size->basicSize send |
|
438 |
(which we can do here, since size is obviously not redefined in a subclass). |
|
439 |
This method is the same as basicSize." |
|
429 | 440 |
|
608 | 441 |
%{ /* NOCONTEXT */ |
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
442 |
REGISTER OBJ cls, slf; |
608 | 443 |
|
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
444 |
slf = self; |
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
445 |
cls = __qClass(slf); |
608 | 446 |
if (cls == String) { |
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
447 |
RETURN ( __MKSMALLINT(__stringSize(slf)) ); |
429 | 448 |
} |
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
449 |
RETURN ( __MKSMALLINT(__stringSize(slf) |
1134 | 450 |
- __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)))); |
608 | 451 |
%} |
2 | 452 |
! ! |
453 |
||
901
c5cab954a51d
removed binary-storage stuff - it was not used anyway
Claus Gittinger <cg@exept.de>
parents:
836
diff
changeset
|
454 |
!String ignoredMethodsFor:'binary storage'! |
276 | 455 |
|
456 |
storeBinaryOn:stream manager:manager |
|
457 |
"append a binary representation of the receiver onto stream. |
|
458 |
Redefined to store short Strings in a more compact way with a |
|
459 |
special type code." |
|
460 |
||
286 | 461 |
|sz "{ Class: SmallInteger }"| |
462 |
||
463 |
sz := self size. |
|
276 | 464 |
"not, if I am a thingy of a subclass" |
286 | 465 |
((self class ~~ String) or:[sz > 255]) ifTrue:[ |
276 | 466 |
^ super storeBinaryOn:stream manager:manager |
467 |
]. |
|
468 |
stream nextPut:manager codeForString. |
|
286 | 469 |
stream nextPut:sz. |
470 |
1 to:sz do:[:index | |
|
276 | 471 |
stream nextPut:(self basicAt:index) asciiValue |
472 |
] |
|
1 | 473 |
! ! |
474 |
||
53 | 475 |
!String methodsFor:'character searching'! |
1 | 476 |
|
477 |
includes:aCharacter |
|
478 |
"return true if the argument, aCharacter is included in the receiver |
|
479 |
- reimplemented here for speed" |
|
480 |
||
481 |
%{ /* NOCONTEXT */ |
|
482 |
||
483 |
REGISTER unsigned char *cp; |
|
484 |
REGISTER int byteValue; |
|
53 | 485 |
OBJ cls; |
1 | 486 |
|
53 | 487 |
if (__isCharacter(aCharacter)) { |
1134 | 488 |
byteValue = __intVal(_characterVal(aCharacter)); |
489 |
cp = __stringVal(self); |
|
329 | 490 |
if ((cls = __qClass(self)) != String) |
1134 | 491 |
cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1 | 492 |
#ifdef FAST_STRCHR |
1134 | 493 |
cp = (unsigned char *) strchr(cp, __intVal(_characterVal(aCharacter))); |
159 | 494 |
if (cp) { |
495 |
RETURN ( true ); |
|
496 |
} |
|
1 | 497 |
#else |
159 | 498 |
while (*cp) { |
499 |
if (*cp == byteValue) { |
|
500 |
RETURN ( true ); |
|
501 |
} |
|
502 |
cp++; |
|
503 |
} |
|
1 | 504 |
#endif |
505 |
} |
|
299 | 506 |
%}. |
1 | 507 |
^ false |
53 | 508 |
|
95 | 509 |
" |
510 |
'hello world' includes:$a |
|
511 |
'hello world' includes:$o |
|
512 |
'hello world' includes:$x |
|
513 |
'hello world' includes:1 |
|
299 | 514 |
'hello world' asTwoByteString includes:$o |
95 | 515 |
" |
1 | 516 |
! |
517 |
||
10 | 518 |
includesAny:aCollection |
519 |
"return true, if the receiver includes any of the characters in the |
|
53 | 520 |
argument, aCollection. |
521 |
- redefined for speed if the argument is a String." |
|
10 | 522 |
|
523 |
%{ /* NOCONTEXT */ |
|
524 |
||
525 |
REGISTER unsigned char *cp; |
|
526 |
REGISTER unsigned char *matchP; |
|
53 | 527 |
OBJ cls; |
10 | 528 |
|
53 | 529 |
if (__isString(aCollection)) { |
1134 | 530 |
matchP = __stringVal(aCollection); |
531 |
cp = __stringVal(self); |
|
329 | 532 |
if ((cls = __qClass(self)) != String) |
1134 | 533 |
cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
53 | 534 |
|
159 | 535 |
while (*cp) { |
329 | 536 |
if (strchr(matchP, *cp)) { |
159 | 537 |
RETURN ( true ); |
538 |
} |
|
539 |
cp++; |
|
540 |
} |
|
541 |
RETURN ( false ); |
|
10 | 542 |
} |
543 |
%} |
|
544 |
. |
|
545 |
^ super includesAny:aCollection |
|
95 | 546 |
|
547 |
" |
|
548 |
'hello world' includesAny:'abcd' |
|
549 |
'hello world' includesAny:'xyz' |
|
550 |
'hello world' includesAny:(Array with:$a with:$b with:$d) |
|
551 |
'hello world' includesAny:(Array with:$x with:$y) |
|
552 |
'hello world' includesAny:(Array with:1 with:2) |
|
553 |
" |
|
10 | 554 |
! |
555 |
||
1 | 556 |
indexOf:aCharacter |
557 |
"return the index of the first occurrences of the argument, aCharacter |
|
558 |
in the receiver or 0 if not found - reimplemented here for speed." |
|
559 |
||
560 |
%{ /* NOCONTEXT */ |
|
561 |
||
562 |
REGISTER unsigned char *cp; |
|
563 |
#ifdef FAST_STRCHR |
|
564 |
char *strchr(); |
|
565 |
#else |
|
566 |
REGISTER int byteValue; |
|
567 |
REGISTER int index; |
|
370 | 568 |
char c; |
1 | 569 |
#endif |
53 | 570 |
OBJ cls; |
1 | 571 |
|
53 | 572 |
if (__isCharacter(aCharacter)) { |
1134 | 573 |
cp = __stringVal(self); |
329 | 574 |
if ((cls = __qClass(self)) != String) |
1134 | 575 |
cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1 | 576 |
#ifdef FAST_STRCHR |
1134 | 577 |
cp = (unsigned char *) strchr(cp, __intVal(_characterVal(aCharacter))); |
159 | 578 |
if (cp) { |
1134 | 579 |
RETURN ( __MKSMALLINT(cp - __stringVal(self) + 1) ); |
159 | 580 |
} |
1 | 581 |
#else |
1134 | 582 |
byteValue = __intVal(_characterVal(aCharacter)); |
159 | 583 |
index = 1; |
370 | 584 |
while (c = *cp++) { |
1134 | 585 |
if (c == byteValue) { RETURN ( __MKSMALLINT(index) ); } |
159 | 586 |
index++; |
587 |
} |
|
1 | 588 |
#endif |
589 |
} |
|
370 | 590 |
%}. |
591 |
"/ cannot include anything but characters ... |
|
1 | 592 |
^ 0 |
95 | 593 |
|
594 |
" |
|
595 |
'hello world' indexOf:(Character space) |
|
596 |
'hello world' indexOf:$A |
|
597 |
" |
|
1 | 598 |
! |
599 |
||
600 |
indexOf:aCharacter startingAt:start |
|
601 |
"return the index of the first occurrence of the argument, aCharacter |
|
602 |
in myself starting at start, anInteger or 0 if not found; |
|
603 |
- reimplemented here for speed" |
|
604 |
||
605 |
%{ /* NOCONTEXT */ |
|
606 |
||
607 |
REGISTER unsigned char *cp; |
|
608 |
REGISTER int index, byteValue; |
|
609 |
#ifdef FAST_STRCHR |
|
610 |
char *strchr(); |
|
370 | 611 |
#else |
612 |
char c; |
|
1 | 613 |
#endif |
614 |
int len; |
|
53 | 615 |
OBJ cls; |
1 | 616 |
|
252 | 617 |
if (__isSmallInteger(start)) { |
159 | 618 |
if (__isCharacter(aCharacter)) { |
1134 | 619 |
byteValue = __intVal(_characterVal(aCharacter)); |
620 |
index = __intVal(start); |
|
159 | 621 |
if (index <= 0) |
622 |
index = 1; |
|
329 | 623 |
if ((cls = __qClass(self)) != String) |
1134 | 624 |
index += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
625 |
len = __stringSize(self); |
|
159 | 626 |
if (index <= len) { |
1134 | 627 |
cp = __stringVal(self) + index - 1; |
1 | 628 |
#ifdef FAST_STRCHR |
159 | 629 |
cp = (unsigned char *) strchr(cp, byteValue); |
630 |
if (cp) { |
|
1134 | 631 |
RETURN ( __MKSMALLINT(cp - __stringVal(self) + 1) ); |
159 | 632 |
} |
1 | 633 |
#else |
370 | 634 |
while (c = *cp++) { |
635 |
if (c == byteValue) { |
|
1134 | 636 |
RETURN ( __MKSMALLINT(index) ); |
159 | 637 |
} |
638 |
index++; |
|
639 |
} |
|
1 | 640 |
#endif |
159 | 641 |
} |
642 |
} |
|
1134 | 643 |
RETURN ( __MKSMALLINT(0) ); |
1 | 644 |
} |
370 | 645 |
%}. |
1 | 646 |
^ super indexOf:aCharacter startingAt:start |
53 | 647 |
|
95 | 648 |
" |
649 |
'hello world' indexOf:$l startingAt:1 |
|
650 |
'hello world' indexOf:$l startingAt:5 |
|
651 |
" |
|
1 | 652 |
! |
653 |
||
516
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
654 |
indexOfAny:aCollectionOfCharacters startingAt:start |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
655 |
"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
|
656 |
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
|
657 |
- 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
|
658 |
|
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
659 |
%{ /* NOCONTEXT */ |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
660 |
|
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
661 |
REGISTER unsigned char *ccp; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
662 |
REGISTER unsigned char *cp; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
663 |
REGISTER int index; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
664 |
char *matchP; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
665 |
char c; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
666 |
int len; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
667 |
OBJ cls; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
668 |
|
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
669 |
if (__isSmallInteger(start) |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
670 |
&& __isString(aCollectionOfCharacters)) { |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
671 |
matchP = __stringVal(aCollectionOfCharacters); |
1134 | 672 |
index = __intVal(start); |
516
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
673 |
if (index <= 0) |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
674 |
index = 1; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
675 |
if ((cls = __qClass(self)) != String) |
1134 | 676 |
index += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
677 |
len = __stringSize(self); |
|
516
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
678 |
if (index <= len) { |
1134 | 679 |
cp = __stringVal(self) + index - 1; |
516
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
680 |
while (c = *cp++) { |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
681 |
for (ccp = matchP; *ccp ; ccp++) { |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
682 |
if (*ccp == c) { |
1134 | 683 |
RETURN ( __MKSMALLINT(index) ); |
516
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
684 |
} |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
685 |
} |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
686 |
index++; |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
687 |
} |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
688 |
} |
1134 | 689 |
RETURN ( __MKSMALLINT(0) ); |
516
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
690 |
} |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
691 |
%}. |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
692 |
"/ |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
693 |
"/ 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
|
694 |
"/ |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
695 |
^ super indexOfAny:aCollectionOfCharacters startingAt:start |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
696 |
|
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
697 |
" |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
698 |
'hello world' indexOfAny:'eoa' startingAt:1 |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
699 |
'hello world' indexOfAny:'eoa' startingAt:6 |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
700 |
'hello world' indexOfAny:'AOE' startingAt:1 |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
701 |
" |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
702 |
! |
17a170eb1112
speedup indexOfAny:startingAt: if arg is a string
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
703 |
|
1 | 704 |
indexOfSeparatorStartingAt:start |
705 |
"return the index of the next separator character" |
|
706 |
||
707 |
%{ /* NOCONTEXT */ |
|
708 |
||
709 |
REGISTER unsigned char *cp; |
|
710 |
REGISTER char c; |
|
711 |
int len, index; |
|
53 | 712 |
OBJ cls; |
1 | 713 |
|
1134 | 714 |
index = __intVal(start); |
1 | 715 |
if (index <= 0) { |
159 | 716 |
index = 1; |
1 | 717 |
} |
329 | 718 |
if ((cls = __qClass(self)) != String) |
1134 | 719 |
index += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
720 |
len = __stringSize(self); |
|
1 | 721 |
if (index > len) { |
1134 | 722 |
RETURN ( __MKSMALLINT(0) ); |
1 | 723 |
} |
1134 | 724 |
cp = __stringVal(self) + index - 1; |
1 | 725 |
while (c = *cp++) { |
329 | 726 |
#ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
159 | 727 |
if (c <= ' ') |
1 | 728 |
#endif |
159 | 729 |
if ((c == ' ') || (c == '\t') || (c == '\n') |
730 |
|| (c == '\r') || (c == '\f')) { |
|
1134 | 731 |
RETURN ( __MKSMALLINT(cp - __stringVal(self)) ); |
159 | 732 |
} |
1 | 733 |
} |
734 |
%} |
|
735 |
. |
|
736 |
^ 0 |
|
53 | 737 |
|
95 | 738 |
" |
739 |
'hello world' indexOfSeparatorStartingAt:3 |
|
740 |
'hello world' indexOfSeparatorStartingAt:7 |
|
741 |
" |
|
608 | 742 |
! |
743 |
||
744 |
occurrencesOf:aCharacter |
|
745 |
"count the occurrences of the argument, aCharacter in myself |
|
746 |
- reimplemented here for speed" |
|
747 |
||
748 |
%{ /* NOCONTEXT */ |
|
749 |
||
750 |
REGISTER unsigned char *cp; |
|
751 |
REGISTER int byteValue; |
|
752 |
REGISTER int count; |
|
753 |
OBJ cls; |
|
754 |
||
755 |
if (__isCharacter(aCharacter)) { |
|
756 |
count = 0; |
|
1134 | 757 |
byteValue = __intVal(_characterVal(aCharacter)); |
758 |
cp = __stringVal(self); |
|
608 | 759 |
if ((cls = __qClass(self)) != String) |
1134 | 760 |
cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
608 | 761 |
while (*cp) { |
762 |
if (*cp++ == byteValue) count++; |
|
763 |
} |
|
1134 | 764 |
RETURN ( __MKSMALLINT(count) ); |
608 | 765 |
} |
766 |
%}. |
|
767 |
^ 0 |
|
768 |
||
769 |
" |
|
770 |
'hello world' occurrencesOf:$a |
|
771 |
'hello world' occurrencesOf:$w |
|
772 |
'hello world' occurrencesOf:$l |
|
773 |
'hello world' occurrencesOf:$x |
|
774 |
'hello world' occurrencesOf:1 |
|
775 |
" |
|
53 | 776 |
! ! |
777 |
||
608 | 778 |
!String methodsFor:'comparing'! |
2 | 779 |
|
608 | 780 |
= aString |
781 |
"Compare the receiver with the argument and return true if the |
|
782 |
receiver is equal to the argument. Otherwise return false. |
|
783 |
This compare is case-sensitive (i.e. 'Foo' is NOT = 'foo'). |
|
784 |
Use sameAs: to compare with case ignored." |
|
1 | 785 |
|
375 | 786 |
%{ /* NOCONTEXT */ |
787 |
||
608 | 788 |
int l1, l2; |
789 |
REGISTER OBJ s = aString; |
|
790 |
char *cp1, *cp2; |
|
791 |
OBJ cls; |
|
792 |
||
793 |
if (s == self) { |
|
794 |
RETURN ( true ); |
|
795 |
} |
|
796 |
if (! __isNonNilObject(s)) { |
|
797 |
RETURN ( false ); |
|
798 |
} |
|
1 | 799 |
|
608 | 800 |
if (((cls = __qClass(s)) == String) || (cls == Symbol) || (cls == __qClass(self))) { |
1134 | 801 |
cp1 = (char *) __stringVal(self); |
802 |
l1 = __stringSize(self); |
|
608 | 803 |
/* |
804 |
* care for instances of subclasses ... |
|
805 |
*/ |
|
806 |
if (__qClass(self) != String) { |
|
1134 | 807 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
608 | 808 |
|
809 |
cp1 += n; |
|
810 |
l1 -= n; |
|
379 | 811 |
} |
375 | 812 |
|
1134 | 813 |
cp2 = (char *) __stringVal(s); |
814 |
l2 = __stringSize(s); |
|
608 | 815 |
/* |
816 |
* care for instances of subclasses ... |
|
817 |
*/ |
|
818 |
if (cls != String) { |
|
1134 | 819 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
608 | 820 |
|
821 |
cp2 += n; |
|
822 |
l2 -= n; |
|
375 | 823 |
} |
608 | 824 |
|
825 |
if (l1 != l2) { |
|
826 |
RETURN ( false ); |
|
827 |
} |
|
828 |
RETURN ( (strncmp(cp1, cp2, l1) == 0) ? true : false ); |
|
1 | 829 |
} |
608 | 830 |
%} |
831 |
. |
|
832 |
^ super = aString |
|
1 | 833 |
|
95 | 834 |
" |
608 | 835 |
'foo' = 'Foo' |
836 |
'foo' sameAs: 'Foo' |
|
95 | 837 |
" |
1 | 838 |
! |
839 |
||
608 | 840 |
> aString |
841 |
"Compare the receiver with the argument and return true if the |
|
842 |
receiver is greater than the argument. Otherwise return false. |
|
843 |
No national variants are honored; use after: for this. |
|
844 |
In contrast to ST-80, case differences are NOT ignored, thus |
|
845 |
'foo' > 'Foo' will return true. |
|
846 |
This may change." |
|
1 | 847 |
|
375 | 848 |
%{ /* NOCONTEXT */ |
849 |
||
608 | 850 |
int len1, len2, cmp; |
851 |
REGISTER OBJ s = aString; |
|
852 |
char *cp1, *cp2; |
|
853 |
OBJ cls; |
|
1 | 854 |
|
608 | 855 |
if (__isNonNilObject(s) |
856 |
&& (((cls = __qClass(s)) == String) || (cls == Symbol) || (cls == __qClass(self)))) { |
|
1134 | 857 |
cp1 = (char *) __stringVal(self); |
858 |
len1 = __stringSize(self); |
|
608 | 859 |
|
860 |
/* |
|
861 |
* care for instances of subclasses ... |
|
862 |
*/ |
|
863 |
if (__qClass(self) != String) { |
|
1134 | 864 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
608 | 865 |
|
866 |
cp1 += n; |
|
867 |
len1 -= n; |
|
379 | 868 |
} |
375 | 869 |
|
1134 | 870 |
cp2 = (char *) __stringVal(s); |
871 |
len2 = __stringSize(s); |
|
608 | 872 |
/* |
873 |
* care for instances of subclasses ... |
|
874 |
*/ |
|
875 |
if (cls != String) { |
|
1134 | 876 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
608 | 877 |
|
878 |
cp2 += n; |
|
879 |
len2 -= n; |
|
375 | 880 |
} |
608 | 881 |
|
882 |
if (len1 <= len2) |
|
883 |
cmp = strncmp(cp1, cp2, len1); |
|
884 |
else |
|
885 |
cmp = strncmp(cp1, cp2, len2); |
|
886 |
||
887 |
if (cmp > 0) { |
|
888 |
RETURN ( true ); |
|
889 |
} |
|
890 |
if ((cmp == 0) && (len1 > len2)) { |
|
891 |
RETURN ( true ); |
|
892 |
} |
|
893 |
RETURN ( false ); |
|
1 | 894 |
} |
370 | 895 |
%}. |
608 | 896 |
^ super > aString |
897 |
! |
|
1 | 898 |
|
608 | 899 |
after:aString |
900 |
"Compare the receiver with the argument and return true if the |
|
901 |
receiver should come after the argument in a sorted list. |
|
902 |
Otherwise return false. |
|
903 |
The comparison is language specific, depending on the value of |
|
904 |
LC_COLLATE, which is initialized from the environment." |
|
1 | 905 |
|
906 |
%{ /* NOCONTEXT */ |
|
907 |
||
608 | 908 |
int cmp; |
909 |
REGISTER OBJ s = aString; |
|
910 |
char *cp1, *cp2; |
|
53 | 911 |
OBJ cls; |
1 | 912 |
|
608 | 913 |
if (__isNonNilObject(s) |
914 |
&& (((cls = __qClass(s)) == String) || (cls == Symbol) || (cls == __qClass(self)))) { |
|
1134 | 915 |
cp1 = (char *) __stringVal(self); |
608 | 916 |
|
917 |
/* |
|
918 |
* care for instances of subclasses ... |
|
919 |
*/ |
|
920 |
if (__qClass(self) != String) { |
|
1134 | 921 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
608 | 922 |
|
923 |
cp1 += n; |
|
924 |
} |
|
1 | 925 |
|
1134 | 926 |
cp2 = (char *) __stringVal(s); |
608 | 927 |
/* |
928 |
* care for instances of subclasses ... |
|
929 |
*/ |
|
930 |
if (cls != String) { |
|
1134 | 931 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
608 | 932 |
|
933 |
cp2 += n; |
|
934 |
} |
|
935 |
||
936 |
#ifdef HAS_STRCOLL |
|
937 |
cmp = strcoll(cp1, cp2); |
|
938 |
#else |
|
939 |
cmp = strcmp(cp1, cp2); |
|
940 |
#endif |
|
941 |
||
942 |
if (cmp > 0) { |
|
943 |
RETURN ( true ); |
|
944 |
} |
|
945 |
RETURN ( false ); |
|
370 | 946 |
} |
329 | 947 |
%}. |
608 | 948 |
" |
949 |
currently, this operation is only defined for strings, symbols |
|
950 |
and subclasses. |
|
951 |
" |
|
952 |
self primitiveFailed |
|
1 | 953 |
! |
954 |
||
608 | 955 |
~= aString |
956 |
"Compare the receiver with the argument and return true if the |
|
957 |
receiver is not equal to the argument. Otherwise return false. |
|
958 |
This compare is case-sensitive (i.e. 'Foo' is NOT = 'foo')" |
|
1 | 959 |
|
608 | 960 |
%{ /* NOCONTEXT */ |
961 |
||
1 | 962 |
int l1, l2; |
608 | 963 |
REGISTER OBJ s = aString; |
964 |
char *cp1, *cp2; |
|
965 |
OBJ cls; |
|
1 | 966 |
|
608 | 967 |
if (s == self) { |
968 |
RETURN ( false ); |
|
969 |
} |
|
970 |
if (! __isNonNilObject(s)) { |
|
971 |
RETURN ( true ); |
|
972 |
} |
|
973 |
if (((cls = __qClass(s)) == String) || (cls == Symbol) || (cls == __qClass(self))) { |
|
1134 | 974 |
cp1 = (char *) __stringVal(self); |
975 |
l1 = __stringSize(self); |
|
608 | 976 |
/* |
977 |
* care for instances of subclasses ... |
|
978 |
*/ |
|
979 |
if (__qClass(self) != String) { |
|
1134 | 980 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
1 | 981 |
|
608 | 982 |
cp1 += n; |
983 |
l1 -= n; |
|
984 |
} |
|
985 |
||
1134 | 986 |
cp2 = (char *) __stringVal(s); |
987 |
l2 = __stringSize(s); |
|
608 | 988 |
/* |
989 |
* care for instances of subclasses ... |
|
990 |
*/ |
|
991 |
if (cls != String) { |
|
1134 | 992 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
608 | 993 |
|
994 |
cp2 += n; |
|
995 |
l2 -= n; |
|
159 | 996 |
} |
62 | 997 |
|
608 | 998 |
if (l1 != l2) { |
999 |
RETURN ( true ); |
|
1000 |
} |
|
1001 |
RETURN ( (strncmp(cp1, cp2, l1) == 0) ? false : true ); |
|
1002 |
} |
|
1003 |
%} |
|
1004 |
. |
|
1005 |
^ super ~= aString |
|
1006 |
! ! |
|
1007 |
||
1008 |
!String methodsFor:'converting'! |
|
1009 |
||
1010 |
asSymbol |
|
1011 |
"return a unique symbol with the name taken from myself. |
|
1012 |
The receiver must be a String, subclass instances are (currently) |
|
1013 |
not allowed." |
|
1014 |
%{ |
|
1015 |
OBJ newSymbol; |
|
62 | 1016 |
|
608 | 1017 |
if (__qClass(self) == String) { |
1134 | 1018 |
newSymbol = __MKSYMBOL(__stringVal(self), (OBJ *)0, __context); |
608 | 1019 |
if (newSymbol) { |
1020 |
RETURN ( newSymbol); |
|
1021 |
} |
|
1022 |
} |
|
1023 |
%}. |
|
1024 |
self class ~~ String ifTrue:[ |
|
1025 |
"/ |
|
1026 |
"/ it is only allowed to as for string symbols - no subclass instances |
|
1027 |
"/ |
|
1028 |
^ self primitiveFailed |
|
1029 |
]. |
|
1030 |
^ ObjectMemory allocationFailureSignal raise. |
|
1031 |
! |
|
1032 |
||
1033 |
asSymbolIfInterned |
|
1034 |
"if a symbol with the receivers characters is already known, return it. |
|
1035 |
Otherwise, return nil. This can be used to query for an existing |
|
1036 |
symbol and is the same as |
|
1037 |
self knownAsSymbol ifTrue:[self asSymbol] ifFalse:[nil] |
|
1038 |
but slightly faster, since the symbol lookup operation is only |
|
1039 |
performed once. |
|
1040 |
The receiver must be a String, subclass instances are (currently) |
|
1041 |
not allowed." |
|
1 | 1042 |
|
608 | 1043 |
%{ /* NOCONTEXT */ |
1044 |
if (__qClass(self) == String) { |
|
1134 | 1045 |
RETURN ( __SYMBOL_OR_NIL(__stringVal(self))); |
608 | 1046 |
} |
1047 |
%}. |
|
1048 |
self primitiveFailed |
|
1049 |
! |
|
1050 |
||
1051 |
literalArrayEncoding |
|
1052 |
"encode myself as an array literal." |
|
1053 |
||
1054 |
^ self |
|
1055 |
||
1056 |
"Modified: 1.9.1995 / 02:25:45 / claus" |
|
1057 |
! |
|
1058 |
||
1059 |
withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1060 |
"return a string with the characters of the receiver where all tabulator characters |
608 | 1061 |
are expanded into spaces (assuming 8-col tabs). |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1062 |
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
|
1063 |
otherwise a new string is returned. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1064 |
This does handle multiline strings. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1065 |
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
|
1066 |
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
|
1067 |
|
993e6ffdbf51
removed external decls for VM functions (should look for more)
Claus Gittinger <cg@exept.de>
parents:
1134
diff
changeset
|
1068 |
%{ /* STACK:700 */ |
608 | 1069 |
char buffer[80*8 + 10]; |
1070 |
char *srcP, *dstP, *cp0; |
|
1071 |
int idx, sz; |
|
1072 |
int any = 0; |
|
1073 |
OBJ newString; |
|
1074 |
char c; |
|
1075 |
||
1076 |
if (__qClass(self) == String) { |
|
1077 |
/* |
|
1078 |
* for small strings (< 80), do it without a prescan ... |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1079 |
* the buffer is large enough to even convert a |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1080 |
* receiver consisting fully of tabs. |
608 | 1081 |
*/ |
1082 |
if (__stringSize(self) < 80) { |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1083 |
idx = 1; |
608 | 1084 |
for (srcP = __stringVal(self), dstP = buffer; (c = *srcP); srcP++) { |
1085 |
if (c == '\t') { |
|
1086 |
any = 1; |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1087 |
while (idx % 8) { |
608 | 1088 |
idx++; |
1089 |
*dstP++ = ' '; |
|
159 | 1090 |
} |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1091 |
idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1092 |
*dstP++ = ' '; |
608 | 1093 |
} else { |
1094 |
*dstP++ = c; |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1095 |
idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1096 |
if (c == '\n') { |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1097 |
idx = 1; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1098 |
} |
608 | 1099 |
} |
1100 |
} |
|
1101 |
if (! any) RETURN(self); |
|
1102 |
*dstP = '\0'; |
|
1103 |
RETURN (__MKSTRING_L(buffer, (dstP-buffer))); |
|
1104 |
} |
|
1105 |
/* |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1106 |
* for large strings, we compute the new size, allocate a new string |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1107 |
* and expand it. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1108 |
* |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1109 |
* first, scan for size ... |
608 | 1110 |
*/ |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1111 |
idx = 1; |
608 | 1112 |
for (srcP = __stringVal(self), sz = 0; (c = *srcP); srcP++) { |
1113 |
if (c == '\t') { |
|
1114 |
any = 1; |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1115 |
while (idx % 8) { |
608 | 1116 |
idx++; |
1117 |
sz++; |
|
1118 |
} |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1119 |
idx++; sz ++; |
608 | 1120 |
} else { |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1121 |
sz++; idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1122 |
if (c == '\n') { |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1123 |
idx = 1; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1124 |
} |
159 | 1125 |
} |
1126 |
} |
|
608 | 1127 |
if (! any) RETURN(self); |
1 | 1128 |
|
608 | 1129 |
/* |
1130 |
* get the string |
|
1131 |
*/ |
|
1132 |
sz = OHDR_SIZE + sz + 1; |
|
835 | 1133 |
__qNew(newString, sz, __context); |
608 | 1134 |
if (newString != nil) { |
1134 | 1135 |
__InstPtr(newString)->o_class = String; |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1136 |
|
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1137 |
/* |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1138 |
* expand |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1139 |
*/ |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1140 |
idx = 1; |
608 | 1141 |
for (srcP = __stringVal(self), dstP = cp0 = __stringVal(newString); (c = *srcP); srcP++) { |
1142 |
if (c == '\t') { |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1143 |
while (idx % 8) { |
608 | 1144 |
idx++; |
1145 |
*dstP++ = ' '; |
|
1146 |
} |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1147 |
idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1148 |
*dstP++ = ' '; |
608 | 1149 |
} else { |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1150 |
*dstP++ = c; idx++; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1151 |
if (c == '\n') { |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1152 |
idx = 1; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
633
diff
changeset
|
1153 |
} |
608 | 1154 |
} |
1155 |
} |
|
1156 |
*dstP++ = '\0'; |
|
1157 |
RETURN (newString); |
|
1158 |
} |
|
1159 |
} |
|
1160 |
%}. |
|
1161 |
^ super withTabsExpanded |
|
1 | 1162 |
! ! |
1163 |
||
1164 |
!String methodsFor:'copying'! |
|
1165 |
||
1166 |
, aString |
|
2 | 1167 |
"return the concatenation of myself and the argument, aString as |
1168 |
a String. |
|
1 | 1169 |
- reimplemented here for speed" |
1170 |
||
1171 |
%{ |
|
95 | 1172 |
int l1, l2, sz; |
1 | 1173 |
char *cp1, *cp2; |
1174 |
REGISTER unsigned char *dstp; |
|
1175 |
REGISTER OBJ s = aString; |
|
329 | 1176 |
REGISTER OBJ _string = String; |
13 | 1177 |
OBJ myClass, argClass, newString; |
1 | 1178 |
|
329 | 1179 |
if (__isNonNilObject(s)) { |
1180 |
myClass = __qClass(self); |
|
1181 |
argClass = __qClass(s); |
|
95 | 1182 |
/* |
368 | 1183 |
* can do it here if |
95 | 1184 |
* either same class or both Strings/Symbols: |
1185 |
*/ |
|
159 | 1186 |
if ((myClass == argClass) |
329 | 1187 |
|| (((myClass == _string) || (myClass == Symbol)) |
1188 |
&& ((argClass == _string) || (argClass == Symbol)))) { |
|
1134 | 1189 |
cp1 = (char *) __stringVal(self); |
1190 |
l1 = __stringSize(self); |
|
329 | 1191 |
if (myClass != _string) { |
1134 | 1192 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
1 | 1193 |
|
159 | 1194 |
cp1 += n; |
1195 |
l1 -= n; |
|
1196 |
} |
|
1 | 1197 |
|
1134 | 1198 |
cp2 = (char *) __stringVal(s); |
1199 |
l2 = __stringSize(s); |
|
329 | 1200 |
if (argClass != _string) { |
1134 | 1201 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(s))->c_ninstvars)); |
1 | 1202 |
|
159 | 1203 |
cp2 += n; |
1204 |
l2 -= n; |
|
1205 |
} |
|
1 | 1206 |
|
95 | 1207 |
sz = OHDR_SIZE + l1 + l2 + 1; |
835 | 1208 |
__qNew(newString, sz, __context); |
159 | 1209 |
if (newString != nil) { |
1134 | 1210 |
__InstPtr(newString)->o_class = String; |
1211 |
dstp = __stringVal(newString); |
|
95 | 1212 |
/* |
1213 |
* refetch in case of a GC |
|
1214 |
*/ |
|
1134 | 1215 |
cp1 = (char *) __stringVal(self); |
1216 |
cp2 = (char *) __stringVal(aString); |
|
1 | 1217 |
#ifdef FAST_MEMCPY |
159 | 1218 |
bcopy(cp1, dstp, l1); |
1219 |
bcopy(cp2, dstp + l1, l2+1); |
|
1 | 1220 |
#else |
1221 |
# ifdef FAST_STRCPY |
|
159 | 1222 |
strcpy(dstp, cp1); |
1223 |
strcpy(dstp + l1, cp2); |
|
1 | 1224 |
# else |
159 | 1225 |
while ((*dstp++ = *cp1++) != '\0') ; |
1226 |
dstp--; |
|
1227 |
while ((*dstp++ = *cp2++) != '\0') ; |
|
1 | 1228 |
# endif |
1229 |
#endif |
|
159 | 1230 |
RETURN ( newString ); |
1231 |
} |
|
1232 |
} |
|
1 | 1233 |
} |
519
0ab6612e060b
expand tabs slightly rewritten (no real change)
Claus Gittinger <cg@exept.de>
parents:
516
diff
changeset
|
1234 |
%}. |
1 | 1235 |
^ super , aString |
1236 |
! |
|
1237 |
||
1238 |
concatenate:string1 and:string2 |
|
1239 |
"return the concatenation of myself and the arguments, string1 and string2. |
|
1240 |
This is equivalent to self , string1 , string2 |
|
1241 |
- generated by compiler when such a construct is detected" |
|
1242 |
||
1243 |
|newString| |
|
1244 |
%{ |
|
95 | 1245 |
int len1, len2, len3, sz; |
1 | 1246 |
#if !defined(FAST_MEMCPY) && !defined(FAST_STRCPY) |
1247 |
REGISTER unsigned char *srcp; |
|
1248 |
#endif |
|
1249 |
REGISTER unsigned char *dstp; |
|
1250 |
||
53 | 1251 |
if ((__isString(self) || __isSymbol(self)) |
1252 |
&& (__isString(string1) || __isSymbol(string1)) |
|
1253 |
&& (__isString(string2) || __isSymbol(string2))) { |
|
1134 | 1254 |
len1 = __stringSize(self); |
1255 |
len2 = __stringSize(string1); |
|
1256 |
len3 = __stringSize(string2); |
|
95 | 1257 |
sz = OHDR_SIZE + len1 + len2 + len3 + 1; |
835 | 1258 |
__qNew(newString, sz, __context); |
159 | 1259 |
if (newString != nil) { |
1134 | 1260 |
__InstPtr(newString)->o_class = String; |
1261 |
dstp = __stringVal(newString); |
|
1 | 1262 |
#ifdef FAST_MEMCPY |
1134 | 1263 |
bcopy(__stringVal(self), dstp, len1); |
1264 |
bcopy(__stringVal(string1), dstp + len1, len2); |
|
1265 |
bcopy(__stringVal(string2), dstp + len1 + len2, len3+1); |
|
1 | 1266 |
#else |
1267 |
# ifdef FAST_STRCPY |
|
1134 | 1268 |
strcpy(dstp, __stringVal(self)); |
1269 |
strcpy(dstp + len1, __stringVal(string1)); |
|
1270 |
strcpy(dstp + len1 + len2, __stringVal(string2)); |
|
1 | 1271 |
# else |
1134 | 1272 |
srcp = __stringVal(self); |
159 | 1273 |
while ((*dstp++ = *srcp++) != '\0') ; |
1274 |
dstp--; |
|
1134 | 1275 |
srcp = __stringVal(string1); |
159 | 1276 |
while ((*dstp++ = *srcp++) != '\0') ; |
1277 |
dstp--; |
|
1134 | 1278 |
srcp = __stringVal(string2); |
159 | 1279 |
while ((*dstp++ = *srcp++) != '\0') ; |
1 | 1280 |
# endif |
1281 |
#endif |
|
159 | 1282 |
RETURN ( newString ); |
1283 |
} |
|
1 | 1284 |
} |
519
0ab6612e060b
expand tabs slightly rewritten (no real change)
Claus Gittinger <cg@exept.de>
parents:
516
diff
changeset
|
1285 |
%}. |
1 | 1286 |
^ super , string1 , string2 |
1287 |
! |
|
1288 |
||
1289 |
concatenate:string1 and:string2 and:string3 |
|
1290 |
"return the concatenation of myself and the string arguments. |
|
1291 |
This is equivalent to self , string1 , string2 , string3 |
|
1292 |
- generated by compiler when such a construct is detected" |
|
1293 |
||
1294 |
|newString| |
|
1295 |
%{ |
|
95 | 1296 |
int len1, len2, len3, len4, sz; |
1 | 1297 |
#if !defined(FAST_MEMCPY) && !defined(FAST_STRCPY) |
1298 |
REGISTER unsigned char *srcp; |
|
1299 |
#endif |
|
1300 |
REGISTER unsigned char *dstp; |
|
1301 |
||
53 | 1302 |
if ((__isString(self) || __isSymbol(self)) |
1303 |
&& (__isString(string1) || __isSymbol(string1)) |
|
1304 |
&& (__isString(string2) || __isSymbol(string2)) |
|
1305 |
&& (__isString(string3) || __isSymbol(string3))) { |
|
1134 | 1306 |
len1 = __stringSize(self); |
1307 |
len2 = __stringSize(string1); |
|
1308 |
len3 = __stringSize(string2); |
|
1309 |
len4 = __stringSize(string3); |
|
95 | 1310 |
sz = OHDR_SIZE + len1 + len2 + len3 + len4 + 1; |
835 | 1311 |
__qNew(newString, sz, __context); |
159 | 1312 |
if (newString != nil) { |
1134 | 1313 |
__InstPtr(newString)->o_class = String; |
1314 |
dstp = __stringVal(newString); |
|
1 | 1315 |
#ifdef FAST_MEMCPY |
1134 | 1316 |
bcopy(__stringVal(self), dstp, len1); |
1317 |
bcopy(__stringVal(string1), dstp + len1, len2); |
|
1318 |
bcopy(__stringVal(string2), dstp + len1 + len2, len3); |
|
1319 |
bcopy(__stringVal(string3), dstp + len1 + len2 + len3, len4+1); |
|
1 | 1320 |
#else |
1321 |
# ifdef FAST_STRCPY |
|
1134 | 1322 |
strcpy(dstp, __stringVal(self)); |
1323 |
strcpy(dstp + len1, __stringVal(string1)); |
|
1324 |
strcpy(dstp + len1 + len2, __stringVal(string2)); |
|
1325 |
strcpy(dstp + len1 + len2 + len3, __stringVal(string3)); |
|
1 | 1326 |
# else |
1134 | 1327 |
srcp = __stringVal(self); |
159 | 1328 |
while ((*dstp++ = *srcp++) != '\0') ; |
1329 |
dstp--; |
|
1134 | 1330 |
srcp = __stringVal(string1); |
159 | 1331 |
while ((*dstp++ = *srcp++) != '\0') ; |
1332 |
dstp--; |
|
1134 | 1333 |
srcp = __stringVal(string2); |
159 | 1334 |
while ((*dstp++ = *srcp++) != '\0') ; |
1335 |
dstp--; |
|
1134 | 1336 |
srcp = __stringVal(string3); |
159 | 1337 |
while ((*dstp++ = *srcp++) != '\0') ; |
1 | 1338 |
# endif |
1339 |
#endif |
|
159 | 1340 |
RETURN ( newString ); |
1341 |
} |
|
1 | 1342 |
} |
519
0ab6612e060b
expand tabs slightly rewritten (no real change)
Claus Gittinger <cg@exept.de>
parents:
516
diff
changeset
|
1343 |
%}. |
1 | 1344 |
^ super , string1 , string2 , string3 |
1345 |
! |
|
1346 |
||
608 | 1347 |
copyFrom:start |
1348 |
"return the substring from start, anInteger to the end. |
|
1349 |
This method will always return a string, even if the receiver |
|
1350 |
is a subclass-instance. This might change if there is a need. |
|
1351 |
- reimplemented here for speed" |
|
1 | 1352 |
|
13 | 1353 |
%{ /* NOCONTEXT */ |
1 | 1354 |
|
608 | 1355 |
#if !defined(FAST_MEMCPY) |
1356 |
REGISTER unsigned char *srcp; |
|
1357 |
#endif |
|
1 | 1358 |
REGISTER unsigned char *dstp; |
608 | 1359 |
REGISTER int count; |
1360 |
int len, index1, sz; |
|
1361 |
OBJ newString; |
|
1362 |
||
1363 |
if (__isSmallInteger(start)) { |
|
1134 | 1364 |
len = __stringSize(self); |
1365 |
index1 = __intVal(start); |
|
608 | 1366 |
if (index1 > 0) { |
1367 |
if (__qClass(self) != String) { |
|
1134 | 1368 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
1 | 1369 |
|
608 | 1370 |
index1 += n; |
1371 |
} |
|
1372 |
if (index1 <= len) { |
|
1373 |
count = len - index1 + 1; |
|
834 | 1374 |
__PROTECT_CONTEXT__ |
608 | 1375 |
sz = OHDR_SIZE + count + 1; |
835 | 1376 |
__qNew(newString, sz, SENDER); |
834 | 1377 |
__UNPROTECT_CONTEXT__ |
608 | 1378 |
if (newString != nil) { |
1134 | 1379 |
__InstPtr(newString)->o_class = String; |
1380 |
dstp = __stringVal(newString); |
|
1 | 1381 |
#ifdef FAST_MEMCPY |
1134 | 1382 |
bcopy(__stringVal(self) + index1 - 1, dstp, count); |
608 | 1383 |
dstp[count] = '\0'; |
1 | 1384 |
#else |
1385 |
# ifdef FAST_STRCPY |
|
1134 | 1386 |
strncpy(dstp, __stringVal(self) + index1 - 1, count); |
608 | 1387 |
dstp[count] = '\0'; |
1 | 1388 |
# else |
1134 | 1389 |
srcp = __stringVal(self) + index1 - 1; |
608 | 1390 |
while (count--) { |
1391 |
*dstp++ = *srcp++; |
|
1392 |
} |
|
1393 |
*dstp = '\0'; |
|
1 | 1394 |
# endif |
1395 |
#endif |
|
608 | 1396 |
RETURN ( newString ); |
1397 |
} |
|
1398 |
} |
|
159 | 1399 |
} |
13 | 1400 |
} |
51 | 1401 |
%}. |
608 | 1402 |
"fall back in case of non-integer index or out-of-bound index; |
1403 |
will eventually lead to an out-of-bound signal raise" |
|
51 | 1404 |
|
608 | 1405 |
^ super copyFrom:start |
1 | 1406 |
! |
1407 |
||
1408 |
copyFrom:start to:stop |
|
1409 |
"return the substring starting at index start, anInteger and ending |
|
53 | 1410 |
at stop, anInteger. This method will always return a string, even |
1411 |
if the receiver is a subclass-instance. This might change if there is a need. |
|
1 | 1412 |
- reimplemented here for speed" |
1413 |
||
13 | 1414 |
%{ /* NOCONTEXT */ |
1415 |
||
1 | 1416 |
#if !defined(FAST_MEMCPY) |
1417 |
REGISTER unsigned char *srcp; |
|
1418 |
#endif |
|
1419 |
REGISTER unsigned char *dstp; |
|
1420 |
REGISTER int count; |
|
95 | 1421 |
int len, sz, index1, index2; |
13 | 1422 |
OBJ newString; |
1 | 1423 |
|
252 | 1424 |
if (__bothSmallInteger(start, stop)) { |
1134 | 1425 |
len = __stringSize(self); |
1426 |
index1 = __intVal(start); |
|
1427 |
index2 = __intVal(stop); |
|
1 | 1428 |
|
159 | 1429 |
if ((index1 <= index2) && (index1 > 0)) { |
329 | 1430 |
if (__qClass(self) != String) { |
1134 | 1431 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
1 | 1432 |
|
159 | 1433 |
index1 += n; |
1434 |
index2 += n; |
|
1435 |
} |
|
1436 |
if (index2 <= len) { |
|
1437 |
count = index2 - index1 + 1; |
|
834 | 1438 |
__PROTECT_CONTEXT__ |
95 | 1439 |
sz = OHDR_SIZE + count + 1; |
835 | 1440 |
__qNew(newString, sz, SENDER); |
834 | 1441 |
__UNPROTECT_CONTEXT__ |
159 | 1442 |
if (newString != nil) { |
1134 | 1443 |
__InstPtr(newString)->o_class = String; |
1444 |
dstp = __stringVal(newString); |
|
1 | 1445 |
#ifdef FAST_MEMCPY |
1134 | 1446 |
bcopy(__stringVal(self) + index1 - 1, dstp, count); |
159 | 1447 |
dstp[count] = '\0'; |
1 | 1448 |
#else |
209 | 1449 |
# ifdef FAST_STRCPY |
1134 | 1450 |
strncpy(dstp, __stringVal(self) + index1 - 1, count); |
209 | 1451 |
dstp[count] = '\0'; |
1452 |
# else |
|
1134 | 1453 |
srcp = __stringVal(self) + index1 - 1; |
159 | 1454 |
while (count--) { |
1455 |
*dstp++ = *srcp++; |
|
1456 |
} |
|
1457 |
*dstp = '\0'; |
|
209 | 1458 |
# endif |
13 | 1459 |
#endif |
159 | 1460 |
RETURN ( newString ); |
1461 |
} |
|
1462 |
} |
|
1463 |
} |
|
375 | 1464 |
/* |
1465 |
* allow empty copy |
|
1466 |
*/ |
|
1467 |
if (index1 > index2) { |
|
834 | 1468 |
__PROTECT_CONTEXT__ |
835 | 1469 |
__qNew(newString, OHDR_SIZE+1, SENDER); |
834 | 1470 |
__UNPROTECT_CONTEXT__ |
375 | 1471 |
if (newString != nil) { |
1134 | 1472 |
__InstPtr(newString)->o_class = String; |
375 | 1473 |
__stringVal(newString)[0] = '\0'; |
1474 |
RETURN ( newString ); |
|
1475 |
} |
|
1476 |
} |
|
1 | 1477 |
} |
51 | 1478 |
%}. |
1479 |
"fall back in case of non-integer index or out-of-bound index; |
|
1480 |
will eventually lead to an out-of-bound signal raise" |
|
1481 |
||
1 | 1482 |
^ super copyFrom:start to:stop |
1483 |
! |
|
1484 |
||
608 | 1485 |
copyWith:aCharacter |
1486 |
"return a new string containing the receivers characters |
|
1487 |
and the single new character, aCharacter. |
|
1488 |
This is different from concatentation, which expects another string |
|
1489 |
as argument, but equivalent to copy-and-addLast. |
|
1490 |
Reimplemented here for more speed" |
|
1491 |
||
1492 |
%{ /* NOCONTEXT */ |
|
1493 |
||
1494 |
int sz; |
|
1495 |
REGISTER unsigned char *dstp; |
|
1496 |
int offs; |
|
1497 |
OBJ cls, newString; |
|
1498 |
||
1499 |
if (__isCharacter(aCharacter)) { |
|
1500 |
sz = __qSize(self) + 1; |
|
1501 |
if ((cls = __qClass(self)) != String) { |
|
1134 | 1502 |
offs = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
608 | 1503 |
sz -= offs; |
1504 |
} else |
|
1505 |
offs = 0; |
|
1506 |
||
834 | 1507 |
__PROTECT_CONTEXT__ |
835 | 1508 |
__qNew(newString, sz, SENDER); |
834 | 1509 |
__UNPROTECT_CONTEXT__ |
608 | 1510 |
if (newString) { |
1134 | 1511 |
__InstPtr(newString)->o_class = String; |
1512 |
dstp = __stringVal(newString); |
|
608 | 1513 |
#ifdef FAST_MEMCPY |
1514 |
sz = sz - OHDR_SIZE - 1 - 1; |
|
1134 | 1515 |
bcopy(__stringVal(self) + offs, dstp, sz); |
608 | 1516 |
dstp += sz; |
1517 |
#else |
|
1518 |
# ifdef FAST_STRCPY |
|
1134 | 1519 |
strcpy(dstp, __stringVal(self) + offs); |
608 | 1520 |
dstp += sz - OHDR_SIZE - 1 - 1; |
1521 |
# else |
|
1522 |
{ |
|
1523 |
REGISTER unsigned char *srcp; |
|
1524 |
||
1134 | 1525 |
srcp = __stringVal(self) + offs; |
608 | 1526 |
while ((*dstp = *srcp++) != '\0') |
1527 |
dstp++; |
|
1528 |
} |
|
1529 |
# endif |
|
1530 |
#endif |
|
1134 | 1531 |
*dstp++ = __intVal(_characterVal(aCharacter)); |
608 | 1532 |
*dstp = '\0'; |
1533 |
RETURN (newString ); |
|
1534 |
} |
|
1535 |
} |
|
1536 |
%}. |
|
1537 |
"fall back in case of non-character arg; |
|
1538 |
will eventually lead to an bad element signal raise" |
|
1539 |
||
1540 |
^ super copyWith:aCharacter |
|
1541 |
! |
|
1542 |
||
1543 |
deepCopy |
|
1544 |
"return a copy of the receiver" |
|
1545 |
||
1546 |
" |
|
1547 |
could be an instance of a subclass which needs deepCopy |
|
1548 |
of its named instvars ... |
|
1549 |
" |
|
1550 |
(self isMemberOf:String) ifTrue:[ |
|
1551 |
^ self copyFrom:1 |
|
1552 |
]. |
|
1553 |
^ super deepCopy |
|
1554 |
! |
|
1555 |
||
1556 |
deepCopyUsing:aDictionary |
|
1557 |
"return a copy of the receiver - reimplemented to be a bit faster" |
|
1558 |
||
1559 |
" |
|
1560 |
could be an instance of a subclass which needs deepCopy |
|
1561 |
of its named instvars ... |
|
1562 |
" |
|
1563 |
(self isMemberOf:String) ifTrue:[ |
|
1564 |
^ self copyFrom:1 |
|
1565 |
]. |
|
1566 |
^ super deepCopy |
|
1567 |
! |
|
1568 |
||
1569 |
shallowCopy |
|
1570 |
"return a copy of the receiver" |
|
1571 |
||
1572 |
(self isMemberOf:String) ifTrue:[ |
|
1573 |
^ self copyFrom:1 |
|
1574 |
]. |
|
1575 |
^ super shallowCopy |
|
1576 |
! |
|
1577 |
||
1578 |
simpleDeepCopy |
|
1579 |
"return a copy of the receiver" |
|
1580 |
||
1581 |
" |
|
1582 |
could be an instance of a subclass which needs deepCopy |
|
1583 |
of its named instvars ... |
|
1584 |
" |
|
1585 |
(self isMemberOf:String) ifTrue:[ |
|
1586 |
^ self copyFrom:1 |
|
1587 |
]. |
|
1588 |
^ super deepCopy |
|
1589 |
! ! |
|
1590 |
||
1591 |
!String methodsFor:'filling and replacing'! |
|
1592 |
||
1593 |
atAllPut:aCharacter |
|
1594 |
"replace all elements with aCharacter |
|
1 | 1595 |
- reimplemented here for speed" |
1596 |
||
13 | 1597 |
%{ /* NOCONTEXT */ |
1598 |
||
608 | 1599 |
#ifndef FAST_MEMSET |
1600 |
REGISTER unsigned char *dst; |
|
1601 |
REGISTER int l; |
|
1 | 1602 |
#endif |
608 | 1603 |
REGISTER int byteValue; |
1 | 1604 |
|
608 | 1605 |
if (__isCharacter(aCharacter) && __isString(self)) { |
1134 | 1606 |
byteValue = __intVal(_characterVal(aCharacter)); |
608 | 1607 |
#ifdef FAST_MEMSET |
1134 | 1608 |
memset(__stringVal(self), byteValue, __qSize(self) - OHDR_SIZE - 1); |
608 | 1609 |
#else |
1134 | 1610 |
dst = __stringVal(self); |
608 | 1611 |
l = __qSize(self) - OHDR_SIZE - 1; |
1612 |
while (l >= 8) { |
|
1613 |
dst[0] = dst[1] = dst[2] = dst[3] = byteValue; |
|
1614 |
dst[4] = dst[5] = dst[6] = dst[7] = byteValue; |
|
1615 |
dst += 8; |
|
1616 |
l -= 8; |
|
1617 |
} |
|
1618 |
while (l--) |
|
1619 |
*dst++ = byteValue; |
|
1620 |
#endif |
|
1621 |
RETURN ( self ); |
|
1622 |
} |
|
1623 |
%} |
|
1624 |
. |
|
1625 |
^ super atAllPut:aCharacter |
|
1 | 1626 |
|
608 | 1627 |
" |
1628 |
(String new:10) atAllPut:$* |
|
1629 |
String new:10 withAll:$* |
|
1630 |
" |
|
1631 |
! |
|
1632 |
||
1633 |
replaceAll:oldCharacter by:newCharacter |
|
1634 |
"replace all oldCharacters by newCharacter in the receiver" |
|
1635 |
||
1636 |
%{ /* NOCONTEXT */ |
|
1637 |
||
1638 |
REGISTER unsigned char *srcp; |
|
1639 |
REGISTER unsigned oldVal, newVal; |
|
1640 |
||
1641 |
if (__isCharacter(oldCharacter) |
|
1642 |
&& __isCharacter(newCharacter) |
|
1643 |
&& __isString(self)) { |
|
1134 | 1644 |
srcp = (unsigned char *)__stringVal(self); |
1645 |
oldVal = __intVal(_characterVal(oldCharacter)); |
|
1646 |
newVal = __intVal(_characterVal(newCharacter)); |
|
608 | 1647 |
while (*srcp) { |
1648 |
if (*srcp == oldVal) |
|
1649 |
*srcp = newVal; |
|
1650 |
srcp++; |
|
159 | 1651 |
} |
608 | 1652 |
RETURN ( self ); |
1 | 1653 |
} |
608 | 1654 |
%} |
1655 |
. |
|
1656 |
^ super replaceAll:oldCharacter by:newCharacter |
|
51 | 1657 |
|
608 | 1658 |
" |
1659 |
'helloWorld' copy replaceAll:$o by:$O |
|
1660 |
" |
|
1661 |
! |
|
1 | 1662 |
|
1663 |
replaceFrom:start to:stop with:aString startingAt:repStart |
|
1664 |
"replace the characters starting at index start, anInteger and ending |
|
1665 |
at stop, anInteger with characters from aString starting at repStart. |
|
1666 |
||
1667 |
- reimplemented here for speed" |
|
1668 |
||
1669 |
%{ /* NOCONTEXT */ |
|
1670 |
||
1671 |
REGISTER unsigned char *srcp, *dstp; |
|
1672 |
REGISTER int count; |
|
1673 |
int len, index1, index2; |
|
1674 |
int repLen, repIndex; |
|
1675 |
||
53 | 1676 |
if ((__isString(aString) || __isSymbol(aString)) |
1677 |
&& __isString(self) |
|
252 | 1678 |
&& __bothSmallInteger(start, stop)) { |
1134 | 1679 |
len = __stringSize(self); |
1680 |
index1 = __intVal(start); |
|
1681 |
index2 = __intVal(stop); |
|
159 | 1682 |
count = index2 - index1 + 1; |
1683 |
if (count <= 0) { |
|
1684 |
RETURN (self); |
|
1685 |
} |
|
1686 |
if ((index2 <= len) && (index1 > 0)) { |
|
1134 | 1687 |
repLen = __stringSize(aString); |
1688 |
repIndex = __intVal(repStart); |
|
159 | 1689 |
if ((repIndex > 0) && ((repIndex + count - 1) <= repLen)) { |
1134 | 1690 |
srcp = __stringVal(aString) + repIndex - 1; |
1691 |
dstp = __stringVal(self) + index1 - 1; |
|
159 | 1692 |
if (aString == self) { |
1693 |
/* take care of overlapping copy */ |
|
1694 |
if (srcp < dstp) { |
|
1695 |
/* must do a reverse copy */ |
|
1696 |
srcp += count; |
|
1697 |
dstp += count; |
|
1698 |
while (count-- > 0) { |
|
1699 |
*--dstp = *--srcp; |
|
1700 |
} |
|
1701 |
RETURN (self); |
|
1702 |
} |
|
1703 |
} |
|
1 | 1704 |
#ifdef FAST_MEMCPY |
159 | 1705 |
bcopy(srcp, dstp, count); |
1 | 1706 |
#else |
159 | 1707 |
while (count-- > 0) { |
1708 |
*dstp++ = *srcp++; |
|
1709 |
} |
|
1 | 1710 |
#endif |
159 | 1711 |
RETURN (self); |
1712 |
} |
|
1713 |
} |
|
1 | 1714 |
} |
1715 |
%} |
|
1716 |
. |
|
1717 |
^ super replaceFrom:start to:stop with:aString startingAt:repStart |
|
1718 |
! |
|
1719 |
||
1720 |
reverse |
|
53 | 1721 |
"in-place reverse the characters of the string." |
1722 |
||
1723 |
"Q: is there a need to redefine it here ?" |
|
1 | 1724 |
|
1725 |
%{ /* NOCONTEXT */ |
|
1726 |
||
1727 |
REGISTER char c; |
|
1728 |
REGISTER unsigned char *hip, *lowp; |
|
1729 |
||
53 | 1730 |
if (__isString(self)) { |
1134 | 1731 |
lowp = __stringVal(self); |
1732 |
hip = lowp + __stringSize(self) - 1; |
|
159 | 1733 |
while (lowp < hip) { |
1734 |
c = *lowp; |
|
1735 |
*lowp = *hip; |
|
1736 |
*hip = c; |
|
1737 |
lowp++; |
|
1738 |
hip--; |
|
1739 |
} |
|
1740 |
RETURN ( self ); |
|
1 | 1741 |
} |
1742 |
%} |
|
1743 |
. |
|
1744 |
^ super reverse |
|
1745 |
! |
|
1746 |
||
1747 |
withoutSeparators |
|
95 | 1748 |
"return a copy of myself without leading and trailing whitespace. |
1749 |
Notice, this is different from String>>withoutSpaces." |
|
1 | 1750 |
|
42 | 1751 |
|startIndex "{ Class: SmallInteger }" |
51 | 1752 |
endIndex "{ Class: SmallInteger }" |
1753 |
sz| |
|
1754 |
||
53 | 1755 |
startIndex := 0. |
51 | 1756 |
%{ |
1757 |
REGISTER unsigned char *cp0; |
|
1758 |
REGISTER unsigned char *cp; |
|
1759 |
REGISTER unsigned char c; |
|
1 | 1760 |
|
51 | 1761 |
/* ignore instances of subclasses ... */ |
329 | 1762 |
if (__qClass(self) == String) { |
1134 | 1763 |
cp = cp0 = __stringVal(self); |
379 | 1764 |
while ((c = *cp) |
329 | 1765 |
#ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
379 | 1766 |
&& (c <= ' ') |
329 | 1767 |
#endif |
379 | 1768 |
&& ((c == ' ') || (c == '\n') || (c == '\t') |
329 | 1769 |
|| (c == '\r') || (c == '\f')) |
1770 |
) { |
|
159 | 1771 |
cp++; |
1772 |
} |
|
1134 | 1773 |
startIndex = __MKSMALLINT(cp - cp0 + 1); |
159 | 1774 |
cp = cp + strlen(cp) - 1; |
1775 |
while ((cp >= cp0) && (*cp == ' ')) cp--; |
|
1776 |
c = *cp; |
|
1777 |
while ((cp >= cp0) && |
|
329 | 1778 |
#ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
1779 |
(c <= ' ') && |
|
1780 |
#endif |
|
159 | 1781 |
((c == ' ') || (c == '\n') || (c == '\t') |
1782 |
|| (c == '\r') || (c == '\f'))) { |
|
1783 |
cp--; |
|
1784 |
c = *cp; |
|
1785 |
} |
|
1134 | 1786 |
endIndex = __MKSMALLINT(cp - cp0 + 1); |
51 | 1787 |
} |
202 | 1788 |
%}. |
1789 |
startIndex == 0 ifTrue:[^ super withoutSeparators]. |
|
1790 |
||
1791 |
startIndex > endIndex ifTrue:[^ '']. |
|
1792 |
((startIndex == 1) and:[endIndex == self size]) ifTrue:[^ self]. |
|
1 | 1793 |
^ self copyFrom:startIndex to:endIndex |
608 | 1794 |
! |
1795 |
||
1796 |
withoutSpaces |
|
1797 |
"return a copy of myself without leading and trailing spaces. |
|
1798 |
Notice, this is different from String>>withoutSeparators." |
|
1799 |
||
1800 |
|startIndex "{ Class: SmallInteger }" |
|
1801 |
endIndex "{ Class: SmallInteger }" |
|
1802 |
sz blank| |
|
1803 |
||
1804 |
startIndex := 0. |
|
1805 |
%{ |
|
1806 |
REGISTER unsigned char *cp0; |
|
1807 |
REGISTER unsigned char *cp; |
|
1808 |
||
1809 |
/* ignore instances of subclasses ... */ |
|
1810 |
if (__qClass(self) == String) { |
|
1134 | 1811 |
cp = cp0 = __stringVal(self); |
608 | 1812 |
while (*cp == ' ') cp++; |
1134 | 1813 |
startIndex = __MKSMALLINT(cp - cp0 + 1); |
608 | 1814 |
cp = cp + strlen(cp) - 1; |
1815 |
while ((cp >= cp0) && (*cp == ' ')) cp--; |
|
1134 | 1816 |
endIndex = __MKSMALLINT(cp - cp0 + 1); |
608 | 1817 |
} |
1818 |
%}. |
|
1819 |
startIndex == 0 ifTrue:[^ super withoutSpaces]. |
|
1820 |
||
1821 |
startIndex > endIndex ifTrue:[^ '']. |
|
1822 |
((startIndex == 1) and:[endIndex == self size]) ifTrue:[^ self]. |
|
1823 |
^ self copyFrom:startIndex to:endIndex |
|
1824 |
! ! |
|
1825 |
||
1826 |
!String methodsFor:'pattern matching'! |
|
1827 |
||
1828 |
endsWith:aString |
|
1829 |
"return true, if the receiver end with something, aString." |
|
1830 |
||
1831 |
%{ /* NOCONTEXT */ |
|
1832 |
||
1833 |
int len1, len2; |
|
1834 |
REGISTER unsigned char *src1, *src2; |
|
1835 |
char c; |
|
1836 |
||
1837 |
if ((__isString(self) || __isSymbol(self)) |
|
1838 |
&& (__isString(aString) || __isSymbol(aString))) { |
|
1839 |
len1 = __qSize(self); |
|
1840 |
len2 = __qSize(aString); |
|
1841 |
if (len1 < len2) { |
|
1842 |
RETURN ( false ); |
|
1843 |
} |
|
1844 |
||
1134 | 1845 |
src1 = __stringVal(self) + len1 - len2; |
1846 |
src2 = __stringVal(aString); |
|
608 | 1847 |
while (c = *src2++) { |
1848 |
if (c != *src1++) { |
|
1849 |
RETURN ( false ); |
|
1850 |
} |
|
1851 |
} |
|
1852 |
RETURN (true); |
|
1853 |
} |
|
1854 |
%}. |
|
1855 |
(aString isMemberOf:Character) ifTrue:[ |
|
1856 |
self size == 0 ifTrue:[^ false]. |
|
1857 |
^ (self at:(self size)) == aString |
|
1858 |
]. |
|
1859 |
^ super endsWith:aString |
|
1860 |
||
1861 |
" |
|
1862 |
'hello world' endsWith:'world' |
|
1863 |
'hello world' endsWith:'earth' |
|
1864 |
'hello world' endsWith:$d |
|
1865 |
'hello world' endsWith:#($r $l $d) |
|
1866 |
" |
|
1867 |
! |
|
1868 |
||
1869 |
startsWith:aString |
|
1870 |
"return true, if the receiver starts with something, aString." |
|
1871 |
||
1872 |
%{ /* NOCONTEXT */ |
|
1873 |
||
1874 |
int len1, len2; |
|
1875 |
REGISTER unsigned char *src1, *src2; |
|
1876 |
char c; |
|
1877 |
||
1878 |
if ((__isString(self) || __isSymbol(self)) |
|
1879 |
&& (__isString(aString) || __isSymbol(aString))) { |
|
1880 |
len1 = __qSize(self); |
|
1881 |
len2 = __qSize(aString); |
|
1882 |
if (len1 < len2) { |
|
1883 |
RETURN ( false ); |
|
1884 |
} |
|
1885 |
||
1134 | 1886 |
src1 = __stringVal(self); |
1887 |
src2 = __stringVal(aString); |
|
608 | 1888 |
while (c = *src2++) { |
1889 |
if (c != *src1++) { |
|
1890 |
RETURN ( false ); |
|
1891 |
} |
|
1892 |
} |
|
1893 |
RETURN (true); |
|
1894 |
} |
|
1895 |
%}. |
|
1896 |
(aString isMemberOf:Character) ifTrue:[ |
|
1897 |
self size == 0 ifTrue:[^ false]. |
|
1898 |
^ (self at:1) == aString |
|
1899 |
]. |
|
1900 |
^ super startsWith:aString |
|
1901 |
||
1902 |
" |
|
1903 |
'hello world' startsWith:'hello' |
|
1904 |
'hello world' startsWith:'hi' |
|
1905 |
'hello world' startsWith:$h |
|
1906 |
'hello world' startsWith:#($h $e $l) |
|
1907 |
" |
|
1908 |
! ! |
|
1909 |
||
1910 |
!String methodsFor:'printing & storing'! |
|
1911 |
||
1912 |
isLiteral |
|
1913 |
"return true, if the receiver can be used as a literal |
|
1914 |
(i.e. can be used in constant arrays)" |
|
1915 |
||
1916 |
^ true |
|
1917 |
! |
|
1918 |
||
1919 |
||
1920 |
"print the receiver on standard output. |
|
1921 |
This method does NOT use the stream classes and will therefore work |
|
1922 |
even in case of emergency." |
|
1923 |
||
1924 |
%{ /* NOCONTEXT */ |
|
1925 |
||
1926 |
if (__qClass(self) == String) { |
|
1134 | 1927 |
fputs(__stringVal(self), stdout); fflush(stdout); |
608 | 1928 |
RETURN (self); |
1929 |
} |
|
1930 |
%} |
|
1931 |
. |
|
1932 |
^ super print |
|
1933 |
! |
|
1934 |
||
1935 |
printfPrintString:formatString |
|
1936 |
"non-portable but sometimes useful. |
|
1937 |
Return a printed representation of the receiver as specified by formatString, |
|
1938 |
which is defined by printf. |
|
1939 |
No checking on buffer overrun is done; the resulting string may not |
|
1940 |
be larger than 799 characters. |
|
1941 |
This method is NONSTANDARD and may be removed without notice." |
|
1942 |
||
1943 |
%{ /* STACK: 1000 */ |
|
1944 |
||
1945 |
char buffer[800]; |
|
1946 |
char *cp; |
|
1947 |
OBJ s; |
|
1948 |
||
1949 |
if (__isString(formatString)) { |
|
1950 |
/* |
|
1951 |
* actually only needed on sparc: since thisContext is |
|
1952 |
* in a global register, which gets destroyed by printf, |
|
1953 |
* manually save it here - very stupid ... |
|
1954 |
*/ |
|
1955 |
||
1134 | 1956 |
cp = (char *)__stringVal(self); |
608 | 1957 |
if (__qClass(self) != String) |
1134 | 1958 |
cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
608 | 1959 |
|
809
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
735
diff
changeset
|
1960 |
__BEGIN_PROTECT_REGISTERS__ |
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
735
diff
changeset
|
1961 |
|
1134 | 1962 |
sprintf(buffer, (char *)__stringVal(formatString), cp); |
809
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
735
diff
changeset
|
1963 |
|
5eef87c2907b
e convenient macro for register saving (sparc only)
Claus Gittinger <cg@exept.de>
parents:
735
diff
changeset
|
1964 |
__END_PROTECT_REGISTERS__ |
608 | 1965 |
|
833 | 1966 |
s = __MKSTRING(buffer COMMA_SND); |
608 | 1967 |
if (s != nil) { |
1968 |
RETURN (s); |
|
1969 |
} |
|
1970 |
} |
|
1971 |
%}. |
|
1972 |
self primitiveFailed |
|
1973 |
||
1974 |
" |
|
1975 |
'hello' printfPrintString:'%%s -> %s' |
|
1976 |
'hello' printfPrintString:'%%10s -> %10s' |
|
1977 |
'hello' printfPrintString:'%%-10s -> %-10s' |
|
1978 |
" |
|
1979 |
! |
|
1980 |
||
1981 |
storeOn:aStream |
|
1982 |
"put the storeString of myself on aStream" |
|
1983 |
||
1984 |
aStream nextPut: $'. |
|
1985 |
(self includes:$') ifTrue:[ |
|
1986 |
self do:[:thisChar | |
|
1987 |
(thisChar == $') ifTrue:[aStream nextPut:thisChar]. |
|
1988 |
aStream nextPut:thisChar |
|
1989 |
] |
|
1990 |
] ifFalse:[ |
|
1991 |
aStream nextPutAll:self |
|
1992 |
]. |
|
1993 |
aStream nextPut:$' |
|
1994 |
! |
|
1995 |
||
1996 |
storeString |
|
1997 |
"return a String for storing myself" |
|
1998 |
||
1999 |
|s n index| |
|
2000 |
||
2001 |
n := self occurrencesOf:$'. |
|
2002 |
n == 0 ifFalse:[ |
|
2003 |
s := String new:(n + 2 + self size). |
|
2004 |
s at:1 put:$'. |
|
2005 |
index := 2. |
|
2006 |
self do:[:thisChar | |
|
2007 |
(thisChar == $') ifTrue:[ |
|
2008 |
s at:index put:thisChar. |
|
2009 |
index := index + 1. |
|
2010 |
]. |
|
2011 |
s at:index put:thisChar. |
|
2012 |
index := index + 1. |
|
2013 |
]. |
|
2014 |
s at:index put:$'. |
|
2015 |
^ s |
|
2016 |
]. |
|
2017 |
^ '''' , self , '''' |
|
1 | 2018 |
! ! |
2019 |
||
2020 |
!String methodsFor:'queries'! |
|
2021 |
||
2022 |
encoding |
|
2023 |
"assume iso8859 encoding" |
|
2024 |
||
2025 |
^ #iso8859 |
|
2026 |
! |
|
2027 |
||
1015 | 2028 |
bitsPerCharacter |
2029 |
^ 8 |
|
2030 |
! |
|
2031 |
||
68 | 2032 |
isEmpty |
2033 |
"return true if the receiver is empty (i.e. if size == 0) |
|
2034 |
Redefined here for performance" |
|
2035 |
||
2036 |
%{ /* NOCONTEXT */ |
|
2037 |
OBJ cls; |
|
2038 |
||
329 | 2039 |
cls = __qClass(self); |
68 | 2040 |
if ((cls == String) || (cls == Symbol)) { |
1134 | 2041 |
RETURN ( (__stringSize(self) == 0) ? true : false); |
68 | 2042 |
} |
2043 |
%} |
|
2044 |
. |
|
2045 |
^ super isEmpty |
|
608 | 2046 |
! |
2047 |
||
2048 |
knownAsSymbol |
|
2049 |
"return true, if there is a symbol with same characters in the |
|
2050 |
system - use to check for existance of a symbol without creating one" |
|
2051 |
||
2052 |
%{ /* NOCONTEXT */ |
|
1134 | 2053 |
RETURN ( __KNOWNASSYMBOL(__stringVal(self)) ); |
608 | 2054 |
%} |
1 | 2055 |
! ! |
608 | 2056 |
|
2057 |
!String methodsFor:'testing'! |
|
2058 |
||
2059 |
isBlank |
|
2060 |
"return true, if the receivers size is 0 or if it contains only spaces. |
|
2061 |
Q: should we care for whiteSpace in general here ?" |
|
2062 |
||
2063 |
%{ /* NOCONTEXT */ |
|
2064 |
||
2065 |
REGISTER unsigned char *src; |
|
2066 |
REGISTER unsigned char c; |
|
2067 |
OBJ cls; |
|
2068 |
||
1134 | 2069 |
src = __stringVal(self); |
608 | 2070 |
if ((cls = __qClass(self)) != String) |
1134 | 2071 |
src += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
608 | 2072 |
|
2073 |
#ifndef NON_ASCII |
|
2074 |
while (*((unsigned *)src) == 0x20202020) { |
|
2075 |
src += 4; |
|
2076 |
} |
|
2077 |
#endif |
|
2078 |
while (c = *src++) |
|
2079 |
if (c != ' ') { |
|
2080 |
RETURN ( false ); |
|
2081 |
} |
|
2082 |
%}. |
|
2083 |
^ true |
|
2084 |
! |
|
2085 |
||
2086 |
levenshteinTo:aString s:substWeight c:caseWeight i:insrtWeight d:deleteWeight |
|
2087 |
"parametrized levenshtein. arguments are the costs for |
|
2088 |
substitution, case-change, insertion and deletion of a character." |
|
2089 |
||
1138
993e6ffdbf51
removed external decls for VM functions (should look for more)
Claus Gittinger <cg@exept.de>
parents:
1134
diff
changeset
|
2090 |
%{ /* STACK: 2000 */ |
608 | 2091 |
|
2092 |
/* |
|
2093 |
* this is very heavy used when correcting errors |
|
2094 |
* (all symbols are searched for best match) - therefore it must be fast |
|
2095 |
*/ |
|
2096 |
{ |
|
2097 |
unsigned short *data; |
|
2098 |
int l1, l2; |
|
2099 |
REGISTER int sz; |
|
2100 |
unsigned char *s1, *s2; |
|
2101 |
int v1, v2, v3, m; |
|
2102 |
REGISTER unsigned short *dp; |
|
2103 |
REGISTER int delta; |
|
2104 |
REGISTER int j; |
|
2105 |
int i; |
|
2106 |
int iW, cW, sW, dW; |
|
1138
993e6ffdbf51
removed external decls for VM functions (should look for more)
Claus Gittinger <cg@exept.de>
parents:
1134
diff
changeset
|
2107 |
# define FASTSIZE 30 /* increase STACK if you increase this ... */ |
608 | 2108 |
unsigned short fastData[(FASTSIZE + 1) * (FASTSIZE + 1)]; |
2109 |
||
2110 |
if ((__isString(self) || __isSymbol(self)) |
|
2111 |
&& (__isString(aString) || __isSymbol(aString)) |
|
2112 |
&& __bothSmallInteger(insrtWeight, caseWeight) |
|
2113 |
&& __bothSmallInteger(substWeight, deleteWeight)) { |
|
1134 | 2114 |
iW = __intVal(insrtWeight); |
2115 |
cW = __intVal(caseWeight); |
|
2116 |
sW = __intVal(substWeight); |
|
2117 |
dW = __intVal(deleteWeight); |
|
2118 |
s1 = __stringVal(self); |
|
2119 |
s2 = __stringVal(aString); |
|
608 | 2120 |
l1 = strlen(s1); |
2121 |
l2 = strlen(s2); |
|
2122 |
||
2123 |
sz = (l1 < l2) ? l2 : l1; |
|
2124 |
delta = sz + 1; |
|
2125 |
if (sz <= FASTSIZE) { |
|
2126 |
data = fastData; |
|
2127 |
} else { |
|
2128 |
/* add ifdef ALLOCA here ... */ |
|
2129 |
data = (unsigned short *)malloc(delta * delta * sizeof(short)); |
|
2130 |
if (! data) goto mallocFailed; |
|
2131 |
} |
|
2132 |
||
2133 |
data[0] = 0; |
|
2134 |
dp = data+1; |
|
2135 |
for (j=1, dp=data+1; j<=sz; j++, dp++) |
|
2136 |
*dp = *(dp-1) + iW; |
|
2137 |
||
2138 |
for (i=1, dp=data+delta; i<=sz; i++, dp+=delta) |
|
2139 |
*dp = *(dp-delta) + dW; |
|
2140 |
||
2141 |
for (i=1; i<=l1; i++) { |
|
2142 |
for (j=1; j<=l2; j++) { |
|
2143 |
dp = data + (i*delta) + j; |
|
2144 |
if (s1[i] != s2[j]) { |
|
2145 |
if (tolower(s1[i]) == tolower(s2[j])) { |
|
2146 |
m = cW; |
|
2147 |
} else { |
|
2148 |
m = sW; |
|
2149 |
} |
|
2150 |
} else |
|
2151 |
m = 0; |
|
2152 |
||
2153 |
v2 = *(dp - 1) + iW; |
|
2154 |
v3 = *(dp - delta) + dW; |
|
2155 |
v1 = *(dp - delta - 1) + m; |
|
2156 |
if (v1 < v2) |
|
2157 |
if (v1 < v3) |
|
2158 |
m = v1; |
|
2159 |
else |
|
2160 |
m = v3; |
|
2161 |
else |
|
2162 |
if (v2 < v3) |
|
2163 |
m = v2; |
|
2164 |
else |
|
2165 |
m = v3; |
|
2166 |
*dp = m; |
|
2167 |
} |
|
2168 |
} |
|
2169 |
m = data[l1 * delta + l2]; |
|
2170 |
if (sz > FASTSIZE) |
|
2171 |
free(data); |
|
1134 | 2172 |
RETURN ( __MKSMALLINT(m) ); |
608 | 2173 |
} |
2174 |
mallocFailed: ; |
|
2175 |
} |
|
2176 |
%}. |
|
2177 |
||
2178 |
^ super levenshteinTo:aString |
|
2179 |
s:substWeight c:caseWeight |
|
2180 |
i:insrtWeight d:deleteWeight |
|
2181 |
||
2182 |
"'ocmprt' levenshteinTo:'computer' |
|
2183 |
'computer' levenshteinTo:'computer' |
|
2184 |
'ocmputer' levenshteinTo:'computer' |
|
2185 |
'cmputer' levenshteinTo:'computer' |
|
2186 |
'Computer' levenshteinTo:'computer'" |
|
2187 |
! ! |
|
2188 |
||
633 | 2189 |
!String class methodsFor:'documentation'! |
2190 |
||
2191 |
version |
|
1143
27325100bdf4
can check index against low..hi with a single compare&branch
Claus Gittinger <cg@exept.de>
parents:
1138
diff
changeset
|
2192 |
^ '$Header: /cvs/stx/stx/libbasic/String.st,v 1.68 1996-04-09 20:46:43 cg Exp $' |
633 | 2193 |
! ! |