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