author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Sat, 09 Apr 2011 18:19:04 +0100 | |
branch | jv |
changeset 17834 | 04ff72c5039a |
parent 17814 | b75a7f0c346b |
child 17841 | 7abcc4aef871 |
permissions | -rw-r--r-- |
17735 | 1 |
" |
2 |
COPYRIGHT (c) 1998 by eXept Software AG |
|
3 |
All Rights Reserved |
|
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 |
" |
|
12 |
"{ Package: 'stx:libbasic' }" |
|
13 |
||
14 |
String variableByteSubclass:#ImmutableString |
|
15 |
instanceVariableNames:'' |
|
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'System-Compiler-Support' |
|
19 |
! |
|
20 |
||
21 |
!ImmutableString class methodsFor:'documentation'! |
|
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1998 by eXept Software AG |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
||
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
40 |
By default, string literals in smalltalk are mutable objects. That |
|
41 |
may lead to some subtle (and hard to find errors), if some method passes |
|
42 |
a string constant as argument to someone else, who changes the |
|
43 |
string using at:put: like messages. Since the string object is kept in |
|
44 |
the first methods literals, the string constant has now been changed without |
|
45 |
having the method's sourcecode reflect this. Thus, the method will |
|
46 |
behave differently from what its source may make you think. |
|
47 |
||
48 |
To help finding this kind of 'feature/bug', the compiler can be |
|
49 |
configured to create instances of this ImmutableString instead of Strings |
|
50 |
for literals. Instances of ImmutableString catch storing accesses and |
|
51 |
enter the debugger. Although useful, this feature is disabled by default |
|
52 |
for compatibility to other smalltalk implementations. |
|
53 |
(Also, if turned on, this makes inspecting string literals entered in |
|
54 |
a workspace somewhat strange: you cannot modify it any longer). |
|
55 |
||
56 |
Turn the ImmutableString feature on by setting the Parsers class variable |
|
57 |
'StringsAreImmutable' to true or use the new launchers settings menu. |
|
58 |
||
59 |
||
60 |
ATTENTION: |
|
61 |
there may be still code around which checks for explicit class being String |
|
62 |
(both in Smalltalk and in primitive code). All code like foo 'class == String' |
|
63 |
or '__isString' will not work with ImmutableStrings. |
|
64 |
A somewhat better approach would be to either add a flag to the object (mutability) |
|
65 |
and check this dynamically (expensive) or to place immutable objects into a readonly |
|
66 |
memory segment (the good solution). We will eventually implement the second in the future... |
|
67 |
||
68 |
[see also:] |
|
69 |
ImmutableArray |
|
70 |
Parser Scanner |
|
71 |
||
72 |
[author:] |
|
73 |
Claus Gittinger |
|
74 |
" |
|
75 |
! ! |
|
76 |
||
77 |
!ImmutableString methodsFor:'accessing'! |
|
78 |
||
79 |
at:index put:value |
|
80 |
"Trigger an error if an immutable string is stored into. |
|
81 |
The store will be performed (for compatibility reasons) if you continue |
|
82 |
in the debugger." |
|
83 |
||
84 |
self noModificationError. |
|
85 |
^ super at:index put:value |
|
86 |
||
87 |
"Created: / 3.8.1998 / 14:45:14 / cg" |
|
88 |
! |
|
89 |
||
90 |
basicAt:index put:value |
|
91 |
"Trigger an error if an immutable string is stored into. |
|
92 |
The store will be performed (for compatibility reasons) if you continue |
|
93 |
in the debugger." |
|
94 |
||
95 |
self noModificationError. |
|
96 |
^ super basicAt:index put:value |
|
97 |
! ! |
|
98 |
||
99 |
!ImmutableString methodsFor:'converting'! |
|
100 |
||
101 |
asImmutableString |
|
102 |
^ self |
|
103 |
! ! |
|
104 |
||
105 |
!ImmutableString methodsFor:'copying'! |
|
106 |
||
107 |
copyEmpty |
|
108 |
"when copying, return a real (mutable) String" |
|
109 |
||
110 |
^ String new:self size |
|
111 |
||
112 |
"Created: / 3.8.1998 / 14:46:22 / cg" |
|
113 |
! |
|
114 |
||
115 |
copyEmptyAndGrow:size |
|
116 |
"when copying, return a real (mutable) String" |
|
117 |
||
118 |
^ String new:size |
|
119 |
||
120 |
"Created: / 3.8.1998 / 14:46:34 / cg" |
|
121 |
! |
|
122 |
||
123 |
postCopy |
|
124 |
"when copied, make me a real (mutable) String" |
|
125 |
||
126 |
self changeClassTo:String. |
|
127 |
||
128 |
"Created: / 3.8.1998 / 14:46:45 / cg" |
|
129 |
! |
|
130 |
||
131 |
postDeepCopy |
|
132 |
"when copied, make it me a real (mutable) String" |
|
133 |
||
134 |
self changeClassTo:String. |
|
135 |
||
136 |
" |
|
137 |
'foobar' asImmutableString copy class |
|
138 |
'foobar' asImmutableString shallowCopy class |
|
139 |
'foobar' asImmutableString deepCopy class |
|
140 |
" |
|
141 |
! |
|
142 |
||
143 |
shallowCopy |
|
144 |
"when copying, return a real (mutable) String" |
|
145 |
||
146 |
|sz| |
|
147 |
||
148 |
sz := self size. |
|
149 |
^ (String new:sz) replaceFrom:1 to:sz with:self startingAt:1 |
|
150 |
||
151 |
"Created: / 3.8.1998 / 14:47:00 / cg" |
|
152 |
! ! |
|
153 |
||
154 |
!ImmutableString methodsFor:'error handling'! |
|
155 |
||
156 |
creator |
|
157 |
"find the method that contains me. |
|
158 |
This works only, if the string is included in a method's literal array. |
|
159 |
Stc compiled code does not put strings into the literal array - Bytecode compiled code does." |
|
160 |
||
161 |
^ Method allSubInstances detect:[:aMethod | (aMethod referencesGlobal:self)] ifNone:nil. |
|
162 |
||
163 |
" |
|
164 |
'hello' creator |
|
165 |
" |
|
166 |
||
167 |
"Created: / 3.8.1998 / 14:47:19 / cg" |
|
168 |
! |
|
169 |
||
170 |
noModificationError |
|
171 |
"a store is attempted - for our convenience, find the method that |
|
172 |
contains me, for a nicer error message" |
|
173 |
||
174 |
|creator msg context| |
|
175 |
||
176 |
creator := self creator. |
|
177 |
creator notNil ifTrue:[ |
|
178 |
msg := ' (' , creator whoString , ')' |
|
179 |
]. |
|
180 |
context := thisContext sender. |
|
181 |
" |
|
182 |
this error is reported on an attempt to store into a literal |
|
183 |
string. The literal was created in creator. |
|
184 |
If you press continue in the debugger, the store will be performed. |
|
185 |
If you don't want this, press abort and check your code. |
|
186 |
Storing into literals is VERY VERY bad coding style. |
|
187 |
" |
|
188 |
NoModificationError |
|
189 |
raiseRequestWith:self |
|
190 |
errorString:msg |
|
191 |
in:context. |
|
192 |
||
193 |
"Created: / 3.8.1998 / 14:47:45 / cg" |
|
194 |
! ! |
|
195 |
||
196 |
!ImmutableString methodsFor:'private'! |
|
197 |
||
198 |
species |
|
199 |
"Copies should be mutable" |
|
200 |
||
201 |
^ String |
|
202 |
||
203 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
204 |
! ! |
|
205 |
||
206 |
!ImmutableString methodsFor:'specials'! |
|
207 |
||
208 |
become:anotherObject |
|
209 |
"trigger an error if I should become something else |
|
210 |
(this would be an even more tricky manipulation)" |
|
211 |
||
212 |
self noModificationError. |
|
213 |
^ super become:anotherObject |
|
214 |
||
215 |
"Created: / 3.8.1998 / 14:48:29 / cg" |
|
216 |
! |
|
217 |
||
218 |
becomeNil |
|
219 |
"trigger an error if I should become nil |
|
220 |
(this would be an even more tricky manipulation)" |
|
221 |
||
222 |
self noModificationError. |
|
223 |
^ super becomeNil |
|
224 |
||
225 |
"Created: / 3.8.1998 / 14:48:37 / cg" |
|
226 |
! ! |
|
227 |
||
228 |
!ImmutableString class methodsFor:'documentation'! |
|
229 |
||
230 |
version |
|
231 |
^ '$Id$' |
|
232 |
! |
|
233 |
||
234 |
version_CVS |
|
17834 | 235 |
^ 'Header: /var/local/cvs/stx/libbasic/ImmutableString.st,v 1.9 2009-11-16 08:59:40 stefan Exp ' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17735
diff
changeset
|
236 |
! |
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17735
diff
changeset
|
237 |
|
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17735
diff
changeset
|
238 |
version_SVN |
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17735
diff
changeset
|
239 |
^ '$Id$' |
17735 | 240 |
! ! |
17767 | 241 |
|
17772
6e0b3c06c364
Method reverted to r10532
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17767
diff
changeset
|
242 |
|
17780 | 243 |
|
17807 | 244 |
|
17814 | 245 |
|
17834 | 246 |