author | Stefan Vogel <sv@exept.de> |
Tue, 10 Jul 2012 09:58:31 +0200 | |
changeset 14173 | 636682b78126 |
parent 14111 | b2c382a874ce |
child 14204 | 435bd4bb0958 |
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 |
|
11596 | 56 |
<resource: #obsolete> |
1 | 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 |
11596 | 150 |
<resource: #obsolete> |
1 | 151 |
"setup my contents from the argument, aString" |
152 |
||
8375
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
153 |
self obsoleteMethodWarning:'use #fromString:'. |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
154 |
^ self fromString:aString |
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 |
|
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
157 |
fromString:aString |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
158 |
"setup my contents from the argument, aString" |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
159 |
|
1 | 160 |
|numberOfLines "{ Class:SmallInteger }" |
161 |
start "{ Class:SmallInteger }" |
|
6176 | 162 |
stop "{ Class:SmallInteger }" |
163 |
| |
|
1 | 164 |
|
165 |
numberOfLines := aString occurrencesOf:(Character cr). |
|
203 | 166 |
(aString endsWith:(Character cr)) ifFalse:[ |
6176 | 167 |
numberOfLines := numberOfLines + 1. |
203 | 168 |
]. |
1 | 169 |
self grow:numberOfLines. |
6176 | 170 |
|
1 | 171 |
start := 1. |
172 |
1 to:numberOfLines do:[:lineNr | |
|
6176 | 173 |
stop := aString indexOf:(Character cr) startingAt:start. |
174 |
stop == 0 ifTrue:[ |
|
175 |
self at:lineNr put:(aString copyFrom:start). |
|
176 |
self from:lineNr+1 to:numberOfLines put:''. |
|
177 |
^ self. |
|
178 |
]. |
|
10 | 179 |
|
6176 | 180 |
self at:lineNr put:(aString copyFrom:start to:stop-1). |
181 |
start := stop + 1 |
|
1 | 182 |
] |
6176 | 183 |
|
184 |
"Modified: / 12.11.2001 / 18:18:42 / cg" |
|
968 | 185 |
! ! |
186 |
||
187 |
!StringCollection methodsFor:'copying'! |
|
188 |
||
189 |
copyEmpty:size |
|
14111 | 190 |
"we have to redefine this, since 'self class new:size' |
191 |
does allocate size nil lines (it does an implicit #grow: size). |
|
192 |
In order to get #collect: working, we should not perform this implicit grow" |
|
968 | 193 |
|
14111 | 194 |
^ super copyEmpty:0. |
968 | 195 |
|
196 |
"Created: 14.2.1996 / 11:05:47 / stefan" |
|
10452 | 197 |
! |
198 |
||
14111 | 199 |
copyEmptyAndGrow:size |
200 |
"performance optimization: |
|
201 |
StringCollections are always grown to size after #new: / #copyEmpty:" |
|
202 |
||
203 |
^ super copyEmpty:size. |
|
204 |
! |
|
205 |
||
10452 | 206 |
withoutLeadingBlankLines |
207 |
"return a copy of the receiver with leading blank lines removed. |
|
208 |
If there are no leading blank lines, the original receiver is returned. |
|
209 |
If all lines are blank, an empty string collection is returned." |
|
210 |
||
211 |
|indexOfFirstNonBlankLine| |
|
212 |
||
11702 | 213 |
indexOfFirstNonBlankLine := self findFirst:[:line | line notEmptyOrNil and:[line isBlank not]]. |
214 |
indexOfFirstNonBlankLine == 0 ifTrue:[ ^ self copyEmpty ]. |
|
215 |
indexOfFirstNonBlankLine == 1 ifTrue:[ ^ self ]. |
|
216 |
^ self copyFrom:indexOfFirstNonBlankLine |
|
10452 | 217 |
|
218 |
" |
|
219 |
'1 |
|
220 |
2 |
|
221 |
3' asStringCollection withoutLeadingBlankLines |
|
222 |
" |
|
223 |
||
224 |
" |
|
225 |
' |
|
226 |
2 |
|
227 |
3' asStringCollection withoutLeadingBlankLines |
|
228 |
" |
|
229 |
||
230 |
" |
|
231 |
' |
|
232 |
||
11702 | 233 |
' asStringCollection withoutLeadingBlankLines |
234 |
" |
|
235 |
! |
|
236 |
||
237 |
withoutTrailingBlankLines |
|
238 |
"return a copy of the receiver with trailing blank lines removed. |
|
239 |
If there are no trailing blank lines, the original receiver is returned. |
|
240 |
If all lines are blank, an empty string collection is returned." |
|
241 |
||
242 |
|indexOfLastNonBlankLine| |
|
243 |
||
244 |
indexOfLastNonBlankLine := self findLast:[:line | line notEmptyOrNil and:[line isBlank not]]. |
|
245 |
indexOfLastNonBlankLine == 0 ifTrue:[ ^ self copyEmpty ]. |
|
246 |
indexOfLastNonBlankLine == self size ifTrue:[ ^ self ]. |
|
247 |
^ self copyTo:indexOfLastNonBlankLine |
|
248 |
||
249 |
" |
|
250 |
'1 |
|
251 |
2 |
|
252 |
3' asStringCollection withoutTrailingBlankLines |
|
253 |
" |
|
254 |
||
255 |
" |
|
256 |
' |
|
257 |
2 |
|
258 |
3 |
|
259 |
' asStringCollection withoutTrailingBlankLines |
|
260 |
" |
|
261 |
||
262 |
" |
|
263 |
' |
|
264 |
||
265 |
' asStringCollection withoutTrailingBlankLines |
|
10452 | 266 |
" |
608 | 267 |
! ! |
268 |
||
14111 | 269 |
|
5251 | 270 |
!StringCollection methodsFor:'printing & storing'! |
1 | 271 |
|
10909
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
272 |
printOn:aStream |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
273 |
"print myself on aStream with embedded cr's" |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
274 |
|
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
275 |
self do:[:eachString| |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
276 |
eachString printOn:aStream. |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
277 |
aStream cr. |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
278 |
]. |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
279 |
! |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
280 |
|
1 | 281 |
printString |
284 | 282 |
"return the receivers printString" |
283 |
||
1 | 284 |
^ self asString |
285 |
! ! |
|
286 |
||
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
287 |
!StringCollection methodsFor:'queries'! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
288 |
|
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
289 |
encoding |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
290 |
self do:[:l | l notNil ifTrue:[^ l encoding]]. |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
291 |
"/ sigh |
14173 | 292 |
^ #unicode |
13331 | 293 |
! |
294 |
||
295 |
stringSize |
|
296 |
^ self inject:0 into:[:sizeSoFar :eachLine | |
|
297 |
sizeSoFar + eachLine size + 1 |
|
298 |
] |
|
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
299 |
! ! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
300 |
|
246 | 301 |
!StringCollection methodsFor:'searching'! |
1 | 302 |
|
303 |
indexOfLineStartingWith:aString |
|
304 |
"return the index of the first line starting with the argument, aString" |
|
305 |
||
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
306 |
|index "{ Class:SmallInteger }" |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
307 |
l| |
1 | 308 |
|
309 |
index := 1. |
|
310 |
[index <= self size] whileTrue:[ |
|
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
311 |
l := self at:index. |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
312 |
(l notNil and:[l startsWith:aString]) ifTrue:[ |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
313 |
^ index |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
314 |
]. |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
315 |
index := index + 1 |
1 | 316 |
]. |
317 |
^ 0 |
|
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
318 |
|
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
319 |
"Modified: 24.2.1996 / 19:08:47 / cg" |
1 | 320 |
! ! |
608 | 321 |
|
1064 | 322 |
!StringCollection methodsFor:'special converting'! |
323 |
||
324 |
withTabs |
|
325 |
"return a new stringCollection consisting of the receivers lines, |
|
326 |
where leading spaces are replaced by tabulator characters (assuming 8-col tabs). |
|
327 |
Notice: lines which do not contain leading spaces, are copied by reference to the |
|
328 |
new stringCollection (i.e. shared); |
|
329 |
otherwise new strings is created. |
|
330 |
Limitation: only the very first spaces are replaced" |
|
331 |
||
332 |
^ self collect:[:string| |
|
333 |
string notNil ifTrue:[ |
|
334 |
string withTabs |
|
335 |
] ifFalse:[ |
|
336 |
string |
|
337 |
] |
|
338 |
] |
|
339 |
||
340 |
" |
|
341 |
(' abcd ') asStringCollection withTabs |
|
342 |
" |
|
343 |
||
344 |
"Created: 4.3.1996 / 17:09:07 / cg" |
|
345 |
"Modified: 4.3.1996 / 17:10:37 / cg" |
|
346 |
! |
|
347 |
||
348 |
withTabsExpanded |
|
349 |
"return a new stringCollection consisting of the receivers lines, |
|
350 |
where tabs are replaced by space characters (assuming 8-col tabs). |
|
351 |
Notice: lines which do not contain any tab, are copied by reference to the |
|
352 |
new stringCollection (i.e. shared); |
|
353 |
otherwise new strings is created." |
|
354 |
||
355 |
^ self collect:[:string| |
|
356 |
string notNil ifTrue:[ |
|
357 |
string withTabsExpanded |
|
358 |
] ifFalse:[ |
|
359 |
string |
|
360 |
] |
|
361 |
] |
|
362 |
||
363 |
" |
|
364 |
|tab| |
|
365 |
tab := String with:Character tab. |
|
366 |
('abcd', tab, 'tef', tab, 'tgh') asStringCollection withTabsExpanded |
|
367 |
" |
|
368 |
||
369 |
"Created: 12.2.1996 / 22:25:56 / stefan" |
|
370 |
"Modified: 14.2.1996 / 11:13:01 / stefan" |
|
371 |
"Modified: 4.3.1996 / 17:10:22 / cg" |
|
372 |
! ! |
|
373 |
||
4007 | 374 |
!StringCollection methodsFor:'testing'! |
375 |
||
376 |
isStringCollection |
|
377 |
"return true, if the receiver is some kind of stringCollection; |
|
378 |
true is returned here - the method is redefined from Object." |
|
379 |
||
380 |
^ true |
|
381 |
||
382 |
||
383 |
! ! |
|
384 |
||
633 | 385 |
!StringCollection class methodsFor:'documentation'! |
386 |
||
387 |
version |
|
14173 | 388 |
^ '$Header: /cvs/stx/stx/libbasic/StringCollection.st,v 1.43 2012-07-10 07:58:31 stefan Exp $' |
13331 | 389 |
! |
390 |
||
391 |
version_CVS |
|
14173 | 392 |
^ '$Header: /cvs/stx/stx/libbasic/StringCollection.st,v 1.43 2012-07-10 07:58:31 stefan Exp $' |
633 | 393 |
! ! |