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