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 |
"
|
|
12 |
|
|
13 |
|
|
14 |
String subclass:#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 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"
|
|
80 |
! !
|
|
81 |
|
|
82 |
!ImmutableString methodsFor:'copying'!
|
|
83 |
|
|
84 |
copyEmpty
|
|
85 |
"when copying, return a real (mutable) String"
|
|
86 |
|
|
87 |
^ String new:self size
|
|
88 |
|
|
89 |
"Created: / 3.8.1998 / 14:46:22 / cg"
|
|
90 |
!
|
|
91 |
|
|
92 |
copyEmptyAndGrow:size
|
|
93 |
"when copying, return a real (mutable) String"
|
|
94 |
|
|
95 |
^ String new:size
|
|
96 |
|
|
97 |
"Created: / 3.8.1998 / 14:46:34 / cg"
|
|
98 |
!
|
|
99 |
|
|
100 |
postCopy
|
|
101 |
"when copied, make it me a real (mutable) String"
|
|
102 |
|
|
103 |
self changeClassTo:String.
|
|
104 |
|
|
105 |
"Created: / 3.8.1998 / 14:46:45 / cg"
|
|
106 |
!
|
|
107 |
|
|
108 |
shallowCopy
|
|
109 |
"when copying, return a real (mutable) String"
|
|
110 |
|
|
111 |
|sz|
|
|
112 |
|
|
113 |
sz := self size.
|
|
114 |
^ (String new:sz) replaceFrom:1 to:sz with:self startingAt:1
|
|
115 |
|
|
116 |
"Created: / 3.8.1998 / 14:47:00 / cg"
|
|
117 |
! !
|
|
118 |
|
|
119 |
!ImmutableString methodsFor:'error handling'!
|
|
120 |
|
|
121 |
creator
|
|
122 |
"find the method that contains me"
|
|
123 |
|
|
124 |
Method allSubInstances do:[:aMethod |
|
|
125 |
(aMethod referencesGlobal:self) ifTrue:[
|
|
126 |
^ aMethod.
|
|
127 |
].
|
|
128 |
].
|
|
129 |
^ nil
|
|
130 |
|
|
131 |
"
|
|
132 |
'hello' creator
|
|
133 |
"
|
|
134 |
|
|
135 |
"Created: / 3.8.1998 / 14:47:19 / cg"
|
|
136 |
!
|
|
137 |
|
|
138 |
notifyStoreError
|
|
139 |
"a store is attempted - for our convenience, find the method that
|
|
140 |
contains me, for a nicer error message"
|
|
141 |
|
|
142 |
|creator msg|
|
|
143 |
|
|
144 |
creator := self creator.
|
|
145 |
msg := 'store into/change of literal'.
|
|
146 |
creator notNil ifTrue:[
|
|
147 |
msg := msg , ' (' , creator whoString , ')'
|
|
148 |
].
|
|
149 |
"
|
|
150 |
this error is reported on an attempt to store into a literal
|
|
151 |
string. The literal was created in creator.
|
|
152 |
If you press continue in the debugger, the store will be performed.
|
|
153 |
If you dont want this, press abort and check your code.
|
|
154 |
Storing into literals is VERY VERY bad coding style.
|
|
155 |
"
|
|
156 |
self error:msg
|
|
157 |
|
|
158 |
"Created: / 3.8.1998 / 14:47:45 / cg"
|
|
159 |
! !
|
|
160 |
|
|
161 |
!ImmutableString methodsFor:'private'!
|
|
162 |
|
|
163 |
species
|
|
164 |
"Copies should be mutable"
|
|
165 |
|
|
166 |
^ String
|
|
167 |
|
|
168 |
"Created: / 3.8.1998 / 14:47:58 / cg"
|
|
169 |
! !
|
|
170 |
|
|
171 |
!ImmutableString methodsFor:'queries'!
|
|
172 |
|
|
173 |
isLiteral
|
|
174 |
"return true, if the receiver can be used as a literal
|
|
175 |
(i.e. can be used in constant strings)"
|
|
176 |
|
|
177 |
"yes, I must be"
|
|
178 |
^ true
|
|
179 |
|
|
180 |
"Created: / 3.8.1998 / 14:48:13 / cg"
|
|
181 |
! !
|
|
182 |
|
|
183 |
!ImmutableString methodsFor:'specials'!
|
|
184 |
|
|
185 |
become:anotherObject
|
|
186 |
"trigger an error if I should become something else
|
|
187 |
(this would be an even more tricky manipulation)"
|
|
188 |
|
|
189 |
self notifyStoreError.
|
|
190 |
^ super become:anotherObject
|
|
191 |
|
|
192 |
"Created: / 3.8.1998 / 14:48:29 / cg"
|
|
193 |
!
|
|
194 |
|
|
195 |
becomeNil
|
|
196 |
"trigger an error if I should become nil
|
|
197 |
(this would be an even more tricky manipulation)"
|
|
198 |
|
|
199 |
self notifyStoreError.
|
|
200 |
^ super becomeNil
|
|
201 |
|
|
202 |
"Created: / 3.8.1998 / 14:48:37 / cg"
|
|
203 |
! !
|
|
204 |
|
|
205 |
!ImmutableString class methodsFor:'documentation'!
|
|
206 |
|
|
207 |
version
|
|
208 |
^ '$Header: /cvs/stx/stx/libcomp/Attic/ImmutableString.st,v 1.1 1998-08-03 17:08:28 cg Exp $'
|
|
209 |
! !
|