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