author | Stefan Vogel <sv@exept.de> |
Thu, 20 Aug 2015 17:24:12 +0200 | |
changeset 18704 | e1dc90e637b9 |
parent 18546 | 23c33cdd1f43 |
child 18705 | 843770030c5f |
child 20034 | 2ba574fda7ab |
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 |
|
3714 | 128 |
! ! |
129 |
||
130 |
!ImmutableString methodsFor:'copying'! |
|
131 |
||
132 |
postCopy |
|
12457 | 133 |
"when copied, make me a real (mutable) String" |
134 |
||
135 |
self changeClassTo:String. |
|
136 |
||
137 |
"Created: / 3.8.1998 / 14:46:45 / cg" |
|
138 |
! |
|
139 |
||
140 |
postDeepCopy |
|
3714 | 141 |
"when copied, make it me a real (mutable) String" |
142 |
||
143 |
self changeClassTo:String. |
|
144 |
||
12457 | 145 |
" |
146 |
'foobar' asImmutableString copy class |
|
147 |
'foobar' asImmutableString shallowCopy class |
|
148 |
'foobar' asImmutableString deepCopy class |
|
149 |
" |
|
3714 | 150 |
! |
151 |
||
152 |
shallowCopy |
|
153 |
"when copying, return a real (mutable) String" |
|
154 |
||
155 |
|sz| |
|
156 |
||
157 |
sz := self size. |
|
158 |
^ (String new:sz) replaceFrom:1 to:sz with:self startingAt:1 |
|
159 |
||
14115
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
160 |
" |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
161 |
'hello world' asImmutableString shallowCopy |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
162 |
" |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
163 |
|
3714 | 164 |
"Created: / 3.8.1998 / 14:47:00 / cg" |
165 |
! ! |
|
166 |
||
167 |
!ImmutableString methodsFor:'private'! |
|
168 |
||
169 |
species |
|
170 |
"Copies should be mutable" |
|
171 |
||
172 |
^ String |
|
173 |
||
174 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
16400 | 175 |
! |
176 |
||
177 |
speciesForCopy |
|
178 |
"Copies should be mutable" |
|
179 |
||
180 |
^ String |
|
181 |
||
182 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
3714 | 183 |
! ! |
184 |
||
14386 | 185 |
!ImmutableString methodsFor:'queries'! |
186 |
||
187 |
isImmutable |
|
188 |
^ true |
|
189 |
! ! |
|
190 |
||
3714 | 191 |
!ImmutableString methodsFor:'specials'! |
192 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
193 |
become:anotherObject |
3714 | 194 |
"trigger an error if I should become something else |
195 |
(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
|
196 |
|
12491 | 197 |
self noModificationError. |
3714 | 198 |
^ super become:anotherObject |
199 |
||
200 |
"Created: / 3.8.1998 / 14:48:29 / cg" |
|
201 |
! |
|
202 |
||
203 |
becomeNil |
|
204 |
"trigger an error if I should become nil |
|
205 |
(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
|
206 |
|
12491 | 207 |
self noModificationError. |
3714 | 208 |
^ super becomeNil |
209 |
||
210 |
"Created: / 3.8.1998 / 14:48:37 / cg" |
|
18704 | 211 |
! |
212 |
||
213 |
becomeSameAs:anotherObject |
|
214 |
"trigger an error if I should become something else |
|
215 |
(this would be an even more tricky manipulation)" |
|
216 |
||
217 |
self noModificationError. |
|
218 |
^ super becomeSameAs:anotherObject |
|
3714 | 219 |
! ! |
220 |
||
221 |
!ImmutableString class methodsFor:'documentation'! |
|
222 |
||
223 |
version |
|
18546 | 224 |
^ '$Header$' |
12457 | 225 |
! |
226 |
||
227 |
version_CVS |
|
18546 | 228 |
^ '$Header$' |
3714 | 229 |
! ! |
16400 | 230 |