author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 17 Jun 2015 06:22:00 +0100 | |
branch | jv |
changeset 18487 | 8735bd9eee2f |
parent 18120 | e3a375d5f6a8 |
child 18555 | 7cc0ec139c8d |
permissions | -rw-r--r-- |
3714 | 1 |
" |
2 |
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
|
3 |
All Rights Reserved |
3714 | 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 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
8807
606a4ec5b693
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
3714
diff
changeset
|
13 |
|
17373 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
8807
606a4ec5b693
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
3714
diff
changeset
|
16 |
String variableByteSubclass:#ImmutableString |
3714 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'System-Compiler-Support' |
|
21 |
! |
|
22 |
||
23 |
!ImmutableString class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
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
|
28 |
All Rights Reserved |
3714 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
||
38 |
! |
|
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
By default, string literals in smalltalk are mutable objects. That |
|
43 |
may lead to some subtle (and hard to find errors), if some method passes |
|
44 |
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
|
45 |
string using at:put: like messages. Since the string object is kept in |
3714 | 46 |
the first methods literals, the string constant has now been changed without |
12464 | 47 |
having the method's sourcecode reflect this. Thus, the method will |
3714 | 48 |
behave differently from what its source may make you think. |
49 |
||
50 |
To help finding this kind of 'feature/bug', the compiler can be |
|
51 |
configured to create instances of this ImmutableString instead of Strings |
|
12464 | 52 |
for literals. Instances of ImmutableString catch storing accesses and |
3714 | 53 |
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
|
54 |
for compatibility to other smalltalk implementations. |
3714 | 55 |
(Also, if turned on, this makes inspecting string literals entered in |
56 |
a workspace somewhat strange: you cannot modify it any longer). |
|
57 |
||
58 |
Turn the ImmutableString feature on by setting the Parsers class variable |
|
59 |
'StringsAreImmutable' to true or use the new launchers settings menu. |
|
60 |
||
12471 | 61 |
|
62 |
ATTENTION: |
|
14222 | 63 |
there may be still code around which checks for explicit class being String |
64 |
(both in Smalltalk and in primitive code). All code like foo 'class == String' |
|
65 |
or '__isString()' will not work with ImmutableStrings. Use '__isStringLike()' instead. |
|
66 |
A somewhat better approach would be to either add a flag to the object (mutability) |
|
67 |
and check this dynamically (expensive) or to place immutable objects into a readonly |
|
68 |
memory segment (the good solution). We will eventually implement the second in the future... |
|
12471 | 69 |
|
3714 | 70 |
[see also:] |
14222 | 71 |
ImmutableArray |
72 |
Parser Scanner |
|
3714 | 73 |
|
74 |
[author:] |
|
14222 | 75 |
Claus Gittinger |
3714 | 76 |
" |
77 |
! ! |
|
78 |
||
17373 | 79 |
!ImmutableString class methodsFor:'queries'! |
80 |
||
81 |
mutableClass |
|
82 |
"answer an equivalent mustable class" |
|
83 |
||
84 |
^ String |
|
85 |
! ! |
|
86 |
||
87 |
!ImmutableString class methodsFor:'testing'! |
|
88 |
||
89 |
hasImmutableInstances |
|
90 |
^ true |
|
91 |
! ! |
|
92 |
||
3714 | 93 |
!ImmutableString methodsFor:'accessing'! |
94 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
95 |
at:index put:value |
3714 | 96 |
"Trigger an error if an immutable string is stored into. |
97 |
The store will be performed (for compatibility reasons) if you continue |
|
98 |
in the debugger." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
99 |
|
12491 | 100 |
self noModificationError. |
3714 | 101 |
^ super at:index put:value |
102 |
||
103 |
"Created: / 3.8.1998 / 14:45:14 / cg" |
|
12457 | 104 |
! |
105 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
106 |
basicAt:index put:value |
12457 | 107 |
"Trigger an error if an immutable string is stored into. |
108 |
The store will be performed (for compatibility reasons) if you continue |
|
109 |
in the debugger." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
110 |
|
12491 | 111 |
self noModificationError. |
12457 | 112 |
^ super basicAt:index put:value |
113 |
! ! |
|
114 |
||
115 |
!ImmutableString methodsFor:'converting'! |
|
116 |
||
117 |
asImmutableString |
|
118 |
^ self |
|
16824 | 119 |
! |
120 |
||
121 |
asMutableCollection |
|
122 |
"return a writable copy of myself" |
|
123 |
||
124 |
^ self copy changeClassTo:String |
|
3714 | 125 |
! ! |
126 |
||
127 |
!ImmutableString methodsFor:'copying'! |
|
128 |
||
129 |
postCopy |
|
12457 | 130 |
"when copied, make me a real (mutable) String" |
131 |
||
132 |
self changeClassTo:String. |
|
133 |
||
134 |
"Created: / 3.8.1998 / 14:46:45 / cg" |
|
135 |
! |
|
136 |
||
137 |
postDeepCopy |
|
3714 | 138 |
"when copied, make it me a real (mutable) String" |
139 |
||
140 |
self changeClassTo:String. |
|
141 |
||
12457 | 142 |
" |
143 |
'foobar' asImmutableString copy class |
|
144 |
'foobar' asImmutableString shallowCopy class |
|
145 |
'foobar' asImmutableString deepCopy class |
|
146 |
" |
|
3714 | 147 |
! |
148 |
||
149 |
shallowCopy |
|
150 |
"when copying, return a real (mutable) String" |
|
151 |
||
152 |
|sz| |
|
153 |
||
154 |
sz := self size. |
|
155 |
^ (String new:sz) replaceFrom:1 to:sz with:self startingAt:1 |
|
156 |
||
14115
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
157 |
" |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
158 |
'hello world' asImmutableString shallowCopy |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
159 |
" |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
160 |
|
3714 | 161 |
"Created: / 3.8.1998 / 14:47:00 / cg" |
162 |
! ! |
|
163 |
||
164 |
!ImmutableString methodsFor:'private'! |
|
165 |
||
166 |
species |
|
167 |
"Copies should be mutable" |
|
168 |
||
169 |
^ String |
|
170 |
||
171 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
16400 | 172 |
! |
173 |
||
174 |
speciesForCopy |
|
175 |
"Copies should be mutable" |
|
176 |
||
177 |
^ String |
|
178 |
||
179 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
3714 | 180 |
! ! |
181 |
||
14386 | 182 |
!ImmutableString methodsFor:'queries'! |
183 |
||
184 |
isImmutable |
|
185 |
^ true |
|
186 |
! ! |
|
187 |
||
3714 | 188 |
!ImmutableString methodsFor:'specials'! |
189 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
190 |
become:anotherObject |
3714 | 191 |
"trigger an error if I should become something else |
192 |
(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
|
193 |
|
12491 | 194 |
self noModificationError. |
3714 | 195 |
^ super become:anotherObject |
196 |
||
197 |
"Created: / 3.8.1998 / 14:48:29 / cg" |
|
198 |
! |
|
199 |
||
200 |
becomeNil |
|
201 |
"trigger an error if I should become nil |
|
202 |
(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
|
203 |
|
12491 | 204 |
self noModificationError. |
3714 | 205 |
^ super becomeNil |
206 |
||
207 |
"Created: / 3.8.1998 / 14:48:37 / cg" |
|
208 |
! ! |
|
209 |
||
210 |
!ImmutableString class methodsFor:'documentation'! |
|
211 |
||
212 |
version |
|
17373 | 213 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableString.st,v 1.16 2015-02-03 13:53:40 stefan Exp $' |
12457 | 214 |
! |
215 |
||
216 |
version_CVS |
|
17373 | 217 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableString.st,v 1.16 2015-02-03 13:53:40 stefan Exp $' |
3714 | 218 |
! ! |
16400 | 219 |