author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Sun, 18 Dec 2016 11:19:27 +0000 | |
branch | jv |
changeset 21242 | 19fabe339f8b |
parent 21027 | ad86468de3a0 |
parent 21127 | 258bf1737046 |
child 23107 | 40173e082cbc |
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 |
||
17512 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
3985 | 16 |
String variableByteSubclass:#Symbol |
1020 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Text' |
|
1 | 21 |
! |
22 |
||
77 | 23 |
!Symbol class methodsFor:'documentation'! |
1 | 24 |
|
88 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
159 | 28 |
All Rights Reserved |
88 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
77 | 39 |
documentation |
40 |
" |
|
41 |
Symbols represent unique strings - every symbol with same printString |
|
42 |
exists exactly once in the system; Symbols are used for selectors, global |
|
345 | 43 |
variable-keys etc. Symbols can also be used to represent things which are |
44 |
enumeration type values in other programming languages (since symbols are |
|
370 | 45 |
created at compile time, comparing them using == is a fast pointer compare). |
345 | 46 |
|
47 |
A symbol may not change its characters - i.e. it is constant over its lifetime. |
|
48 |
Other than that, symbols behave much like strings. |
|
1290 | 49 |
|
17512 | 50 |
The most prominent use of symbols is as key for message sending: methods are stored |
51 |
in a classes method dictionary, where symbols are used as key to map names to methods. |
|
52 |
Also, symbols are used as key to map class names (global names) to actual class objects. |
|
53 |
||
54 |
Special ST/X feature: |
|
18360
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
55 |
The ST/X VM method lookup supports selector namespaces for method extensions. |
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
56 |
This means, that a class may contain method extensions in another namespace, |
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
57 |
which are only seen and invoked if called from a class within that namespace. |
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
58 |
Technically, this is done by storing the method under a special namespace-selector, |
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
59 |
which is a symbol consisting of the user visible name, prefixed by ':<ns>::'. |
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
60 |
The VM's method lookup algorithm contains special handling code for such constructs. |
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
61 |
Thus, if two methods are stored as 'foo' and ':NS::foo' are present in a class, |
19478 | 62 |
any send of 'foo' from wthin the NS-namespace will invoke the second method. |
18360
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
63 |
Any other send will invoke the first one. |
17512 | 64 |
|
65 |
||
1290 | 66 |
[author:] |
18360
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
67 |
Claus Gittinger |
77 | 68 |
" |
69 |
! ! |
|
1 | 70 |
|
77 | 71 |
!Symbol class methodsFor:'instance creation'! |
1 | 72 |
|
62 | 73 |
basicNew:size |
74 |
"redefined to return a string instead of a symbol - |
|
75 |
this allows all copy methods inherited from String to |
|
76 |
return strings containing the symbols characters. |
|
1020 | 77 |
Real symbols are only created with #intern: or #asSymbol." |
62 | 78 |
|
79 |
^ String new:size |
|
1020 | 80 |
|
81 |
"Modified: 26.2.1996 / 12:51:38 / cg" |
|
62 | 82 |
! |
83 |
||
77 | 84 |
fromString:aString |
85 |
"same as intern: for Symbol, but may be used to create interned instances |
|
86 |
of subclasses. |
|
87 |
Notice: this fails, if you try to intern an instance of a subclass, AND |
|
88 |
a symbol with the same name already exists. In this case, the original |
|
89 |
symbol is returned. To use it for enum-type symbols, make certain, that the |
|
90 |
names are unique (for example by including the classes name as a prefix-string)." |
|
91 |
||
267 | 92 |
|sym len| |
77 | 93 |
|
267 | 94 |
sym := aString asSymbolIfInterned. |
95 |
sym notNil ifTrue:[ |
|
96 |
^ sym |
|
77 | 97 |
]. |
98 |
||
99 |
" |
|
100 |
create a new uninterned instance first |
|
101 |
" |
|
102 |
len := aString size. |
|
267 | 103 |
sym := super basicNew:len. |
104 |
sym replaceFrom:1 to:len with:aString. |
|
77 | 105 |
|
106 |
"now, intern it" |
|
107 |
%{ |
|
329 | 108 |
sym = __INTERNSYMBOL(sym, (OBJ *)0, __context); |
77 | 109 |
%}. |
267 | 110 |
^ sym |
77 | 111 |
! |
112 |
||
608 | 113 |
intern:aString |
114 |
"return a unique symbol with printname taken from the String-argument" |
|
115 |
||
116 |
%{ /* NOCONTEXT */ |
|
117 |
OBJ newSymbol; |
|
118 |
||
119 |
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
|
120 |
RETURN (aString); |
608 | 121 |
} |
12473
828e275ddf8b
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11859
diff
changeset
|
122 |
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
|
123 |
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
|
124 |
if (newSymbol != nil) { |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
125 |
RETURN (newSymbol); |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
126 |
} |
608 | 127 |
} |
128 |
%}. |
|
12473
828e275ddf8b
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11859
diff
changeset
|
129 |
(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
|
130 |
"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
|
131 |
^ self mustBeString |
608 | 132 |
]. |
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
133 |
^ AllocationFailure raise. |
608 | 134 |
! |
135 |
||
1 | 136 |
internCharacter:aCharacter |
137 |
"return a unique symbol with printname taken from the Character-argument" |
|
138 |
||
139 |
^ self intern:(aCharacter asString) |
|
608 | 140 |
! |
141 |
||
142 |
new:size |
|
143 |
"redefined to return a string instead of a symbol - |
|
144 |
this allows all copy methods inherited from String to |
|
145 |
return strings containing the symbols characters. |
|
146 |
Real symbols are only created with intern: or asSymbol." |
|
147 |
||
148 |
^ String new:size |
|
149 |
! ! |
|
150 |
||
2570
26616a00d5aa
added #tableSize for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2565
diff
changeset
|
151 |
|
21017 | 152 |
|
153 |
||
1 | 154 |
!Symbol class methodsFor:'queries'! |
155 |
||
608 | 156 |
findInterned:aString |
157 |
"for ST-80 compatibility - if the argument, aString is known |
|
158 |
as Symbol, return this symbol. Otherwise return nil." |
|
159 |
||
160 |
^ aString asSymbolIfInterned |
|
161 |
! |
|
162 |
||
163 |
hasInterned:aString |
|
164 |
"return true, if the argument, aString is known as Symbol; |
|
165 |
false otherwise" |
|
166 |
||
167 |
^ aString knownAsSymbol |
|
168 |
! |
|
169 |
||
1 | 170 |
hasInterned:aString ifTrue:trueBlock |
171 |
"for ST-80 compatibility - if the argument, aString is known |
|
172 |
as Symbol, evaluate the block with the corresponding symbol |
|
173 |
as argument and return true; otherwise return false" |
|
174 |
||
275 | 175 |
|sym| |
176 |
||
177 |
(sym := aString asSymbolIfInterned) notNil ifTrue:[ |
|
178 |
trueBlock value:sym. |
|
159 | 179 |
^ true |
1 | 180 |
]. |
181 |
^ false |
|
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
182 |
! |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
183 |
|
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
184 |
hasSharedInstances |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
185 |
"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
|
186 |
with the same value are identical. |
4654 | 187 |
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
|
188 |
|
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
189 |
^ true |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
190 |
|
1 | 191 |
! ! |
192 |
||
13423 | 193 |
|
11819 | 194 |
!Symbol methodsFor:'Compatibility-Squeak'! |
195 |
||
13690 | 196 |
precedence |
197 |
"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
|
198 |
unary < binary < keyword" |
13690 | 199 |
|
200 |
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
|
201 |
ifTrue: [^ 0]. |
13690 | 202 |
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
|
203 |
ifFalse: [^ 2]. |
13690 | 204 |
self last = $: |
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
205 |
ifTrue: [^ 3]. |
13690 | 206 |
^ 1 |
207 |
||
208 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13881
diff
changeset
|
209 |
self assert:(#foo precedence < #+ precedence). |
13690 | 210 |
self assert:(#+ precedence < #key: precedence). |
211 |
self assert:(#foo precedence < #key: precedence). |
|
212 |
" |
|
213 |
||
214 |
"Created: / 12-09-2011 / 14:53:54 / cg" |
|
11819 | 215 |
! ! |
216 |
||
7261 | 217 |
!Symbol methodsFor:'Compatibility-VW'! |
7153 | 218 |
|
219 |
<< catalogID |
|
220 |
"create and return a new UserMessage, with the receiver as key, |
|
221 |
and the argument as cataglogID. |
|
222 |
VW compatibility." |
|
223 |
||
224 |
^ UserMessage key:self catalogID:catalogID |
|
225 |
||
226 |
" |
|
11859 | 227 |
(#theFooMessage << #myMessages) |
228 |
(#theFooMessage << #myMessages >> 'cannot read subclass of metaclass') |
|
229 |
(#theFooMessage >> 'cannot read subclass of metaclass') |
|
7153 | 230 |
" |
231 |
! |
|
232 |
||
233 |
>> aString |
|
234 |
"create and return a new UserMessage, with the receiver as key, |
|
235 |
and the argument as defaultString. |
|
236 |
VW compatibility." |
|
237 |
||
11859 | 238 |
^ UserMessage key:self defaultString:aString |
7153 | 239 |
|
240 |
" |
|
11859 | 241 |
(#theFooMessage << #myMessages) |
242 |
(#theFooMessage << #myMessages >> 'cannot read subclass of metaclass') |
|
243 |
(#theFooMessage >> 'cannot read subclass of metaclass') |
|
7153 | 244 |
" |
245 |
! ! |
|
246 |
||
21017 | 247 |
|
1 | 248 |
!Symbol methodsFor:'accessing'! |
249 |
||
250 |
basicAt:index put:something |
|
345 | 251 |
"report an error if an interned symbol is about to be changed |
1219 | 252 |
- interned symbols may NOT be changed. |
253 |
For uninterned symbols, this is allowed." |
|
1 | 254 |
|
77 | 255 |
self knownAsSymbol ifTrue:[ |
16890
8fa6938075c2
refer to the VM's internal symbol hash algorithm,
Claus Gittinger <cg@exept.de>
parents:
16774
diff
changeset
|
256 |
self noModificationError. |
8fa6938075c2
refer to the VM's internal symbol hash algorithm,
Claus Gittinger <cg@exept.de>
parents:
16774
diff
changeset
|
257 |
"Even if you hit continue - you cannot change symbols. |
8fa6938075c2
refer to the VM's internal symbol hash algorithm,
Claus Gittinger <cg@exept.de>
parents:
16774
diff
changeset
|
258 |
Raise again non-resumable" |
8fa6938075c2
refer to the VM's internal symbol hash algorithm,
Claus Gittinger <cg@exept.de>
parents:
16774
diff
changeset
|
259 |
NoModificationError raiseWith:self errorString:' - interned symbols cannot be changed'. |
77 | 260 |
]. |
261 |
" |
|
262 |
uninterned - allow change |
|
263 |
" |
|
264 |
^ super basicAt:index put:something |
|
1219 | 265 |
|
266 |
"Modified: 19.4.1996 / 11:16:10 / cg" |
|
13423 | 267 |
! |
268 |
||
269 |
nameSpace |
|
16735 | 270 |
"if I have the format of a namespace-selector, |
271 |
retrieve the namespace. Otherwise, return nil. |
|
18619 | 272 |
Also return nil, if that namespace does not exist (in contrast to nameSpacePart). |
273 |
Namespace selectors have a special, fix defined format, which is also known in the VM. |
|
16735 | 274 |
They must be of the form :<ns>::<sel>, |
17512 | 275 |
where <ns> is the namespace and <sel> is the raw selector. |
276 |
This special format (a symbol starting with a colon) was chosen, because almost every other selector |
|
18360
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
277 |
is legal, and this can be checked quickly by just looking at the first character." |
16735 | 278 |
|
17516 | 279 |
|nsPart| |
280 |
||
281 |
nsPart := self nameSpaceAndSelectorParts first. |
|
18619 | 282 |
nsPart isNil ifTrue:[^ nil]. |
17516 | 283 |
^ Smalltalk at:nsPart asSymbol |
16735 | 284 |
|
285 |
" |
|
286 |
#':foo:' nameSpace -> nil (bad format) |
|
287 |
#':foo::bar' nameSpace -> nil (non existing) |
|
17516 | 288 |
#':Tools::bar' nameSpace -> Tools |
18619 | 289 |
#'bar' nameSpace -> nil |
290 |
#'bar:' nameSpace -> nil |
|
16735 | 291 |
" |
13423 | 292 |
|
293 |
"Created: / 20-07-2010 / 10:41:53 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
294 |
! |
|
295 |
||
296 |
nameSpaceAndSelector |
|
16735 | 297 |
"return a two element tuple consisting of the namespace and the raw selector. |
298 |
If I do not have the format of a namespace-selector, or the namespace is non-existing, |
|
299 |
the first element of the returned tuple will be nil. |
|
18619 | 300 |
Namespace selectors have a special, fix defined format, which is also known in the VM. |
16735 | 301 |
They must be of the form :<ns>::<sel>, |
17512 | 302 |
where <ns> is the namespace and <sel> is the raw selector. |
303 |
This special format (a symbol starting with a colon) was chosen, because almost every other selector |
|
18360
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
304 |
is legal, and this can be checked quickly by just looking at the first character." |
16735 | 305 |
|
17516 | 306 |
|parts ns| |
13423 | 307 |
|
308 |
self isNameSpaceSelector ifFalse:[ |
|
18701 | 309 |
^ Array with:nil with:self |
13423 | 310 |
]. |
17516 | 311 |
parts := self nameSpaceAndSelectorParts. |
312 |
ns := Smalltalk at:parts first asSymbol. |
|
313 |
^ Array with:ns with:parts second |
|
13423 | 314 |
|
16735 | 315 |
" |
316 |
#':foo:bar' nameSpaceAndSelector -> #(nil #':foo:bar') |
|
317 |
#':foo::bar' nameSpaceAndSelector -> #(nil #bar) |
|
318 |
#':Tools::foo' nameSpaceAndSelector -> #(Tools (* NameSpace *) #foo) |
|
319 |
" |
|
320 |
||
13423 | 321 |
"Created: / 20-07-2010 / 10:23:36 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
322 |
! |
|
323 |
||
17516 | 324 |
nameSpaceAndSelectorParts |
325 |
"return a two element tuple consisting of the namespace name and the raw selector. |
|
18701 | 326 |
If I do not have the format of a namespace-selector, |
17516 | 327 |
the first element of the returned tuple will be nil. |
18619 | 328 |
Namespace selectors have a special, fix defined format, which is also known in the VM. |
17516 | 329 |
They must be of the form :<ns>::<sel>, |
330 |
where <ns> is the namespace and <sel> is the raw selector. |
|
331 |
This special format (a symbol starting with a colon) was chosen, because almost every other selector |
|
18360
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
332 |
is legal, and this can be checked quickly by just looking at the first character." |
17516 | 333 |
|
334 |
|nsPart selPart idx| |
|
335 |
||
336 |
self isNameSpaceSelector ifFalse:[ |
|
18701 | 337 |
^ Array with:nil with:self |
17516 | 338 |
]. |
339 |
idx := self indexOf:$: startingAt:3. |
|
340 |
nsPart := self copyFrom:2 to:idx - 1. |
|
341 |
selPart := self copyFrom:idx + 2. |
|
342 |
^ Array with:nsPart with:selPart asSymbol |
|
343 |
||
344 |
" |
|
18619 | 345 |
#':foo:bar' nameSpaceAndSelectorParts -> #(nil #':foo:bar') |
346 |
#':foo:bar' nameSpaceAndSelector -> #(nil #':foo:bar') |
|
347 |
||
348 |
#':foo::bar' nameSpaceAndSelectorParts -> #('foo' #bar) |
|
349 |
#':foo::bar' nameSpaceAndSelector -> #(nil #bar) |
|
350 |
||
351 |
#'bar' nameSpaceAndSelectorParts -> #(nil #bar) |
|
352 |
#'bar' nameSpaceAndSelector -> #(nil #bar) |
|
353 |
||
354 |
#':Tools::foo' nameSpaceAndSelectorParts -> #('Tools' #foo) |
|
355 |
#':Tools::foo' nameSpaceAndSelector -> #(Tools (* NameSpace *) #foo) |
|
17516 | 356 |
" |
357 |
||
358 |
"Created: / 20-07-2010 / 10:23:36 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
359 |
! |
|
360 |
||
361 |
nameSpacePart |
|
362 |
"if I have the format of a namespace-selector, |
|
363 |
retrieve the namespace name. Otherwise, return nil. |
|
18619 | 364 |
Namespace selectors have a special, fix defined format, which is also known in the VM. |
17516 | 365 |
They must be of the form :<ns>::<sel>, |
366 |
where <ns> is the namespace and <sel> is the raw selector. |
|
367 |
This special format (a symbol starting with a colon) was chosen, because almost every other selector |
|
18360
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
368 |
is legal, and this can be checked quickly by just looking at the first character." |
17516 | 369 |
|
370 |
^ self nameSpaceAndSelectorParts first. |
|
371 |
||
372 |
" |
|
373 |
#':foo:' nameSpacePart -> nil (bad format) |
|
374 |
#':foo::bar' nameSpacePart -> 'foo' |
|
375 |
#':Tools::bar' nameSpacePart -> 'Tools' |
|
376 |
" |
|
377 |
! |
|
378 |
||
13423 | 379 |
selector |
17512 | 380 |
<resource: #obsolete> |
381 |
||
18360
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
382 |
"if I have the format of a namespace-selector, retrieve the raw selector. |
17512 | 383 |
Otherwise, return myself. |
384 |
Namespace selectors have a special, fix defined format, which is also known in the VM. |
|
16735 | 385 |
They must be of the form :<ns>::<sel>, |
17512 | 386 |
where <ns> is the namespace and <sel> is the raw selector. |
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
387 |
This special format (a symbol starting with a colon) was chosen, |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
388 |
because almost every other selector is legal, |
19714 | 389 |
and this can be checked quickly in the VM, by just looking at the first character." |
17512 | 390 |
|
391 |
self obsoleteMethodWarning:'use selectorWithoutNameSpace'. |
|
18360
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
392 |
^ self selectorWithoutNameSpace |
17512 | 393 |
! |
394 |
||
395 |
selectorWithoutNameSpace |
|
18360
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
396 |
"if I have the format of a namespace-selector, retrieve the raw selector. |
17512 | 397 |
Otherwise, return myself. |
398 |
Namespace selectors have a special, fix defined format, which is also known in the VM. |
|
399 |
They must be of the form :<ns>::<sel>, |
|
400 |
where <ns> is the namespace and <sel> is the raw selector. |
|
401 |
This special format (a symbol starting with a colon) was chosen, because almost every other selector |
|
18360
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
402 |
is legal, and this can be checked quickly by just looking at the first character." |
16735 | 403 |
|
18619 | 404 |
^ self nameSpaceAndSelectorParts second |
16735 | 405 |
|
406 |
" |
|
17516 | 407 |
#':foo:' selectorWithoutNameSpace -> #':foo:' (bad format) |
408 |
#':foo::bar' selectorWithoutNameSpace -> bar |
|
409 |
#':Tools::bar' selectorWithoutNameSpace -> bar |
|
16735 | 410 |
" |
13423 | 411 |
|
412 |
"Created: / 20-07-2010 / 10:41:32 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
1 | 413 |
! ! |
414 |
||
13423 | 415 |
|
608 | 416 |
!Symbol methodsFor:'comparing'! |
159 | 417 |
|
608 | 418 |
= something |
419 |
"return true, if the receiver and argument consist of the same characters. |
|
18360
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
420 |
Redefined here, for more efficient comparison of symbols |
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
421 |
(which can to be compared using #==). |
608 | 422 |
If the argument is a symbol, we use a quick pointer compare, instead of |
423 |
the inherited value compare." |
|
13 | 424 |
|
608 | 425 |
%{ /* NOCONTEXT */ |
18701 | 426 |
#ifdef __SCHTEAM__ |
427 |
if (something.isSymbol()) { |
|
428 |
return context._RETURN ( self == something ? STObject.True : STObject.False ); |
|
429 |
} |
|
430 |
if (something.isSTString()) { |
|
431 |
return context._RETURN ( something.isStringEqual(self) ? STObject.True : STObject.False ); |
|
432 |
} |
|
433 |
if (something == STObject.Nil) { |
|
434 |
return context._RETURN_false ( ); |
|
435 |
} |
|
436 |
#else |
|
608 | 437 |
OBJ cls; |
13 | 438 |
|
18360
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
439 |
if (something == self) RETURN(true); |
608 | 440 |
if (! __isNonNilObject(something)) RETURN(false); |
441 |
if ((cls = __qClass(something)) == Symbol) { |
|
18360
21b8e3adff85
can avoid a string compare sometimes when comparing symbols
Claus Gittinger <cg@exept.de>
parents:
17516
diff
changeset
|
442 |
RETURN (false); |
608 | 443 |
} |
14658 | 444 |
if (cls == String || cls == ImmutableString) { |
16890
8fa6938075c2
refer to the VM's internal symbol hash algorithm,
Claus Gittinger <cg@exept.de>
parents:
16774
diff
changeset
|
445 |
RETURN (strcmp(__stringVal(self), __stringVal(something)) == 0 ? true : false); |
345 | 446 |
} |
18701 | 447 |
#endif |
345 | 448 |
%}. |
608 | 449 |
"fall back; could be a TwoByteString, or a collection of Characters" |
1 | 450 |
|
608 | 451 |
^ super = something |
427 | 452 |
! |
453 |
||
18487
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
454 |
hash |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
455 |
"return an integer useful as a hash-key. |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
456 |
This default method uses whichever hash algorithm |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
457 |
used in the ST/X VM (which is actually fnv-1a)" |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
458 |
|
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
459 |
%{ /* NOCONTEXT */ |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
460 |
#ifdef __SCHTEAM__ |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
461 |
STString me = self.asSTString(); |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
462 |
long h = me.hash_fnv1a(); |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
463 |
return __c__._RETURN(STInteger._new(h)); |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
464 |
#else |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
465 |
extern unsigned int __symbolHash(char *); |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
466 |
unsigned char *cp; |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
467 |
unsigned int h; |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
468 |
|
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
469 |
cp = __stringVal(self); |
18488
cb7a21f38b77
Optimize for fast-path in String>hash, String>>hash_fnv1a and Symbol>>hash to avoid jumps
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18487
diff
changeset
|
470 |
if (__qClass(self) == Symbol) { |
cb7a21f38b77
Optimize for fast-path in String>hash, String>>hash_fnv1a and Symbol>>hash to avoid jumps
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18487
diff
changeset
|
471 |
cont: |
cb7a21f38b77
Optimize for fast-path in String>hash, String>>hash_fnv1a and Symbol>>hash to avoid jumps
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18487
diff
changeset
|
472 |
/* Use __symbolHash() to make sure it's the same used by the VM */ |
cb7a21f38b77
Optimize for fast-path in String>hash, String>>hash_fnv1a and Symbol>>hash to avoid jumps
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18487
diff
changeset
|
473 |
h = __symbolHash(cp); |
cb7a21f38b77
Optimize for fast-path in String>hash, String>>hash_fnv1a and Symbol>>hash to avoid jumps
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18487
diff
changeset
|
474 |
RETURN(__mkSmallInteger(h)); |
cb7a21f38b77
Optimize for fast-path in String>hash, String>>hash_fnv1a and Symbol>>hash to avoid jumps
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18487
diff
changeset
|
475 |
} else { |
18487
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
476 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
18488
cb7a21f38b77
Optimize for fast-path in String>hash, String>>hash_fnv1a and Symbol>>hash to avoid jumps
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18487
diff
changeset
|
477 |
cp += n; |
cb7a21f38b77
Optimize for fast-path in String>hash, String>>hash_fnv1a and Symbol>>hash to avoid jumps
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18487
diff
changeset
|
478 |
goto cont; |
18487
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
479 |
} |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
480 |
#endif /* not SCHTEAM */ |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
481 |
%}. |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
482 |
^ self primitiveFailed |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
483 |
|
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
484 |
! |
8735bd9eee2f
Use inlined FNV1a hash for String
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18366
diff
changeset
|
485 |
|
356 | 486 |
identityHash |
487 |
"interned symbols can return a better hash key" |
|
488 |
||
13881 | 489 |
%{ /* NOCONTEXT */ |
490 |
||
16891
93560afff48f
oops - hashes should fit into a smallInt
Claus Gittinger <cg@exept.de>
parents:
16890
diff
changeset
|
491 |
REGISTER unsigned int val; // yes: only 32 bit |
356 | 492 |
|
14651 | 493 |
if (__qIsSymbol(self)) { |
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
494 |
val = __GET_HASH(self); |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
495 |
/* |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
496 |
* only do it, if I have no standard hash key |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
497 |
* assigned (which can only happen due to a #become:, |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
498 |
* or by creating a symbol uninterned, and interning it |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
499 |
* after it got a hashKey assigned. |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
500 |
*/ |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
501 |
if (val == 0) { |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
502 |
extern unsigned int __symbolHash(char *); |
14651 | 503 |
|
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
504 |
val = __symbolHash(__stringVal(self)); |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
505 |
// make sure, it fits into a smallInt |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
506 |
val = (val ^ (val >> 30)) & 0x3FFFFFFF; |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
507 |
} else { |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
508 |
val = __MAKE_HASH__(val); |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
509 |
} |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
510 |
RETURN ( __mkSmallInteger(val) ); |
14651 | 511 |
} |
356 | 512 |
%}. |
14651 | 513 |
|
514 |
^ super identityHash. |
|
3985 | 515 |
|
516 |
" |
|
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
517 |
|hashColl hashSet| |
3985 | 518 |
|
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
519 |
hashColl := OrderedCollection new:20000. |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
520 |
Symbol allInstancesDo:[:instance | |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
521 |
hashColl add:instance identityHash |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
522 |
]. |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
523 |
hashSet := hashColl asSet. |
3985 | 524 |
|
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
525 |
Transcript showCR:'Symbols: ', hashColl size printString, |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
526 |
' unique hash keys: ', hashSet size printString, |
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
19714
diff
changeset
|
527 |
' collisions:', (hashColl size - hashSet size) printString. |
3985 | 528 |
" |
13881 | 529 |
|
530 |
"Modified (comment): / 26-12-2011 / 14:32:10 / cg" |
|
370 | 531 |
! |
532 |
||
533 |
~= something |
|
534 |
"return true, if the receiver and argument do not consist of the same characters. |
|
11859 | 535 |
Redefined here, for more efficient #~= comparison of symbols |
370 | 536 |
(which ought to be compared using #~~). |
537 |
If the argument is a symbol, we use a quick pointer compare, instead of |
|
538 |
the inherited value compare." |
|
539 |
||
540 |
%{ /* NOCONTEXT */ |
|
18701 | 541 |
#ifdef __SCHTEAM__ |
542 |
if (something.isSymbol()) { |
|
543 |
return context._RETURN ( self == something ? STObject.False : STObject.True ); |
|
544 |
} |
|
545 |
if (something.isSTString()) { |
|
546 |
return context._RETURN ( something.isStringEqual(self) ? STObject.False : STObject.True ); |
|
547 |
} |
|
548 |
if (something == STObject.Nil) { |
|
549 |
return context._RETURN_true ( ); |
|
550 |
} |
|
551 |
#else |
|
370 | 552 |
OBJ cls; |
553 |
||
379 | 554 |
if (! __isNonNilObject(something)) RETURN(true); /* cannot be equal */ |
370 | 555 |
if ((cls = __qClass(something)) == Symbol) { |
16890
8fa6938075c2
refer to the VM's internal symbol hash algorithm,
Claus Gittinger <cg@exept.de>
parents:
16774
diff
changeset
|
556 |
RETURN (self == something ? false : true); |
370 | 557 |
} |
14658 | 558 |
if (cls == String || cls == ImmutableString) { |
16890
8fa6938075c2
refer to the VM's internal symbol hash algorithm,
Claus Gittinger <cg@exept.de>
parents:
16774
diff
changeset
|
559 |
RETURN (strcmp(__stringVal(self), __stringVal(something)) == 0 ? false : true); |
370 | 560 |
} |
18701 | 561 |
#endif |
370 | 562 |
%}. |
563 |
"fall back; could be a TwoByteString, or a collection of Characters" |
|
564 |
||
565 |
^ super ~= something |
|
356 | 566 |
! ! |
567 |
||
608 | 568 |
!Symbol methodsFor:'converting'! |
569 |
||
570 |
asString |
|
571 |
"return a string with printname taken from mine" |
|
572 |
||
573 |
%{ /* NOCONTEXT */ |
|
574 |
OBJ s; |
|
575 |
||
2565 | 576 |
s = __MKSTRING_ST(self); |
608 | 577 |
if (s != nil) { |
11859 | 578 |
RETURN (s); |
608 | 579 |
} |
580 |
%}. |
|
5434 | 581 |
^ (String new:(self size)) |
11859 | 582 |
replaceFrom:1 with:self startingAt:1 |
608 | 583 |
! |
584 |
||
585 |
asSymbol |
|
19467 | 586 |
"Return a unique symbol with the name taken from the receiver's characters. |
608 | 587 |
Since I am a symbol - just return myself" |
588 |
||
589 |
^ self |
|
590 |
! |
|
591 |
||
592 |
asSymbolIfInterned |
|
17185 | 593 |
"If a symbol with the receiver's characters is already known, return it. Otherwise, return nil. |
608 | 594 |
Since I am a symbol - just return myself" |
595 |
||
596 |
^ self |
|
21017 | 597 |
! |
598 |
||
599 |
asSymbolIfInternedOrSelf |
|
600 |
^ self |
|
608 | 601 |
! ! |
602 |
||
603 |
!Symbol methodsFor:'copying'! |
|
604 |
||
605 |
copy |
|
606 |
"return a copy of myself |
|
4729 | 607 |
- reimplemented here since symbols are immutable." |
608 | 608 |
|
609 |
^ self |
|
610 |
! |
|
611 |
||
3659 | 612 |
copyReplaceAll:oldElement with:newElement |
613 |
"return a copy of the receiver as a string, where all elements equal to oldElement |
|
614 |
have been replaced by newElement." |
|
615 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
616 |
"/ 'Warning: #copyReplaceAll:with: will change semantics as defined in ANSI soon' errorPrintCR. |
3659 | 617 |
^ self asString replaceAll:oldElement with:newElement |
618 |
||
619 |
"Created: / 18.7.1998 / 23:03:38 / cg" |
|
620 |
! |
|
621 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
622 |
copyReplacing:oldElement withObject:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
623 |
"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
|
624 |
have been replaced by newElement. |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
625 |
ANSI version of what used to be #copyReplaceAll:with:" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
626 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
627 |
^ self asString replaceAll:oldElement with:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
628 |
! |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6001
diff
changeset
|
629 |
|
608 | 630 |
deepCopy |
631 |
"return a copy of myself |
|
4729 | 632 |
- reimplemented here since symbols are immutable." |
608 | 633 |
|
634 |
^ self |
|
635 |
! |
|
636 |
||
10950 | 637 |
deepCopyUsing:aDictionary postCopySelector:postCopySelector |
8383 | 638 |
"return a deep copy of myself |
4729 | 639 |
- reimplemented here since symbols are immutable." |
608 | 640 |
|
641 |
^ self |
|
642 |
! |
|
643 |
||
644 |
shallowCopy |
|
645 |
"return a copy of myself |
|
10581 | 646 |
- reimplemented here since symbols are immutable and unique, |
647 |
so we return the receiver." |
|
608 | 648 |
|
649 |
^ self |
|
10581 | 650 |
|
651 |
"Modified: / 31-05-2007 / 15:32:30 / cg" |
|
608 | 652 |
! |
653 |
||
654 |
simpleDeepCopy |
|
655 |
"return a copy of myself |
|
4729 | 656 |
- reimplemented here since symbols are immutable." |
608 | 657 |
|
658 |
^ self |
|
659 |
! ! |
|
660 |
||
16154 | 661 |
!Symbol methodsFor:'evaluation'! |
662 |
||
19605 | 663 |
value: el |
664 |
"this is sent by collection enumeration methods, |
|
665 |
if a symbol is given instead of a block as loop-block argument" |
|
666 |
||
667 |
^ el perform:self. |
|
668 |
||
669 |
" |
|
670 |
this allows us to say: |
|
671 |
||
672 |
#(1 2 3 4) do: #printCR |
|
673 |
#(1 -2 -3 4) collect: #abs |
|
674 |
someCollection collect: #withoutSeparators |
|
675 |
" |
|
676 |
! |
|
677 |
||
16154 | 678 |
value: el value:arg |
16890
8fa6938075c2
refer to the VM's internal symbol hash algorithm,
Claus Gittinger <cg@exept.de>
parents:
16774
diff
changeset
|
679 |
"this is sent by collection enumeration methods, |
16155 | 680 |
if a symbol is given instead of a block as loop-block argument" |
16154 | 681 |
|
682 |
^ el perform:self with:arg. |
|
683 |
||
684 |
" |
|
685 |
this allows us to say: |
|
686 |
||
687 |
#(1 2 3 4) with:#(10 20 30 40) collect: #+ |
|
688 |
" |
|
689 |
! ! |
|
690 |
||
14651 | 691 |
|
608 | 692 |
!Symbol methodsFor:'printing & storing'! |
693 |
||
694 |
printOn:aStream |
|
4392 | 695 |
"append a user printed representation of the receiver to aStream. |
696 |
The format is suitable for a human - not meant to be read back. |
|
697 |
||
608 | 698 |
In contrast to ST-80, this does return the symbols characters |
699 |
without any leading #. Therefore, you can directly send symbols' |
|
700 |
printStrings to some output device. This is incompatible, but easier |
|
701 |
to use." |
|
702 |
||
703 |
"/ ST-80 behavior: |
|
704 |
||
705 |
"/ self storeOn:aStream |
|
706 |
||
707 |
aStream nextPutAll:self |
|
708 |
! |
|
709 |
||
710 |
printString |
|
711 |
"return a printed representation of the receiver. |
|
712 |
In contrast to ST-80, this does return the symbols characters |
|
713 |
without any leading #. Therefore, you can directly send symbols' |
|
714 |
printStrings to some output device. This is incompatible, but easier |
|
715 |
to use." |
|
716 |
||
717 |
"/ ST-80 behavior: |
|
718 |
"/ ^ self storeString |
|
719 |
||
720 |
^ self asString |
|
721 |
! |
|
722 |
||
7600
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
723 |
storeArrayElementOn:aStream |
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
724 |
"store myself on a stream. |
8368 | 725 |
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
|
726 |
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
|
727 |
|
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
728 |
|storeString| |
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
729 |
|
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
730 |
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
|
731 |
(self == #true or:[self == #false or:[self == #nil or:[(storeString at:2) == $']]]) ifTrue:[ |
11859 | 732 |
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
|
733 |
] ifFalse:[ |
11859 | 734 |
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
|
735 |
]. |
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
736 |
! |
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
737 |
|
608 | 738 |
storeOn:aStream |
739 |
"store myself on a stream" |
|
740 |
||
741 |
aStream nextPutAll:(self storeString) |
|
742 |
! |
|
743 |
||
744 |
storeString |
|
745 |
"return a String for storing the receiver" |
|
746 |
||
11859 | 747 |
|sz "{Class: SmallInteger }" |
5941
e194c325bee8
storeString fixed for symbols like #'foo:bar'
Claus Gittinger <cg@exept.de>
parents:
5896
diff
changeset
|
748 |
c anyColon| |
1125 | 749 |
|
7600
5a3fee21c032
#storeString - omit # from Symbols and Arrays that are elements of an array.
Stefan Vogel <sv@exept.de>
parents:
7350
diff
changeset
|
750 |
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
|
751 |
(sz ~~ 0 and:[(self at:1) isLetter]) ifTrue:[ |
11859 | 752 |
anyColon := false. |
753 |
2 to:sz do:[:index | |
|
754 |
c := self at:index. |
|
755 |
c == $: ifTrue:[ |
|
756 |
(index == sz or:[(self at:(index+1)) isLetterOrDigit]) ifFalse:[ |
|
757 |
^ '#' , super storeString. |
|
758 |
]. |
|
759 |
anyColon := true. |
|
760 |
] ifFalse:[ |
|
761 |
c isLetterOrDigit ifFalse:[ |
|
762 |
^ '#' , super storeString |
|
763 |
]. |
|
764 |
]. |
|
765 |
]. |
|
766 |
"no colon in symbol or symbol ends with a colon" |
|
767 |
(anyColon and:[c ~~ $:]) ifFalse:[ |
|
768 |
^ '#' , self |
|
769 |
]. |
|
608 | 770 |
]. |
5896 | 771 |
^ '#' , super storeString |
3285
bd5827c97b21
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3086
diff
changeset
|
772 |
|
3286
c6987c5f15a8
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3285
diff
changeset
|
773 |
" |
11859 | 774 |
#'abc' storeString |
775 |
#'abc:' storeString |
|
776 |
#'abc:def:' storeString |
|
777 |
#'abc:def' storeString |
|
778 |
#'abc::def' storeString |
|
779 |
#'abc &^*' storeString |
|
780 |
#'abcdef::' storeString |
|
781 |
#'hello''world' storeString |
|
782 |
#'' storeString |
|
783 |
#'''' storeString |
|
784 |
#'_hello' storeString |
|
785 |
#'123' storeString |
|
3286
c6987c5f15a8
Do not need quotes in #storeString when a symbol contains a ':'.
Stefan Vogel <sv@exept.de>
parents:
3285
diff
changeset
|
786 |
" |
608 | 787 |
! ! |
788 |
||
789 |
!Symbol methodsFor:'queries'! |
|
790 |
||
791 |
species |
|
792 |
"when copying, or concatenating, return instances of this class" |
|
793 |
||
794 |
^ String |
|
795 |
! ! |
|
796 |
||
77 | 797 |
!Symbol methodsFor:'system primitives'! |
798 |
||
799 |
become:anotherObject |
|
800 |
"make all references to the receiver become references to anotherObject |
|
95 | 801 |
and vice-versa. For symbols, some special action is required, to |
802 |
correctly handle a become of the global dictionaries. |
|
77 | 803 |
Anyway: this is very dangerous - mysterous side-effects are to be |
95 | 804 |
expected. |
805 |
||
806 |
Notice: because of the danger here, this method may report an error |
|
159 | 807 |
in future versions" |
77 | 808 |
|
809 |
(Smalltalk includesKey:self) ifTrue:[ |
|
159 | 810 |
super become:anotherObject. |
77 | 811 |
%{ |
159 | 812 |
__rehashSystemDictionaries(); |
77 | 813 |
%}. |
814 |
] ifFalse:[ |
|
159 | 815 |
super become:anotherObject |
77 | 816 |
] |
817 |
! |
|
818 |
||
819 |
becomeNil |
|
820 |
"make all references to the receiver become nil - effectively getting |
|
95 | 821 |
rid of the receiver. For symbols, this is not allowed, if the receiver |
7765 | 822 |
is used as a key in some SystemDictionary. |
95 | 823 |
This can be a very dangerous operation - be warned. |
824 |
||
825 |
Notice: because of the danger here, this method may report an error |
|
11859 | 826 |
in future versions" |
77 | 827 |
|
828 |
(Smalltalk includesKey:self) ifTrue:[ |
|
11859 | 829 |
self primitiveFailed |
77 | 830 |
] ifFalse:[ |
11859 | 831 |
super becomeNil |
77 | 832 |
] |
13388 | 833 |
! |
834 |
||
835 |
grow:newSize |
|
13389 | 836 |
"blocked" |
13388 | 837 |
|
13389 | 838 |
self fixedSizeError |
13388 | 839 |
|
840 |
"Created: / 20-06-2011 / 14:57:36 / cg" |
|
13389 | 841 |
! |
842 |
||
843 |
removeAll |
|
844 |
"blocked" |
|
845 |
||
846 |
self fixedSizeError |
|
847 |
||
848 |
"Created: / 20-06-2011 / 14:59:02 / cg" |
|
77 | 849 |
! ! |
850 |
||
16774 | 851 |
!Symbol methodsFor:'testing'! |
852 |
||
853 |
isSingleByteString |
|
854 |
"returns true only for strings and immutable strings. |
|
855 |
Must replace foo isMemberOf:String and foo class == String" |
|
856 |
||
857 |
^ false |
|
21127 | 858 |
! |
859 |
||
860 |
isSymbol |
|
861 |
"return true, if the receiver is some kind of symbol. |
|
862 |
Since I am a symbol, return always true" |
|
863 |
||
864 |
^ true |
|
16774 | 865 |
! ! |
866 |
||
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
867 |
!Symbol methodsFor:'tracing'! |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
868 |
|
4682 | 869 |
traceInto:aRequestor level:level from:referrer |
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
870 |
"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
|
871 |
|
4682 | 872 |
^ aRequestor traceSymbol:self level:level from:referrer |
4651
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
873 |
|
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
874 |
|
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
875 |
! ! |
18a6ac30b36f
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
876 |
|
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8383
diff
changeset
|
877 |
!Symbol methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8383
diff
changeset
|
878 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8383
diff
changeset
|
879 |
acceptVisitor:aVisitor with:aParameter |
16735 | 880 |
"dispatch for visitor pattern; send #visitSymbol:with: to aVisitor" |
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8383
diff
changeset
|
881 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8383
diff
changeset
|
882 |
^ aVisitor visitSymbol:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8383
diff
changeset
|
883 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8383
diff
changeset
|
884 |
|
632 | 885 |
!Symbol class methodsFor:'documentation'! |
886 |
||
16155 | 887 |
version |
18619 | 888 |
^ '$Header$' |
16155 | 889 |
! |
890 |
||
13423 | 891 |
version_CVS |
18619 | 892 |
^ '$Header$' |
12473
828e275ddf8b
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11859
diff
changeset
|
893 |
! |
828e275ddf8b
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11859
diff
changeset
|
894 |
|
13423 | 895 |
version_SVN |
15524 | 896 |
^ '$ Id: Symbol.st 10648 2011-06-23 15:55:10Z vranyj1 $' |
632 | 897 |
! ! |
21017 | 898 |