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