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