author | Claus Gittinger <cg@exept.de> |
Sat, 22 Mar 1997 16:19:44 +0100 | |
changeset 2484 | 05bd2cc43192 |
parent 1290 | 15ba3221b89b |
child 3601 | c879b9a15f38 |
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 |
||
1 | 98 |
from:aString |
99 |
"setup my contents from the argument, aString" |
|
100 |
||
101 |
|numberOfLines "{ Class:SmallInteger }" |
|
102 |
start "{ Class:SmallInteger }" |
|
103 |
stop "{ Class:SmallInteger }" | |
|
104 |
||
105 |
numberOfLines := aString occurrencesOf:(Character cr). |
|
203 | 106 |
(aString endsWith:(Character cr)) ifFalse:[ |
107 |
numberOfLines := numberOfLines + 1. |
|
108 |
]. |
|
1 | 109 |
self grow:numberOfLines. |
110 |
start := 1. |
|
111 |
1 to:numberOfLines do:[:lineNr | |
|
160 | 112 |
stop := aString indexOf:(Character cr) startingAt:start. |
113 |
stop == 0 ifTrue:[ |
|
114 |
stop := aString size |
|
115 |
] ifFalse: [ |
|
116 |
stop := stop - 1. |
|
117 |
]. |
|
10 | 118 |
|
160 | 119 |
(stop < start) ifTrue: [ |
120 |
self at:lineNr put:(String new:0) |
|
121 |
] ifFalse: [ |
|
122 |
self at:lineNr put:(aString copyFrom:start to:stop) |
|
123 |
]. |
|
124 |
start := stop + 2 |
|
1 | 125 |
] |
968 | 126 |
! ! |
127 |
||
128 |
!StringCollection methodsFor:'copying'! |
|
129 |
||
130 |
copyEmpty:size |
|
131 |
"we have to redefine this, since 'self class new:size' does allocate size nil lines. |
|
132 |
In order to get collect working, we have to undo this allocation" |
|
133 |
||
134 |
^ (super copyEmpty:size) grow:0. |
|
135 |
||
136 |
"Created: 14.2.1996 / 11:05:47 / stefan" |
|
608 | 137 |
! ! |
138 |
||
246 | 139 |
!StringCollection methodsFor:'printing'! |
1 | 140 |
|
141 |
printString |
|
284 | 142 |
"return the receivers printString" |
143 |
||
1 | 144 |
^ self asString |
145 |
! ! |
|
146 |
||
246 | 147 |
!StringCollection methodsFor:'searching'! |
1 | 148 |
|
149 |
indexOfLineStartingWith:aString |
|
150 |
"return the index of the first line starting with the argument, aString" |
|
151 |
||
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
152 |
|index "{ Class:SmallInteger }" |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
153 |
l| |
1 | 154 |
|
155 |
index := 1. |
|
156 |
[index <= self size] whileTrue:[ |
|
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
157 |
l := self at:index. |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
158 |
(l notNil and:[l startsWith:aString]) ifTrue:[ |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
159 |
^ index |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
160 |
]. |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
161 |
index := index + 1 |
1 | 162 |
]. |
163 |
^ 0 |
|
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
164 |
|
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
165 |
"Modified: 24.2.1996 / 19:08:47 / cg" |
1 | 166 |
! ! |
608 | 167 |
|
1064 | 168 |
!StringCollection methodsFor:'special converting'! |
169 |
||
170 |
withTabs |
|
171 |
"return a new stringCollection consisting of the receivers lines, |
|
172 |
where leading spaces are replaced by tabulator characters (assuming 8-col tabs). |
|
173 |
Notice: lines which do not contain leading spaces, are copied by reference to the |
|
174 |
new stringCollection (i.e. shared); |
|
175 |
otherwise new strings is created. |
|
176 |
Limitation: only the very first spaces are replaced" |
|
177 |
||
178 |
^ self collect:[:string| |
|
179 |
string notNil ifTrue:[ |
|
180 |
string withTabs |
|
181 |
] ifFalse:[ |
|
182 |
string |
|
183 |
] |
|
184 |
] |
|
185 |
||
186 |
" |
|
187 |
(' abcd ') asStringCollection withTabs |
|
188 |
" |
|
189 |
||
190 |
"Created: 4.3.1996 / 17:09:07 / cg" |
|
191 |
"Modified: 4.3.1996 / 17:10:37 / cg" |
|
192 |
! |
|
193 |
||
194 |
withTabsExpanded |
|
195 |
"return a new stringCollection consisting of the receivers lines, |
|
196 |
where tabs are replaced by space characters (assuming 8-col tabs). |
|
197 |
Notice: lines which do not contain any tab, are copied by reference to the |
|
198 |
new stringCollection (i.e. shared); |
|
199 |
otherwise new strings is created." |
|
200 |
||
201 |
^ self collect:[:string| |
|
202 |
string notNil ifTrue:[ |
|
203 |
string withTabsExpanded |
|
204 |
] ifFalse:[ |
|
205 |
string |
|
206 |
] |
|
207 |
] |
|
208 |
||
209 |
" |
|
210 |
|tab| |
|
211 |
tab := String with:Character tab. |
|
212 |
('abcd', tab, 'tef', tab, 'tgh') asStringCollection withTabsExpanded |
|
213 |
" |
|
214 |
||
215 |
"Created: 12.2.1996 / 22:25:56 / stefan" |
|
216 |
"Modified: 14.2.1996 / 11:13:01 / stefan" |
|
217 |
"Modified: 4.3.1996 / 17:10:22 / cg" |
|
218 |
! ! |
|
219 |
||
633 | 220 |
!StringCollection class methodsFor:'documentation'! |
221 |
||
222 |
version |
|
1290 | 223 |
^ '$Header: /cvs/stx/stx/libbasic/StringCollection.st,v 1.25 1996-04-25 16:18:04 cg Exp $' |
633 | 224 |
! ! |