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:
|
203
|
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 ...
|
89
|
46 |
"
|
|
47 |
! !
|
1
|
48 |
|
246
|
49 |
!StringCollection class methodsFor:'instance creation'!
|
1
|
50 |
|
|
51 |
from:aString
|
|
52 |
"return a new text object with lines taken from the argument, aString"
|
|
53 |
|
|
54 |
^ (self new:1) from:aString
|
|
55 |
!
|
|
56 |
|
|
57 |
fromArray:anArray
|
|
58 |
"return a new text object with lines taken from the argument, an array
|
|
59 |
of strings"
|
|
60 |
|
246
|
61 |
|newStringCollection
|
1
|
62 |
size "{ Class: SmallInteger }" |
|
|
63 |
|
|
64 |
size := anArray size.
|
246
|
65 |
newStringCollection := self new:size.
|
1
|
66 |
1 to:size do:[:line |
|
246
|
67 |
newStringCollection at:line put:(anArray at:line)
|
1
|
68 |
].
|
246
|
69 |
^ newStringCollection
|
731
|
70 |
!
|
|
71 |
|
|
72 |
new:size
|
|
73 |
"return a new string collection with size empty lines"
|
|
74 |
|
|
75 |
^ (super new:size) grow:size
|
1
|
76 |
! !
|
|
77 |
|
246
|
78 |
!StringCollection methodsFor:'converting'!
|
1
|
79 |
|
|
80 |
asString
|
|
81 |
"return myself as a string with embedded cr's"
|
|
82 |
|
262
|
83 |
^ self asStringWith:Character cr
|
|
84 |
from:1 to:(self size)
|
|
85 |
compressTabs:false
|
|
86 |
final:Character cr
|
87
|
87 |
!
|
|
88 |
|
608
|
89 |
asStringCollection
|
|
90 |
"return the receiver as a stringCollection - thats easy"
|
|
91 |
|
|
92 |
^ self
|
|
93 |
!
|
|
94 |
|
1
|
95 |
from:aString
|
|
96 |
"setup my contents from the argument, aString"
|
|
97 |
|
|
98 |
|numberOfLines "{ Class:SmallInteger }"
|
|
99 |
start "{ Class:SmallInteger }"
|
|
100 |
stop "{ Class:SmallInteger }" |
|
|
101 |
|
|
102 |
numberOfLines := aString occurrencesOf:(Character cr).
|
203
|
103 |
(aString endsWith:(Character cr)) ifFalse:[
|
|
104 |
numberOfLines := numberOfLines + 1.
|
|
105 |
].
|
1
|
106 |
self grow:numberOfLines.
|
|
107 |
start := 1.
|
|
108 |
1 to:numberOfLines do:[:lineNr |
|
160
|
109 |
stop := aString indexOf:(Character cr) startingAt:start.
|
|
110 |
stop == 0 ifTrue:[
|
|
111 |
stop := aString size
|
|
112 |
] ifFalse: [
|
|
113 |
stop := stop - 1.
|
|
114 |
].
|
10
|
115 |
|
160
|
116 |
(stop < start) ifTrue: [
|
|
117 |
self at:lineNr put:(String new:0)
|
|
118 |
] ifFalse: [
|
|
119 |
self at:lineNr put:(aString copyFrom:start to:stop)
|
|
120 |
].
|
|
121 |
start := stop + 2
|
1
|
122 |
]
|
968
|
123 |
!
|
|
124 |
|
|
125 |
withTabsExpanded
|
|
126 |
^ self collect:[:string|
|
|
127 |
string notNil ifTrue:[
|
|
128 |
string withTabsExpanded
|
|
129 |
] ifFalse:[
|
|
130 |
string
|
|
131 |
]
|
|
132 |
]
|
|
133 |
|
|
134 |
"
|
|
135 |
|tab|
|
|
136 |
tab := String with:Character tab.
|
|
137 |
('abcd', tab, 'tef', tab, 'tgh') asStringCollection withTabsExpanded
|
|
138 |
"
|
|
139 |
|
|
140 |
"Created: 12.2.1996 / 22:25:56 / stefan"
|
|
141 |
"Modified: 14.2.1996 / 11:13:01 / stefan"
|
|
142 |
! !
|
|
143 |
|
|
144 |
!StringCollection methodsFor:'copying'!
|
|
145 |
|
|
146 |
copyEmpty:size
|
|
147 |
"we have to redefine this, since 'self class new:size' does allocate size nil lines.
|
|
148 |
In order to get collect working, we have to undo this allocation"
|
|
149 |
|
|
150 |
^ (super copyEmpty:size) grow:0.
|
|
151 |
|
|
152 |
"Created: 14.2.1996 / 11:05:47 / stefan"
|
608
|
153 |
! !
|
|
154 |
|
246
|
155 |
!StringCollection methodsFor:'printing'!
|
1
|
156 |
|
|
157 |
printString
|
284
|
158 |
"return the receivers printString"
|
|
159 |
|
1
|
160 |
^ self asString
|
|
161 |
! !
|
|
162 |
|
246
|
163 |
!StringCollection methodsFor:'searching'!
|
1
|
164 |
|
|
165 |
indexOfLineStartingWith:aString
|
|
166 |
"return the index of the first line starting with the argument, aString"
|
|
167 |
|
|
168 |
|index "{ Class:SmallInteger }" |
|
|
169 |
|
|
170 |
index := 1.
|
|
171 |
[index <= self size] whileTrue:[
|
160
|
172 |
((self at:index) startsWith:aString) ifTrue:[
|
|
173 |
^ index
|
|
174 |
].
|
|
175 |
index := index + 1
|
1
|
176 |
].
|
|
177 |
^ 0
|
|
178 |
! !
|
608
|
179 |
|
633
|
180 |
!StringCollection class methodsFor:'documentation'!
|
|
181 |
|
|
182 |
version
|
968
|
183 |
^ '$Header: /cvs/stx/stx/libbasic/StringCollection.st,v 1.22 1996-02-14 11:12:28 stefan Exp $'
|
633
|
184 |
! !
|