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 |
|
|
13 |
VariableArray subclass:#Text
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Text'
|
|
18 |
!
|
|
19 |
|
|
20 |
Text comment:'
|
89
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
160
|
22 |
All Rights Reserved
|
97
|
23 |
|
160
|
24 |
$Header: /cvs/stx/stx/libbasic/StringCollection.st,v 1.9 1994-10-10 00:53:50 claus Exp $
|
89
|
25 |
'!
|
1
|
26 |
|
89
|
27 |
!Text class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
160
|
32 |
All Rights Reserved
|
1
|
33 |
|
89
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
1
|
42 |
|
89
|
43 |
version
|
|
44 |
"
|
160
|
45 |
$Header: /cvs/stx/stx/libbasic/StringCollection.st,v 1.9 1994-10-10 00:53:50 claus Exp $
|
89
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
160
|
51 |
Text is an array of lines which are strings.
|
|
52 |
WARNING:
|
|
53 |
This class is temporary (a historic leftover) - it may change or
|
|
54 |
even vanish in the future. Use OrderedCollections or other standard
|
|
55 |
classes to represent collections of strings.
|
89
|
56 |
|
|
57 |
Also, Text is a very bad name - there is something totally different
|
|
58 |
also named Text in ST-80 ...
|
160
|
59 |
|
89
|
60 |
So, even if kept, it will be definitely be renamed to something like
|
|
61 |
StringCollection in one of the next versions.
|
|
62 |
"
|
|
63 |
! !
|
1
|
64 |
|
|
65 |
!Text class methodsFor:'instance creation'!
|
|
66 |
|
|
67 |
from:aString
|
|
68 |
"return a new text object with lines taken from the argument, aString"
|
|
69 |
|
|
70 |
^ (self new:1) from:aString
|
|
71 |
!
|
|
72 |
|
|
73 |
fromArray:anArray
|
|
74 |
"return a new text object with lines taken from the argument, an array
|
|
75 |
of strings"
|
|
76 |
|
|
77 |
|newText
|
|
78 |
size "{ Class: SmallInteger }" |
|
|
79 |
|
|
80 |
size := anArray size.
|
|
81 |
newText := self new:size.
|
|
82 |
1 to:size do:[:line |
|
160
|
83 |
newText at:line put:(anArray at:line)
|
1
|
84 |
].
|
|
85 |
^ newText
|
|
86 |
! !
|
|
87 |
|
|
88 |
!Text methodsFor:'growing'!
|
|
89 |
|
|
90 |
grow:newSize
|
|
91 |
"grow to newsize - new elements are initialized with empty strings -
|
|
92 |
not nil"
|
|
93 |
|
|
94 |
|oldSize "{ Class:SmallInteger }"|
|
|
95 |
|
|
96 |
oldSize := tally.
|
|
97 |
super grow:newSize.
|
|
98 |
(oldSize < newSize) ifTrue:[
|
160
|
99 |
contentsArray from:(oldSize + 1) to:newSize put:''
|
1
|
100 |
]
|
|
101 |
!
|
|
102 |
|
|
103 |
add:aString
|
|
104 |
"append the argument, aString to myself -
|
|
105 |
we increase physical size by 50 to avoid lots of copying around"
|
|
106 |
|
|
107 |
|oldSize "{ Class:SmallInteger }"|
|
|
108 |
|
|
109 |
oldSize := tally.
|
|
110 |
super grow:(oldSize + 50).
|
|
111 |
tally := oldSize + 1.
|
|
112 |
contentsArray at:tally put:aString
|
|
113 |
! !
|
|
114 |
|
|
115 |
!Text methodsFor:'converting'!
|
|
116 |
|
|
117 |
asString
|
|
118 |
"return myself as a string with embedded cr's"
|
|
119 |
|
160
|
120 |
^ self asStringFrom:1 to:(self size)
|
1
|
121 |
!
|
|
122 |
|
87
|
123 |
asStringFrom:firstLine to:lastLine
|
|
124 |
"return part of myself as a string with embedded cr's"
|
|
125 |
|
160
|
126 |
^ self asStringFrom:firstLine to:lastLine compressTabs:false
|
|
127 |
!
|
|
128 |
|
|
129 |
asStringFrom:firstLine to:lastLine compressTabs:compressTabs
|
|
130 |
"return part of myself as a string with embedded cr's.
|
|
131 |
If the argument compressTabs is true, leading spaces are converted
|
|
132 |
to tab-characters (8col tabs)"
|
|
133 |
|
87
|
134 |
|totalLength "{ Class:SmallInteger }"
|
|
135 |
pos "{ Class:SmallInteger }"
|
160
|
136 |
newString lineString spaces idx nTabs cutOff|
|
87
|
137 |
|
160
|
138 |
"
|
|
139 |
first accumulate the size of the string, to avoid
|
|
140 |
countless reallocations. The size is not exact, but gives
|
|
141 |
an upper bound ...
|
|
142 |
"
|
87
|
143 |
totalLength := 0.
|
|
144 |
firstLine to:lastLine do:[:lineIndex |
|
160
|
145 |
lineString := self at:lineIndex.
|
|
146 |
lineString isNil ifTrue:[
|
|
147 |
totalLength := totalLength + 1
|
|
148 |
] ifFalse: [
|
|
149 |
totalLength := totalLength + lineString size + 1
|
|
150 |
].
|
|
151 |
0
|
87
|
152 |
].
|
160
|
153 |
|
|
154 |
spaces := ' '.
|
87
|
155 |
newString := String new:totalLength.
|
|
156 |
pos := 1.
|
|
157 |
firstLine to:lastLine do:[:lineIndex |
|
160
|
158 |
lineString := self at:lineIndex.
|
|
159 |
lineString isNil ifFalse:[
|
|
160 |
compressTabs ifTrue:[
|
|
161 |
"
|
|
162 |
mhmh: could use withTabs from String-class here,
|
|
163 |
but we should avoid creating too many temporary strings
|
|
164 |
(especially, since this method is typically used when converting
|
|
165 |
big texts such as when saving in the filebrowser ...).
|
|
166 |
Therefore, we convert tabs inline here doing a direct replace
|
|
167 |
in newString."
|
|
168 |
|
|
169 |
idx := lineString findFirst:[:c | (c ~~ Character space)].
|
|
170 |
nTabs := (idx-1) // 8.
|
|
171 |
nTabs ~~ 0 ifTrue:[
|
|
172 |
"any tabs"
|
|
173 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab).
|
|
174 |
pos := pos + nTabs.
|
|
175 |
newString replaceFrom:pos with:lineString startingAt:(nTabs * 8 + 1).
|
|
176 |
pos := pos + lineString size - (nTabs * 8).
|
|
177 |
] ifFalse:[
|
|
178 |
newString replaceFrom:pos with:lineString.
|
|
179 |
pos := pos + (lineString size)
|
|
180 |
].
|
|
181 |
] ifFalse:[
|
|
182 |
newString replaceFrom:pos with:lineString.
|
|
183 |
pos := pos + (lineString size)
|
|
184 |
]
|
|
185 |
].
|
|
186 |
newString at:pos put:(Character cr).
|
|
187 |
pos := pos + 1
|
|
188 |
].
|
|
189 |
pos ~~ totalLength ifTrue:[
|
|
190 |
^ newString copyTo:(pos - 1)
|
87
|
191 |
].
|
|
192 |
^ newString
|
|
193 |
!
|
|
194 |
|
1
|
195 |
from:aString
|
|
196 |
"setup my contents from the argument, aString"
|
|
197 |
|
|
198 |
|numberOfLines "{ Class:SmallInteger }"
|
|
199 |
start "{ Class:SmallInteger }"
|
|
200 |
stop "{ Class:SmallInteger }" |
|
|
201 |
|
|
202 |
numberOfLines := aString occurrencesOf:(Character cr).
|
|
203 |
numberOfLines := numberOfLines + 1.
|
|
204 |
self grow:numberOfLines.
|
|
205 |
start := 1.
|
|
206 |
1 to:numberOfLines do:[:lineNr |
|
160
|
207 |
stop := aString indexOf:(Character cr) startingAt:start.
|
|
208 |
stop == 0 ifTrue:[
|
|
209 |
stop := aString size
|
|
210 |
] ifFalse: [
|
|
211 |
stop := stop - 1.
|
|
212 |
].
|
10
|
213 |
|
160
|
214 |
(stop < start) ifTrue: [
|
|
215 |
self at:lineNr put:(String new:0)
|
|
216 |
] ifFalse: [
|
|
217 |
self at:lineNr put:(aString copyFrom:start to:stop)
|
|
218 |
].
|
|
219 |
start := stop + 2
|
1
|
220 |
]
|
|
221 |
!
|
|
222 |
|
|
223 |
asText
|
|
224 |
^ self
|
|
225 |
! !
|
|
226 |
|
|
227 |
!Text methodsFor:'printing'!
|
|
228 |
|
|
229 |
printString
|
|
230 |
^ self asString
|
|
231 |
! !
|
|
232 |
|
|
233 |
!Text methodsFor:'searching'!
|
|
234 |
|
|
235 |
indexOfLineStartingWith:aString
|
|
236 |
"return the index of the first line starting with the argument, aString"
|
|
237 |
|
|
238 |
|index "{ Class:SmallInteger }" |
|
|
239 |
|
|
240 |
index := 1.
|
|
241 |
[index <= self size] whileTrue:[
|
160
|
242 |
((self at:index) startsWith:aString) ifTrue:[
|
|
243 |
^ index
|
|
244 |
].
|
|
245 |
index := index + 1
|
1
|
246 |
].
|
|
247 |
^ 0
|
|
248 |
! !
|