author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 31 Oct 2011 22:19:21 +0000 | |
branch | jv |
changeset 17892 | d86c8bd5ece3 |
parent 17869 | 9610c6c94e71 |
child 17910 | 8d796ca8bd1d |
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 |
|
190 |
"we have to redefine this, since 'self class new:size' does allocate size nil lines. |
|
191 |
In order to get collect working, we have to undo this allocation" |
|
192 |
||
193 |
^ (super copyEmpty:size) grow:0. |
|
194 |
||
195 |
"Created: 14.2.1996 / 11:05:47 / stefan" |
|
10452 | 196 |
! |
197 |
||
198 |
withoutLeadingBlankLines |
|
199 |
"return a copy of the receiver with leading blank lines removed. |
|
200 |
If there are no leading blank lines, the original receiver is returned. |
|
201 |
If all lines are blank, an empty string collection is returned." |
|
202 |
||
203 |
|indexOfFirstNonBlankLine| |
|
204 |
||
11702 | 205 |
indexOfFirstNonBlankLine := self findFirst:[:line | line notEmptyOrNil and:[line isBlank not]]. |
206 |
indexOfFirstNonBlankLine == 0 ifTrue:[ ^ self copyEmpty ]. |
|
207 |
indexOfFirstNonBlankLine == 1 ifTrue:[ ^ self ]. |
|
208 |
^ self copyFrom:indexOfFirstNonBlankLine |
|
10452 | 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 |
||
11702 | 225 |
' asStringCollection withoutLeadingBlankLines |
226 |
" |
|
227 |
! |
|
228 |
||
229 |
withoutTrailingBlankLines |
|
230 |
"return a copy of the receiver with trailing blank lines removed. |
|
231 |
If there are no trailing blank lines, the original receiver is returned. |
|
232 |
If all lines are blank, an empty string collection is returned." |
|
233 |
||
234 |
|indexOfLastNonBlankLine| |
|
235 |
||
236 |
indexOfLastNonBlankLine := self findLast:[:line | line notEmptyOrNil and:[line isBlank not]]. |
|
237 |
indexOfLastNonBlankLine == 0 ifTrue:[ ^ self copyEmpty ]. |
|
238 |
indexOfLastNonBlankLine == self size ifTrue:[ ^ self ]. |
|
239 |
^ self copyTo:indexOfLastNonBlankLine |
|
240 |
||
241 |
" |
|
242 |
'1 |
|
243 |
2 |
|
244 |
3' asStringCollection withoutTrailingBlankLines |
|
245 |
" |
|
246 |
||
247 |
" |
|
248 |
' |
|
249 |
2 |
|
250 |
3 |
|
251 |
' asStringCollection withoutTrailingBlankLines |
|
252 |
" |
|
253 |
||
254 |
" |
|
255 |
' |
|
256 |
||
257 |
' asStringCollection withoutTrailingBlankLines |
|
10452 | 258 |
" |
608 | 259 |
! ! |
260 |
||
17865 | 261 |
|
5251 | 262 |
!StringCollection methodsFor:'printing & storing'! |
1 | 263 |
|
10909
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
264 |
printOn:aStream |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
265 |
"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
|
266 |
|
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
267 |
self do:[:eachString| |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
268 |
eachString printOn:aStream. |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
269 |
aStream cr. |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
270 |
]. |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
271 |
! |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
272 |
|
1 | 273 |
printString |
284 | 274 |
"return the receivers printString" |
275 |
||
1 | 276 |
^ self asString |
277 |
! ! |
|
278 |
||
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
279 |
!StringCollection methodsFor:'queries'! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
280 |
|
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
281 |
encoding |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
282 |
self do:[:l | l notNil ifTrue:[^ l encoding]]. |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
283 |
"/ sigh |
7982 | 284 |
^ #'unicode' |
17841 | 285 |
! |
286 |
||
287 |
stringSize |
|
288 |
^ self inject:0 into:[:sizeSoFar :eachLine | |
|
289 |
sizeSoFar + eachLine size + 1 |
|
290 |
] |
|
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
291 |
! ! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
292 |
|
246 | 293 |
!StringCollection methodsFor:'searching'! |
1 | 294 |
|
295 |
indexOfLineStartingWith:aString |
|
296 |
"return the index of the first line starting with the argument, aString" |
|
297 |
||
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
298 |
|index "{ Class:SmallInteger }" |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
299 |
l| |
1 | 300 |
|
301 |
index := 1. |
|
302 |
[index <= self size] whileTrue:[ |
|
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
303 |
l := self at:index. |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
304 |
(l notNil and:[l startsWith:aString]) ifTrue:[ |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
305 |
^ index |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
306 |
]. |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
307 |
index := index + 1 |
1 | 308 |
]. |
309 |
^ 0 |
|
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
310 |
|
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
311 |
"Modified: 24.2.1996 / 19:08:47 / cg" |
1 | 312 |
! ! |
608 | 313 |
|
1064 | 314 |
!StringCollection methodsFor:'special converting'! |
315 |
||
316 |
withTabs |
|
317 |
"return a new stringCollection consisting of the receivers lines, |
|
318 |
where leading spaces are replaced by tabulator characters (assuming 8-col tabs). |
|
319 |
Notice: lines which do not contain leading spaces, are copied by reference to the |
|
320 |
new stringCollection (i.e. shared); |
|
321 |
otherwise new strings is created. |
|
322 |
Limitation: only the very first spaces are replaced" |
|
323 |
||
324 |
^ self collect:[:string| |
|
325 |
string notNil ifTrue:[ |
|
326 |
string withTabs |
|
327 |
] ifFalse:[ |
|
328 |
string |
|
329 |
] |
|
330 |
] |
|
331 |
||
332 |
" |
|
333 |
(' abcd ') asStringCollection withTabs |
|
334 |
" |
|
335 |
||
336 |
"Created: 4.3.1996 / 17:09:07 / cg" |
|
337 |
"Modified: 4.3.1996 / 17:10:37 / cg" |
|
338 |
! |
|
339 |
||
340 |
withTabsExpanded |
|
341 |
"return a new stringCollection consisting of the receivers lines, |
|
342 |
where tabs are replaced by space characters (assuming 8-col tabs). |
|
343 |
Notice: lines which do not contain any tab, are copied by reference to the |
|
344 |
new stringCollection (i.e. shared); |
|
345 |
otherwise new strings is created." |
|
346 |
||
347 |
^ self collect:[:string| |
|
348 |
string notNil ifTrue:[ |
|
349 |
string withTabsExpanded |
|
350 |
] ifFalse:[ |
|
351 |
string |
|
352 |
] |
|
353 |
] |
|
354 |
||
355 |
" |
|
356 |
|tab| |
|
357 |
tab := String with:Character tab. |
|
358 |
('abcd', tab, 'tef', tab, 'tgh') asStringCollection withTabsExpanded |
|
359 |
" |
|
360 |
||
361 |
"Created: 12.2.1996 / 22:25:56 / stefan" |
|
362 |
"Modified: 14.2.1996 / 11:13:01 / stefan" |
|
363 |
"Modified: 4.3.1996 / 17:10:22 / cg" |
|
364 |
! ! |
|
365 |
||
4007 | 366 |
!StringCollection methodsFor:'testing'! |
367 |
||
368 |
isStringCollection |
|
369 |
"return true, if the receiver is some kind of stringCollection; |
|
370 |
true is returned here - the method is redefined from Object." |
|
371 |
||
372 |
^ true |
|
373 |
||
374 |
||
375 |
! ! |
|
376 |
||
633 | 377 |
!StringCollection class methodsFor:'documentation'! |
378 |
||
379 |
version |
|
17892 | 380 |
^ '$Header: /cvs/stx/stx/libbasic/StringCollection.st,v 1.41 2011/04/11 09:17:14 stefan Exp $' |
17841 | 381 |
! |
382 |
||
383 |
version_CVS |
|
17845 | 384 |
^ '§Header: /cvs/stx/stx/libbasic/StringCollection.st,v 1.41 2011/04/11 09:17:14 stefan Exp §' |
17865 | 385 |
! |
386 |
||
387 |
version_SVN |
|
17892 | 388 |
^ '$Id: StringCollection.st 10729 2011-10-31 22:19:21Z vranyj1 $' |
17846 | 389 |
! ! |
17892 | 390 |