author | Patrik Svestka <patrik.svestka@gmail.com> |
Tue, 09 Apr 2019 11:34:04 +0200 | |
branch | jv |
changeset 24093 | 0f94f6c8c9d4 |
parent 23547 | c69c97cec351 |
child 25420 | a18d0d048b1f |
permissions | -rw-r--r-- |
3714 | 1 |
" |
2 |
COPYRIGHT (c) 1998 by eXept Software AG |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
3 |
All Rights Reserved |
3714 | 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 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
8807
606a4ec5b693
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
3714
diff
changeset
|
13 |
|
17373 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
8807
606a4ec5b693
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
3714
diff
changeset
|
16 |
String variableByteSubclass:#ImmutableString |
3714 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'System-Compiler-Support' |
|
21 |
! |
|
22 |
||
23 |
!ImmutableString class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1998 by eXept Software AG |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
28 |
All Rights Reserved |
3714 | 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 |
! |
|
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
By default, string literals in smalltalk are mutable objects. That |
|
43 |
may lead to some subtle (and hard to find errors), if some method passes |
|
44 |
a string constant as argument to someone else, who changes the |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
45 |
string using at:put: like messages. Since the string object is kept in |
3714 | 46 |
the first methods literals, the string constant has now been changed without |
12464 | 47 |
having the method's sourcecode reflect this. Thus, the method will |
3714 | 48 |
behave differently from what its source may make you think. |
49 |
||
50 |
To help finding this kind of 'feature/bug', the compiler can be |
|
51 |
configured to create instances of this ImmutableString instead of Strings |
|
12464 | 52 |
for literals. Instances of ImmutableString catch storing accesses and |
3714 | 53 |
enter the debugger. Although useful, this feature is disabled by default |
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
54 |
for compatibility to other smalltalk implementations. |
3714 | 55 |
(Also, if turned on, this makes inspecting string literals entered in |
56 |
a workspace somewhat strange: you cannot modify it any longer). |
|
57 |
||
58 |
Turn the ImmutableString feature on by setting the Parsers class variable |
|
59 |
'StringsAreImmutable' to true or use the new launchers settings menu. |
|
60 |
||
18546 | 61 |
This class should be used only by the compiler. |
12471 | 62 |
|
63 |
ATTENTION: |
|
14222 | 64 |
there may be still code around which checks for explicit class being String |
65 |
(both in Smalltalk and in primitive code). All code like foo 'class == String' |
|
66 |
or '__isString()' will not work with ImmutableStrings. Use '__isStringLike()' instead. |
|
67 |
A somewhat better approach would be to either add a flag to the object (mutability) |
|
68 |
and check this dynamically (expensive) or to place immutable objects into a readonly |
|
69 |
memory segment (the good solution). We will eventually implement the second in the future... |
|
12471 | 70 |
|
3714 | 71 |
[see also:] |
14222 | 72 |
ImmutableArray |
73 |
Parser Scanner |
|
3714 | 74 |
|
75 |
[author:] |
|
14222 | 76 |
Claus Gittinger |
3714 | 77 |
" |
78 |
! ! |
|
79 |
||
17373 | 80 |
!ImmutableString class methodsFor:'queries'! |
81 |
||
82 |
mutableClass |
|
83 |
"answer an equivalent mustable class" |
|
84 |
||
85 |
^ String |
|
86 |
! ! |
|
87 |
||
88 |
!ImmutableString class methodsFor:'testing'! |
|
89 |
||
90 |
hasImmutableInstances |
|
91 |
^ true |
|
92 |
! ! |
|
93 |
||
3714 | 94 |
!ImmutableString methodsFor:'accessing'! |
95 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
96 |
at:index put:value |
3714 | 97 |
"Trigger an error if an immutable string is stored into. |
98 |
The store will be performed (for compatibility reasons) if you continue |
|
99 |
in the debugger." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
100 |
|
12491 | 101 |
self noModificationError. |
3714 | 102 |
^ super at:index put:value |
103 |
||
104 |
"Created: / 3.8.1998 / 14:45:14 / cg" |
|
12457 | 105 |
! |
106 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
107 |
basicAt:index put:value |
12457 | 108 |
"Trigger an error if an immutable string is stored into. |
109 |
The store will be performed (for compatibility reasons) if you continue |
|
110 |
in the debugger." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
111 |
|
12491 | 112 |
self noModificationError. |
12457 | 113 |
^ super basicAt:index put:value |
114 |
! ! |
|
115 |
||
116 |
!ImmutableString methodsFor:'converting'! |
|
117 |
||
118 |
asImmutableString |
|
119 |
^ self |
|
16824 | 120 |
! |
121 |
||
122 |
asMutableCollection |
|
123 |
"return a writable copy of myself" |
|
124 |
||
125 |
^ self copy changeClassTo:String |
|
20525 | 126 |
! |
127 |
||
128 |
beImmutable |
|
129 |
"that's what I am" |
|
130 |
||
131 |
^ self |
|
132 |
! |
|
133 |
||
134 |
beMutable |
|
135 |
"you never go back" |
|
136 |
||
137 |
^ self shouldNotImplement |
|
3714 | 138 |
! ! |
139 |
||
140 |
!ImmutableString methodsFor:'copying'! |
|
141 |
||
20034 | 142 |
copy |
143 |
"return a copy of the receiver" |
|
144 |
||
145 |
^ self copyFrom:1 |
|
146 |
||
147 |
" |
|
148 |
'abcd' asImmutableString copy |
|
149 |
" |
|
150 |
! |
|
151 |
||
152 |
deepCopy |
|
153 |
"when copying, return a real (mutable) String" |
|
154 |
||
155 |
(self class == ImmutableString) ifTrue:[ |
|
156 |
^ self copyFrom:1 |
|
157 |
]. |
|
158 |
^ super deepCopy |
|
159 |
||
160 |
" |
|
161 |
'hello world' asImmutableString deepCopy |
|
162 |
" |
|
163 |
! |
|
164 |
||
165 |
deepCopyUsing:aDictionary postCopySelector:postCopySelector |
|
166 |
"return a deep copy of the receiver - reimplemented to be a bit faster" |
|
167 |
||
168 |
" |
|
169 |
could be an instance of a subclass which needs deepCopy |
|
170 |
of its named instvars ... |
|
171 |
" |
|
172 |
(self class == ImmutableString) ifTrue:[ |
|
173 |
^ self copyFrom:1 |
|
174 |
]. |
|
175 |
^ super deepCopyUsing:aDictionary postCopySelector:postCopySelector |
|
176 |
||
177 |
" |
|
178 |
'hello world' asImmutableString deepCopyUsing:nil postCopySelector:nil |
|
179 |
" |
|
180 |
! |
|
181 |
||
3714 | 182 |
postCopy |
12457 | 183 |
"when copied, make me a real (mutable) String" |
184 |
||
185 |
self changeClassTo:String. |
|
186 |
||
187 |
"Created: / 3.8.1998 / 14:46:45 / cg" |
|
188 |
! |
|
189 |
||
190 |
postDeepCopy |
|
3714 | 191 |
"when copied, make it me a real (mutable) String" |
192 |
||
193 |
self changeClassTo:String. |
|
194 |
||
12457 | 195 |
" |
196 |
'foobar' asImmutableString copy class |
|
197 |
'foobar' asImmutableString shallowCopy class |
|
198 |
'foobar' asImmutableString deepCopy class |
|
199 |
" |
|
3714 | 200 |
! |
201 |
||
202 |
shallowCopy |
|
203 |
"when copying, return a real (mutable) String" |
|
204 |
||
20034 | 205 |
(self class == ImmutableString) ifTrue:[ |
206 |
^ self copyFrom:1 |
|
207 |
]. |
|
208 |
^ super shallowCopy |
|
3714 | 209 |
|
14115
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
210 |
" |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
211 |
'hello world' asImmutableString shallowCopy |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
212 |
" |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
213 |
|
3714 | 214 |
"Created: / 3.8.1998 / 14:47:00 / cg" |
20034 | 215 |
! |
216 |
||
217 |
simpleDeepCopy |
|
218 |
"when copying, return a real (mutable) String" |
|
219 |
||
220 |
(self class == ImmutableString) ifTrue:[ |
|
221 |
^ self copyFrom:1 |
|
222 |
]. |
|
223 |
^ super simpleDeepCopy |
|
224 |
||
225 |
" |
|
226 |
'hello world' asImmutableString simpleDeepCopy |
|
227 |
" |
|
3714 | 228 |
! ! |
229 |
||
230 |
!ImmutableString methodsFor:'private'! |
|
231 |
||
232 |
species |
|
233 |
"Copies should be mutable" |
|
234 |
||
235 |
^ String |
|
236 |
||
237 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
16400 | 238 |
! |
239 |
||
240 |
speciesForCopy |
|
241 |
"Copies should be mutable" |
|
242 |
||
243 |
^ String |
|
244 |
||
245 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
3714 | 246 |
! ! |
247 |
||
14386 | 248 |
!ImmutableString methodsFor:'queries'! |
249 |
||
250 |
isImmutable |
|
251 |
^ true |
|
252 |
! ! |
|
253 |
||
3714 | 254 |
!ImmutableString methodsFor:'specials'! |
255 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
256 |
become:anotherObject |
3714 | 257 |
"trigger an error if I should become something else |
258 |
(this would be an even more tricky manipulation)" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
259 |
|
12491 | 260 |
self noModificationError. |
3714 | 261 |
^ super become:anotherObject |
262 |
||
263 |
"Created: / 3.8.1998 / 14:48:29 / cg" |
|
264 |
! |
|
265 |
||
266 |
becomeNil |
|
267 |
"trigger an error if I should become nil |
|
268 |
(this would be an even more tricky manipulation)" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
269 |
|
12491 | 270 |
self noModificationError. |
3714 | 271 |
^ super becomeNil |
272 |
||
273 |
"Created: / 3.8.1998 / 14:48:37 / cg" |
|
18704 | 274 |
! |
275 |
||
276 |
becomeSameAs:anotherObject |
|
277 |
"trigger an error if I should become something else |
|
278 |
(this would be an even more tricky manipulation)" |
|
279 |
||
280 |
self noModificationError. |
|
281 |
^ super becomeSameAs:anotherObject |
|
3714 | 282 |
! ! |
283 |
||
284 |
!ImmutableString class methodsFor:'documentation'! |
|
285 |
||
286 |
version |
|
18546 | 287 |
^ '$Header$' |
12457 | 288 |
! |
289 |
||
290 |
version_CVS |
|
18546 | 291 |
^ '$Header$' |
3714 | 292 |
! ! |
16400 | 293 |