1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
text is an array of lines which are strings.
|
|
26 |
this is just temporary - may change in the future.
|
10
|
27 |
Also, Text is a bad name - there is something totally different also named Text
|
|
28 |
in ST-80 ...
|
1
|
29 |
|
87
|
30 |
$Header: /cvs/stx/stx/libbasic/StringCollection.st,v 1.6 1994-06-02 11:36:22 claus Exp $
|
1
|
31 |
'!
|
|
32 |
|
|
33 |
!Text class methodsFor:'instance creation'!
|
|
34 |
|
|
35 |
from:aString
|
|
36 |
"return a new text object with lines taken from the argument, aString"
|
|
37 |
|
|
38 |
^ (self new:1) from:aString
|
|
39 |
!
|
|
40 |
|
|
41 |
fromArray:anArray
|
|
42 |
"return a new text object with lines taken from the argument, an array
|
|
43 |
of strings"
|
|
44 |
|
|
45 |
|newText
|
|
46 |
size "{ Class: SmallInteger }" |
|
|
47 |
|
|
48 |
size := anArray size.
|
|
49 |
newText := self new:size.
|
|
50 |
1 to:size do:[:line |
|
|
51 |
newText at:line put:(anArray at:line)
|
|
52 |
].
|
|
53 |
^ newText
|
|
54 |
! !
|
|
55 |
|
|
56 |
!Text methodsFor:'growing'!
|
|
57 |
|
|
58 |
grow:newSize
|
|
59 |
"grow to newsize - new elements are initialized with empty strings -
|
|
60 |
not nil"
|
|
61 |
|
|
62 |
|oldSize "{ Class:SmallInteger }"|
|
|
63 |
|
|
64 |
oldSize := tally.
|
|
65 |
super grow:newSize.
|
|
66 |
(oldSize < newSize) ifTrue:[
|
|
67 |
contentsArray from:(oldSize + 1) to:newSize put:''
|
|
68 |
]
|
|
69 |
!
|
|
70 |
|
|
71 |
add:aString
|
|
72 |
"append the argument, aString to myself -
|
|
73 |
we increase physical size by 50 to avoid lots of copying around"
|
|
74 |
|
|
75 |
|oldSize "{ Class:SmallInteger }"|
|
|
76 |
|
|
77 |
oldSize := tally.
|
|
78 |
super grow:(oldSize + 50).
|
|
79 |
tally := oldSize + 1.
|
|
80 |
contentsArray at:tally put:aString
|
|
81 |
! !
|
|
82 |
|
|
83 |
!Text methodsFor:'converting'!
|
|
84 |
|
|
85 |
asString
|
|
86 |
"return myself as a string with embedded cr's"
|
|
87 |
|
|
88 |
|totalLength "{ Class:SmallInteger }"
|
|
89 |
pos "{ Class:SmallInteger }"
|
|
90 |
newString |
|
|
91 |
|
|
92 |
totalLength := 0.
|
|
93 |
self do:[:lineString |
|
|
94 |
lineString isNil ifTrue:[
|
|
95 |
totalLength := totalLength + 1
|
|
96 |
] ifFalse: [
|
|
97 |
totalLength := totalLength + lineString size + 1
|
|
98 |
].
|
|
99 |
0
|
|
100 |
].
|
|
101 |
newString := String new:totalLength.
|
|
102 |
pos := 1.
|
|
103 |
self do:[:lineString |
|
|
104 |
lineString isNil ifFalse:[
|
|
105 |
newString replaceFrom:pos with:lineString.
|
|
106 |
pos := pos + (lineString size)
|
|
107 |
].
|
|
108 |
newString at:pos put:(Character cr).
|
|
109 |
pos := pos + 1
|
|
110 |
].
|
|
111 |
^ newString
|
|
112 |
!
|
|
113 |
|
87
|
114 |
asStringFrom:firstLine to:lastLine
|
|
115 |
"return part of myself as a string with embedded cr's"
|
|
116 |
|
|
117 |
|totalLength "{ Class:SmallInteger }"
|
|
118 |
pos "{ Class:SmallInteger }"
|
|
119 |
newString lineString|
|
|
120 |
|
|
121 |
totalLength := 0.
|
|
122 |
firstLine to:lastLine do:[:lineIndex |
|
|
123 |
lineString := self at:lineIndex.
|
|
124 |
lineString isNil ifTrue:[
|
|
125 |
totalLength := totalLength + 1
|
|
126 |
] ifFalse: [
|
|
127 |
totalLength := totalLength + lineString size + 1
|
|
128 |
].
|
|
129 |
0
|
|
130 |
].
|
|
131 |
newString := String new:totalLength.
|
|
132 |
pos := 1.
|
|
133 |
firstLine to:lastLine do:[:lineIndex |
|
|
134 |
lineString := self at:lineIndex.
|
|
135 |
lineString isNil ifFalse:[
|
|
136 |
newString replaceFrom:pos with:lineString.
|
|
137 |
pos := pos + (lineString size)
|
|
138 |
].
|
|
139 |
newString at:pos put:(Character cr).
|
|
140 |
pos := pos + 1
|
|
141 |
].
|
|
142 |
^ newString
|
|
143 |
!
|
|
144 |
|
1
|
145 |
from:aString
|
|
146 |
"setup my contents from the argument, aString"
|
|
147 |
|
|
148 |
|numberOfLines "{ Class:SmallInteger }"
|
|
149 |
start "{ Class:SmallInteger }"
|
|
150 |
stop "{ Class:SmallInteger }" |
|
|
151 |
|
|
152 |
numberOfLines := aString occurrencesOf:(Character cr).
|
|
153 |
numberOfLines := numberOfLines + 1.
|
|
154 |
self grow:numberOfLines.
|
|
155 |
start := 1.
|
|
156 |
1 to:numberOfLines do:[:lineNr |
|
10
|
157 |
stop := aString indexOf:(Character cr) startingAt:start.
|
87
|
158 |
stop == 0 ifTrue:[
|
|
159 |
stop := aString size
|
|
160 |
] ifFalse: [
|
10
|
161 |
stop := stop - 1.
|
87
|
162 |
].
|
10
|
163 |
|
1
|
164 |
(stop < start) ifTrue: [
|
|
165 |
self at:lineNr put:(String new:0)
|
|
166 |
] ifFalse: [
|
|
167 |
self at:lineNr put:(aString copyFrom:start to:stop)
|
|
168 |
].
|
|
169 |
start := stop + 2
|
|
170 |
]
|
|
171 |
!
|
|
172 |
|
|
173 |
asText
|
|
174 |
^ self
|
|
175 |
! !
|
|
176 |
|
|
177 |
!Text methodsFor:'printing'!
|
|
178 |
|
|
179 |
printString
|
|
180 |
^ self asString
|
|
181 |
! !
|
|
182 |
|
|
183 |
!Text methodsFor:'searching'!
|
|
184 |
|
|
185 |
indexOfLineStartingWith:aString
|
|
186 |
"return the index of the first line starting with the argument, aString"
|
|
187 |
|
|
188 |
|index "{ Class:SmallInteger }" |
|
|
189 |
|
|
190 |
index := 1.
|
|
191 |
[index <= self size] whileTrue:[
|
|
192 |
((self at:index) startsWith:aString) ifTrue:[
|
|
193 |
^ index
|
|
194 |
].
|
|
195 |
index := index + 1
|
|
196 |
].
|
|
197 |
^ 0
|
|
198 |
! !
|