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