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