author | Jan Vrany <jan.vrany@labware.com> |
Tue, 01 Jun 2021 20:19:13 +0100 | |
branch | jv |
changeset 25424 | 51bd8a6b196f |
parent 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 |
||
25420
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
118 |
asImmutableCollection |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
119 |
^ self |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
120 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
121 |
"Created: / 15-03-2019 / 13:53:13 / Stefan Vogel" |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
122 |
! |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
123 |
|
12457 | 124 |
asImmutableString |
125 |
^ self |
|
16824 | 126 |
! |
127 |
||
128 |
asMutableCollection |
|
129 |
"return a writable copy of myself" |
|
130 |
||
25420
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
131 |
^ self copyFrom:1. "/ #copyFrom: always raturns a String |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
132 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
133 |
"Modified (comment): / 15-03-2019 / 13:26:43 / Stefan Vogel" |
20525 | 134 |
! |
135 |
||
136 |
beImmutable |
|
137 |
"that's what I am" |
|
138 |
||
139 |
^ self |
|
140 |
! |
|
141 |
||
142 |
beMutable |
|
143 |
"you never go back" |
|
144 |
||
145 |
^ self shouldNotImplement |
|
3714 | 146 |
! ! |
147 |
||
148 |
!ImmutableString methodsFor:'copying'! |
|
149 |
||
20034 | 150 |
copy |
151 |
"return a copy of the receiver" |
|
152 |
||
153 |
^ self copyFrom:1 |
|
154 |
||
155 |
" |
|
156 |
'abcd' asImmutableString copy |
|
157 |
" |
|
158 |
! |
|
159 |
||
160 |
deepCopy |
|
161 |
"when copying, return a real (mutable) String" |
|
162 |
||
163 |
(self class == ImmutableString) ifTrue:[ |
|
164 |
^ self copyFrom:1 |
|
165 |
]. |
|
166 |
^ super deepCopy |
|
167 |
||
168 |
" |
|
169 |
'hello world' asImmutableString deepCopy |
|
170 |
" |
|
171 |
! |
|
172 |
||
173 |
deepCopyUsing:aDictionary postCopySelector:postCopySelector |
|
174 |
"return a deep copy of the receiver - reimplemented to be a bit faster" |
|
175 |
||
25420
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
176 |
"when copying, return a real (mutable) String" |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
177 |
|
20034 | 178 |
" |
179 |
could be an instance of a subclass which needs deepCopy |
|
180 |
of its named instvars ... |
|
181 |
" |
|
182 |
(self class == ImmutableString) ifTrue:[ |
|
183 |
^ self copyFrom:1 |
|
184 |
]. |
|
185 |
^ super deepCopyUsing:aDictionary postCopySelector:postCopySelector |
|
186 |
||
187 |
" |
|
188 |
'hello world' asImmutableString deepCopyUsing:nil postCopySelector:nil |
|
189 |
" |
|
190 |
||
25420
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
191 |
"Modified (comment): / 23-05-2020 / 21:00:39 / cg" |
3714 | 192 |
! |
193 |
||
194 |
shallowCopy |
|
195 |
"when copying, return a real (mutable) String" |
|
196 |
||
20034 | 197 |
(self class == ImmutableString) ifTrue:[ |
198 |
^ self copyFrom:1 |
|
199 |
]. |
|
200 |
^ super shallowCopy |
|
3714 | 201 |
|
14115
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
202 |
" |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
203 |
'hello world' asImmutableString shallowCopy |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
204 |
" |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
205 |
|
3714 | 206 |
"Created: / 3.8.1998 / 14:47:00 / cg" |
20034 | 207 |
! |
208 |
||
209 |
simpleDeepCopy |
|
210 |
"when copying, return a real (mutable) String" |
|
211 |
||
212 |
(self class == ImmutableString) ifTrue:[ |
|
213 |
^ self copyFrom:1 |
|
214 |
]. |
|
215 |
^ super simpleDeepCopy |
|
216 |
||
217 |
" |
|
218 |
'hello world' asImmutableString simpleDeepCopy |
|
219 |
" |
|
3714 | 220 |
! ! |
221 |
||
25420
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
222 |
!ImmutableString methodsFor:'copying-private'! |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
223 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
224 |
postCopy |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
225 |
"when copied, make me a real (mutable) String" |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
226 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
227 |
self changeClassTo:String. |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
228 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
229 |
"Created: / 3.8.1998 / 14:46:45 / cg" |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
230 |
! |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
231 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
232 |
postDeepCopy |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
233 |
"when copied, make it me a real (mutable) String" |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
234 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
235 |
self changeClassTo:String. |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
236 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
237 |
" |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
238 |
'foobar' asImmutableString copy class |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
239 |
'foobar' asImmutableString shallowCopy class |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
240 |
'foobar' asImmutableString deepCopy class |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
241 |
" |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
242 |
! ! |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
243 |
|
3714 | 244 |
!ImmutableString methodsFor:'private'! |
245 |
||
246 |
species |
|
247 |
"Copies should be mutable" |
|
248 |
||
249 |
^ String |
|
250 |
||
251 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
16400 | 252 |
! |
253 |
||
254 |
speciesForCopy |
|
255 |
"Copies should be mutable" |
|
256 |
||
257 |
^ String |
|
258 |
||
259 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
3714 | 260 |
! ! |
261 |
||
14386 | 262 |
!ImmutableString methodsFor:'queries'! |
263 |
||
264 |
isImmutable |
|
25420
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
265 |
"return true, if the receiver is immutable. |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
266 |
Since I am an immutable string, return always true here" |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
267 |
|
14386 | 268 |
^ true |
25420
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
269 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
23547
diff
changeset
|
270 |
"Modified (comment): / 09-06-2019 / 14:54:49 / Claus Gittinger" |
14386 | 271 |
! ! |
272 |
||
3714 | 273 |
!ImmutableString methodsFor:'specials'! |
274 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
275 |
become:anotherObject |
3714 | 276 |
"trigger an error if I should become something else |
277 |
(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
|
278 |
|
12491 | 279 |
self noModificationError. |
3714 | 280 |
^ super become:anotherObject |
281 |
||
282 |
"Created: / 3.8.1998 / 14:48:29 / cg" |
|
283 |
! |
|
284 |
||
285 |
becomeNil |
|
286 |
"trigger an error if I should become nil |
|
287 |
(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
|
288 |
|
12491 | 289 |
self noModificationError. |
3714 | 290 |
^ super becomeNil |
291 |
||
292 |
"Created: / 3.8.1998 / 14:48:37 / cg" |
|
18704 | 293 |
! |
294 |
||
295 |
becomeSameAs:anotherObject |
|
296 |
"trigger an error if I should become something else |
|
297 |
(this would be an even more tricky manipulation)" |
|
298 |
||
299 |
self noModificationError. |
|
300 |
^ super becomeSameAs:anotherObject |
|
3714 | 301 |
! ! |
302 |
||
303 |
!ImmutableString class methodsFor:'documentation'! |
|
304 |
||
305 |
version |
|
18546 | 306 |
^ '$Header$' |
12457 | 307 |
! |
308 |
||
309 |
version_CVS |
|
18546 | 310 |
^ '$Header$' |
3714 | 311 |
! ! |
16400 | 312 |