0
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1990-93 by Claus Gittinger
|
|
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 |
ArrayedCollection subclass:#VariableString
|
|
14 |
instanceVariableNames:'contents'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Indexed'
|
|
18 |
!
|
|
19 |
|
|
20 |
VariableString comment:'
|
|
21 |
|
|
22 |
COPYRIGHT (c) 1990-93 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
VariableStrings can grow and shrink - in contrast to Strings which are
|
|
26 |
fixed in size - this may change in the future.
|
|
27 |
|
|
28 |
%W% %E%
|
|
29 |
'!
|
|
30 |
|
|
31 |
!VariableString class methodsFor:'initialization'!
|
|
32 |
|
|
33 |
initialize
|
1
|
34 |
"clear my superclass - this makes all messages be misunderstood"
|
0
|
35 |
|
1
|
36 |
self setSuperclass: nil
|
0
|
37 |
! !
|
|
38 |
|
|
39 |
!VariableString class methodsFor:'instance creation'!
|
|
40 |
|
|
41 |
new
|
|
42 |
"return a new VariableString - with size 0"
|
|
43 |
|
|
44 |
^ self new:0
|
|
45 |
!
|
|
46 |
|
|
47 |
new:size
|
|
48 |
"return a new VariableString"
|
|
49 |
|
|
50 |
^ (self basicNew) setContents:(String new:size)
|
|
51 |
!
|
|
52 |
|
|
53 |
copyFrom:aString
|
|
54 |
"return a new VariableString with contents copied from the argument"
|
|
55 |
|
|
56 |
^ self basicNew setContents:(String copyFrom:aString)
|
|
57 |
! !
|
|
58 |
|
|
59 |
!VariableString methodsFor:'accessing'!
|
|
60 |
|
|
61 |
at:index
|
|
62 |
"return the element at index"
|
|
63 |
|
|
64 |
^ contents at:index
|
|
65 |
!
|
|
66 |
|
|
67 |
at:index put:anObject
|
|
68 |
"set the element at index"
|
|
69 |
|
|
70 |
^ contents at:index put:anObject
|
|
71 |
! !
|
|
72 |
|
|
73 |
!VariableString methodsFor:'queries'!
|
|
74 |
|
|
75 |
size
|
|
76 |
^ contents size
|
|
77 |
!
|
|
78 |
|
|
79 |
isFixedSize
|
|
80 |
"return true if the receiver cannot grow - this will vanish once
|
|
81 |
Arrays and Strings learn how to grow ..."
|
|
82 |
|
|
83 |
^ false
|
|
84 |
! !
|
|
85 |
|
|
86 |
!VariableString methodsFor:'filling & replacing'!
|
|
87 |
|
|
88 |
replaceFrom:start to:stop with:aCollection startingAt:repStart
|
|
89 |
"reimplemented for speed
|
|
90 |
- can use Strings fast replace if aCollection
|
|
91 |
is String or VariableString"
|
|
92 |
|
|
93 |
|col|
|
|
94 |
|
|
95 |
col := aCollection.
|
|
96 |
(aCollection isKindOf:VariableString) ifTrue:[
|
|
97 |
((stop - start + repStart) <= aCollection size) ifTrue:[
|
|
98 |
col := aCollection getContents
|
|
99 |
]
|
|
100 |
].
|
|
101 |
(col isMemberOf:String) ifTrue:[
|
|
102 |
(stop <= (contents size)) ifTrue:[
|
|
103 |
^ contents replaceFrom:start to:stop with:col startingAt:repStart
|
|
104 |
]
|
|
105 |
].
|
|
106 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
|
|
107 |
! !
|
|
108 |
|
|
109 |
!VariableString methodsFor:'converting'!
|
|
110 |
|
|
111 |
asText
|
|
112 |
^ contents asText
|
|
113 |
! !
|
|
114 |
|
|
115 |
!VariableString methodsFor:'kludges'!
|
|
116 |
|
|
117 |
shallowCopy:anArray
|
|
118 |
"return a shallow copy of the receiver
|
|
119 |
have to kludge the kludge ... - shallow copy the contents array"
|
|
120 |
|
|
121 |
|newText|
|
|
122 |
|
|
123 |
newText := self class basicNew.
|
|
124 |
newText setContents:(contents shallowCopy).
|
|
125 |
^ newText
|
|
126 |
! !
|
|
127 |
|
|
128 |
!VariableString methodsFor:'private'!
|
|
129 |
|
|
130 |
getContents
|
|
131 |
"return the contents array"
|
|
132 |
|
|
133 |
^ contents
|
|
134 |
!
|
|
135 |
|
|
136 |
setContents:aString
|
|
137 |
"set the contents"
|
|
138 |
|
|
139 |
contents := aString
|
|
140 |
! !
|
|
141 |
|
|
142 |
!VariableString methodsFor:'error handling'!
|
|
143 |
|
|
144 |
doesNotUnderstand:aMessage
|
|
145 |
"this is funny: all message we do not understand, are passed
|
|
146 |
on to the string - so we do not have to care for all
|
|
147 |
possible messages ...(thanks to the Message class)"
|
|
148 |
|
|
149 |
^ contents perform:(aMessage selector)
|
|
150 |
withArguments:(aMessage arguments)
|
|
151 |
! !
|
|
152 |
|
|
153 |
!VariableString methodsFor:'grow & shrink'!
|
|
154 |
|
|
155 |
grow:newSize
|
|
156 |
|old oldSize|
|
|
157 |
|
|
158 |
oldSize := contents size.
|
|
159 |
(newSize < oldSize) ifTrue:[
|
|
160 |
contents := contents copyFrom:1 to:newSize
|
|
161 |
] ifFalse:[
|
|
162 |
(newSize > oldSize) ifTrue:[
|
|
163 |
old := contents.
|
|
164 |
(newSize > (oldSize * 2)) ifTrue:[
|
|
165 |
contents := String new:newSize
|
|
166 |
] ifFalse:[
|
|
167 |
contents := String new:(oldSize * 2)
|
|
168 |
].
|
|
169 |
contents replaceFrom:1 to:oldSize
|
|
170 |
with:old startingAt:1
|
|
171 |
]
|
|
172 |
]
|
|
173 |
! !
|