author | Claus Gittinger <cg@exept.de> |
Wed, 03 Mar 2004 21:46:47 +0100 | |
changeset 8046 | f1357bb6aa47 |
parent 7982 | 0412a26c4621 |
child 8375 | b43b5d1c494d |
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 |
||
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
125 |
encodeFrom:oldEncoding into:newEncoding |
7916 | 126 |
|enc| |
127 |
||
128 |
enc := CharacterEncoder encoderToEncodeFrom:oldEncoding into:newEncoding. |
|
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
129 |
^ self collect:[:line | |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
130 |
line isNil |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
131 |
ifTrue:[ nil ] |
7916 | 132 |
ifFalse:[ enc encodeString:line] |
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
133 |
] |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
134 |
! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
135 |
|
1 | 136 |
from:aString |
137 |
"setup my contents from the argument, aString" |
|
138 |
||
139 |
|numberOfLines "{ Class:SmallInteger }" |
|
140 |
start "{ Class:SmallInteger }" |
|
6176 | 141 |
stop "{ Class:SmallInteger }" |
142 |
| |
|
1 | 143 |
|
144 |
numberOfLines := aString occurrencesOf:(Character cr). |
|
203 | 145 |
(aString endsWith:(Character cr)) ifFalse:[ |
6176 | 146 |
numberOfLines := numberOfLines + 1. |
203 | 147 |
]. |
1 | 148 |
self grow:numberOfLines. |
6176 | 149 |
|
1 | 150 |
start := 1. |
151 |
1 to:numberOfLines do:[:lineNr | |
|
6176 | 152 |
stop := aString indexOf:(Character cr) startingAt:start. |
153 |
stop == 0 ifTrue:[ |
|
154 |
self at:lineNr put:(aString copyFrom:start). |
|
155 |
self from:lineNr+1 to:numberOfLines put:''. |
|
156 |
^ self. |
|
157 |
]. |
|
10 | 158 |
|
6176 | 159 |
self at:lineNr put:(aString copyFrom:start to:stop-1). |
160 |
start := stop + 1 |
|
1 | 161 |
] |
6176 | 162 |
|
163 |
"Modified: / 12.11.2001 / 18:18:42 / cg" |
|
968 | 164 |
! ! |
165 |
||
166 |
!StringCollection methodsFor:'copying'! |
|
167 |
||
168 |
copyEmpty:size |
|
169 |
"we have to redefine this, since 'self class new:size' does allocate size nil lines. |
|
170 |
In order to get collect working, we have to undo this allocation" |
|
171 |
||
172 |
^ (super copyEmpty:size) grow:0. |
|
173 |
||
174 |
"Created: 14.2.1996 / 11:05:47 / stefan" |
|
608 | 175 |
! ! |
176 |
||
5251 | 177 |
!StringCollection methodsFor:'printing & storing'! |
1 | 178 |
|
179 |
printString |
|
284 | 180 |
"return the receivers printString" |
181 |
||
1 | 182 |
^ self asString |
183 |
! ! |
|
184 |
||
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
185 |
!StringCollection methodsFor:'queries'! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
186 |
|
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
187 |
encoding |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
188 |
self do:[:l | l notNil ifTrue:[^ l encoding]]. |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
189 |
"/ sigh |
7982 | 190 |
^ #'unicode' |
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
191 |
! ! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
192 |
|
246 | 193 |
!StringCollection methodsFor:'searching'! |
1 | 194 |
|
195 |
indexOfLineStartingWith:aString |
|
196 |
"return the index of the first line starting with the argument, aString" |
|
197 |
||
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
198 |
|index "{ Class:SmallInteger }" |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
199 |
l| |
1 | 200 |
|
201 |
index := 1. |
|
202 |
[index <= self size] whileTrue:[ |
|
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
203 |
l := self at:index. |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
204 |
(l notNil and:[l startsWith:aString]) ifTrue:[ |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
205 |
^ index |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
206 |
]. |
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
207 |
index := index + 1 |
1 | 208 |
]. |
209 |
^ 0 |
|
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
210 |
|
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
211 |
"Modified: 24.2.1996 / 19:08:47 / cg" |
1 | 212 |
! ! |
608 | 213 |
|
1064 | 214 |
!StringCollection methodsFor:'special converting'! |
215 |
||
216 |
withTabs |
|
217 |
"return a new stringCollection consisting of the receivers lines, |
|
218 |
where leading spaces are replaced by tabulator characters (assuming 8-col tabs). |
|
219 |
Notice: lines which do not contain leading spaces, are copied by reference to the |
|
220 |
new stringCollection (i.e. shared); |
|
221 |
otherwise new strings is created. |
|
222 |
Limitation: only the very first spaces are replaced" |
|
223 |
||
224 |
^ self collect:[:string| |
|
225 |
string notNil ifTrue:[ |
|
226 |
string withTabs |
|
227 |
] ifFalse:[ |
|
228 |
string |
|
229 |
] |
|
230 |
] |
|
231 |
||
232 |
" |
|
233 |
(' abcd ') asStringCollection withTabs |
|
234 |
" |
|
235 |
||
236 |
"Created: 4.3.1996 / 17:09:07 / cg" |
|
237 |
"Modified: 4.3.1996 / 17:10:37 / cg" |
|
238 |
! |
|
239 |
||
240 |
withTabsExpanded |
|
241 |
"return a new stringCollection consisting of the receivers lines, |
|
242 |
where tabs are replaced by space characters (assuming 8-col tabs). |
|
243 |
Notice: lines which do not contain any tab, are copied by reference to the |
|
244 |
new stringCollection (i.e. shared); |
|
245 |
otherwise new strings is created." |
|
246 |
||
247 |
^ self collect:[:string| |
|
248 |
string notNil ifTrue:[ |
|
249 |
string withTabsExpanded |
|
250 |
] ifFalse:[ |
|
251 |
string |
|
252 |
] |
|
253 |
] |
|
254 |
||
255 |
" |
|
256 |
|tab| |
|
257 |
tab := String with:Character tab. |
|
258 |
('abcd', tab, 'tef', tab, 'tgh') asStringCollection withTabsExpanded |
|
259 |
" |
|
260 |
||
261 |
"Created: 12.2.1996 / 22:25:56 / stefan" |
|
262 |
"Modified: 14.2.1996 / 11:13:01 / stefan" |
|
263 |
"Modified: 4.3.1996 / 17:10:22 / cg" |
|
264 |
! ! |
|
265 |
||
4007 | 266 |
!StringCollection methodsFor:'testing'! |
267 |
||
268 |
isStringCollection |
|
269 |
"return true, if the receiver is some kind of stringCollection; |
|
270 |
true is returned here - the method is redefined from Object." |
|
271 |
||
272 |
^ true |
|
273 |
||
274 |
||
275 |
! ! |
|
276 |
||
633 | 277 |
!StringCollection class methodsFor:'documentation'! |
278 |
||
279 |
version |
|
7982 | 280 |
^ '$Header: /cvs/stx/stx/libbasic/StringCollection.st,v 1.34 2004-02-21 12:08:11 cg Exp $' |
633 | 281 |
! ! |