author | Claus Gittinger <cg@exept.de> |
Mon, 27 May 2019 15:01:44 +0200 | |
changeset 24172 | ddd5ab5bc403 |
parent 23922 | 24e48e381aa1 |
child 24309 | 54640603c048 |
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 |
||
23922 | 118 |
asImmutableCollection |
119 |
^ self |
|
120 |
||
121 |
"Created: / 15-03-2019 / 13:53:13 / Stefan Vogel" |
|
122 |
! |
|
123 |
||
12457 | 124 |
asImmutableString |
125 |
^ self |
|
16824 | 126 |
! |
127 |
||
128 |
asMutableCollection |
|
129 |
"return a writable copy of myself" |
|
130 |
||
23917 | 131 |
^ self copyFrom:1. "/ #copyFrom: always raturns a String |
132 |
||
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 |
||
176 |
" |
|
177 |
could be an instance of a subclass which needs deepCopy |
|
178 |
of its named instvars ... |
|
179 |
" |
|
180 |
(self class == ImmutableString) ifTrue:[ |
|
181 |
^ self copyFrom:1 |
|
182 |
]. |
|
183 |
^ super deepCopyUsing:aDictionary postCopySelector:postCopySelector |
|
184 |
||
185 |
" |
|
186 |
'hello world' asImmutableString deepCopyUsing:nil postCopySelector:nil |
|
187 |
" |
|
188 |
! |
|
189 |
||
3714 | 190 |
shallowCopy |
191 |
"when copying, return a real (mutable) String" |
|
192 |
||
20034 | 193 |
(self class == ImmutableString) ifTrue:[ |
194 |
^ self copyFrom:1 |
|
195 |
]. |
|
196 |
^ super shallowCopy |
|
3714 | 197 |
|
14115
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
198 |
" |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
199 |
'hello world' asImmutableString shallowCopy |
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 |
|
3714 | 202 |
"Created: / 3.8.1998 / 14:47:00 / cg" |
20034 | 203 |
! |
204 |
||
205 |
simpleDeepCopy |
|
206 |
"when copying, return a real (mutable) String" |
|
207 |
||
208 |
(self class == ImmutableString) ifTrue:[ |
|
209 |
^ self copyFrom:1 |
|
210 |
]. |
|
211 |
^ super simpleDeepCopy |
|
212 |
||
213 |
" |
|
214 |
'hello world' asImmutableString simpleDeepCopy |
|
215 |
" |
|
3714 | 216 |
! ! |
217 |
||
24172 | 218 |
!ImmutableString methodsFor:'copying-private'! |
219 |
||
220 |
postCopy |
|
221 |
"when copied, make me a real (mutable) String" |
|
222 |
||
223 |
self changeClassTo:String. |
|
224 |
||
225 |
"Created: / 3.8.1998 / 14:46:45 / cg" |
|
226 |
! |
|
227 |
||
228 |
postDeepCopy |
|
229 |
"when copied, make it me a real (mutable) String" |
|
230 |
||
231 |
self changeClassTo:String. |
|
232 |
||
233 |
" |
|
234 |
'foobar' asImmutableString copy class |
|
235 |
'foobar' asImmutableString shallowCopy class |
|
236 |
'foobar' asImmutableString deepCopy class |
|
237 |
" |
|
238 |
! ! |
|
239 |
||
3714 | 240 |
!ImmutableString methodsFor:'private'! |
241 |
||
242 |
species |
|
243 |
"Copies should be mutable" |
|
244 |
||
245 |
^ String |
|
246 |
||
247 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
16400 | 248 |
! |
249 |
||
250 |
speciesForCopy |
|
251 |
"Copies should be mutable" |
|
252 |
||
253 |
^ String |
|
254 |
||
255 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
3714 | 256 |
! ! |
257 |
||
14386 | 258 |
!ImmutableString methodsFor:'queries'! |
259 |
||
260 |
isImmutable |
|
261 |
^ true |
|
262 |
! ! |
|
263 |
||
3714 | 264 |
!ImmutableString methodsFor:'specials'! |
265 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
266 |
become:anotherObject |
3714 | 267 |
"trigger an error if I should become something else |
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 become:anotherObject |
272 |
||
273 |
"Created: / 3.8.1998 / 14:48:29 / cg" |
|
274 |
! |
|
275 |
||
276 |
becomeNil |
|
277 |
"trigger an error if I should become nil |
|
278 |
(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
|
279 |
|
12491 | 280 |
self noModificationError. |
3714 | 281 |
^ super becomeNil |
282 |
||
283 |
"Created: / 3.8.1998 / 14:48:37 / cg" |
|
18704 | 284 |
! |
285 |
||
286 |
becomeSameAs:anotherObject |
|
287 |
"trigger an error if I should become something else |
|
288 |
(this would be an even more tricky manipulation)" |
|
289 |
||
290 |
self noModificationError. |
|
291 |
^ super becomeSameAs:anotherObject |
|
3714 | 292 |
! ! |
293 |
||
294 |
!ImmutableString class methodsFor:'documentation'! |
|
295 |
||
296 |
version |
|
18546 | 297 |
^ '$Header$' |
12457 | 298 |
! |
299 |
||
300 |
version_CVS |
|
18546 | 301 |
^ '$Header$' |
3714 | 302 |
! ! |
16400 | 303 |