author | Stefan Vogel <sv@exept.de> |
Tue, 15 Jan 2013 16:00:19 +0100 | |
changeset 14651 | 10791ebb7482 |
parent 13886 | bc20c434afc7 |
child 14658 | 6c0c3a9eb2c2 |
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)) { |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
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)) { |
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
105 |
newSymbol = __MKSYMBOL(__stringVal(aString), (OBJ *)0); |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
106 |
if (newSymbol != nil) { |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
107 |
RETURN (newSymbol); |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
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:[ |
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
112 |
"only allowed to intern strings" |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
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 |
|
13423 | 146 |
|
1 | 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 |
||
13423 | 186 |
|
11819 | 187 |
!Symbol methodsFor:'Compatibility-Squeak'! |
188 |
||
189 |
isUnary |
|
190 |
^ self isUnarySelector |
|
13690 | 191 |
! |
192 |
||
193 |
precedence |
|
194 |
"the precedence in an expression; 0 is highest; |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
195 |
unary < binary < keyword" |
13690 | 196 |
|
197 |
self size = 0 |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
198 |
ifTrue: [^ 0]. |
13690 | 199 |
self first isLetter |
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
200 |
ifFalse: [^ 2]. |
13690 | 201 |
self last = $: |
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
202 |
ifTrue: [^ 3]. |
13690 | 203 |
^ 1 |
204 |
||
205 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
206 |
self assert:(#foo precedence < #+ precedence). |
13690 | 207 |
self assert:(#+ precedence < #key: precedence). |
208 |
self assert:(#foo precedence < #key: precedence). |
|
209 |
" |
|
210 |
||
211 |
"Created: / 12-09-2011 / 14:53:54 / cg" |
|
11819 | 212 |
! ! |
213 |
||
7261 | 214 |
!Symbol methodsFor:'Compatibility-VW'! |
7153 | 215 |
|
216 |
<< catalogID |
|
217 |
"create and return a new UserMessage, with the receiver as key, |
|
218 |
and the argument as cataglogID. |
|
219 |
VW compatibility." |
|
220 |
||
221 |
^ UserMessage key:self catalogID:catalogID |
|
222 |
||
223 |
" |
|
11859 | 224 |
(#theFooMessage << #myMessages) |
225 |
(#theFooMessage << #myMessages >> 'cannot read subclass of metaclass') |
|
226 |
(#theFooMessage >> 'cannot read subclass of metaclass') |
|
7153 | 227 |
" |
228 |
! |
|
229 |
||
230 |
>> aString |
|
231 |
"create and return a new UserMessage, with the receiver as key, |
|
232 |
and the argument as defaultString. |
|
233 |
VW compatibility." |
|
234 |
||
11859 | 235 |
^ UserMessage key:self defaultString:aString |
7153 | 236 |
|
237 |
" |
|
11859 | 238 |
(#theFooMessage << #myMessages) |
239 |
(#theFooMessage << #myMessages >> 'cannot read subclass of metaclass') |
|
240 |
(#theFooMessage >> 'cannot read subclass of metaclass') |
|
7153 | 241 |
" |
242 |
! ! |
|
243 |
||
1 | 244 |
!Symbol methodsFor:'accessing'! |
245 |
||
246 |
basicAt:index put:something |
|
345 | 247 |
"report an error if an interned symbol is about to be changed |
1219 | 248 |
- interned symbols may NOT be changed. |
249 |
For uninterned symbols, this is allowed." |
|
1 | 250 |
|
77 | 251 |
self knownAsSymbol ifTrue:[ |
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
252 |
self error:'interned symbols may not be changed'. |
77 | 253 |
]. |
254 |
" |
|
255 |
uninterned - allow change |
|
256 |
" |
|
257 |
^ super basicAt:index put:something |
|
1219 | 258 |
|
259 |
"Modified: 19.4.1996 / 11:16:10 / cg" |
|
13423 | 260 |
! |
261 |
||
262 |
nameSpace |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
263 |
^ self isNameSpaceSelector |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
264 |
ifTrue: [ self nameSpaceAndSelector first ] |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
265 |
ifFalse:[ nil ] |
13423 | 266 |
|
267 |
"Created: / 20-07-2010 / 10:41:53 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
268 |
! |
|
269 |
||
270 |
nameSpaceAndSelector |
|
271 |
|nsPart selPart idx ns| |
|
272 |
||
273 |
self isNameSpaceSelector ifFalse:[ |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
274 |
^ Array with:nil with:self |
13423 | 275 |
]. |
276 |
idx := self indexOf:$: startingAt:3. |
|
277 |
nsPart := self copyFrom:2 to:idx - 1. |
|
278 |
ns := Smalltalk at:nsPart asSymbol. |
|
279 |
selPart := self copyFrom:idx + 2. |
|
280 |
^ Array with:ns with:selPart asSymbol |
|
281 |
||
282 |
"Created: / 20-07-2010 / 10:23:36 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
283 |
! |
|
284 |
||
285 |
selector |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
286 |
^ self isNameSpaceSelector |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
287 |
ifTrue: [ self nameSpaceAndSelector second ] |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
288 |
ifFalse:[ self ] |
13423 | 289 |
|
290 |
"Created: / 20-07-2010 / 10:41:32 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
1 | 291 |
! ! |
292 |
||
13423 | 293 |
|
608 | 294 |
!Symbol methodsFor:'comparing'! |
159 | 295 |
|
608 | 296 |
= something |
297 |
"return true, if the receiver and argument consist of the same characters. |
|
11859 | 298 |
Redefined here, for more efficient #= comparison of symbols |
608 | 299 |
(which ought to be compared using #==). |
300 |
If the argument is a symbol, we use a quick pointer compare, instead of |
|
301 |
the inherited value compare." |
|
13 | 302 |
|
608 | 303 |
%{ /* NOCONTEXT */ |
304 |
OBJ cls; |
|
13 | 305 |
|
608 | 306 |
if (! __isNonNilObject(something)) RETURN(false); |
307 |
if ((cls = __qClass(something)) == Symbol) { |
|
308 |
RETURN (self == something ? true : false); |
|
309 |
} |
|
310 |
if (cls == String) { |
|
11859 | 311 |
RETURN (strcmp(__stringVal(self), __stringVal(something)) == 0 ? true : false); |
345 | 312 |
} |
313 |
%}. |
|
608 | 314 |
"fall back; could be a TwoByteString, or a collection of Characters" |
1 | 315 |
|
608 | 316 |
^ super = something |
427 | 317 |
! |
318 |
||
356 | 319 |
identityHash |
320 |
"interned symbols can return a better hash key" |
|
321 |
||
13881 | 322 |
"/ immediately after any change, execute (maybe in a debugger): |
323 |
"/ Set allSubInstancesDo:[:s | s rehash] |
|
356 | 324 |
|
13881 | 325 |
%{ /* NOCONTEXT */ |
13886 | 326 |
/* for now, this is needed... */ |
14651 | 327 |
#undef HASH_DRAGONBOOK |
328 |
#define HASH_SDBM |
|
13881 | 329 |
|
330 |
REGISTER unsigned INT val; |
|
14651 | 331 |
REGISTER unsigned char *cp; |
356 | 332 |
int l; |
333 |
||
14651 | 334 |
if (__qIsSymbol(self)) { |
335 |
val = __GET_HASH(self); |
|
336 |
/* |
|
337 |
* only do it, if I have no standard hash key |
|
338 |
* assigned (which can only happen due to a #become:, |
|
339 |
* or by creating a symbol uninterned, and interning it |
|
340 |
* after it got a hashKey assigned. |
|
341 |
*/ |
|
342 |
if (val == 0) { |
|
343 |
cp = __stringVal(self); |
|
344 |
l = __stringSize(self); |
|
13881 | 345 |
#ifdef HASH_DRAGONBOOK |
14651 | 346 |
/* |
347 |
* this is the dragon-book algorithm |
|
348 |
* We have tested 5-bit shifts as well: |
|
349 |
* |
|
350 |
* ST/X Symbols: 17807 |
|
351 |
* Hashkey collisions (4bit): 14 0.07% |
|
352 |
* Hashkey collisions (5bit): 300 1.68% |
|
353 |
*/ |
|
356 | 354 |
|
14651 | 355 |
if (l > 0) { |
356 |
val = cp[0]; |
|
357 |
if (l > 1) { |
|
358 |
val = (val << 4) + cp[1]; |
|
359 |
if (l > 2) { |
|
360 |
val = (val << 4) + cp[2]; |
|
361 |
if (l > 3) { |
|
362 |
val = (val << 4) + cp[3]; |
|
363 |
if (l > 4) { |
|
364 |
val = (val << 4) + cp[4]; |
|
365 |
if (l > 5) { |
|
366 |
val = (val << 4) + cp[5]; |
|
367 |
REGISTER unsigned char *ce; |
|
13881 | 368 |
|
14651 | 369 |
if (l > 6) { |
370 |
val = (val << 4) + cp[6]; |
|
371 |
for (ce = cp + l, cp += 7; cp < ce; cp++) { |
|
372 |
REGISTER unsigned INT g; |
|
373 |
||
374 |
if (g = (val & 0xF0000000)) { |
|
375 |
val ^= g >> 24; |
|
376 |
val ^= g; |
|
377 |
} |
|
378 |
val = (val << 4) + *cp; |
|
379 |
} |
|
380 |
} |
|
381 |
} |
|
382 |
} |
|
383 |
} |
|
384 |
} |
|
385 |
} |
|
386 |
} else { |
|
387 |
val = 0; |
|
388 |
} |
|
389 |
val = (val * 31415821) & _MAX_INT; |
|
13881 | 390 |
#else |
391 |
# ifdef HASH_SDBM |
|
14651 | 392 |
/* |
393 |
* this is the sdbm algorithm |
|
394 |
* |
|
395 |
* ST/X Symbols: 51404 |
|
396 |
* Hashkey collisions (dragonBook): 54 |
|
397 |
* Hashkey collisions (sdbm): 2 |
|
398 |
*/ |
|
399 |
val = 0; |
|
400 |
while (l >= 4) { |
|
401 |
l -= 4; |
|
402 |
val = (val * 65599) + cp[0]; |
|
403 |
val = (val * 65599) + cp[1]; |
|
404 |
val = (val * 65599) + cp[2]; |
|
405 |
val = (val * 65599) + cp[3]; |
|
406 |
cp += 4; |
|
407 |
} |
|
408 |
while (l--) { |
|
409 |
val = (val * 65599) + *cp++; |
|
410 |
} |
|
411 |
val = val & _MAX_INT; |
|
13881 | 412 |
# else |
14651 | 413 |
# error "Undefined Hash Algorithm" |
13881 | 414 |
# endif |
415 |
#endif |
|
14651 | 416 |
} else { |
417 |
val = __MAKE_HASH__(val); |
|
418 |
} |
|
419 |
RETURN ( __mkSmallInteger(val) ); |
|
420 |
} |
|
356 | 421 |
%}. |
14651 | 422 |
|
423 |
^ super identityHash. |
|
3985 | 424 |
|
425 |
" |
|
14651 | 426 |
|hashColl hashSet| |
3985 | 427 |
|
14651 | 428 |
hashColl := OrderedCollection new:20000. |
429 |
Symbol allInstancesDo:[:instance | |
|
430 |
hashColl add:instance identityHash |
|
431 |
]. |
|
432 |
hashSet := hashColl asSet. |
|
3985 | 433 |
|
14651 | 434 |
Transcript showCR:'Symbols: ', hashColl size printString, |
435 |
' unique hash keys: ', hashSet size printString, |
|
436 |
' collisions:', (hashColl size - hashSet size) printString. |
|
3985 | 437 |
" |
13881 | 438 |
|
439 |
"Modified (comment): / 26-12-2011 / 14:32:10 / cg" |
|
370 | 440 |
! |
441 |
||
442 |
~= something |
|
443 |
"return true, if the receiver and argument do not consist of the same characters. |
|
11859 | 444 |
Redefined here, for more efficient #~= comparison of symbols |
370 | 445 |
(which ought to be compared using #~~). |
446 |
If the argument is a symbol, we use a quick pointer compare, instead of |
|
447 |
the inherited value compare." |
|
448 |
||
449 |
%{ /* NOCONTEXT */ |
|
450 |
OBJ cls; |
|
451 |
||
379 | 452 |
if (! __isNonNilObject(something)) RETURN(true); /* cannot be equal */ |
370 | 453 |
if ((cls = __qClass(something)) == Symbol) { |
379 | 454 |
RETURN (self == something ? false : true); |
370 | 455 |
} |
456 |
if (cls == String) { |
|
379 | 457 |
RETURN (strcmp(__stringVal(self), __stringVal(something)) == 0 ? false : true); |
370 | 458 |
} |
459 |
%}. |
|
460 |
"fall back; could be a TwoByteString, or a collection of Characters" |
|
461 |
||
462 |
^ super ~= something |
|
356 | 463 |
! ! |
464 |
||
608 | 465 |
!Symbol methodsFor:'converting'! |
466 |
||
467 |
asString |
|
468 |
"return a string with printname taken from mine" |
|
469 |
||
470 |
%{ /* NOCONTEXT */ |
|
471 |
OBJ s; |
|
472 |
||
2565 | 473 |
s = __MKSTRING_ST(self); |
608 | 474 |
if (s != nil) { |
11859 | 475 |
RETURN (s); |
608 | 476 |
} |
477 |
%}. |
|
5434 | 478 |
^ (String new:(self size)) |
11859 | 479 |
replaceFrom:1 with:self startingAt:1 |
608 | 480 |
! |
481 |
||
482 |
asSymbol |
|
9231 | 483 |
"Return a unique symbol with the name taken from the receivers characters. |
608 | 484 |
Since I am a symbol - just return myself" |
485 |
||
486 |
^ self |
|
487 |
! |
|
488 |
||
489 |
asSymbolIfInterned |
|
11859 | 490 |
"If a symbol with the receivers characters is already known, return it. Otherwise, return nil. |
608 | 491 |
Since I am a symbol - just return myself" |
492 |
||
493 |
^ self |
|
494 |
! ! |
|
495 |
||
496 |
!Symbol methodsFor:'copying'! |
|
497 |
||
498 |
copy |
|
499 |
"return a copy of myself |
|
4729 | 500 |
- reimplemented here since symbols are immutable." |
608 | 501 |
|
502 |
^ self |
|
503 |
! |
|
504 |
||
3659 | 505 |
copyReplaceAll:oldElement with:newElement |
506 |
"return a copy of the receiver as a string, where all elements equal to oldElement |
|
507 |
have been replaced by newElement." |
|
508 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
509 |
"/ 'Warning: #copyReplaceAll:with: will change semantics as defined in ANSI soon' errorPrintCR. |
3659 | 510 |
^ self asString replaceAll:oldElement with:newElement |
511 |
||
512 |
"Created: / 18.7.1998 / 23:03:38 / cg" |
|
513 |
! |
|
514 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
515 |
copyReplacing:oldElement withObject:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
516 |
"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
|
517 |
have been replaced by newElement. |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
518 |
ANSI version of what used to be #copyReplaceAll:with:" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
519 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
520 |
^ self asString replaceAll:oldElement with:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
521 |
! |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
522 |
|
608 | 523 |
deepCopy |
524 |
"return a copy of myself |
|
4729 | 525 |
- reimplemented here since symbols are immutable." |
608 | 526 |
|
527 |
^ self |
|
528 |
! |
|
529 |
||
10950 | 530 |
deepCopyUsing:aDictionary postCopySelector:postCopySelector |
8383 | 531 |
"return a deep copy of myself |
4729 | 532 |
- reimplemented here since symbols are immutable." |
608 | 533 |
|
534 |
^ self |
|
535 |
! |
|
536 |
||
537 |
shallowCopy |
|
538 |
"return a copy of myself |
|
10581 | 539 |
- reimplemented here since symbols are immutable and unique, |
540 |
so we return the receiver." |
|
608 | 541 |
|
542 |
^ self |
|
10581 | 543 |
|
544 |
"Modified: / 31-05-2007 / 15:32:30 / cg" |
|
608 | 545 |
! |
546 |
||
547 |
simpleDeepCopy |
|
548 |
"return a copy of myself |
|
4729 | 549 |
- reimplemented here since symbols are immutable." |
608 | 550 |
|
551 |
^ self |
|
552 |
! ! |
|
553 |
||
14651 | 554 |
|
608 | 555 |
!Symbol methodsFor:'printing & storing'! |
556 |
||
557 |
printOn:aStream |
|
4392 | 558 |
"append a user printed representation of the receiver to aStream. |
559 |
The format is suitable for a human - not meant to be read back. |
|
560 |
||
608 | 561 |
In contrast to ST-80, this does return the symbols characters |
562 |
without any leading #. Therefore, you can directly send symbols' |
|
563 |
printStrings to some output device. This is incompatible, but easier |
|
564 |
to use." |
|
565 |
||
566 |
"/ ST-80 behavior: |
|
567 |
||
568 |
"/ self storeOn:aStream |
|
569 |
||
570 |
aStream nextPutAll:self |
|
571 |
! |
|
572 |
||
573 |
printString |
|
574 |
"return a printed representation of the receiver. |
|
575 |
In contrast to ST-80, this does return the symbols characters |
|
576 |
without any leading #. Therefore, you can directly send symbols' |
|
577 |
printStrings to some output device. This is incompatible, but easier |
|
578 |
to use." |
|
579 |
||
580 |
"/ ST-80 behavior: |
|
581 |
"/ ^ self storeString |
|
582 |
||
583 |
^ self asString |
|
584 |
! |
|
585 |
||
7600
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
586 |
storeArrayElementOn:aStream |
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
587 |
"store myself on a stream. |
8368 | 588 |
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
|
589 |
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
|
590 |
|
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
591 |
|storeString| |
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
592 |
|
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
593 |
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
|
594 |
(self == #true or:[self == #false or:[self == #nil or:[(storeString at:2) == $']]]) ifTrue:[ |
11859 | 595 |
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
|
596 |
] ifFalse:[ |
11859 | 597 |
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
|
598 |
]. |
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
599 |
! |
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
600 |
|
608 | 601 |
storeOn:aStream |
602 |
"store myself on a stream" |
|
603 |
||
604 |
aStream nextPutAll:(self storeString) |
|
605 |
! |
|
606 |
||
607 |
storeString |
|
608 |
"return a String for storing the receiver" |
|
609 |
||
11859 | 610 |
|sz "{Class: SmallInteger }" |
5941
e194c325bee8
storeString fixed for symbols like #'foo:bar'
Claus Gittinger <cg@exept.de>
parents:
5896
diff
changeset
|
611 |
c anyColon| |
1125 | 612 |
|
7600
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
613 |
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
|
614 |
(sz ~~ 0 and:[(self at:1) isLetter]) ifTrue:[ |
11859 | 615 |
anyColon := false. |
616 |
2 to:sz do:[:index | |
|
617 |
c := self at:index. |
|
618 |
c == $: ifTrue:[ |
|
619 |
(index == sz or:[(self at:(index+1)) isLetterOrDigit]) ifFalse:[ |
|
620 |
^ '#' , super storeString. |
|
621 |
]. |
|
622 |
anyColon := true. |
|
623 |
] ifFalse:[ |
|
624 |
c isLetterOrDigit ifFalse:[ |
|
625 |
^ '#' , super storeString |
|
626 |
]. |
|
627 |
]. |
|
628 |
]. |
|
629 |
"no colon in symbol or symbol ends with a colon" |
|
630 |
(anyColon and:[c ~~ $:]) ifFalse:[ |
|
631 |
^ '#' , self |
|
632 |
]. |
|
608 | 633 |
]. |
5896 | 634 |
^ '#' , super storeString |
3285
bd5827c97b21
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3086
diff
changeset
|
635 |
|
3286
c6987c5f15a8
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3285
diff
changeset
|
636 |
" |
11859 | 637 |
#'abc' storeString |
638 |
#'abc:' storeString |
|
639 |
#'abc:def:' storeString |
|
640 |
#'abc:def' storeString |
|
641 |
#'abc::def' storeString |
|
642 |
#'abc &^*' storeString |
|
643 |
#'abcdef::' storeString |
|
644 |
#'hello''world' storeString |
|
645 |
#'' storeString |
|
646 |
#'''' storeString |
|
647 |
#'_hello' storeString |
|
648 |
#'123' storeString |
|
3286
c6987c5f15a8
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3285
diff
changeset
|
649 |
" |
608 | 650 |
! ! |
651 |
||
652 |
!Symbol methodsFor:'queries'! |
|
653 |
||
4987 | 654 |
isInfix |
655 |
"return true, if the receiver is a binary message selector" |
|
656 |
||
7350
a62d53937a6b
ansified (isAlphaNumeric -> isLetterOrDigit)
Claus Gittinger <cg@exept.de>
parents:
7301
diff
changeset
|
657 |
^ self first isLetterOrDigit not |
4987 | 658 |
|
659 |
" |
|
11859 | 660 |
#at:put: isInfix |
661 |
#at: isInfix |
|
662 |
#+ isInfix |
|
663 |
#size isInfix |
|
4987 | 664 |
" |
665 |
||
666 |
"Created: / 1.11.1997 / 12:34:55 / cg" |
|
667 |
"Modified: / 1.11.1997 / 12:36:37 / cg" |
|
668 |
! |
|
669 |
||
3086 | 670 |
isKeyword |
671 |
"return true, if the receiver is a keyword message selector" |
|
672 |
||
673 |
^ self includes:$: |
|
674 |
||
675 |
" |
|
11859 | 676 |
#at:put: isKeyword |
677 |
#at: isKeyword |
|
678 |
#+ isKeyword |
|
679 |
#size isKeyword |
|
3086 | 680 |
" |
681 |
||
682 |
"Created: / 1.11.1997 / 12:34:55 / cg" |
|
683 |
"Modified: / 1.11.1997 / 12:36:37 / cg" |
|
684 |
! |
|
685 |
||
11859 | 686 |
isSymbol |
608 | 687 |
"return true, if the receiver is some kind of symbol. |
688 |
Since I am a symbol, return always true" |
|
689 |
||
690 |
^ true |
|
691 |
! |
|
692 |
||
693 |
keywords |
|
694 |
"assuming the receiver is a keyword message selector, |
|
695 |
return the individual keywords (i.e. break it up at colons) |
|
696 |
and return these as a collection. |
|
13471
1054964b45e6
comment/format in: #keywords
Claus Gittinger <cg@exept.de>
parents:
13423
diff
changeset
|
697 |
For binary and unary selectors, the result may be nonsense (an array containing the receiver)." |
608 | 698 |
|
699 |
|coll s part| |
|
700 |
||
701 |
coll := OrderedCollection new. |
|
702 |
s := ReadStream on:self. |
|
703 |
[s atEnd] whileFalse:[ |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
704 |
part := s through:$:. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
705 |
coll add:part |
608 | 706 |
]. |
707 |
^ coll asArray |
|
708 |
||
709 |
" |
|
11859 | 710 |
#at:put: keywords |
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
711 |
#at: keywords |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
712 |
#+ keywords |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
713 |
#size keywords |
608 | 714 |
" |
13471
1054964b45e6
comment/format in: #keywords
Claus Gittinger <cg@exept.de>
parents:
13423
diff
changeset
|
715 |
|
1054964b45e6
comment/format in: #keywords
Claus Gittinger <cg@exept.de>
parents:
13423
diff
changeset
|
716 |
"Modified (Comment): / 30-06-2011 / 17:46:21 / cg" |
608 | 717 |
! |
718 |
||
719 |
species |
|
720 |
"when copying, or concatenating, return instances of this class" |
|
721 |
||
722 |
^ String |
|
723 |
! ! |
|
724 |
||
77 | 725 |
!Symbol methodsFor:'system primitives'! |
726 |
||
727 |
become:anotherObject |
|
728 |
"make all references to the receiver become references to anotherObject |
|
95 | 729 |
and vice-versa. For symbols, some special action is required, to |
730 |
correctly handle a become of the global dictionaries. |
|
77 | 731 |
Anyway: this is very dangerous - mysterous side-effects are to be |
95 | 732 |
expected. |
733 |
||
734 |
Notice: because of the danger here, this method may report an error |
|
159 | 735 |
in future versions" |
77 | 736 |
|
737 |
(Smalltalk includesKey:self) ifTrue:[ |
|
159 | 738 |
super become:anotherObject. |
77 | 739 |
%{ |
159 | 740 |
__rehashSystemDictionaries(); |
77 | 741 |
%}. |
742 |
] ifFalse:[ |
|
159 | 743 |
super become:anotherObject |
77 | 744 |
] |
745 |
! |
|
746 |
||
747 |
becomeNil |
|
748 |
"make all references to the receiver become nil - effectively getting |
|
95 | 749 |
rid of the receiver. For symbols, this is not allowed, if the receiver |
7765 | 750 |
is used as a key in some SystemDictionary. |
95 | 751 |
This can be a very dangerous operation - be warned. |
752 |
||
753 |
Notice: because of the danger here, this method may report an error |
|
11859 | 754 |
in future versions" |
77 | 755 |
|
756 |
(Smalltalk includesKey:self) ifTrue:[ |
|
11859 | 757 |
self primitiveFailed |
77 | 758 |
] ifFalse:[ |
11859 | 759 |
super becomeNil |
77 | 760 |
] |
13388 | 761 |
! |
762 |
||
763 |
grow:newSize |
|
13389 | 764 |
"blocked" |
13388 | 765 |
|
13389 | 766 |
self fixedSizeError |
13388 | 767 |
|
768 |
"Created: / 20-06-2011 / 14:57:36 / cg" |
|
13389 | 769 |
! |
770 |
||
771 |
removeAll |
|
772 |
"blocked" |
|
773 |
||
774 |
self fixedSizeError |
|
775 |
||
776 |
"Created: / 20-06-2011 / 14:59:02 / cg" |
|
77 | 777 |
! ! |
778 |
||
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
779 |
!Symbol methodsFor:'tracing'! |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
780 |
|
4682 | 781 |
traceInto:aRequestor level:level from:referrer |
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
782 |
"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
|
783 |
|
4682 | 784 |
^ aRequestor traceSymbol:self level:level from:referrer |
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
785 |
|
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
786 |
|
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
787 |
! ! |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
788 |
|
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8383
diff
changeset
|
789 |
!Symbol methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8383
diff
changeset
|
790 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8383
diff
changeset
|
791 |
acceptVisitor:aVisitor with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8383
diff
changeset
|
792 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8383
diff
changeset
|
793 |
^ aVisitor visitSymbol:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8383
diff
changeset
|
794 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8383
diff
changeset
|
795 |
|
632 | 796 |
!Symbol class methodsFor:'documentation'! |
797 |
||
13423 | 798 |
version_CVS |
14651 | 799 |
^ '$Header: /cvs/stx/stx/libbasic/Symbol.st,v 1.100 2013-01-15 15:00:19 stefan Exp $' |
12473
828e275ddf8b
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11859
diff
changeset
|
800 |
! |
828e275ddf8b
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11859
diff
changeset
|
801 |
|
13423 | 802 |
version_SVN |
13471
1054964b45e6
comment/format in: #keywords
Claus Gittinger <cg@exept.de>
parents:
13423
diff
changeset
|
803 |
^ '§ Id: Symbol.st 10648 2011-06-23 15:55:10Z vranyj1 §' |
632 | 804 |
! ! |
14651 | 805 |