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