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