author | Claus Gittinger <cg@exept.de> |
Thu, 05 Nov 2009 15:34:58 +0100 | |
changeset 2236 | 7f6acc1b9127 |
parent 1772 | 782a3e74a69f |
child 2238 | 692e8f538efa |
permissions | -rw-r--r-- |
753 | 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 |
" |
|
1574
a34e93fcf9ff
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
753
diff
changeset
|
12 |
"{ Package: 'stx:libcomp' }" |
a34e93fcf9ff
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
753
diff
changeset
|
13 |
|
a34e93fcf9ff
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
753
diff
changeset
|
14 |
String variableByteSubclass:#ImmutableString |
753 | 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 methods 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 string 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 |
[see also:] |
|
60 |
ImmutableArray |
|
61 |
Parser Scanner |
|
62 |
||
63 |
[author:] |
|
64 |
Claus Gittinger |
|
65 |
" |
|
66 |
||
67 |
! ! |
|
68 |
||
69 |
!ImmutableString methodsFor:'accessing'! |
|
70 |
||
71 |
at:index put:value |
|
72 |
"Trigger an error if an immutable string is stored into. |
|
73 |
The store will be performed (for compatibility reasons) if you continue |
|
74 |
in the debugger." |
|
75 |
||
76 |
self notifyStoreError. |
|
77 |
^ super at:index put:value |
|
78 |
||
79 |
"Created: / 3.8.1998 / 14:45:14 / cg" |
|
2236 | 80 |
! |
81 |
||
82 |
basicAt:index put:value |
|
83 |
"Trigger an error if an immutable string is stored into. |
|
84 |
The store will be performed (for compatibility reasons) if you continue |
|
85 |
in the debugger." |
|
86 |
||
87 |
self notifyStoreError. |
|
88 |
^ super basicAt:index put:value |
|
89 |
! ! |
|
90 |
||
91 |
!ImmutableString methodsFor:'converting'! |
|
92 |
||
93 |
asImmutableString |
|
94 |
^ self |
|
753 | 95 |
! ! |
96 |
||
97 |
!ImmutableString methodsFor:'copying'! |
|
98 |
||
99 |
copyEmpty |
|
100 |
"when copying, return a real (mutable) String" |
|
101 |
||
102 |
^ String new:self size |
|
103 |
||
104 |
"Created: / 3.8.1998 / 14:46:22 / cg" |
|
105 |
! |
|
106 |
||
107 |
copyEmptyAndGrow:size |
|
108 |
"when copying, return a real (mutable) String" |
|
109 |
||
110 |
^ String new:size |
|
111 |
||
112 |
"Created: / 3.8.1998 / 14:46:34 / cg" |
|
113 |
! |
|
114 |
||
115 |
postCopy |
|
2236 | 116 |
"when copied, make me a real (mutable) String" |
117 |
||
118 |
self changeClassTo:String. |
|
119 |
||
120 |
"Created: / 3.8.1998 / 14:46:45 / cg" |
|
121 |
! |
|
122 |
||
123 |
postDeepCopy |
|
753 | 124 |
"when copied, make it me a real (mutable) String" |
125 |
||
126 |
self changeClassTo:String. |
|
127 |
||
2236 | 128 |
" |
129 |
'foobar' asImmutableString copy class |
|
130 |
'foobar' asImmutableString shallowCopy class |
|
131 |
'foobar' asImmutableString deepCopy class |
|
132 |
" |
|
753 | 133 |
! |
134 |
||
135 |
shallowCopy |
|
136 |
"when copying, return a real (mutable) String" |
|
137 |
||
138 |
|sz| |
|
139 |
||
140 |
sz := self size. |
|
141 |
^ (String new:sz) replaceFrom:1 to:sz with:self startingAt:1 |
|
142 |
||
143 |
"Created: / 3.8.1998 / 14:47:00 / cg" |
|
144 |
! ! |
|
145 |
||
146 |
!ImmutableString methodsFor:'error handling'! |
|
147 |
||
148 |
creator |
|
149 |
"find the method that contains me" |
|
150 |
||
1698 | 151 |
^ Method allSubInstances detect:[:aMethod | (aMethod referencesGlobal:self)] ifNone:nil. |
753 | 152 |
|
153 |
" |
|
154 |
'hello' creator |
|
155 |
" |
|
156 |
||
157 |
"Created: / 3.8.1998 / 14:47:19 / cg" |
|
158 |
! |
|
159 |
||
160 |
notifyStoreError |
|
161 |
"a store is attempted - for our convenience, find the method that |
|
162 |
contains me, for a nicer error message" |
|
163 |
||
164 |
|creator msg| |
|
165 |
||
166 |
creator := self creator. |
|
167 |
msg := 'store into/change of literal'. |
|
168 |
creator notNil ifTrue:[ |
|
169 |
msg := msg , ' (' , creator whoString , ')' |
|
170 |
]. |
|
171 |
" |
|
172 |
this error is reported on an attempt to store into a literal |
|
173 |
string. The literal was created in creator. |
|
174 |
If you press continue in the debugger, the store will be performed. |
|
1574
a34e93fcf9ff
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
753
diff
changeset
|
175 |
If you don't want this, press abort and check your code. |
753 | 176 |
Storing into literals is VERY VERY bad coding style. |
177 |
" |
|
1574
a34e93fcf9ff
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
753
diff
changeset
|
178 |
self error:msg mayProceed:true |
753 | 179 |
|
180 |
"Created: / 3.8.1998 / 14:47:45 / cg" |
|
181 |
! ! |
|
182 |
||
183 |
!ImmutableString methodsFor:'private'! |
|
184 |
||
185 |
species |
|
186 |
"Copies should be mutable" |
|
187 |
||
188 |
^ String |
|
189 |
||
190 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
191 |
! ! |
|
192 |
||
193 |
!ImmutableString methodsFor:'specials'! |
|
194 |
||
195 |
become:anotherObject |
|
196 |
"trigger an error if I should become something else |
|
197 |
(this would be an even more tricky manipulation)" |
|
198 |
||
199 |
self notifyStoreError. |
|
200 |
^ super become:anotherObject |
|
201 |
||
202 |
"Created: / 3.8.1998 / 14:48:29 / cg" |
|
203 |
! |
|
204 |
||
205 |
becomeNil |
|
206 |
"trigger an error if I should become nil |
|
207 |
(this would be an even more tricky manipulation)" |
|
208 |
||
209 |
self notifyStoreError. |
|
210 |
^ super becomeNil |
|
211 |
||
212 |
"Created: / 3.8.1998 / 14:48:37 / cg" |
|
213 |
! ! |
|
214 |
||
215 |
!ImmutableString class methodsFor:'documentation'! |
|
216 |
||
217 |
version |
|
2236 | 218 |
^ '$Header: /cvs/stx/stx/libcomp/Attic/ImmutableString.st,v 1.5 2009-11-05 14:34:58 cg Exp $' |
219 |
! |
|
220 |
||
221 |
version_CVS |
|
222 |
^ '$Header: /cvs/stx/stx/libcomp/Attic/ImmutableString.st,v 1.5 2009-11-05 14:34:58 cg Exp $' |
|
753 | 223 |
! ! |