author | Stefan Vogel <sv@exept.de> |
Wed, 29 Jan 2014 17:08:17 +0100 | |
changeset 15923 | 6bc82606b914 |
parent 14386 | 27b494f48340 |
child 16400 | 6c38fd2957bc |
child 18011 | deb0c3355881 |
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 |
|
606a4ec5b693
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
3714
diff
changeset
|
14 |
String variableByteSubclass:#ImmutableString |
3714 | 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 |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
26 |
All Rights Reserved |
3714 | 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 |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
43 |
string using at:put: like messages. Since the string object is kept in |
3714 | 44 |
the first methods literals, the string constant has now been changed without |
12464 | 45 |
having the method's sourcecode reflect this. Thus, the method will |
3714 | 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 |
|
12464 | 50 |
for literals. Instances of ImmutableString catch storing accesses and |
3714 | 51 |
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
|
52 |
for compatibility to other smalltalk implementations. |
3714 | 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 |
||
12471 | 59 |
|
60 |
ATTENTION: |
|
14222 | 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. Use '__isStringLike()' instead. |
|
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... |
|
12471 | 67 |
|
3714 | 68 |
[see also:] |
14222 | 69 |
ImmutableArray |
70 |
Parser Scanner |
|
3714 | 71 |
|
72 |
[author:] |
|
14222 | 73 |
Claus Gittinger |
3714 | 74 |
" |
75 |
! ! |
|
76 |
||
77 |
!ImmutableString methodsFor:'accessing'! |
|
78 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
79 |
at:index put:value |
3714 | 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." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
83 |
|
12491 | 84 |
self noModificationError. |
3714 | 85 |
^ super at:index put:value |
86 |
||
87 |
"Created: / 3.8.1998 / 14:45:14 / cg" |
|
12457 | 88 |
! |
89 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
90 |
basicAt:index put:value |
12457 | 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." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
94 |
|
12491 | 95 |
self noModificationError. |
12457 | 96 |
^ super basicAt:index put:value |
97 |
! ! |
|
98 |
||
99 |
!ImmutableString methodsFor:'converting'! |
|
100 |
||
101 |
asImmutableString |
|
102 |
^ self |
|
3714 | 103 |
! ! |
104 |
||
105 |
!ImmutableString methodsFor:'copying'! |
|
106 |
||
107 |
postCopy |
|
12457 | 108 |
"when copied, make me a real (mutable) String" |
109 |
||
110 |
self changeClassTo:String. |
|
111 |
||
112 |
"Created: / 3.8.1998 / 14:46:45 / cg" |
|
113 |
! |
|
114 |
||
115 |
postDeepCopy |
|
3714 | 116 |
"when copied, make it me a real (mutable) String" |
117 |
||
118 |
self changeClassTo:String. |
|
119 |
||
12457 | 120 |
" |
121 |
'foobar' asImmutableString copy class |
|
122 |
'foobar' asImmutableString shallowCopy class |
|
123 |
'foobar' asImmutableString deepCopy class |
|
124 |
" |
|
3714 | 125 |
! |
126 |
||
127 |
shallowCopy |
|
128 |
"when copying, return a real (mutable) String" |
|
129 |
||
130 |
|sz| |
|
131 |
||
132 |
sz := self size. |
|
133 |
^ (String new:sz) replaceFrom:1 to:sz with:self startingAt:1 |
|
134 |
||
14115
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
135 |
" |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
136 |
'hello world' asImmutableString shallowCopy |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
137 |
" |
8599762e8a45
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14025
diff
changeset
|
138 |
|
3714 | 139 |
"Created: / 3.8.1998 / 14:47:00 / cg" |
140 |
! ! |
|
141 |
||
142 |
!ImmutableString methodsFor:'private'! |
|
143 |
||
144 |
species |
|
145 |
"Copies should be mutable" |
|
146 |
||
147 |
^ String |
|
148 |
||
149 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
150 |
! ! |
|
151 |
||
14386 | 152 |
!ImmutableString methodsFor:'queries'! |
153 |
||
154 |
isImmutable |
|
155 |
^ true |
|
156 |
! ! |
|
157 |
||
3714 | 158 |
!ImmutableString methodsFor:'specials'! |
159 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12491
diff
changeset
|
160 |
become:anotherObject |
3714 | 161 |
"trigger an error if I should become something else |
162 |
(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
|
163 |
|
12491 | 164 |
self noModificationError. |
3714 | 165 |
^ super become:anotherObject |
166 |
||
167 |
"Created: / 3.8.1998 / 14:48:29 / cg" |
|
168 |
! |
|
169 |
||
170 |
becomeNil |
|
171 |
"trigger an error if I should become nil |
|
172 |
(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
|
173 |
|
12491 | 174 |
self noModificationError. |
3714 | 175 |
^ super becomeNil |
176 |
||
177 |
"Created: / 3.8.1998 / 14:48:37 / cg" |
|
178 |
! ! |
|
179 |
||
180 |
!ImmutableString class methodsFor:'documentation'! |
|
181 |
||
182 |
version |
|
14386 | 183 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableString.st,v 1.13 2012-10-16 15:11:35 cg Exp $' |
12457 | 184 |
! |
185 |
||
186 |
version_CVS |
|
14386 | 187 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableString.st,v 1.13 2012-10-16 15:11:35 cg Exp $' |
3714 | 188 |
! ! |