author | Claus Gittinger <cg@exept.de> |
Fri, 07 Jul 2000 16:13:34 +0200 | |
changeset 5449 | bf1de72fd2bb |
parent 5434 | c496158fea85 |
child 5471 | a57eeb01c5ab |
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 |
||
5434 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
3985 | 15 |
String variableByteSubclass:#Symbol |
1020 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Text' |
|
1 | 20 |
! |
21 |
||
77 | 22 |
!Symbol class methodsFor:'documentation'! |
1 | 23 |
|
88 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
159 | 27 |
All Rights Reserved |
88 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
77 | 38 |
documentation |
39 |
" |
|
40 |
Symbols represent unique strings - every symbol with same printString |
|
41 |
exists exactly once in the system; Symbols are used for selectors, global |
|
345 | 42 |
variable-keys etc. Symbols can also be used to represent things which are |
43 |
enumeration type values in other programming languages (since symbols are |
|
370 | 44 |
created at compile time, comparing them using == is a fast pointer compare). |
345 | 45 |
|
46 |
A symbol may not change its characters - i.e. it is constant over its lifetime. |
|
47 |
Other than that, symbols behave much like strings. |
|
1290 | 48 |
|
49 |
[author:] |
|
2200 | 50 |
Claus Gittinger |
77 | 51 |
" |
52 |
! ! |
|
1 | 53 |
|
77 | 54 |
!Symbol class methodsFor:'instance creation'! |
1 | 55 |
|
62 | 56 |
basicNew:size |
57 |
"redefined to return a string instead of a symbol - |
|
58 |
this allows all copy methods inherited from String to |
|
59 |
return strings containing the symbols characters. |
|
1020 | 60 |
Real symbols are only created with #intern: or #asSymbol." |
62 | 61 |
|
62 |
^ String new:size |
|
1020 | 63 |
|
64 |
"Modified: 26.2.1996 / 12:51:38 / cg" |
|
62 | 65 |
! |
66 |
||
77 | 67 |
fromString:aString |
68 |
"same as intern: for Symbol, but may be used to create interned instances |
|
69 |
of subclasses. |
|
70 |
Notice: this fails, if you try to intern an instance of a subclass, AND |
|
71 |
a symbol with the same name already exists. In this case, the original |
|
72 |
symbol is returned. To use it for enum-type symbols, make certain, that the |
|
73 |
names are unique (for example by including the classes name as a prefix-string)." |
|
74 |
||
267 | 75 |
|sym len| |
77 | 76 |
|
267 | 77 |
sym := aString asSymbolIfInterned. |
78 |
sym notNil ifTrue:[ |
|
79 |
^ sym |
|
77 | 80 |
]. |
81 |
||
82 |
" |
|
83 |
create a new uninterned instance first |
|
84 |
" |
|
85 |
len := aString size. |
|
267 | 86 |
sym := super basicNew:len. |
87 |
sym replaceFrom:1 to:len with:aString. |
|
77 | 88 |
|
89 |
"now, intern it" |
|
90 |
%{ |
|
329 | 91 |
sym = __INTERNSYMBOL(sym, (OBJ *)0, __context); |
77 | 92 |
%}. |
267 | 93 |
^ sym |
77 | 94 |
! |
95 |
||
608 | 96 |
intern:aString |
97 |
"return a unique symbol with printname taken from the String-argument" |
|
98 |
||
99 |
%{ /* NOCONTEXT */ |
|
100 |
OBJ newSymbol; |
|
101 |
||
102 |
if (__isSymbol(aString)) { |
|
103 |
RETURN (aString); |
|
104 |
} |
|
105 |
if (__isString(aString)) { |
|
2200 | 106 |
newSymbol = __MKSYMBOL(__stringVal(aString), (OBJ *)0); |
608 | 107 |
if (newSymbol != nil) { |
108 |
RETURN (newSymbol); |
|
109 |
} |
|
110 |
} |
|
111 |
%}. |
|
112 |
aString class ~~ String ifTrue:[ |
|
113 |
"/ |
|
114 |
"/ only allowed to intern strings |
|
115 |
"/ |
|
116 |
^ self mustBeString |
|
117 |
]. |
|
118 |
^ ObjectMemory allocationFailureSignal raise. |
|
119 |
! |
|
120 |
||
1 | 121 |
internCharacter:aCharacter |
122 |
"return a unique symbol with printname taken from the Character-argument" |
|
123 |
||
124 |
^ self intern:(aCharacter asString) |
|
608 | 125 |
! |
126 |
||
127 |
new:size |
|
128 |
"redefined to return a string instead of a symbol - |
|
129 |
this allows all copy methods inherited from String to |
|
130 |
return strings containing the symbols characters. |
|
131 |
Real symbols are only created with intern: or asSymbol." |
|
132 |
||
133 |
^ String new:size |
|
134 |
! ! |
|
135 |
||
5185
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
136 |
!Symbol class methodsFor:'Compatibility - ST80'! |
2570
26616a00d5aa
added #tableSize for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2565
diff
changeset
|
137 |
|
26616a00d5aa
added #tableSize for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2565
diff
changeset
|
138 |
tableSize |
26616a00d5aa
added #tableSize for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2565
diff
changeset
|
139 |
"return the size of the systems symbol table" |
26616a00d5aa
added #tableSize for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2565
diff
changeset
|
140 |
|
4838 | 141 |
"/ claus: I dont know, if the returned value should be exact. |
142 |
"/ the number below is just arbitrary ... |
|
2570
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 |
^ 10000 |
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 |
"Created: 18.4.1997 / 20:52:20 / cg" |
26616a00d5aa
added #tableSize for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2565
diff
changeset
|
147 |
! ! |
26616a00d5aa
added #tableSize for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2565
diff
changeset
|
148 |
|
608 | 149 |
!Symbol class methodsFor:'binary storage'! |
150 |
||
1253 | 151 |
binaryDefinitionFrom:stream manager:manager |
152 |
"retrieve a symbol from a binary input stream (nonpublic interface)" |
|
153 |
||
154 |
^ self intern:(super binaryDefinitionFrom:stream manager:manager) |
|
155 |
||
3086 | 156 |
"Modified: / 2.11.1997 / 16:17:06 / cg" |
1 | 157 |
! ! |
158 |
||
159 |
!Symbol class methodsFor:'queries'! |
|
160 |
||
608 | 161 |
findInterned:aString |
162 |
"for ST-80 compatibility - if the argument, aString is known |
|
163 |
as Symbol, return this symbol. Otherwise return nil." |
|
164 |
||
165 |
^ aString asSymbolIfInterned |
|
166 |
! |
|
167 |
||
168 |
hasInterned:aString |
|
169 |
"return true, if the argument, aString is known as Symbol; |
|
170 |
false otherwise" |
|
171 |
||
172 |
^ aString knownAsSymbol |
|
173 |
! |
|
174 |
||
1 | 175 |
hasInterned:aString ifTrue:trueBlock |
176 |
"for ST-80 compatibility - if the argument, aString is known |
|
177 |
as Symbol, evaluate the block with the corresponding symbol |
|
178 |
as argument and return true; otherwise return false" |
|
179 |
||
275 | 180 |
|sym| |
181 |
||
182 |
(sym := aString asSymbolIfInterned) notNil ifTrue:[ |
|
183 |
trueBlock value:sym. |
|
159 | 184 |
^ true |
1 | 185 |
]. |
186 |
^ false |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
187 |
! |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
188 |
|
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
189 |
hasSharedInstances |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
190 |
"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
|
191 |
with the same value are identical. |
4654 | 192 |
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
|
193 |
|
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
194 |
^ true |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
195 |
|
1 | 196 |
! ! |
197 |
||
198 |
!Symbol methodsFor:'accessing'! |
|
199 |
||
200 |
basicAt:index put:something |
|
345 | 201 |
"report an error if an interned symbol is about to be changed |
1219 | 202 |
- interned symbols may NOT be changed. |
203 |
For uninterned symbols, this is allowed." |
|
1 | 204 |
|
77 | 205 |
self knownAsSymbol ifTrue:[ |
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
206 |
self error:'interned symbols may not be changed'. |
77 | 207 |
]. |
208 |
" |
|
209 |
uninterned - allow change |
|
210 |
" |
|
211 |
^ super basicAt:index put:something |
|
1219 | 212 |
|
213 |
"Modified: 19.4.1996 / 11:16:10 / cg" |
|
252 | 214 |
! |
215 |
||
216 |
byteAt:index put:value |
|
217 |
"report an error if interned - interned symbols may not be changed." |
|
218 |
||
219 |
^ self basicAt:index put:(value asCharacter) |
|
1 | 220 |
! ! |
221 |
||
3086 | 222 |
!Symbol methodsFor:'binary storage'! |
223 |
||
224 |
storeBinaryDefinitionOn:stream manager:manager |
|
225 |
"append a binary representation of the receiver onto stream. |
|
226 |
Redefined since short Symbol can be stored with a special type code |
|
227 |
in a more compact way. |
|
228 |
This is an internal interface for the binary storage mechanism." |
|
229 |
||
230 |
|myClass myBasicSize| |
|
231 |
||
232 |
"/ not for subclasses with named instVars. |
|
233 |
(myClass := self class) instSize ~~ 0 ifTrue:[ |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
234 |
^ super storeBinaryDefinitionOn:stream manager:manager |
3086 | 235 |
]. |
236 |
||
237 |
myBasicSize := self basicSize. |
|
238 |
||
239 |
"/ can use a more compact representation; |
|
240 |
"/ but not for subclasses ... |
|
241 |
||
242 |
((myClass == Symbol) |
|
243 |
and:[myBasicSize <= 255]) ifTrue:[ |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
244 |
"/ special encoding: <codeForSymbol> <len> <bytes> ... |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
245 |
stream nextPut:(manager codeForSymbol); nextPut:myBasicSize. |
3086 | 246 |
] ifFalse:[ |
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
247 |
manager putIdOfClass:myClass on:stream. |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
248 |
stream nextNumber:4 put:myBasicSize. |
3086 | 249 |
]. |
250 |
stream nextPutBytes:myBasicSize from:self startingAt:1. |
|
251 |
||
252 |
"Modified: / 2.11.1997 / 15:28:56 / cg" |
|
253 |
"Created: / 2.11.1997 / 16:13:47 / cg" |
|
254 |
! ! |
|
255 |
||
608 | 256 |
!Symbol methodsFor:'comparing'! |
159 | 257 |
|
608 | 258 |
= something |
259 |
"return true, if the receiver and argument consist of the same characters. |
|
260 |
Redefined here, for more efficient #= comparison of symbols |
|
261 |
(which ought to be compared using #==). |
|
262 |
If the argument is a symbol, we use a quick pointer compare, instead of |
|
263 |
the inherited value compare." |
|
13 | 264 |
|
608 | 265 |
%{ /* NOCONTEXT */ |
266 |
OBJ cls; |
|
13 | 267 |
|
608 | 268 |
if (! __isNonNilObject(something)) RETURN(false); |
269 |
if ((cls = __qClass(something)) == Symbol) { |
|
270 |
RETURN (self == something ? true : false); |
|
271 |
} |
|
272 |
if (cls == String) { |
|
273 |
RETURN (strcmp(__stringVal(self), __stringVal(something)) == 0 ? true : false); |
|
345 | 274 |
} |
275 |
%}. |
|
608 | 276 |
"fall back; could be a TwoByteString, or a collection of Characters" |
1 | 277 |
|
608 | 278 |
^ super = something |
427 | 279 |
! |
280 |
||
356 | 281 |
identityHash |
282 |
"interned symbols can return a better hash key" |
|
283 |
||
284 |
%{ /* NOCONTEXT */ |
|
285 |
||
3985 | 286 |
REGISTER unsigned int g, val; |
287 |
REGISTER unsigned char *cp, *ce; |
|
356 | 288 |
int l; |
289 |
||
290 |
if (__Class(self) == Symbol) { |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
291 |
val = __GET_HASH(self); |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
292 |
/* |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
293 |
* only do it, if I have no standard hash key |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
294 |
* assigned (which can only happen due to a #become:, |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
295 |
* or by creating a symbol uninterned, and interning it |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
296 |
* after it got a hashKey assigned. |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
297 |
*/ |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
298 |
if (val == 0) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
299 |
cp = __stringVal(self); |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
300 |
l = __stringSize(self); |
356 | 301 |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
302 |
/* |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
303 |
* this is the dragon-book algorithm |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
304 |
* We have tested 5-bit shifts as well: |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
305 |
* |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
306 |
* ST/X Symbols: 17807 |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
307 |
* Hashkey collisions (4bit): 14 0.07% |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
308 |
* Hashkey collisions (5bit): 300 1.68% |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
309 |
*/ |
356 | 310 |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
311 |
if (l > 0) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
312 |
val = cp[0]; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
313 |
if (l > 1) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
314 |
val = (val << 4) + cp[1]; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
315 |
if (l > 2) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
316 |
val = (val << 4) + cp[2]; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
317 |
if (l > 3) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
318 |
val = (val << 4) + cp[3]; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
319 |
if (l > 4) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
320 |
val = (val << 4) + cp[4]; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
321 |
if (l > 5) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
322 |
val = (val << 4) + cp[5]; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
323 |
if (l > 6) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
324 |
val = (val << 4) + cp[6]; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
325 |
for (ce = cp + l, cp += 7; cp < ce; cp++) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
326 |
if (g = (val & 0xF0000000)) { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
327 |
val ^= g >> 24; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
328 |
val ^= g; |
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 |
val = (val << 4) + *cp; |
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 |
} |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
337 |
} |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
338 |
} else { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
339 |
val = 0; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
340 |
} |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
341 |
val = (val * 31415821) & 0x3fffffff; |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
342 |
} else { |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
343 |
val = __MAKE_HASH__(val); |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
344 |
} |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
345 |
RETURN ( __MKSMALLINT(val) ); |
356 | 346 |
} |
347 |
%}. |
|
348 |
^ super identityHash |
|
3985 | 349 |
|
350 |
" |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
351 |
|hashColl hashSet| |
3985 | 352 |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
353 |
hashColl := OrderedCollection new:20000. |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
354 |
Symbol allInstancesDo:[:instance | |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
355 |
hashColl add:instance identityHash |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
356 |
]. |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
357 |
hashSet := hashColl asSet. |
3985 | 358 |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
359 |
Transcript showCR:'Symbols: ', hashColl size printString, |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
360 |
' unique hash keys: ', hashSet size printString, |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
361 |
' collisions:', (hashColl size - hashSet size) printString. |
3985 | 362 |
" |
363 |
||
370 | 364 |
! |
365 |
||
366 |
~= something |
|
367 |
"return true, if the receiver and argument do not consist of the same characters. |
|
368 |
Redefined here, for more efficient #~= comparison of symbols |
|
369 |
(which ought to be compared using #~~). |
|
370 |
If the argument is a symbol, we use a quick pointer compare, instead of |
|
371 |
the inherited value compare." |
|
372 |
||
373 |
%{ /* NOCONTEXT */ |
|
374 |
OBJ cls; |
|
375 |
||
379 | 376 |
if (! __isNonNilObject(something)) RETURN(true); /* cannot be equal */ |
370 | 377 |
if ((cls = __qClass(something)) == Symbol) { |
379 | 378 |
RETURN (self == something ? false : true); |
370 | 379 |
} |
380 |
if (cls == String) { |
|
379 | 381 |
RETURN (strcmp(__stringVal(self), __stringVal(something)) == 0 ? false : true); |
370 | 382 |
} |
383 |
%}. |
|
384 |
"fall back; could be a TwoByteString, or a collection of Characters" |
|
385 |
||
386 |
^ super ~= something |
|
356 | 387 |
! ! |
388 |
||
608 | 389 |
!Symbol methodsFor:'converting'! |
390 |
||
391 |
asString |
|
392 |
"return a string with printname taken from mine" |
|
393 |
||
394 |
%{ /* NOCONTEXT */ |
|
395 |
OBJ s; |
|
396 |
||
2565 | 397 |
s = __MKSTRING_ST(self); |
608 | 398 |
if (s != nil) { |
5434 | 399 |
RETURN (s); |
608 | 400 |
} |
401 |
%}. |
|
5434 | 402 |
^ (String new:(self size)) |
403 |
replaceFrom:1 with:self startingAt:1 |
|
608 | 404 |
! |
405 |
||
406 |
asSymbol |
|
407 |
"return the receiver as a symbol. |
|
408 |
Since I am a symbol - just return myself" |
|
409 |
||
410 |
^ self |
|
411 |
! |
|
412 |
||
413 |
asSymbolIfInterned |
|
414 |
"return the receiver as a symbol if there is one. |
|
415 |
Since I am a symbol - just return myself" |
|
416 |
||
417 |
^ self |
|
2416 | 418 |
! |
419 |
||
420 |
decodeAsLiteralArray |
|
421 |
"given a literalEncoding in the receiver, |
|
422 |
create & return the corresponding object. |
|
423 |
The inverse operation to #literalArrayEncoding." |
|
424 |
||
425 |
^ self |
|
426 |
||
427 |
"Created: 25.2.1997 / 19:15:26 / cg" |
|
428 |
"Modified: 25.2.1997 / 19:17:40 / cg" |
|
608 | 429 |
! ! |
430 |
||
431 |
!Symbol methodsFor:'copying'! |
|
432 |
||
433 |
copy |
|
434 |
"return a copy of myself |
|
4729 | 435 |
- reimplemented here since symbols are immutable." |
608 | 436 |
|
437 |
^ self |
|
438 |
! |
|
439 |
||
3659 | 440 |
copyReplaceAll:oldElement with:newElement |
441 |
"return a copy of the receiver as a string, where all elements equal to oldElement |
|
442 |
have been replaced by newElement." |
|
443 |
||
444 |
^ self asString replaceAll:oldElement with:newElement |
|
445 |
||
446 |
"Created: / 18.7.1998 / 23:03:38 / cg" |
|
447 |
! |
|
448 |
||
608 | 449 |
deepCopy |
450 |
"return a copy of myself |
|
4729 | 451 |
- reimplemented here since symbols are immutable." |
608 | 452 |
|
453 |
^ self |
|
454 |
! |
|
455 |
||
456 |
deepCopyUsing:aDictionary |
|
457 |
"return a copy of myself |
|
4729 | 458 |
- reimplemented here since symbols are immutable." |
608 | 459 |
|
460 |
^ self |
|
461 |
! |
|
462 |
||
463 |
shallowCopy |
|
464 |
"return a copy of myself |
|
4729 | 465 |
- reimplemented here since symbols are immutable" |
608 | 466 |
|
467 |
^ self |
|
468 |
! |
|
469 |
||
470 |
simpleDeepCopy |
|
471 |
"return a copy of myself |
|
4729 | 472 |
- reimplemented here since symbols are immutable." |
608 | 473 |
|
474 |
^ self |
|
475 |
! ! |
|
476 |
||
477 |
!Symbol methodsFor:'printing & storing'! |
|
478 |
||
479 |
displayString |
|
480 |
"return a string for displaying the receiver" |
|
481 |
||
482 |
^ self storeString |
|
483 |
! |
|
484 |
||
485 |
printOn:aStream |
|
4392 | 486 |
"append a user printed representation of the receiver to aStream. |
487 |
The format is suitable for a human - not meant to be read back. |
|
488 |
||
608 | 489 |
In contrast to ST-80, this does return the symbols characters |
490 |
without any leading #. Therefore, you can directly send symbols' |
|
491 |
printStrings to some output device. This is incompatible, but easier |
|
492 |
to use." |
|
493 |
||
494 |
"/ ST-80 behavior: |
|
495 |
||
496 |
"/ self storeOn:aStream |
|
497 |
||
498 |
aStream nextPutAll:self |
|
499 |
! |
|
500 |
||
501 |
printString |
|
502 |
"return a printed representation of the receiver. |
|
503 |
In contrast to ST-80, this does return the symbols characters |
|
504 |
without any leading #. Therefore, you can directly send symbols' |
|
505 |
printStrings to some output device. This is incompatible, but easier |
|
506 |
to use." |
|
507 |
||
508 |
"/ ST-80 behavior: |
|
509 |
"/ ^ self storeString |
|
510 |
||
511 |
^ self asString |
|
512 |
! |
|
513 |
||
514 |
storeOn:aStream |
|
515 |
"store myself on a stream" |
|
516 |
||
517 |
aStream nextPutAll:(self storeString) |
|
518 |
! |
|
519 |
||
520 |
storeString |
|
521 |
"return a String for storing the receiver" |
|
522 |
||
4098 | 523 |
|sz "{Class: SmallInteger }" |
524 |
c| |
|
1125 | 525 |
|
526 |
(sz := self size) > 0 ifTrue:[ "/ sigh |
|
5185
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
527 |
(self at:1) isLetter ifTrue:[ |
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
528 |
2 to:sz do:[:index | |
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
529 |
((c := self at:index) isLetterOrDigit |
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
530 |
or:[c == $: |
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
531 |
and:[index == sz |
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
532 |
or:[(self at:(index+1)) isLetterOrDigit]]] |
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
533 |
) ifFalse:[ |
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
534 |
^ '#' , super storeString |
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
535 |
]. |
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
536 |
]. |
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
537 |
^ '#' , self |
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
538 |
] |
608 | 539 |
]. |
540 |
^ '#''' , self , '''' |
|
3285
bd5827c97b21
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3086
diff
changeset
|
541 |
|
3286
c6987c5f15a8
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3285
diff
changeset
|
542 |
" |
4098 | 543 |
#'abc' storeString |
544 |
#'abc:' storeString |
|
545 |
#'abc:def:' storeString |
|
546 |
#'abc:def' storeString |
|
547 |
#'abc::def' storeString |
|
548 |
#'abc &^*' storeString |
|
549 |
#'abcdef::' storeString |
|
5185
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
550 |
#'hello''world' storeString |
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
551 |
#'' storeString |
15a129151b3b
fixed bug in storeString (must duplicate single quotes)
Claus Gittinger <cg@exept.de>
parents:
4987
diff
changeset
|
552 |
#'_hello' storeString |
3286
c6987c5f15a8
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3285
diff
changeset
|
553 |
" |
c6987c5f15a8
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3285
diff
changeset
|
554 |
|
c6987c5f15a8
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3285
diff
changeset
|
555 |
"Modified: / 13.2.1998 / 22:06:15 / stefan" |
608 | 556 |
! ! |
557 |
||
558 |
!Symbol methodsFor:'queries'! |
|
559 |
||
4987 | 560 |
isInfix |
561 |
"return true, if the receiver is a binary message selector" |
|
562 |
||
563 |
^ self first isAlphaNumeric not |
|
564 |
||
565 |
" |
|
566 |
#at:put: isInfix |
|
567 |
#at: isInfix |
|
568 |
#+ isInfix |
|
569 |
#size isInfix |
|
570 |
" |
|
571 |
||
572 |
"Created: / 1.11.1997 / 12:34:55 / cg" |
|
573 |
"Modified: / 1.11.1997 / 12:36:37 / cg" |
|
574 |
! |
|
575 |
||
3086 | 576 |
isKeyword |
577 |
"return true, if the receiver is a keyword message selector" |
|
578 |
||
579 |
^ self includes:$: |
|
580 |
||
581 |
" |
|
582 |
#at:put: isKeyword |
|
583 |
#at: isKeyword |
|
584 |
#+ isKeyword |
|
585 |
#size isKeyword |
|
586 |
" |
|
587 |
||
588 |
"Created: / 1.11.1997 / 12:34:55 / cg" |
|
589 |
"Modified: / 1.11.1997 / 12:36:37 / cg" |
|
590 |
! |
|
591 |
||
608 | 592 |
isSymbol |
593 |
"return true, if the receiver is some kind of symbol. |
|
594 |
Since I am a symbol, return always true" |
|
595 |
||
596 |
^ true |
|
597 |
! |
|
598 |
||
599 |
keywords |
|
600 |
"assuming the receiver is a keyword message selector, |
|
601 |
return the individual keywords (i.e. break it up at colons) |
|
602 |
and return these as a collection. |
|
603 |
For binary and unary selectors, the result may be nonsense." |
|
604 |
||
605 |
|coll s part| |
|
606 |
||
607 |
coll := OrderedCollection new. |
|
608 |
s := ReadStream on:self. |
|
609 |
[s atEnd] whileFalse:[ |
|
610 |
part := s through:$:. |
|
611 |
coll add:part |
|
612 |
]. |
|
613 |
^ coll asArray |
|
614 |
||
615 |
" |
|
616 |
#at:put: keywords |
|
617 |
#at: keywords |
|
618 |
#+ keywords |
|
619 |
#size keywords |
|
620 |
" |
|
621 |
! |
|
622 |
||
623 |
species |
|
624 |
"when copying, or concatenating, return instances of this class" |
|
625 |
||
626 |
^ String |
|
627 |
! ! |
|
628 |
||
77 | 629 |
!Symbol methodsFor:'system primitives'! |
630 |
||
631 |
become:anotherObject |
|
632 |
"make all references to the receiver become references to anotherObject |
|
95 | 633 |
and vice-versa. For symbols, some special action is required, to |
634 |
correctly handle a become of the global dictionaries. |
|
77 | 635 |
Anyway: this is very dangerous - mysterous side-effects are to be |
95 | 636 |
expected. |
637 |
||
638 |
Notice: because of the danger here, this method may report an error |
|
159 | 639 |
in future versions" |
77 | 640 |
|
641 |
(Smalltalk includesKey:self) ifTrue:[ |
|
159 | 642 |
super become:anotherObject. |
77 | 643 |
%{ |
159 | 644 |
__rehashSystemDictionaries(); |
77 | 645 |
%}. |
646 |
] ifFalse:[ |
|
159 | 647 |
super become:anotherObject |
77 | 648 |
] |
649 |
! |
|
650 |
||
651 |
becomeNil |
|
652 |
"make all references to the receiver become nil - effectively getting |
|
95 | 653 |
rid of the receiver. For symbols, this is not allowed, if the receiver |
77 | 654 |
is used as a key in some SytemDictionary. |
95 | 655 |
This can be a very dangerous operation - be warned. |
656 |
||
657 |
Notice: because of the danger here, this method may report an error |
|
159 | 658 |
in future versions" |
77 | 659 |
|
660 |
(Smalltalk includesKey:self) ifTrue:[ |
|
159 | 661 |
self primitiveFailed |
77 | 662 |
] ifFalse:[ |
159 | 663 |
super becomeNil |
77 | 664 |
] |
665 |
! ! |
|
666 |
||
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
667 |
!Symbol methodsFor:'tracing'! |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
668 |
|
4682 | 669 |
traceInto:aRequestor level:level from:referrer |
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
670 |
"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
|
671 |
|
4682 | 672 |
^ aRequestor traceSymbol:self level:level from:referrer |
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
673 |
|
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
674 |
|
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 |
|
632 | 677 |
!Symbol class methodsFor:'documentation'! |
678 |
||
679 |
version |
|
5434 | 680 |
^ '$Header: /cvs/stx/stx/libbasic/Symbol.st,v 1.63 2000-06-28 17:18:30 cg Exp $' |
632 | 681 |
! ! |