author | Claus Gittinger <cg@exept.de> |
Sat, 13 Nov 1999 13:16:32 +0100 | |
changeset 4987 | 060aedc5972c |
parent 4838 | 170e81a97135 |
child 5185 | 15a129151b3b |
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 |
||
3985 | 13 |
String variableByteSubclass:#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 |
||
4838 | 134 |
!Symbol class methodsFor:'Compatibility - ST-80'! |
2570
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 |
|
4838 | 139 |
"/ claus: I dont know, if the returned value should be exact. |
140 |
"/ the number below is just arbitrary ... |
|
2570
26616a00d5aa
added #tableSize for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2565
diff
changeset
|
141 |
|
26616a00d5aa
added #tableSize for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2565
diff
changeset
|
142 |
^ 10000 |
26616a00d5aa
added #tableSize for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2565
diff
changeset
|
143 |
|
26616a00d5aa
added #tableSize for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2565
diff
changeset
|
144 |
"Created: 18.4.1997 / 20:52:20 / cg" |
26616a00d5aa
added #tableSize for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2565
diff
changeset
|
145 |
! ! |
26616a00d5aa
added #tableSize for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2565
diff
changeset
|
146 |
|
608 | 147 |
!Symbol class methodsFor:'binary storage'! |
148 |
||
1253 | 149 |
binaryDefinitionFrom:stream manager:manager |
150 |
"retrieve a symbol from a binary input stream (nonpublic interface)" |
|
151 |
||
152 |
^ self intern:(super binaryDefinitionFrom:stream manager:manager) |
|
153 |
||
3086 | 154 |
"Modified: / 2.11.1997 / 16:17:06 / cg" |
1 | 155 |
! ! |
156 |
||
157 |
!Symbol class methodsFor:'queries'! |
|
158 |
||
608 | 159 |
findInterned:aString |
160 |
"for ST-80 compatibility - if the argument, aString is known |
|
161 |
as Symbol, return this symbol. Otherwise return nil." |
|
162 |
||
163 |
^ aString asSymbolIfInterned |
|
164 |
! |
|
165 |
||
166 |
hasInterned:aString |
|
167 |
"return true, if the argument, aString is known as Symbol; |
|
168 |
false otherwise" |
|
169 |
||
170 |
^ aString knownAsSymbol |
|
171 |
! |
|
172 |
||
1 | 173 |
hasInterned:aString ifTrue:trueBlock |
174 |
"for ST-80 compatibility - if the argument, aString is known |
|
175 |
as Symbol, evaluate the block with the corresponding symbol |
|
176 |
as argument and return true; otherwise return false" |
|
177 |
||
275 | 178 |
|sym| |
179 |
||
180 |
(sym := aString asSymbolIfInterned) notNil ifTrue:[ |
|
181 |
trueBlock value:sym. |
|
159 | 182 |
^ true |
1 | 183 |
]. |
184 |
^ false |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
185 |
! |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
186 |
|
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
187 |
hasSharedInstances |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
188 |
"return true if this class has shared instances, that is, instances |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
189 |
with the same value are identical. |
4654 | 190 |
True returned here - there is only one of each symbol (per contents)." |
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
191 |
|
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
192 |
^ true |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
193 |
|
1 | 194 |
! ! |
195 |
||
196 |
!Symbol methodsFor:'accessing'! |
|
197 |
||
198 |
basicAt:index put:something |
|
345 | 199 |
"report an error if an interned symbol is about to be changed |
1219 | 200 |
- interned symbols may NOT be changed. |
201 |
For uninterned symbols, this is allowed." |
|
1 | 202 |
|
77 | 203 |
self knownAsSymbol ifTrue:[ |
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
204 |
self error:'interned symbols may not be changed'. |
77 | 205 |
]. |
206 |
" |
|
207 |
uninterned - allow change |
|
208 |
" |
|
209 |
^ super basicAt:index put:something |
|
1219 | 210 |
|
211 |
"Modified: 19.4.1996 / 11:16:10 / cg" |
|
252 | 212 |
! |
213 |
||
214 |
byteAt:index put:value |
|
215 |
"report an error if interned - interned symbols may not be changed." |
|
216 |
||
217 |
^ self basicAt:index put:(value asCharacter) |
|
1 | 218 |
! ! |
219 |
||
3086 | 220 |
!Symbol methodsFor:'binary storage'! |
221 |
||
222 |
storeBinaryDefinitionOn:stream manager:manager |
|
223 |
"append a binary representation of the receiver onto stream. |
|
224 |
Redefined since short Symbol can be stored with a special type code |
|
225 |
in a more compact way. |
|
226 |
This is an internal interface for the binary storage mechanism." |
|
227 |
||
228 |
|myClass myBasicSize| |
|
229 |
||
230 |
"/ not for subclasses with named instVars. |
|
231 |
(myClass := self class) instSize ~~ 0 ifTrue:[ |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
232 |
^ super storeBinaryDefinitionOn:stream manager:manager |
3086 | 233 |
]. |
234 |
||
235 |
myBasicSize := self basicSize. |
|
236 |
||
237 |
"/ can use a more compact representation; |
|
238 |
"/ but not for subclasses ... |
|
239 |
||
240 |
((myClass == Symbol) |
|
241 |
and:[myBasicSize <= 255]) ifTrue:[ |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
242 |
"/ special encoding: <codeForSymbol> <len> <bytes> ... |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
243 |
stream nextPut:(manager codeForSymbol); nextPut:myBasicSize. |
3086 | 244 |
] ifFalse:[ |
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
245 |
manager putIdOfClass:myClass on:stream. |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
246 |
stream nextNumber:4 put:myBasicSize. |
3086 | 247 |
]. |
248 |
stream nextPutBytes:myBasicSize from:self startingAt:1. |
|
249 |
||
250 |
"Modified: / 2.11.1997 / 15:28:56 / cg" |
|
251 |
"Created: / 2.11.1997 / 16:13:47 / cg" |
|
252 |
! ! |
|
253 |
||
608 | 254 |
!Symbol methodsFor:'comparing'! |
159 | 255 |
|
608 | 256 |
= something |
257 |
"return true, if the receiver and argument consist of the same characters. |
|
258 |
Redefined here, for more efficient #= comparison of symbols |
|
259 |
(which ought to be compared using #==). |
|
260 |
If the argument is a symbol, we use a quick pointer compare, instead of |
|
261 |
the inherited value compare." |
|
13 | 262 |
|
608 | 263 |
%{ /* NOCONTEXT */ |
264 |
OBJ cls; |
|
13 | 265 |
|
608 | 266 |
if (! __isNonNilObject(something)) RETURN(false); |
267 |
if ((cls = __qClass(something)) == Symbol) { |
|
268 |
RETURN (self == something ? true : false); |
|
269 |
} |
|
270 |
if (cls == String) { |
|
271 |
RETURN (strcmp(__stringVal(self), __stringVal(something)) == 0 ? true : false); |
|
345 | 272 |
} |
273 |
%}. |
|
608 | 274 |
"fall back; could be a TwoByteString, or a collection of Characters" |
1 | 275 |
|
608 | 276 |
^ super = something |
427 | 277 |
! |
278 |
||
356 | 279 |
identityHash |
280 |
"interned symbols can return a better hash key" |
|
281 |
||
282 |
%{ /* NOCONTEXT */ |
|
283 |
||
3985 | 284 |
REGISTER unsigned int g, val; |
285 |
REGISTER unsigned char *cp, *ce; |
|
356 | 286 |
int l; |
287 |
||
288 |
if (__Class(self) == Symbol) { |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
289 |
val = __GET_HASH(self); |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
290 |
/* |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
291 |
* only do it, if I have no standard hash key |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
292 |
* assigned (which can only happen due to a #become:, |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
293 |
* or by creating a symbol uninterned, and interning it |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
294 |
* after it got a hashKey assigned. |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
295 |
*/ |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
296 |
if (val == 0) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
297 |
cp = __stringVal(self); |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
298 |
l = __stringSize(self); |
356 | 299 |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
300 |
/* |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
301 |
* this is the dragon-book algorithm |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
302 |
* We have tested 5-bit shifts as well: |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
303 |
* |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
304 |
* ST/X Symbols: 17807 |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
305 |
* Hashkey collisions (4bit): 14 0.07% |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
306 |
* Hashkey collisions (5bit): 300 1.68% |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
307 |
*/ |
356 | 308 |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
309 |
if (l > 0) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
310 |
val = cp[0]; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
311 |
if (l > 1) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
312 |
val = (val << 4) + cp[1]; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
313 |
if (l > 2) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
314 |
val = (val << 4) + cp[2]; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
315 |
if (l > 3) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
316 |
val = (val << 4) + cp[3]; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
317 |
if (l > 4) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
318 |
val = (val << 4) + cp[4]; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
319 |
if (l > 5) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
320 |
val = (val << 4) + cp[5]; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
321 |
if (l > 6) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
322 |
val = (val << 4) + cp[6]; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
323 |
for (ce = cp + l, cp += 7; cp < ce; cp++) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
324 |
if (g = (val & 0xF0000000)) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
325 |
val ^= g >> 24; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
326 |
val ^= g; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
327 |
} |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
328 |
val = (val << 4) + *cp; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
329 |
} |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
330 |
} |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
331 |
} |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
332 |
} |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
333 |
} |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
334 |
} |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
335 |
} |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
336 |
} else { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
337 |
val = 0; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
338 |
} |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
339 |
val = (val * 31415821) & 0x3fffffff; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
340 |
} else { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
341 |
val = __MAKE_HASH__(val); |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
342 |
} |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
343 |
RETURN ( __MKSMALLINT(val) ); |
356 | 344 |
} |
345 |
%}. |
|
346 |
^ super identityHash |
|
3985 | 347 |
|
348 |
" |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
349 |
|hashColl hashSet| |
3985 | 350 |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
351 |
hashColl := OrderedCollection new:20000. |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
352 |
Symbol allInstancesDo:[:instance | |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
353 |
hashColl add:instance identityHash |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
354 |
]. |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
355 |
hashSet := hashColl asSet. |
3985 | 356 |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
357 |
Transcript showCR:'Symbols: ', hashColl size printString, |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
358 |
' unique hash keys: ', hashSet size printString, |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
359 |
' collisions:', (hashColl size - hashSet size) printString. |
3985 | 360 |
" |
361 |
||
370 | 362 |
! |
363 |
||
364 |
~= something |
|
365 |
"return true, if the receiver and argument do not consist of the same characters. |
|
366 |
Redefined here, for more efficient #~= comparison of symbols |
|
367 |
(which ought to be compared using #~~). |
|
368 |
If the argument is a symbol, we use a quick pointer compare, instead of |
|
369 |
the inherited value compare." |
|
370 |
||
371 |
%{ /* NOCONTEXT */ |
|
372 |
OBJ cls; |
|
373 |
||
379 | 374 |
if (! __isNonNilObject(something)) RETURN(true); /* cannot be equal */ |
370 | 375 |
if ((cls = __qClass(something)) == Symbol) { |
379 | 376 |
RETURN (self == something ? false : true); |
370 | 377 |
} |
378 |
if (cls == String) { |
|
379 | 379 |
RETURN (strcmp(__stringVal(self), __stringVal(something)) == 0 ? false : true); |
370 | 380 |
} |
381 |
%}. |
|
382 |
"fall back; could be a TwoByteString, or a collection of Characters" |
|
383 |
||
384 |
^ super ~= something |
|
356 | 385 |
! ! |
386 |
||
608 | 387 |
!Symbol methodsFor:'converting'! |
388 |
||
389 |
asString |
|
390 |
"return a string with printname taken from mine" |
|
391 |
||
392 |
%{ /* NOCONTEXT */ |
|
393 |
OBJ s; |
|
394 |
||
2565 | 395 |
s = __MKSTRING_ST(self); |
608 | 396 |
if (s != nil) { |
397 |
RETURN (s); |
|
398 |
} |
|
399 |
%}. |
|
400 |
" |
|
401 |
memory allocation (for the new string) failed. |
|
402 |
When we arrive here, there was no memory, even after a garbage collect. |
|
403 |
This means, that the VM wanted to get some more memory from the |
|
404 |
OS, which was not kind enough to give it. |
|
405 |
Bad luck - you should increase the swap space on your machine, |
|
406 |
or if you have set a soft memoryLimit, increase it. |
|
407 |
" |
|
408 |
^ ObjectMemory allocationFailureSignal raise. |
|
409 |
! |
|
410 |
||
411 |
asSymbol |
|
412 |
"return the receiver as a symbol. |
|
413 |
Since I am a symbol - just return myself" |
|
414 |
||
415 |
^ self |
|
416 |
! |
|
417 |
||
418 |
asSymbolIfInterned |
|
419 |
"return the receiver as a symbol if there is one. |
|
420 |
Since I am a symbol - just return myself" |
|
421 |
||
422 |
^ self |
|
2416 | 423 |
! |
424 |
||
425 |
decodeAsLiteralArray |
|
426 |
"given a literalEncoding in the receiver, |
|
427 |
create & return the corresponding object. |
|
428 |
The inverse operation to #literalArrayEncoding." |
|
429 |
||
430 |
^ self |
|
431 |
||
432 |
"Created: 25.2.1997 / 19:15:26 / cg" |
|
433 |
"Modified: 25.2.1997 / 19:17:40 / cg" |
|
608 | 434 |
! ! |
435 |
||
436 |
!Symbol methodsFor:'copying'! |
|
437 |
||
438 |
copy |
|
439 |
"return a copy of myself |
|
4729 | 440 |
- reimplemented here since symbols are immutable." |
608 | 441 |
|
442 |
^ self |
|
443 |
! |
|
444 |
||
3659 | 445 |
copyReplaceAll:oldElement with:newElement |
446 |
"return a copy of the receiver as a string, where all elements equal to oldElement |
|
447 |
have been replaced by newElement." |
|
448 |
||
449 |
^ self asString replaceAll:oldElement with:newElement |
|
450 |
||
451 |
"Created: / 18.7.1998 / 23:03:38 / cg" |
|
452 |
! |
|
453 |
||
608 | 454 |
deepCopy |
455 |
"return a copy of myself |
|
4729 | 456 |
- reimplemented here since symbols are immutable." |
608 | 457 |
|
458 |
^ self |
|
459 |
! |
|
460 |
||
461 |
deepCopyUsing:aDictionary |
|
462 |
"return a copy of myself |
|
4729 | 463 |
- reimplemented here since symbols are immutable." |
608 | 464 |
|
465 |
^ self |
|
466 |
! |
|
467 |
||
468 |
shallowCopy |
|
469 |
"return a copy of myself |
|
4729 | 470 |
- reimplemented here since symbols are immutable" |
608 | 471 |
|
472 |
^ self |
|
473 |
! |
|
474 |
||
475 |
simpleDeepCopy |
|
476 |
"return a copy of myself |
|
4729 | 477 |
- reimplemented here since symbols are immutable." |
608 | 478 |
|
479 |
^ self |
|
480 |
! ! |
|
481 |
||
482 |
!Symbol methodsFor:'printing & storing'! |
|
483 |
||
484 |
displayString |
|
485 |
"return a string for displaying the receiver" |
|
486 |
||
487 |
^ self storeString |
|
488 |
! |
|
489 |
||
490 |
printOn:aStream |
|
4392 | 491 |
"append a user printed representation of the receiver to aStream. |
492 |
The format is suitable for a human - not meant to be read back. |
|
493 |
||
608 | 494 |
In contrast to ST-80, this does return the symbols characters |
495 |
without any leading #. Therefore, you can directly send symbols' |
|
496 |
printStrings to some output device. This is incompatible, but easier |
|
497 |
to use." |
|
498 |
||
499 |
"/ ST-80 behavior: |
|
500 |
||
501 |
"/ self storeOn:aStream |
|
502 |
||
503 |
aStream nextPutAll:self |
|
504 |
! |
|
505 |
||
506 |
printString |
|
507 |
"return a printed representation of the receiver. |
|
508 |
In contrast to ST-80, this does return the symbols characters |
|
509 |
without any leading #. Therefore, you can directly send symbols' |
|
510 |
printStrings to some output device. This is incompatible, but easier |
|
511 |
to use." |
|
512 |
||
513 |
"/ ST-80 behavior: |
|
514 |
"/ ^ self storeString |
|
515 |
||
516 |
^ self asString |
|
517 |
! |
|
518 |
||
519 |
storeOn:aStream |
|
520 |
"store myself on a stream" |
|
521 |
||
522 |
aStream nextPutAll:(self storeString) |
|
523 |
! |
|
524 |
||
525 |
storeString |
|
526 |
"return a String for storing the receiver" |
|
527 |
||
4098 | 528 |
|sz "{Class: SmallInteger }" |
529 |
c| |
|
1125 | 530 |
|
531 |
(sz := self size) > 0 ifTrue:[ "/ sigh |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
532 |
(self at:1) isLetter ifTrue:[ |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
533 |
2 to:sz do:[:index | |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
534 |
((c := self at:index) isLetterOrDigit |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
535 |
or:[c == $: |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
536 |
and:[index == sz |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
537 |
or:[(self at:(index+1)) isLetterOrDigit]]] |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
538 |
) ifFalse:[ |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
539 |
^ '#''' , self , '''' |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
540 |
]. |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
541 |
]. |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
542 |
^ '#' , self |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
543 |
] |
608 | 544 |
]. |
545 |
^ '#''' , self , '''' |
|
3285
bd5827c97b21
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3086
diff
changeset
|
546 |
|
3286
c6987c5f15a8
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3285
diff
changeset
|
547 |
" |
4098 | 548 |
#'abc' storeString |
549 |
#'abc:' storeString |
|
550 |
#'abc:def:' storeString |
|
551 |
#'abc:def' storeString |
|
552 |
#'abc::def' storeString |
|
553 |
#'abc &^*' storeString |
|
554 |
#'abcdef::' storeString |
|
3286
c6987c5f15a8
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3285
diff
changeset
|
555 |
" |
c6987c5f15a8
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3285
diff
changeset
|
556 |
|
c6987c5f15a8
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3285
diff
changeset
|
557 |
"Modified: / 13.2.1998 / 22:06:15 / stefan" |
608 | 558 |
! ! |
559 |
||
560 |
!Symbol methodsFor:'queries'! |
|
561 |
||
4987 | 562 |
isInfix |
563 |
"return true, if the receiver is a binary message selector" |
|
564 |
||
565 |
^ self first isAlphaNumeric not |
|
566 |
||
567 |
" |
|
568 |
#at:put: isInfix |
|
569 |
#at: isInfix |
|
570 |
#+ isInfix |
|
571 |
#size isInfix |
|
572 |
" |
|
573 |
||
574 |
"Created: / 1.11.1997 / 12:34:55 / cg" |
|
575 |
"Modified: / 1.11.1997 / 12:36:37 / cg" |
|
576 |
! |
|
577 |
||
3086 | 578 |
isKeyword |
579 |
"return true, if the receiver is a keyword message selector" |
|
580 |
||
581 |
^ self includes:$: |
|
582 |
||
583 |
" |
|
584 |
#at:put: isKeyword |
|
585 |
#at: isKeyword |
|
586 |
#+ isKeyword |
|
587 |
#size isKeyword |
|
588 |
" |
|
589 |
||
590 |
"Created: / 1.11.1997 / 12:34:55 / cg" |
|
591 |
"Modified: / 1.11.1997 / 12:36:37 / cg" |
|
592 |
! |
|
593 |
||
608 | 594 |
isSymbol |
595 |
"return true, if the receiver is some kind of symbol. |
|
596 |
Since I am a symbol, return always true" |
|
597 |
||
598 |
^ true |
|
599 |
! |
|
600 |
||
601 |
keywords |
|
602 |
"assuming the receiver is a keyword message selector, |
|
603 |
return the individual keywords (i.e. break it up at colons) |
|
604 |
and return these as a collection. |
|
605 |
For binary and unary selectors, the result may be nonsense." |
|
606 |
||
607 |
|coll s part| |
|
608 |
||
609 |
coll := OrderedCollection new. |
|
610 |
s := ReadStream on:self. |
|
611 |
[s atEnd] whileFalse:[ |
|
612 |
part := s through:$:. |
|
613 |
coll add:part |
|
614 |
]. |
|
615 |
^ coll asArray |
|
616 |
||
617 |
" |
|
618 |
#at:put: keywords |
|
619 |
#at: keywords |
|
620 |
#+ keywords |
|
621 |
#size keywords |
|
622 |
" |
|
623 |
! |
|
624 |
||
625 |
species |
|
626 |
"when copying, or concatenating, return instances of this class" |
|
627 |
||
628 |
^ String |
|
629 |
! ! |
|
630 |
||
77 | 631 |
!Symbol methodsFor:'system primitives'! |
632 |
||
633 |
become:anotherObject |
|
634 |
"make all references to the receiver become references to anotherObject |
|
95 | 635 |
and vice-versa. For symbols, some special action is required, to |
636 |
correctly handle a become of the global dictionaries. |
|
77 | 637 |
Anyway: this is very dangerous - mysterous side-effects are to be |
95 | 638 |
expected. |
639 |
||
640 |
Notice: because of the danger here, this method may report an error |
|
159 | 641 |
in future versions" |
77 | 642 |
|
643 |
(Smalltalk includesKey:self) ifTrue:[ |
|
159 | 644 |
super become:anotherObject. |
77 | 645 |
%{ |
159 | 646 |
__rehashSystemDictionaries(); |
77 | 647 |
%}. |
648 |
] ifFalse:[ |
|
159 | 649 |
super become:anotherObject |
77 | 650 |
] |
651 |
! |
|
652 |
||
653 |
becomeNil |
|
654 |
"make all references to the receiver become nil - effectively getting |
|
95 | 655 |
rid of the receiver. For symbols, this is not allowed, if the receiver |
77 | 656 |
is used as a key in some SytemDictionary. |
95 | 657 |
This can be a very dangerous operation - be warned. |
658 |
||
659 |
Notice: because of the danger here, this method may report an error |
|
159 | 660 |
in future versions" |
77 | 661 |
|
662 |
(Smalltalk includesKey:self) ifTrue:[ |
|
159 | 663 |
self primitiveFailed |
77 | 664 |
] ifFalse:[ |
159 | 665 |
super becomeNil |
77 | 666 |
] |
667 |
! ! |
|
668 |
||
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
669 |
!Symbol methodsFor:'tracing'! |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
670 |
|
4682 | 671 |
traceInto:aRequestor level:level from:referrer |
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
672 |
"double dispatch into tracer, passing my type implicitely in the selector" |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
673 |
|
4682 | 674 |
^ aRequestor traceSymbol:self level:level from:referrer |
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
675 |
|
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
676 |
|
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
677 |
! ! |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
678 |
|
632 | 679 |
!Symbol class methodsFor:'documentation'! |
680 |
||
681 |
version |
|
4987 | 682 |
^ '$Header: /cvs/stx/stx/libbasic/Symbol.st,v 1.61 1999-11-13 12:16:32 cg Exp $' |
632 | 683 |
! ! |