author | Claus Gittinger <cg@exept.de> |
Wed, 15 Feb 2017 22:29:42 +0100 | |
changeset 21455 | a657a28cab85 |
parent 20525 | d7da56a61fc0 |
child 20579 | 9add81aadb7a |
child 23917 | d98b4d786122 |
permissions | -rw-r--r-- |
18546 | 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 |
||
120 |
asImmutableString |
|
121 |
^ self |
|
16824 | 122 |
! |
123 |
||
124 |
asMutableCollection |
|
125 |
"return a writable copy of myself" |
|
126 |
||
127 |
^ self copy changeClassTo:String |
|
20525 | 128 |
! |
129 |
||
130 |
beImmutable |
|
131 |
"that's what I am" |
|
132 |
||
133 |
^ self |
|
134 |
! |
|
135 |
||
136 |
beMutable |
|
137 |
"you never go back" |
|
138 |
||
139 |
^ self shouldNotImplement |
|
3714 | 140 |
! ! |
141 |
||
142 |
!ImmutableString methodsFor:'copying'! |
|
143 |
||
20034 | 144 |
copy |
145 |
"return a copy of the receiver" |
|
146 |
||
147 |
^ self copyFrom:1 |
|
148 |
||
149 |
" |
|
150 |
'abcd' asImmutableString copy |
|
151 |
" |
|
152 |
! |
|
153 |
||
154 |
deepCopy |
|
155 |
"when copying, return a real (mutable) String" |
|
156 |
||
157 |
(self class == ImmutableString) ifTrue:[ |
|
158 |
^ self copyFrom:1 |
|
159 |
]. |
|
160 |
^ super deepCopy |
|
161 |
||
162 |
" |
|
163 |
'hello world' asImmutableString deepCopy |
|
164 |
" |
|
165 |
! |
|
166 |
||
167 |
deepCopyUsing:aDictionary postCopySelector:postCopySelector |
|
168 |
"return a deep copy of the receiver - reimplemented to be a bit faster" |
|
169 |
||
170 |
" |
|
171 |
could be an instance of a subclass which needs deepCopy |
|
172 |
of its named instvars ... |
|
173 |
" |
|
174 |
(self class == ImmutableString) ifTrue:[ |
|
175 |
^ self copyFrom:1 |
|
176 |
]. |
|
177 |
^ super deepCopyUsing:aDictionary postCopySelector:postCopySelector |
|
178 |
||
179 |
" |
|
180 |
'hello world' asImmutableString deepCopyUsing:nil postCopySelector:nil |
|
181 |
" |
|
182 |
! |
|
183 |
||
3714 | 184 |
postCopy |
12457 | 185 |
"when copied, make me a real (mutable) String" |
186 |
||
187 |
self changeClassTo:String. |
|
188 |
||
189 |
"Created: / 3.8.1998 / 14:46:45 / cg" |
|
190 |
! |
|
191 |
||
192 |
postDeepCopy |
|
3714 | 193 |
"when copied, make it me a real (mutable) String" |
194 |
||
195 |
self changeClassTo:String. |
|
196 |
||
12457 | 197 |
" |
198 |
'foobar' asImmutableString copy class |
|
199 |
'foobar' asImmutableString shallowCopy class |
|
200 |
'foobar' asImmutableString deepCopy class |
|
201 |
" |
|
3714 | 202 |
! |
203 |
||
204 |
shallowCopy |
|
205 |
"when copying, return a real (mutable) String" |
|
206 |
||
20034 | 207 |
(self class == ImmutableString) ifTrue:[ |
208 |
^ self copyFrom:1 |
|
209 |
]. |
|
210 |
^ super shallowCopy |
|
3714 | 211 |
|
14115
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 |
'hello world' asImmutableString shallowCopy |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
214 |
" |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
215 |
|
3714 | 216 |
"Created: / 3.8.1998 / 14:47:00 / cg" |
20034 | 217 |
! |
218 |
||
219 |
simpleDeepCopy |
|
220 |
"when copying, return a real (mutable) String" |
|
221 |
||
222 |
(self class == ImmutableString) ifTrue:[ |
|
223 |
^ self copyFrom:1 |
|
224 |
]. |
|
225 |
^ super simpleDeepCopy |
|
226 |
||
227 |
" |
|
228 |
'hello world' asImmutableString simpleDeepCopy |
|
229 |
" |
|
3714 | 230 |
! ! |
231 |
||
232 |
!ImmutableString methodsFor:'private'! |
|
233 |
||
234 |
species |
|
235 |
"Copies should be mutable" |
|
236 |
||
237 |
^ String |
|
238 |
||
239 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
16400 | 240 |
! |
241 |
||
242 |
speciesForCopy |
|
243 |
"Copies should be mutable" |
|
244 |
||
245 |
^ String |
|
246 |
||
247 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
3714 | 248 |
! ! |
249 |
||
14386 | 250 |
!ImmutableString methodsFor:'queries'! |
251 |
||
252 |
isImmutable |
|
253 |
^ true |
|
254 |
! ! |
|
255 |
||
3714 | 256 |
!ImmutableString methodsFor:'specials'! |
257 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
258 |
become:anotherObject |
3714 | 259 |
"trigger an error if I should become something else |
260 |
(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
|
261 |
|
12491 | 262 |
self noModificationError. |
3714 | 263 |
^ super become:anotherObject |
264 |
||
265 |
"Created: / 3.8.1998 / 14:48:29 / cg" |
|
266 |
! |
|
267 |
||
268 |
becomeNil |
|
269 |
"trigger an error if I should become nil |
|
270 |
(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
|
271 |
|
12491 | 272 |
self noModificationError. |
3714 | 273 |
^ super becomeNil |
274 |
||
275 |
"Created: / 3.8.1998 / 14:48:37 / cg" |
|
18704 | 276 |
! |
277 |
||
278 |
becomeSameAs:anotherObject |
|
279 |
"trigger an error if I should become something else |
|
280 |
(this would be an even more tricky manipulation)" |
|
281 |
||
282 |
self noModificationError. |
|
283 |
^ super becomeSameAs:anotherObject |
|
3714 | 284 |
! ! |
285 |
||
286 |
!ImmutableString class methodsFor:'documentation'! |
|
287 |
||
288 |
version |
|
18546 | 289 |
^ '$Header$' |
12457 | 290 |
! |
291 |
||
292 |
version_CVS |
|
18546 | 293 |
^ '$Header$' |
3714 | 294 |
! ! |
16400 | 295 |