author | Claus Gittinger <cg@exept.de> |
Sat, 06 Apr 1996 15:23:05 +0200 | |
changeset 1138 | 993e6ffdbf51 |
parent 1134 | 7cb7cd7ebeb8 |
child 1219 | 054f7eff0c30 |
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 |
||
13 |
String subclass:#Symbol |
|
1020 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Text' |
|
1 | 18 |
! |
19 |
||
77 | 20 |
!Symbol class methodsFor:'documentation'! |
1 | 21 |
|
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
159 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
77 | 36 |
documentation |
37 |
" |
|
38 |
Symbols represent unique strings - every symbol with same printString |
|
39 |
exists exactly once in the system; Symbols are used for selectors, global |
|
345 | 40 |
variable-keys etc. Symbols can also be used to represent things which are |
41 |
enumeration type values in other programming languages (since symbols are |
|
370 | 42 |
created at compile time, comparing them using == is a fast pointer compare). |
345 | 43 |
|
44 |
A symbol may not change its characters - i.e. it is constant over its lifetime. |
|
45 |
Other than that, symbols behave much like strings. |
|
77 | 46 |
" |
47 |
! ! |
|
1 | 48 |
|
77 | 49 |
!Symbol class methodsFor:'instance creation'! |
1 | 50 |
|
62 | 51 |
basicNew:size |
52 |
"redefined to return a string instead of a symbol - |
|
53 |
this allows all copy methods inherited from String to |
|
54 |
return strings containing the symbols characters. |
|
1020 | 55 |
Real symbols are only created with #intern: or #asSymbol." |
62 | 56 |
|
57 |
^ String new:size |
|
1020 | 58 |
|
59 |
"Modified: 26.2.1996 / 12:51:38 / cg" |
|
62 | 60 |
! |
61 |
||
77 | 62 |
fromString:aString |
63 |
"same as intern: for Symbol, but may be used to create interned instances |
|
64 |
of subclasses. |
|
65 |
Notice: this fails, if you try to intern an instance of a subclass, AND |
|
66 |
a symbol with the same name already exists. In this case, the original |
|
67 |
symbol is returned. To use it for enum-type symbols, make certain, that the |
|
68 |
names are unique (for example by including the classes name as a prefix-string)." |
|
69 |
||
267 | 70 |
|sym len| |
77 | 71 |
|
267 | 72 |
sym := aString asSymbolIfInterned. |
73 |
sym notNil ifTrue:[ |
|
74 |
^ sym |
|
77 | 75 |
]. |
76 |
||
77 |
" |
|
78 |
create a new uninterned instance first |
|
79 |
" |
|
80 |
len := aString size. |
|
267 | 81 |
sym := super basicNew:len. |
82 |
sym replaceFrom:1 to:len with:aString. |
|
77 | 83 |
|
84 |
"now, intern it" |
|
85 |
%{ |
|
329 | 86 |
sym = __INTERNSYMBOL(sym, (OBJ *)0, __context); |
77 | 87 |
%}. |
267 | 88 |
^ sym |
77 | 89 |
! |
90 |
||
608 | 91 |
intern:aString |
92 |
"return a unique symbol with printname taken from the String-argument" |
|
93 |
||
94 |
%{ /* NOCONTEXT */ |
|
95 |
OBJ newSymbol; |
|
96 |
||
97 |
if (__isSymbol(aString)) { |
|
98 |
RETURN (aString); |
|
99 |
} |
|
100 |
if (__isString(aString)) { |
|
1134 | 101 |
newSymbol = __MKSYMBOL(__stringVal(aString), (OBJ *)0, SENDER); |
608 | 102 |
if (newSymbol != nil) { |
103 |
RETURN (newSymbol); |
|
104 |
} |
|
105 |
} |
|
106 |
%}. |
|
107 |
aString class ~~ String ifTrue:[ |
|
108 |
"/ |
|
109 |
"/ only allowed to intern strings |
|
110 |
"/ |
|
111 |
^ self mustBeString |
|
112 |
]. |
|
113 |
^ ObjectMemory allocationFailureSignal raise. |
|
114 |
! |
|
115 |
||
1 | 116 |
internCharacter:aCharacter |
117 |
"return a unique symbol with printname taken from the Character-argument" |
|
118 |
||
119 |
^ self intern:(aCharacter asString) |
|
608 | 120 |
! |
121 |
||
122 |
new:size |
|
123 |
"redefined to return a string instead of a symbol - |
|
124 |
this allows all copy methods inherited from String to |
|
125 |
return strings containing the symbols characters. |
|
126 |
Real symbols are only created with intern: or asSymbol." |
|
127 |
||
128 |
^ String new:size |
|
129 |
! ! |
|
130 |
||
131 |
!Symbol class methodsFor:'binary storage'! |
|
132 |
||
133 |
binaryDefinitionFrom: stream manager: manager |
|
134 |
^ self intern: (super binaryDefinitionFrom: stream manager: manager) |
|
1 | 135 |
! ! |
136 |
||
137 |
!Symbol class methodsFor:'queries'! |
|
138 |
||
608 | 139 |
findInterned:aString |
140 |
"for ST-80 compatibility - if the argument, aString is known |
|
141 |
as Symbol, return this symbol. Otherwise return nil." |
|
142 |
||
143 |
^ aString asSymbolIfInterned |
|
144 |
! |
|
145 |
||
146 |
hasInterned:aString |
|
147 |
"return true, if the argument, aString is known as Symbol; |
|
148 |
false otherwise" |
|
149 |
||
150 |
^ aString knownAsSymbol |
|
151 |
! |
|
152 |
||
1 | 153 |
hasInterned:aString ifTrue:trueBlock |
154 |
"for ST-80 compatibility - if the argument, aString is known |
|
155 |
as Symbol, evaluate the block with the corresponding symbol |
|
156 |
as argument and return true; otherwise return false" |
|
157 |
||
275 | 158 |
|sym| |
159 |
||
160 |
(sym := aString asSymbolIfInterned) notNil ifTrue:[ |
|
161 |
trueBlock value:sym. |
|
159 | 162 |
^ true |
1 | 163 |
]. |
164 |
^ false |
|
165 |
! ! |
|
166 |
||
167 |
!Symbol methodsFor:'accessing'! |
|
168 |
||
169 |
basicAt:index put:something |
|
345 | 170 |
"report an error if an interned symbol is about to be changed |
171 |
- interned symbols may NOT be changed." |
|
1 | 172 |
|
77 | 173 |
self knownAsSymbol ifTrue:[ |
345 | 174 |
self error:'interned symbols may not be changed'. |
159 | 175 |
^ something |
77 | 176 |
]. |
177 |
" |
|
178 |
uninterned - allow change |
|
179 |
" |
|
180 |
^ super basicAt:index put:something |
|
252 | 181 |
! |
182 |
||
183 |
byteAt:index put:value |
|
184 |
"report an error if interned - interned symbols may not be changed." |
|
185 |
||
186 |
^ self basicAt:index put:(value asCharacter) |
|
1 | 187 |
! ! |
188 |
||
608 | 189 |
!Symbol methodsFor:'comparing'! |
159 | 190 |
|
608 | 191 |
= something |
192 |
"return true, if the receiver and argument consist of the same characters. |
|
193 |
Redefined here, for more efficient #= comparison of symbols |
|
194 |
(which ought to be compared using #==). |
|
195 |
If the argument is a symbol, we use a quick pointer compare, instead of |
|
196 |
the inherited value compare." |
|
13 | 197 |
|
608 | 198 |
%{ /* NOCONTEXT */ |
199 |
OBJ cls; |
|
13 | 200 |
|
608 | 201 |
if (! __isNonNilObject(something)) RETURN(false); |
202 |
if ((cls = __qClass(something)) == Symbol) { |
|
203 |
RETURN (self == something ? true : false); |
|
204 |
} |
|
205 |
if (cls == String) { |
|
206 |
RETURN (strcmp(__stringVal(self), __stringVal(something)) == 0 ? true : false); |
|
345 | 207 |
} |
208 |
%}. |
|
608 | 209 |
"fall back; could be a TwoByteString, or a collection of Characters" |
1 | 210 |
|
608 | 211 |
^ super = something |
427 | 212 |
! |
213 |
||
356 | 214 |
identityHash |
215 |
"interned symbols can return a better hash key" |
|
216 |
||
217 |
%{ /* NOCONTEXT */ |
|
218 |
||
219 |
REGISTER int g, val; |
|
220 |
REGISTER unsigned char *cp, *cp0; |
|
221 |
int l; |
|
222 |
||
223 |
if (__Class(self) == Symbol) { |
|
375 | 224 |
val = __GET_HASH(self); |
356 | 225 |
/* |
226 |
* only do it, if I have no standard hash key |
|
375 | 227 |
* assigned (which can only happen due to a #become:, |
228 |
* or by creating a symbol uninterned, and interning it |
|
229 |
* after it got a hashKey assigned. |
|
356 | 230 |
*/ |
231 |
if (val == 0) { |
|
1134 | 232 |
cp = __stringVal(self); |
233 |
l = __stringSize(self); |
|
356 | 234 |
|
235 |
/* |
|
236 |
* this is the dragon-book algorithm |
|
237 |
* |
|
238 |
* the algorithm hashes pretty good: |
|
239 |
* with (currently) 9963 symbols in the system, |
|
240 |
* there are only about 200 hash key collisions. |
|
241 |
* where the maximum collision count in these 200 |
|
242 |
* is 3. This means, that in most situations, |
|
243 |
* a single probe will find the right element in |
|
244 |
* a symbol-hashed collection. |
|
245 |
*/ |
|
246 |
val = 0; |
|
247 |
for (cp0 = cp, cp += l - 1; cp >= cp0; cp--) { |
|
248 |
val = (val << 5) + (*cp & 0x1F); |
|
249 |
if (g = (val & 0x3E000000)) |
|
250 |
val ^= g >> 25 /* 23 */ /* 25 */; |
|
251 |
val &= 0x3FFFFFFF; |
|
252 |
} |
|
253 |
||
254 |
if (l) { |
|
255 |
l |= 1; |
|
256 |
val = (val * l) & 0x3FFFFFFF; |
|
257 |
} |
|
375 | 258 |
} else { |
259 |
val <<= __HASH_SHIFT__; |
|
356 | 260 |
} |
1134 | 261 |
RETURN ( __MKSMALLINT(val) ); |
356 | 262 |
} |
263 |
%}. |
|
264 |
^ super identityHash |
|
370 | 265 |
! |
266 |
||
267 |
~= something |
|
268 |
"return true, if the receiver and argument do not consist of the same characters. |
|
269 |
Redefined here, for more efficient #~= comparison of symbols |
|
270 |
(which ought to be compared using #~~). |
|
271 |
If the argument is a symbol, we use a quick pointer compare, instead of |
|
272 |
the inherited value compare." |
|
273 |
||
274 |
%{ /* NOCONTEXT */ |
|
275 |
OBJ cls; |
|
276 |
||
379 | 277 |
if (! __isNonNilObject(something)) RETURN(true); /* cannot be equal */ |
370 | 278 |
if ((cls = __qClass(something)) == Symbol) { |
379 | 279 |
RETURN (self == something ? false : true); |
370 | 280 |
} |
281 |
if (cls == String) { |
|
379 | 282 |
RETURN (strcmp(__stringVal(self), __stringVal(something)) == 0 ? false : true); |
370 | 283 |
} |
284 |
%}. |
|
285 |
"fall back; could be a TwoByteString, or a collection of Characters" |
|
286 |
||
287 |
^ super ~= something |
|
356 | 288 |
! ! |
289 |
||
608 | 290 |
!Symbol methodsFor:'converting'! |
291 |
||
292 |
asString |
|
293 |
"return a string with printname taken from mine" |
|
294 |
||
295 |
%{ /* NOCONTEXT */ |
|
296 |
OBJ s; |
|
297 |
||
298 |
s = __MKSTRING_ST(self COMMA_SND); |
|
299 |
if (s != nil) { |
|
300 |
RETURN (s); |
|
301 |
} |
|
302 |
%}. |
|
303 |
" |
|
304 |
memory allocation (for the new string) failed. |
|
305 |
When we arrive here, there was no memory, even after a garbage collect. |
|
306 |
This means, that the VM wanted to get some more memory from the |
|
307 |
OS, which was not kind enough to give it. |
|
308 |
Bad luck - you should increase the swap space on your machine, |
|
309 |
or if you have set a soft memoryLimit, increase it. |
|
310 |
" |
|
311 |
^ ObjectMemory allocationFailureSignal raise. |
|
312 |
! |
|
313 |
||
314 |
asSymbol |
|
315 |
"return the receiver as a symbol. |
|
316 |
Since I am a symbol - just return myself" |
|
317 |
||
318 |
^ self |
|
319 |
! |
|
320 |
||
321 |
asSymbolIfInterned |
|
322 |
"return the receiver as a symbol if there is one. |
|
323 |
Since I am a symbol - just return myself" |
|
324 |
||
325 |
^ self |
|
326 |
! |
|
327 |
||
328 |
knownAsSymbol |
|
329 |
"return true if the receiver is known as a symbol. |
|
330 |
Since I am a symbol - just return true" |
|
331 |
||
332 |
^ true |
|
333 |
! ! |
|
334 |
||
335 |
!Symbol methodsFor:'copying'! |
|
336 |
||
337 |
copy |
|
338 |
"return a copy of myself |
|
339 |
- reimplemented here since symbols are unique and copy should |
|
340 |
return a new string with my characters." |
|
341 |
||
342 |
^ self |
|
343 |
! |
|
344 |
||
345 |
deepCopy |
|
346 |
"return a copy of myself |
|
347 |
- reimplemented here since symbols are unique and copy should |
|
348 |
return a new string with my characters." |
|
349 |
||
350 |
^ self |
|
351 |
! |
|
352 |
||
353 |
deepCopyUsing:aDictionary |
|
354 |
"return a copy of myself |
|
355 |
- reimplemented here since symbols are unique and copy should |
|
356 |
return a new string with my characters." |
|
357 |
||
358 |
^ self |
|
359 |
! |
|
360 |
||
361 |
shallowCopy |
|
362 |
"return a copy of myself |
|
363 |
- reimplemented here since symbols are unique" |
|
364 |
||
365 |
^ self |
|
366 |
! |
|
367 |
||
368 |
simpleDeepCopy |
|
369 |
"return a copy of myself |
|
370 |
- reimplemented here since symbols are unique and copy should |
|
371 |
return a new string with my characters." |
|
372 |
||
373 |
^ self |
|
374 |
! ! |
|
375 |
||
376 |
!Symbol methodsFor:'printing & storing'! |
|
377 |
||
378 |
displayString |
|
379 |
"return a string for displaying the receiver" |
|
380 |
||
381 |
^ self storeString |
|
382 |
! |
|
383 |
||
384 |
printOn:aStream |
|
385 |
"append a printed representation of the receiver on aStream. |
|
386 |
In contrast to ST-80, this does return the symbols characters |
|
387 |
without any leading #. Therefore, you can directly send symbols' |
|
388 |
printStrings to some output device. This is incompatible, but easier |
|
389 |
to use." |
|
390 |
||
391 |
"/ ST-80 behavior: |
|
392 |
||
393 |
"/ self storeOn:aStream |
|
394 |
||
395 |
aStream nextPutAll:self |
|
396 |
! |
|
397 |
||
398 |
printString |
|
399 |
"return a printed representation of the receiver. |
|
400 |
In contrast to ST-80, this does return the symbols characters |
|
401 |
without any leading #. Therefore, you can directly send symbols' |
|
402 |
printStrings to some output device. This is incompatible, but easier |
|
403 |
to use." |
|
404 |
||
405 |
"/ ST-80 behavior: |
|
406 |
"/ ^ self storeString |
|
407 |
||
408 |
^ self asString |
|
409 |
! |
|
410 |
||
411 |
storeOn:aStream |
|
412 |
"store myself on a stream" |
|
413 |
||
414 |
aStream nextPutAll:(self storeString) |
|
415 |
! |
|
416 |
||
417 |
storeString |
|
418 |
"return a String for storing the receiver" |
|
419 |
||
1125 | 420 |
|sz| |
421 |
||
422 |
(sz := self size) > 0 ifTrue:[ "/ sigh |
|
608 | 423 |
(self at:1) isLetter ifTrue:[ |
1125 | 424 |
2 to:sz do:[:index | |
608 | 425 |
(self at:index) isLetterOrDigit ifFalse:[ |
426 |
^ '#''' , self , '''' |
|
427 |
]. |
|
428 |
]. |
|
429 |
^ '#' , self |
|
430 |
] |
|
431 |
]. |
|
432 |
^ '#''' , self , '''' |
|
433 |
! ! |
|
434 |
||
435 |
!Symbol methodsFor:'queries'! |
|
436 |
||
437 |
isSymbol |
|
438 |
"return true, if the receiver is some kind of symbol. |
|
439 |
Since I am a symbol, return always true" |
|
440 |
||
441 |
^ true |
|
442 |
! |
|
443 |
||
444 |
keywords |
|
445 |
"assuming the receiver is a keyword message selector, |
|
446 |
return the individual keywords (i.e. break it up at colons) |
|
447 |
and return these as a collection. |
|
448 |
For binary and unary selectors, the result may be nonsense." |
|
449 |
||
450 |
|coll s part| |
|
451 |
||
452 |
coll := OrderedCollection new. |
|
453 |
s := ReadStream on:self. |
|
454 |
[s atEnd] whileFalse:[ |
|
455 |
part := s through:$:. |
|
456 |
coll add:part |
|
457 |
]. |
|
458 |
^ coll asArray |
|
459 |
||
460 |
" |
|
461 |
#at:put: keywords |
|
462 |
#at: keywords |
|
463 |
#+ keywords |
|
464 |
#size keywords |
|
465 |
" |
|
466 |
! |
|
467 |
||
468 |
species |
|
469 |
"when copying, or concatenating, return instances of this class" |
|
470 |
||
471 |
^ String |
|
472 |
! ! |
|
473 |
||
77 | 474 |
!Symbol methodsFor:'system primitives'! |
475 |
||
476 |
become:anotherObject |
|
477 |
"make all references to the receiver become references to anotherObject |
|
95 | 478 |
and vice-versa. For symbols, some special action is required, to |
479 |
correctly handle a become of the global dictionaries. |
|
77 | 480 |
Anyway: this is very dangerous - mysterous side-effects are to be |
95 | 481 |
expected. |
482 |
||
483 |
Notice: because of the danger here, this method may report an error |
|
159 | 484 |
in future versions" |
77 | 485 |
|
486 |
(Smalltalk includesKey:self) ifTrue:[ |
|
159 | 487 |
super become:anotherObject. |
77 | 488 |
%{ |
159 | 489 |
__rehashSystemDictionaries(); |
77 | 490 |
%}. |
491 |
] ifFalse:[ |
|
159 | 492 |
super become:anotherObject |
77 | 493 |
] |
494 |
! |
|
495 |
||
496 |
becomeNil |
|
497 |
"make all references to the receiver become nil - effectively getting |
|
95 | 498 |
rid of the receiver. For symbols, this is not allowed, if the receiver |
77 | 499 |
is used as a key in some SytemDictionary. |
95 | 500 |
This can be a very dangerous operation - be warned. |
501 |
||
502 |
Notice: because of the danger here, this method may report an error |
|
159 | 503 |
in future versions" |
77 | 504 |
|
505 |
(Smalltalk includesKey:self) ifTrue:[ |
|
159 | 506 |
self primitiveFailed |
77 | 507 |
] ifFalse:[ |
159 | 508 |
super becomeNil |
77 | 509 |
] |
510 |
! ! |
|
511 |
||
632 | 512 |
!Symbol class methodsFor:'documentation'! |
513 |
||
514 |
version |
|
1138
993e6ffdbf51
removed external decls for VM functions (should look for more)
Claus Gittinger <cg@exept.de>
parents:
1134
diff
changeset
|
515 |
^ '$Header: /cvs/stx/stx/libbasic/Symbol.st,v 1.39 1996-04-06 13:23:05 cg Exp $' |
632 | 516 |
! ! |