author | Claus Gittinger <cg@exept.de> |
Mon, 20 Feb 2006 22:29:35 +0100 | |
changeset 9161 | 1ad4d98f8f62 |
parent 8375 | b43b5d1c494d |
child 10452 | 2f486960b688 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
160 | 3 |
All Rights Reserved |
1 | 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 |
||
6561 | 13 |
"{ Package: 'stx:libbasic' }" |
6176 | 14 |
|
647 | 15 |
OrderedCollection subclass:#StringCollection |
968 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Text' |
|
1 | 20 |
! |
21 |
||
246 | 22 |
!StringCollection class methodsFor:'documentation'! |
89 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
160 | 27 |
All Rights Reserved |
1 | 28 |
|
89 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
89 | 38 |
documentation |
39 |
" |
|
262 | 40 |
StringCollection is an variable sized array of lines which are strings. |
160 | 41 |
WARNING: |
1290 | 42 |
This class is temporary (a historic leftover) - it may change or |
43 |
even vanish in the future. Use OrderedCollections or other standard |
|
44 |
classes to represent collections of strings. |
|
89 | 45 |
|
262 | 46 |
StringCollection used to be called Text, but this is a very bad name |
47 |
- there is something totally different also named Text in ST-80 ... |
|
1290 | 48 |
|
49 |
[author:] |
|
50 |
Claus Gittinger |
|
89 | 51 |
" |
52 |
! ! |
|
1 | 53 |
|
246 | 54 |
!StringCollection class methodsFor:'instance creation'! |
1 | 55 |
|
56 |
from:aString |
|
57 |
"return a new text object with lines taken from the argument, aString" |
|
58 |
||
8375
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
59 |
self obsoleteMethodWarning:'use #fromString:'. |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
60 |
^ self fromString:aString. |
1 | 61 |
! |
62 |
||
63 |
fromArray:anArray |
|
64 |
"return a new text object with lines taken from the argument, an array |
|
65 |
of strings" |
|
66 |
||
246 | 67 |
|newStringCollection |
1 | 68 |
size "{ Class: SmallInteger }" | |
69 |
||
70 |
size := anArray size. |
|
246 | 71 |
newStringCollection := self new:size. |
1 | 72 |
1 to:size do:[:line | |
246 | 73 |
newStringCollection at:line put:(anArray at:line) |
1 | 74 |
]. |
246 | 75 |
^ newStringCollection |
731 | 76 |
! |
77 |
||
8375
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
78 |
fromString:aString |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
79 |
"return a new text object with lines taken from the argument, aString" |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
80 |
|
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
81 |
^ (self new:1) fromString:aString |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
82 |
! |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
83 |
|
731 | 84 |
new:size |
85 |
"return a new string collection with size empty lines" |
|
86 |
||
87 |
^ (super new:size) grow:size |
|
9161 | 88 |
! |
89 |
||
90 |
withSize:size |
|
91 |
"return a new string collection with size empty lines" |
|
92 |
||
93 |
^ (super new:size) grow:size |
|
1 | 94 |
! ! |
95 |
||
246 | 96 |
!StringCollection methodsFor:'converting'! |
1 | 97 |
|
98 |
asString |
|
99 |
"return myself as a string with embedded cr's" |
|
100 |
||
7829 | 101 |
^ self |
102 |
asStringWith:Character cr |
|
103 |
from:1 to:(self size) |
|
104 |
compressTabs:false |
|
105 |
final:Character cr |
|
87 | 106 |
! |
107 |
||
608 | 108 |
asStringCollection |
109 |
"return the receiver as a stringCollection - thats easy" |
|
110 |
||
111 |
^ self |
|
112 |
! |
|
113 |
||
3601
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
114 |
asStringWithoutEmphasis |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
115 |
"return myself as a string with embedded cr's, but drop any emphasis" |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
116 |
|
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
117 |
^ self |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
118 |
asStringWith:Character cr |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
119 |
from:1 to:(self size) |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
120 |
compressTabs:false |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
121 |
final:Character cr |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
122 |
withEmphasis:false |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
123 |
|
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
124 |
"Created: / 17.6.1998 / 12:32:48 / cg" |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
125 |
! |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
126 |
|
7829 | 127 |
asStringWithoutFinalCR |
128 |
"return myself as a string with embedded cr's |
|
129 |
but do not add a final CR" |
|
130 |
||
131 |
^ self |
|
132 |
asStringWith:Character cr |
|
133 |
from:1 to:(self size) |
|
134 |
compressTabs:false |
|
135 |
final:nil |
|
136 |
! |
|
137 |
||
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
138 |
encodeFrom:oldEncoding into:newEncoding |
7916 | 139 |
|enc| |
140 |
||
141 |
enc := CharacterEncoder encoderToEncodeFrom:oldEncoding into:newEncoding. |
|
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
142 |
^ self collect:[:line | |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
143 |
line isNil |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
144 |
ifTrue:[ nil ] |
7916 | 145 |
ifFalse:[ enc encodeString:line] |
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
146 |
] |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
147 |
! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
148 |
|
1 | 149 |
from:aString |
150 |
"setup my contents from the argument, aString" |
|
151 |
||
8375
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
152 |
self obsoleteMethodWarning:'use #fromString:'. |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
153 |
^ self fromString:aString |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
154 |
! |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
155 |
|
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
156 |
fromString:aString |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
157 |
"setup my contents from the argument, aString" |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
158 |
|
1 | 159 |
|numberOfLines "{ Class:SmallInteger }" |
160 |
start "{ Class:SmallInteger }" |
|
6176 | 161 |
stop "{ Class:SmallInteger }" |
162 |
| |
|
1 | 163 |
|
164 |
numberOfLines := aString occurrencesOf:(Character cr). |
|
203 | 165 |
(aString endsWith:(Character cr)) ifFalse:[ |
6176 | 166 |
numberOfLines := numberOfLines + 1. |
203 | 167 |
]. |
1 | 168 |
self grow:numberOfLines. |
6176 | 169 |
|
1 | 170 |
start := 1. |
171 |
1 to:numberOfLines do:[:lineNr | |
|
6176 | 172 |
stop := aString indexOf:(Character cr) startingAt:start. |
173 |
stop == 0 ifTrue:[ |
|
174 |
self at:lineNr put:(aString copyFrom:start). |
|
175 |
self from:lineNr+1 to:numberOfLines put:''. |
|
176 |
^ self. |
|
177 |
]. |
|
10 | 178 |
|
6176 | 179 |
self at:lineNr put:(aString copyFrom:start to:stop-1). |
180 |
start := stop + 1 |
|
1 | 181 |
] |
6176 | 182 |
|
183 |
"Modified: / 12.11.2001 / 18:18:42 / cg" |
|
968 | 184 |
! ! |
185 |
||
186 |
!StringCollection methodsFor:'copying'! |
|
187 |
||
188 |
copyEmpty:size |
|
189 |
"we have to redefine this, since 'self class new:size' does allocate size nil lines. |
|
190 |
In order to get collect working, we have to undo this allocation" |
|
191 |
||
192 |
^ (super copyEmpty:size) grow:0. |
|
193 |
||
194 |
"Created: 14.2.1996 / 11:05:47 / stefan" |
|
608 | 195 |
! ! |
196 |
||
5251 | 197 |
!StringCollection methodsFor:'printing & storing'! |
1 | 198 |
|
199 |
printString |
|
284 | 200 |
"return the receivers printString" |
201 |
||
1 | 202 |
^ self asString |
203 |
! ! |
|
204 |
||
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
205 |
!StringCollection methodsFor:'queries'! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
206 |
|
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
207 |
encoding |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
208 |
self do:[:l | l notNil ifTrue:[^ l encoding]]. |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
209 |
"/ sigh |
7982 | 210 |
^ #'unicode' |
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
211 |
! ! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
212 |
|
246 | 213 |
!StringCollection methodsFor:'searching'! |
1 | 214 |
|
215 |
indexOfLineStartingWith:aString |
|
216 |
"return the index of the first line starting with the argument, aString" |
|
217 |
||
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
218 |
|index "{ Class:SmallInteger }" |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
219 |
l| |
1 | 220 |
|
221 |
index := 1. |
|
222 |
[index <= self size] whileTrue:[ |
|
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
223 |
l := self at:index. |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
224 |
(l notNil and:[l startsWith:aString]) ifTrue:[ |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
225 |
^ index |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
226 |
]. |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
227 |
index := index + 1 |
1 | 228 |
]. |
229 |
^ 0 |
|
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
230 |
|
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
231 |
"Modified: 24.2.1996 / 19:08:47 / cg" |
1 | 232 |
! ! |
608 | 233 |
|
1064 | 234 |
!StringCollection methodsFor:'special converting'! |
235 |
||
236 |
withTabs |
|
237 |
"return a new stringCollection consisting of the receivers lines, |
|
238 |
where leading spaces are replaced by tabulator characters (assuming 8-col tabs). |
|
239 |
Notice: lines which do not contain leading spaces, are copied by reference to the |
|
240 |
new stringCollection (i.e. shared); |
|
241 |
otherwise new strings is created. |
|
242 |
Limitation: only the very first spaces are replaced" |
|
243 |
||
244 |
^ self collect:[:string| |
|
245 |
string notNil ifTrue:[ |
|
246 |
string withTabs |
|
247 |
] ifFalse:[ |
|
248 |
string |
|
249 |
] |
|
250 |
] |
|
251 |
||
252 |
" |
|
253 |
(' abcd ') asStringCollection withTabs |
|
254 |
" |
|
255 |
||
256 |
"Created: 4.3.1996 / 17:09:07 / cg" |
|
257 |
"Modified: 4.3.1996 / 17:10:37 / cg" |
|
258 |
! |
|
259 |
||
260 |
withTabsExpanded |
|
261 |
"return a new stringCollection consisting of the receivers lines, |
|
262 |
where tabs are replaced by space characters (assuming 8-col tabs). |
|
263 |
Notice: lines which do not contain any tab, are copied by reference to the |
|
264 |
new stringCollection (i.e. shared); |
|
265 |
otherwise new strings is created." |
|
266 |
||
267 |
^ self collect:[:string| |
|
268 |
string notNil ifTrue:[ |
|
269 |
string withTabsExpanded |
|
270 |
] ifFalse:[ |
|
271 |
string |
|
272 |
] |
|
273 |
] |
|
274 |
||
275 |
" |
|
276 |
|tab| |
|
277 |
tab := String with:Character tab. |
|
278 |
('abcd', tab, 'tef', tab, 'tgh') asStringCollection withTabsExpanded |
|
279 |
" |
|
280 |
||
281 |
"Created: 12.2.1996 / 22:25:56 / stefan" |
|
282 |
"Modified: 14.2.1996 / 11:13:01 / stefan" |
|
283 |
"Modified: 4.3.1996 / 17:10:22 / cg" |
|
284 |
! ! |
|
285 |
||
4007 | 286 |
!StringCollection methodsFor:'testing'! |
287 |
||
288 |
isStringCollection |
|
289 |
"return true, if the receiver is some kind of stringCollection; |
|
290 |
true is returned here - the method is redefined from Object." |
|
291 |
||
292 |
^ true |
|
293 |
||
294 |
||
295 |
! ! |
|
296 |
||
633 | 297 |
!StringCollection class methodsFor:'documentation'! |
298 |
||
299 |
version |
|
9161 | 300 |
^ '$Header: /cvs/stx/stx/libbasic/StringCollection.st,v 1.36 2006-02-20 21:29:35 cg Exp $' |
633 | 301 |
! ! |