author | Claus Gittinger <cg@exept.de> |
Wed, 14 Jan 2004 17:03:44 +0100 | |
changeset 7829 | 96a7b0065d77 |
parent 6561 | 93afb15812f8 |
child 7899 | 7577df77ba95 |
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 |
||
59 |
^ (self new:1) from:aString |
|
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 |
||
77 |
new:size |
|
78 |
"return a new string collection with size empty lines" |
|
79 |
||
80 |
^ (super new:size) grow:size |
|
1 | 81 |
! ! |
82 |
||
246 | 83 |
!StringCollection methodsFor:'converting'! |
1 | 84 |
|
85 |
asString |
|
86 |
"return myself as a string with embedded cr's" |
|
87 |
||
7829 | 88 |
^ self |
89 |
asStringWith:Character cr |
|
90 |
from:1 to:(self size) |
|
91 |
compressTabs:false |
|
92 |
final:Character cr |
|
87 | 93 |
! |
94 |
||
608 | 95 |
asStringCollection |
96 |
"return the receiver as a stringCollection - thats easy" |
|
97 |
||
98 |
^ self |
|
99 |
! |
|
100 |
||
3601
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
101 |
asStringWithoutEmphasis |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
102 |
"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
|
103 |
|
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
104 |
^ self |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
105 |
asStringWith:Character cr |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
106 |
from:1 to:(self size) |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
107 |
compressTabs:false |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
108 |
final:Character cr |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
109 |
withEmphasis:false |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
110 |
|
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
111 |
"Created: / 17.6.1998 / 12:32:48 / cg" |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
112 |
! |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
113 |
|
7829 | 114 |
asStringWithoutFinalCR |
115 |
"return myself as a string with embedded cr's |
|
116 |
but do not add a final CR" |
|
117 |
||
118 |
^ self |
|
119 |
asStringWith:Character cr |
|
120 |
from:1 to:(self size) |
|
121 |
compressTabs:false |
|
122 |
final:nil |
|
123 |
! |
|
124 |
||
1 | 125 |
from:aString |
126 |
"setup my contents from the argument, aString" |
|
127 |
||
128 |
|numberOfLines "{ Class:SmallInteger }" |
|
129 |
start "{ Class:SmallInteger }" |
|
6176 | 130 |
stop "{ Class:SmallInteger }" |
131 |
| |
|
1 | 132 |
|
133 |
numberOfLines := aString occurrencesOf:(Character cr). |
|
203 | 134 |
(aString endsWith:(Character cr)) ifFalse:[ |
6176 | 135 |
numberOfLines := numberOfLines + 1. |
203 | 136 |
]. |
1 | 137 |
self grow:numberOfLines. |
6176 | 138 |
|
1 | 139 |
start := 1. |
140 |
1 to:numberOfLines do:[:lineNr | |
|
6176 | 141 |
stop := aString indexOf:(Character cr) startingAt:start. |
142 |
stop == 0 ifTrue:[ |
|
143 |
self at:lineNr put:(aString copyFrom:start). |
|
144 |
self from:lineNr+1 to:numberOfLines put:''. |
|
145 |
^ self. |
|
146 |
]. |
|
10 | 147 |
|
6176 | 148 |
self at:lineNr put:(aString copyFrom:start to:stop-1). |
149 |
start := stop + 1 |
|
1 | 150 |
] |
6176 | 151 |
|
152 |
"Modified: / 12.11.2001 / 18:18:42 / cg" |
|
968 | 153 |
! ! |
154 |
||
155 |
!StringCollection methodsFor:'copying'! |
|
156 |
||
157 |
copyEmpty:size |
|
158 |
"we have to redefine this, since 'self class new:size' does allocate size nil lines. |
|
159 |
In order to get collect working, we have to undo this allocation" |
|
160 |
||
161 |
^ (super copyEmpty:size) grow:0. |
|
162 |
||
163 |
"Created: 14.2.1996 / 11:05:47 / stefan" |
|
608 | 164 |
! ! |
165 |
||
5251 | 166 |
!StringCollection methodsFor:'printing & storing'! |
1 | 167 |
|
168 |
printString |
|
284 | 169 |
"return the receivers printString" |
170 |
||
1 | 171 |
^ self asString |
172 |
! ! |
|
173 |
||
246 | 174 |
!StringCollection methodsFor:'searching'! |
1 | 175 |
|
176 |
indexOfLineStartingWith:aString |
|
177 |
"return the index of the first line starting with the argument, aString" |
|
178 |
||
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
179 |
|index "{ Class:SmallInteger }" |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
180 |
l| |
1 | 181 |
|
182 |
index := 1. |
|
183 |
[index <= self size] whileTrue:[ |
|
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
184 |
l := self at:index. |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
185 |
(l notNil and:[l startsWith:aString]) ifTrue:[ |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
186 |
^ index |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
187 |
]. |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
188 |
index := index + 1 |
1 | 189 |
]. |
190 |
^ 0 |
|
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
191 |
|
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
192 |
"Modified: 24.2.1996 / 19:08:47 / cg" |
1 | 193 |
! ! |
608 | 194 |
|
1064 | 195 |
!StringCollection methodsFor:'special converting'! |
196 |
||
197 |
withTabs |
|
198 |
"return a new stringCollection consisting of the receivers lines, |
|
199 |
where leading spaces are replaced by tabulator characters (assuming 8-col tabs). |
|
200 |
Notice: lines which do not contain leading spaces, are copied by reference to the |
|
201 |
new stringCollection (i.e. shared); |
|
202 |
otherwise new strings is created. |
|
203 |
Limitation: only the very first spaces are replaced" |
|
204 |
||
205 |
^ self collect:[:string| |
|
206 |
string notNil ifTrue:[ |
|
207 |
string withTabs |
|
208 |
] ifFalse:[ |
|
209 |
string |
|
210 |
] |
|
211 |
] |
|
212 |
||
213 |
" |
|
214 |
(' abcd ') asStringCollection withTabs |
|
215 |
" |
|
216 |
||
217 |
"Created: 4.3.1996 / 17:09:07 / cg" |
|
218 |
"Modified: 4.3.1996 / 17:10:37 / cg" |
|
219 |
! |
|
220 |
||
221 |
withTabsExpanded |
|
222 |
"return a new stringCollection consisting of the receivers lines, |
|
223 |
where tabs are replaced by space characters (assuming 8-col tabs). |
|
224 |
Notice: lines which do not contain any tab, are copied by reference to the |
|
225 |
new stringCollection (i.e. shared); |
|
226 |
otherwise new strings is created." |
|
227 |
||
228 |
^ self collect:[:string| |
|
229 |
string notNil ifTrue:[ |
|
230 |
string withTabsExpanded |
|
231 |
] ifFalse:[ |
|
232 |
string |
|
233 |
] |
|
234 |
] |
|
235 |
||
236 |
" |
|
237 |
|tab| |
|
238 |
tab := String with:Character tab. |
|
239 |
('abcd', tab, 'tef', tab, 'tgh') asStringCollection withTabsExpanded |
|
240 |
" |
|
241 |
||
242 |
"Created: 12.2.1996 / 22:25:56 / stefan" |
|
243 |
"Modified: 14.2.1996 / 11:13:01 / stefan" |
|
244 |
"Modified: 4.3.1996 / 17:10:22 / cg" |
|
245 |
! ! |
|
246 |
||
4007 | 247 |
!StringCollection methodsFor:'testing'! |
248 |
||
249 |
isStringCollection |
|
250 |
"return true, if the receiver is some kind of stringCollection; |
|
251 |
true is returned here - the method is redefined from Object." |
|
252 |
||
253 |
^ true |
|
254 |
||
255 |
||
256 |
! ! |
|
257 |
||
633 | 258 |
!StringCollection class methodsFor:'documentation'! |
259 |
||
260 |
version |
|
7829 | 261 |
^ '$Header: /cvs/stx/stx/libbasic/StringCollection.st,v 1.31 2004-01-14 16:03:44 cg Exp $' |
633 | 262 |
! ! |