author | Stefan Vogel <sv@exept.de> |
Tue, 03 Jul 2018 23:38:44 +0200 | |
changeset 23189 | 15ea23cfead5 |
parent 22573 | 0749ebab7a10 |
child 23336 | e1bfc3a32918 |
permissions | -rw-r--r-- |
22573 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
5 | 4 |
COPYRIGHT (c) 1989 by Claus Gittinger |
160 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
6561 | 14 |
"{ Package: 'stx:libbasic' }" |
6176 | 15 |
|
19466 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
647 | 18 |
OrderedCollection subclass:#StringCollection |
968 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Collections-Text' |
|
1 | 23 |
! |
24 |
||
246 | 25 |
!StringCollection class methodsFor:'documentation'! |
89 | 26 |
|
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
160 | 30 |
All Rights Reserved |
1 | 31 |
|
89 | 32 |
This software is furnished under a license and may be used |
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
1 | 40 |
|
89 | 41 |
documentation |
42 |
" |
|
20479 | 43 |
StringCollection is a variable sized array of lines which are strings. |
160 | 44 |
WARNING: |
1290 | 45 |
This class is temporary (a historic leftover) - it may change or |
46 |
even vanish in the future. Use OrderedCollections or other standard |
|
47 |
classes to represent collections of strings. |
|
89 | 48 |
|
262 | 49 |
StringCollection used to be called Text, but this is a very bad name |
50 |
- there is something totally different also named Text in ST-80 ... |
|
1290 | 51 |
|
52 |
[author:] |
|
53 |
Claus Gittinger |
|
89 | 54 |
" |
55 |
! ! |
|
1 | 56 |
|
246 | 57 |
!StringCollection class methodsFor:'instance creation'! |
1 | 58 |
|
59 |
from:aString |
|
11596 | 60 |
<resource: #obsolete> |
1 | 61 |
"return a new text object with lines taken from the argument, aString" |
62 |
||
8375
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
63 |
self obsoleteMethodWarning:'use #fromString:'. |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
64 |
^ self fromString:aString. |
1 | 65 |
! |
66 |
||
67 |
fromArray:anArray |
|
68 |
"return a new text object with lines taken from the argument, an array |
|
69 |
of strings" |
|
70 |
||
246 | 71 |
|newStringCollection |
1 | 72 |
size "{ Class: SmallInteger }" | |
73 |
||
74 |
size := anArray size. |
|
246 | 75 |
newStringCollection := self new:size. |
1 | 76 |
1 to:size do:[:line | |
246 | 77 |
newStringCollection at:line put:(anArray at:line) |
1 | 78 |
]. |
246 | 79 |
^ newStringCollection |
731 | 80 |
! |
81 |
||
8375
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
82 |
fromString:aString |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
83 |
"return a new text object with lines taken from the argument, aString" |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
84 |
|
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
85 |
^ (self new:1) fromString:aString |
14245 | 86 |
|
87 |
"Modified: / 25-07-2012 / 18:52:47 / cg" |
|
8375
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
88 |
! |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
89 |
|
731 | 90 |
new:size |
91 |
"return a new string collection with size empty lines" |
|
92 |
||
93 |
^ (super new:size) grow:size |
|
16712 | 94 |
|
95 |
" |
|
96 |
StringCollection new:10 |
|
97 |
" |
|
9161 | 98 |
! |
99 |
||
22278 | 100 |
newWithCapacity:size |
101 |
"return a new empty string collection with size capacity" |
|
102 |
||
103 |
^ super new:size. |
|
104 |
||
105 |
" |
|
106 |
StringCollection newWithCapacity:10 |
|
107 |
" |
|
108 |
||
109 |
"Created: / 21-09-2017 / 15:37:49 / stefan" |
|
110 |
! |
|
111 |
||
16712 | 112 |
newWithSize:size |
9161 | 113 |
"return a new string collection with size empty lines" |
114 |
||
16712 | 115 |
^ self new:size "/ already does what we want |
1 | 116 |
! ! |
117 |
||
246 | 118 |
!StringCollection methodsFor:'converting'! |
1 | 119 |
|
120 |
asString |
|
121 |
"return myself as a string with embedded cr's" |
|
122 |
||
22278 | 123 |
^ [ |
124 |
self |
|
125 |
asStringWith:Character cr |
|
126 |
from:1 to:(self size) |
|
127 |
compressTabs:false |
|
128 |
final:Character cr |
|
129 |
] on:ConversionError do:[:ex| |
|
130 |
"non-string elements trigger a ComversionError. Fall back..." |
|
131 |
super printString. |
|
132 |
]. |
|
133 |
||
134 |
" |
|
135 |
#('This' 'is' 'some' 'text') asStringCollection asString |
|
136 |
#('This' 22 'some' 'text') asStringCollection asString |
|
137 |
" |
|
138 |
||
139 |
"Modified (format): / 21-09-2017 / 12:52:00 / stefan" |
|
87 | 140 |
! |
141 |
||
608 | 142 |
asStringCollection |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
143 |
"return the receiver as a stringCollection - that's easy" |
608 | 144 |
|
145 |
^ self |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
146 |
|
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
147 |
"Modified (comment): / 25-07-2012 / 18:11:14 / cg" |
608 | 148 |
! |
149 |
||
3601
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
150 |
asStringWithoutEmphasis |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
151 |
"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
|
152 |
|
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
153 |
^ self |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
154 |
asStringWith:Character cr |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
155 |
from:1 to:(self size) |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
156 |
compressTabs:false |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
157 |
final:Character cr |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
158 |
withEmphasis:false |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
159 |
|
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
160 |
"Created: / 17.6.1998 / 12:32:48 / cg" |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
161 |
! |
c879b9a15f38
added #asStringWithoutEmphasis
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
162 |
|
7829 | 163 |
asStringWithoutFinalCR |
164 |
"return myself as a string with embedded cr's |
|
165 |
but do not add a final CR" |
|
166 |
||
167 |
^ self |
|
168 |
asStringWith:Character cr |
|
169 |
from:1 to:(self size) |
|
170 |
compressTabs:false |
|
171 |
final:nil |
|
172 |
! |
|
173 |
||
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
174 |
encodeFrom:oldEncoding into:newEncoding |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
175 |
"encode each line" |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
176 |
|
7916 | 177 |
|enc| |
178 |
||
179 |
enc := CharacterEncoder encoderToEncodeFrom:oldEncoding into:newEncoding. |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
180 |
^ self |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
181 |
collect:[:line | |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
182 |
line isNil |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
183 |
ifTrue:[ nil ] |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
184 |
ifFalse:[ enc encodeString:line] |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
185 |
] |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
186 |
|
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
187 |
"Modified (comment): / 25-07-2012 / 18:09:40 / cg" |
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
188 |
! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
189 |
|
1 | 190 |
from:aString |
22573 | 191 |
"setup my contents from the argument, aString. |
192 |
Obsolete, as it conflicts with the from:index |
|
193 |
message, as inherited from SeqCollection. |
|
194 |
For a migration time, check which interface is wanted here, |
|
195 |
and issue an obsoleteMessageWarning as required." |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
196 |
|
11596 | 197 |
<resource: #obsolete> |
1 | 198 |
|
22573 | 199 |
aString isInteger ifTrue:[ |
200 |
^ super from:aString |
|
201 |
]. |
|
8375
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
202 |
self obsoleteMethodWarning:'use #fromString:'. |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
203 |
^ self fromString:aString |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
204 |
|
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
205 |
"Modified (format): / 25-07-2012 / 18:02:34 / cg" |
8375
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
206 |
! |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
207 |
|
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
208 |
fromString:aString |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
209 |
"setup my contents from the argument, aString. |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
210 |
AString should be delimited by newline characters" |
8375
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
211 |
|
1 | 212 |
|numberOfLines "{ Class:SmallInteger }" |
213 |
start "{ Class:SmallInteger }" |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
214 |
stop "{ Class:SmallInteger }" |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
215 |
delimiter |
6176 | 216 |
| |
1 | 217 |
|
14245 | 218 |
aString isEmpty ifTrue:[self grow:0. ^ self ]. |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
219 |
|
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
220 |
delimiter := Character cr. |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
221 |
|
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
222 |
numberOfLines := aString occurrencesOf:delimiter. |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
223 |
numberOfLines == 0 ifTrue:[ |
21439 | 224 |
"/ check if it's a return-delimited string |
22354 | 225 |
numberOfLines := aString occurrencesOf:(Character return). |
226 |
numberOfLines > 0 ifTrue:[ |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
227 |
delimiter := Character return. |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
228 |
]. |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
229 |
]. |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
230 |
aString last == delimiter ifFalse:[ |
6176 | 231 |
numberOfLines := numberOfLines + 1. |
203 | 232 |
]. |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
233 |
|
1 | 234 |
self grow:numberOfLines. |
6176 | 235 |
|
1 | 236 |
start := 1. |
237 |
1 to:numberOfLines do:[:lineNr | |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
238 |
stop := aString indexOf:delimiter startingAt:start. |
6176 | 239 |
stop == 0 ifTrue:[ |
240 |
self at:lineNr put:(aString copyFrom:start). |
|
241 |
self from:lineNr+1 to:numberOfLines put:''. |
|
22354 | 242 |
^ self |
6176 | 243 |
]. |
10 | 244 |
|
6176 | 245 |
self at:lineNr put:(aString copyFrom:start to:stop-1). |
246 |
start := stop + 1 |
|
1 | 247 |
] |
6176 | 248 |
|
14245 | 249 |
"Modified: / 25-07-2012 / 18:52:58 / cg" |
21439 | 250 |
"Modified (format): / 13-02-2017 / 20:31:40 / cg" |
22354 | 251 |
"Modified (format): / 14-11-2017 / 16:26:27 / mawalch" |
968 | 252 |
! ! |
253 |
||
254 |
!StringCollection methodsFor:'copying'! |
|
255 |
||
256 |
copyEmpty:size |
|
14111 | 257 |
"we have to redefine this, since 'self class new:size' |
258 |
does allocate size nil lines (it does an implicit #grow: size). |
|
259 |
In order to get #collect: working, we should not perform this implicit grow" |
|
968 | 260 |
|
22278 | 261 |
^ self class newWithCapacity:size. |
968 | 262 |
|
22278 | 263 |
"Created: / 14-02-1996 / 11:05:47 / stefan" |
264 |
"Modified: / 21-09-2017 / 15:43:49 / stefan" |
|
10452 | 265 |
! |
266 |
||
14111 | 267 |
copyEmptyAndGrow:size |
268 |
"performance optimization: |
|
22278 | 269 |
StringCollections are always grown to size after #new: (containing nil elements)" |
270 |
||
271 |
^ self species new:size. |
|
14111 | 272 |
|
22278 | 273 |
" |
274 |
#('some' 'text' 'bla') asStringCollection copyEmptyAndGrow:20 |
|
275 |
" |
|
276 |
||
277 |
"Modified: / 21-09-2017 / 15:46:18 / stefan" |
|
14111 | 278 |
! |
279 |
||
19873 | 280 |
withoutLeadingAndTrailingBlankLines |
281 |
"return a copy of the receiver with leading and trailing blank lines removed. |
|
282 |
If there are no trailing blank lines, the original receiver is returned. |
|
283 |
If all lines are blank, an empty string collection is returned." |
|
284 |
||
285 |
^ self withoutLeadingBlankLines withoutTrailingBlankLines. |
|
286 |
||
287 |
" |
|
288 |
'1 |
|
289 |
2 |
|
290 |
3' asStringCollection withoutLeadingAndTrailingBlankLines |
|
291 |
" |
|
292 |
||
293 |
" |
|
294 |
' |
|
295 |
||
296 |
2 |
|
297 |
3 |
|
298 |
||
299 |
' asStringCollection withoutLeadingAndTrailingBlankLines |
|
300 |
" |
|
301 |
! |
|
302 |
||
10452 | 303 |
withoutLeadingBlankLines |
304 |
"return a copy of the receiver with leading blank lines removed. |
|
305 |
If there are no leading blank lines, the original receiver is returned. |
|
306 |
If all lines are blank, an empty string collection is returned." |
|
307 |
||
308 |
|indexOfFirstNonBlankLine| |
|
309 |
||
11702 | 310 |
indexOfFirstNonBlankLine := self findFirst:[:line | line notEmptyOrNil and:[line isBlank not]]. |
311 |
indexOfFirstNonBlankLine == 0 ifTrue:[ ^ self copyEmpty ]. |
|
312 |
indexOfFirstNonBlankLine == 1 ifTrue:[ ^ self ]. |
|
313 |
^ self copyFrom:indexOfFirstNonBlankLine |
|
10452 | 314 |
|
315 |
" |
|
316 |
'1 |
|
317 |
2 |
|
318 |
3' asStringCollection withoutLeadingBlankLines |
|
319 |
" |
|
320 |
||
321 |
" |
|
322 |
' |
|
323 |
2 |
|
324 |
3' asStringCollection withoutLeadingBlankLines |
|
325 |
" |
|
326 |
||
327 |
" |
|
328 |
' |
|
329 |
||
11702 | 330 |
' asStringCollection withoutLeadingBlankLines |
331 |
" |
|
332 |
! |
|
333 |
||
334 |
withoutTrailingBlankLines |
|
335 |
"return a copy of the receiver with trailing blank lines removed. |
|
336 |
If there are no trailing blank lines, the original receiver is returned. |
|
337 |
If all lines are blank, an empty string collection is returned." |
|
338 |
||
339 |
|indexOfLastNonBlankLine| |
|
340 |
||
341 |
indexOfLastNonBlankLine := self findLast:[:line | line notEmptyOrNil and:[line isBlank not]]. |
|
342 |
indexOfLastNonBlankLine == 0 ifTrue:[ ^ self copyEmpty ]. |
|
343 |
indexOfLastNonBlankLine == self size ifTrue:[ ^ self ]. |
|
344 |
^ self copyTo:indexOfLastNonBlankLine |
|
345 |
||
346 |
" |
|
347 |
'1 |
|
348 |
2 |
|
17083 | 349 |
3' asStringCollection withoutTrailingBlankLines |
11702 | 350 |
" |
351 |
||
352 |
" |
|
353 |
' |
|
354 |
2 |
|
355 |
3 |
|
356 |
' asStringCollection withoutTrailingBlankLines |
|
357 |
" |
|
358 |
||
359 |
" |
|
360 |
' |
|
17083 | 361 |
2 |
362 |
3 |
|
363 |
||
364 |
||
365 |
' asStringCollection withoutTrailingBlankLines |
|
366 |
" |
|
367 |
" |
|
368 |
' |
|
11702 | 369 |
|
370 |
' asStringCollection withoutTrailingBlankLines |
|
10452 | 371 |
" |
608 | 372 |
! ! |
373 |
||
22278 | 374 |
!StringCollection methodsFor:'enumerating'! |
375 |
||
376 |
collect:aBlock |
|
377 |
"evaluate the argument, aBlock for every element in the collection |
|
378 |
and return a collection of the results. |
|
379 |
||
380 |
Redefined, to change the result collection to an OrderedCollection if |
|
381 |
not all elements are valid for a StringCollection (must be string or nil)." |
|
382 |
||
383 |
|newCollection newElement onlyStrings |
|
384 |
start "{ Class:SmallInteger }" |
|
385 |
stop "{ Class:SmallInteger }" | |
|
386 |
||
387 |
onlyStrings := true. "/ ... until proven otherwise |
|
388 |
newCollection := self class newWithCapacity:(self size). |
|
389 |
stop := lastIndex. |
|
390 |
start := firstIndex. |
|
391 |
start to:stop do:[:index | |
|
392 |
newElement := aBlock value:(contentsArray at:index). |
|
393 |
(onlyStrings and:[newElement notNil and:[newElement isString not]]) ifTrue:[ |
|
394 |
onlyStrings := false. |
|
395 |
newCollection := newCollection asOrderedCollection. |
|
396 |
]. |
|
397 |
newCollection add:newElement. |
|
398 |
]. |
|
399 |
^ newCollection |
|
400 |
||
401 |
" |
|
402 |
#('this' 'is' 'some' nil 'text') asStringCollection collect:[:i | i] |
|
403 |
#('this' true 'some' nil 'text') asStringCollection collect:[:i | i ] |
|
404 |
" |
|
405 |
||
406 |
"Created: / 21-09-2017 / 14:31:31 / stefan" |
|
407 |
"Modified: / 21-09-2017 / 15:55:21 / stefan" |
|
408 |
! |
|
409 |
||
410 |
collect:collectBlock thenSelect:selectBlock |
|
411 |
"combination of collect followed by select; |
|
412 |
redefined to avoid the creation of an intermediate (garbage) collection. |
|
413 |
||
414 |
Redefined, to change the result collection to an OrderedCollection if |
|
415 |
not all elements are valid for a StringCollection (must be string or nil)." |
|
416 |
||
417 |
|newCollection newElement onlyStrings |
|
418 |
start "{ Class:SmallInteger }" |
|
419 |
stop "{ Class:SmallInteger }" | |
|
420 |
||
421 |
onlyStrings := true. "/ ... until proven otherwise |
|
422 |
newCollection := self class newWithCapacity:(self size). |
|
423 |
stop := lastIndex. |
|
424 |
start := firstIndex. |
|
425 |
start to:stop do:[:index | |
|
426 |
newElement := collectBlock value:(contentsArray at:index). |
|
427 |
(selectBlock value:newElement) ifTrue:[ |
|
428 |
(onlyStrings and:[newElement notNil and:[newElement isString not]]) ifTrue:[ |
|
429 |
onlyStrings := false. |
|
430 |
newCollection := newCollection asOrderedCollection. |
|
431 |
]. |
|
432 |
newCollection add:newElement. |
|
433 |
] |
|
434 |
]. |
|
435 |
^ newCollection |
|
436 |
||
437 |
" |
|
438 |
#('this' 'is' 'some' nil 'text') asStringCollection collect:[:i | i] thenSelect:[:e| e notNil] |
|
439 |
#('this' true 'some' nil 'text') asStringCollection collect:[:i | i ] thenSelect:[:e| e notNil] |
|
440 |
" |
|
441 |
||
442 |
"Created: / 21-09-2017 / 14:34:46 / stefan" |
|
443 |
"Modified: / 21-09-2017 / 15:55:05 / stefan" |
|
444 |
! |
|
445 |
||
446 |
select:selectBlock thenCollect:collectBlock |
|
447 |
"combination of select followed by collect; |
|
448 |
redefined to avoid the creation of an intermediate (garbage) collection. |
|
449 |
||
450 |
Redefined, to change the result collection to an OrderedCollection if |
|
451 |
not all elements are valid for a StringCollection (must be string or nil)." |
|
452 |
||
453 |
|newCollection element newElement onlyStrings |
|
454 |
start "{ Class:SmallInteger }" |
|
455 |
stop "{ Class:SmallInteger }" | |
|
456 |
||
457 |
onlyStrings := true. "/ ... until proven otherwise |
|
458 |
newCollection := self class newWithCapacity:(self size). |
|
459 |
stop := lastIndex. |
|
460 |
start := firstIndex. |
|
461 |
start to:stop do:[:index | |
|
462 |
element := contentsArray at:index. |
|
463 |
(selectBlock value:element) ifTrue:[ |
|
464 |
newElement := collectBlock value:element. |
|
465 |
(onlyStrings and:[newElement notNil and:[newElement isString not]]) ifTrue:[ |
|
466 |
onlyStrings := false. |
|
467 |
newCollection := newCollection asOrderedCollection. |
|
468 |
]. |
|
469 |
newCollection add:newElement. |
|
470 |
] |
|
471 |
]. |
|
472 |
^ newCollection |
|
473 |
||
474 |
" |
|
475 |
#('this' 'is' 'some' nil 'text') asStringCollection select:[:e| e notNil] thenCollect:[:i | i] |
|
476 |
#('this' true 'some' nil 'text') asStringCollection select:[:e| e notNil] thenCollect:[:i | i] |
|
477 |
" |
|
478 |
||
479 |
"Created: / 21-09-2017 / 14:42:20 / stefan" |
|
480 |
"Modified: / 21-09-2017 / 15:54:33 / stefan" |
|
481 |
! ! |
|
482 |
||
14111 | 483 |
|
5251 | 484 |
!StringCollection methodsFor:'printing & storing'! |
1 | 485 |
|
10909
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
486 |
printOn:aStream |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
487 |
"print myself on aStream with embedded cr's" |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
488 |
|
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
489 |
self do:[:eachString| |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
490 |
eachString printOn:aStream. |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
491 |
aStream cr. |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
492 |
]. |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
493 |
! |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
494 |
|
1 | 495 |
printString |
19466 | 496 |
"return the receiver's printString" |
284 | 497 |
|
1 | 498 |
^ self asString |
499 |
! ! |
|
500 |
||
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
501 |
!StringCollection methodsFor:'queries'! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
502 |
|
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
503 |
encoding |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
504 |
"the encoding; ask my first line" |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
505 |
|
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
506 |
self do:[:l | l notNil ifTrue:[^ l encoding]]. |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
507 |
"/ sigh |
14173 | 508 |
^ #unicode |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
509 |
|
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
510 |
"Modified (comment): / 25-07-2012 / 18:09:15 / cg" |
13331 | 511 |
! |
512 |
||
513 |
stringSize |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
514 |
"the size of the string if I was converted" |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
515 |
|
13331 | 516 |
^ self inject:0 into:[:sizeSoFar :eachLine | |
517 |
sizeSoFar + eachLine size + 1 |
|
518 |
] |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
519 |
|
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
520 |
"Modified (comment): / 25-07-2012 / 18:02:19 / cg" |
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
521 |
! ! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
522 |
|
246 | 523 |
!StringCollection methodsFor:'searching'! |
1 | 524 |
|
525 |
indexOfLineStartingWith:aString |
|
526 |
"return the index of the first line starting with the argument, aString" |
|
527 |
||
16460 | 528 |
^ self findFirst:[:l | l notNil and:[l startsWith:aString]] ifNone:0. |
1 | 529 |
|
16460 | 530 |
"/ |sz "{ Class:SmallInteger }" |
531 |
"/ l| |
|
532 |
"/ |
|
533 |
"/ sz := self size. |
|
534 |
"/ 1 to:sz do:[:index | |
|
535 |
"/ l := self at:index. |
|
536 |
"/ (l notNil and:[l startsWith:aString]) ifTrue:[ |
|
537 |
"/ ^ index |
|
538 |
"/ ]. |
|
539 |
"/ ]. |
|
540 |
"/ ^ 0 |
|
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
541 |
|
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
542 |
"Modified: 24.2.1996 / 19:08:47 / cg" |
1 | 543 |
! ! |
608 | 544 |
|
1064 | 545 |
!StringCollection methodsFor:'special converting'! |
546 |
||
14204 | 547 |
decodeFrom: encodingSymbol |
548 |
"given the receiver encoded as described by encodingSymbol, |
|
549 |
convert it into internal ST/X (unicode) encoding and return a |
|
550 |
corresponding StringCollection." |
|
551 |
||
552 |
|myEncoding encoder| |
|
553 |
||
554 |
encodingSymbol isNil ifTrue:[^ self]. |
|
555 |
||
556 |
encoder := CharacterEncoder encoderToEncodeFrom:#unicode into:encodingSymbol. |
|
557 |
encoder isNil ifTrue:[^ self]. |
|
558 |
||
559 |
^ self |
|
560 |
collect:[:eachLineOrNil | |
|
561 |
eachLineOrNil isNil ifTrue:[ |
|
562 |
eachLineOrNil |
|
563 |
] ifFalse:[ |
|
564 |
encoder decodeString: eachLineOrNil |
|
565 |
] |
|
566 |
] |
|
567 |
||
568 |
"Created: / 12-07-2012 / 14:30:25 / cg" |
|
569 |
! |
|
570 |
||
1064 | 571 |
withTabs |
19466 | 572 |
"return a new stringCollection consisting of the receiver's lines, |
1064 | 573 |
where leading spaces are replaced by tabulator characters (assuming 8-col tabs). |
574 |
Notice: lines which do not contain leading spaces, are copied by reference to the |
|
575 |
new stringCollection (i.e. shared); |
|
576 |
otherwise new strings is created. |
|
577 |
Limitation: only the very first spaces are replaced" |
|
578 |
||
579 |
^ self collect:[:string| |
|
580 |
string notNil ifTrue:[ |
|
581 |
string withTabs |
|
582 |
] ifFalse:[ |
|
583 |
string |
|
584 |
] |
|
585 |
] |
|
586 |
||
587 |
" |
|
588 |
(' abcd ') asStringCollection withTabs |
|
589 |
" |
|
590 |
||
591 |
"Created: 4.3.1996 / 17:09:07 / cg" |
|
592 |
"Modified: 4.3.1996 / 17:10:37 / cg" |
|
593 |
! |
|
594 |
||
595 |
withTabsExpanded |
|
19466 | 596 |
"return a new stringCollection consisting of the receiver's lines, |
1064 | 597 |
where tabs are replaced by space characters (assuming 8-col tabs). |
598 |
Notice: lines which do not contain any tab, are copied by reference to the |
|
599 |
new stringCollection (i.e. shared); |
|
600 |
otherwise new strings is created." |
|
601 |
||
15752 | 602 |
^ self withTabsExpanded:8 |
603 |
||
604 |
" |
|
605 |
|tab| |
|
606 |
tab := String with:Character tab. |
|
607 |
('abcd', tab, 'tef', tab, 'tgh') asStringCollection withTabsExpanded |
|
608 |
" |
|
609 |
||
610 |
"Created: 12.2.1996 / 22:25:56 / stefan" |
|
611 |
"Modified: 14.2.1996 / 11:13:01 / stefan" |
|
612 |
"Modified: 4.3.1996 / 17:10:22 / cg" |
|
613 |
! |
|
614 |
||
615 |
withTabsExpanded:n |
|
19466 | 616 |
"return a new stringCollection consisting of the receiver's lines, |
15753 | 617 |
where tabs are replaced by space characters (assuming n-col tabs). |
15752 | 618 |
Notice: lines which do not contain any tab, are copied by reference to the |
619 |
new stringCollection (i.e. shared); |
|
620 |
otherwise new strings is created." |
|
621 |
||
1064 | 622 |
^ self collect:[:string| |
623 |
string notNil ifTrue:[ |
|
15752 | 624 |
string withTabsExpanded:n |
1064 | 625 |
] ifFalse:[ |
626 |
string |
|
627 |
] |
|
628 |
] |
|
629 |
||
630 |
" |
|
631 |
|tab| |
|
632 |
tab := String with:Character tab. |
|
633 |
('abcd', tab, 'tef', tab, 'tgh') asStringCollection withTabsExpanded |
|
634 |
" |
|
635 |
||
636 |
"Created: 12.2.1996 / 22:25:56 / stefan" |
|
637 |
"Modified: 14.2.1996 / 11:13:01 / stefan" |
|
638 |
"Modified: 4.3.1996 / 17:10:22 / cg" |
|
639 |
! ! |
|
640 |
||
4007 | 641 |
!StringCollection methodsFor:'testing'! |
642 |
||
643 |
isStringCollection |
|
644 |
"return true, if the receiver is some kind of stringCollection; |
|
645 |
true is returned here - the method is redefined from Object." |
|
646 |
||
647 |
^ true |
|
648 |
||
649 |
||
650 |
! ! |
|
651 |
||
633 | 652 |
!StringCollection class methodsFor:'documentation'! |
653 |
||
654 |
version |
|
19466 | 655 |
^ '$Header$' |
13331 | 656 |
! |
657 |
||
658 |
version_CVS |
|
19466 | 659 |
^ '$Header$' |
633 | 660 |
! ! |
15752 | 661 |