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