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