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