author | Claus Gittinger <cg@exept.de> |
Tue, 09 Jul 2019 20:55:17 +0200 | |
changeset 24417 | 03b083548da2 |
parent 23515 | 021c8bca5ab8 |
child 24798 | ba93f73dbc93 |
permissions | -rw-r--r-- |
23515 | 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 |
||
7829 | 150 |
asStringWithoutFinalCR |
151 |
"return myself as a string with embedded cr's |
|
152 |
but do not add a final CR" |
|
153 |
||
154 |
^ self |
|
155 |
asStringWith:Character cr |
|
156 |
from:1 to:(self size) |
|
157 |
compressTabs:false |
|
158 |
final:nil |
|
159 |
! |
|
160 |
||
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
161 |
encodeFrom:oldEncoding into:newEncoding |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
162 |
"encode each line" |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
163 |
|
7916 | 164 |
|enc| |
165 |
||
166 |
enc := CharacterEncoder encoderToEncodeFrom:oldEncoding into:newEncoding. |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
167 |
^ self |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
168 |
collect:[:line | |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
169 |
line isNil |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
170 |
ifTrue:[ nil ] |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
171 |
ifFalse:[ enc encodeString:line] |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
172 |
] |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
173 |
|
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
174 |
"Modified (comment): / 25-07-2012 / 18:09:40 / cg" |
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
175 |
! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
176 |
|
1 | 177 |
from:aString |
22573 | 178 |
"setup my contents from the argument, aString. |
179 |
Obsolete, as it conflicts with the from:index |
|
180 |
message, as inherited from SeqCollection. |
|
181 |
For a migration time, check which interface is wanted here, |
|
182 |
and issue an obsoleteMessageWarning as required." |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
183 |
|
11596 | 184 |
<resource: #obsolete> |
1 | 185 |
|
22573 | 186 |
aString isInteger ifTrue:[ |
187 |
^ super from:aString |
|
188 |
]. |
|
8375
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
189 |
self obsoleteMethodWarning:'use #fromString:'. |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
190 |
^ self fromString:aString |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
191 |
|
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
192 |
"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
|
193 |
! |
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
194 |
|
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
195 |
fromString:aString |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
196 |
"setup my contents from the argument, aString. |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
197 |
AString should be delimited by newline characters" |
8375
b43b5d1c494d
from: is obsoleteed by fromString:
Claus Gittinger <cg@exept.de>
parents:
7982
diff
changeset
|
198 |
|
1 | 199 |
|numberOfLines "{ Class:SmallInteger }" |
200 |
start "{ Class:SmallInteger }" |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
201 |
stop "{ Class:SmallInteger }" |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
202 |
delimiter |
6176 | 203 |
| |
1 | 204 |
|
14245 | 205 |
aString isEmpty ifTrue:[self grow:0. ^ self ]. |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
206 |
|
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
207 |
delimiter := Character cr. |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
208 |
|
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
209 |
numberOfLines := aString occurrencesOf:delimiter. |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
210 |
numberOfLines == 0 ifTrue:[ |
21439 | 211 |
"/ check if it's a return-delimited string |
22354 | 212 |
numberOfLines := aString occurrencesOf:(Character return). |
213 |
numberOfLines > 0 ifTrue:[ |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
214 |
delimiter := Character return. |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
215 |
]. |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
216 |
]. |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
217 |
aString last == delimiter ifFalse:[ |
6176 | 218 |
numberOfLines := numberOfLines + 1. |
203 | 219 |
]. |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
220 |
|
1 | 221 |
self grow:numberOfLines. |
6176 | 222 |
|
1 | 223 |
start := 1. |
224 |
1 to:numberOfLines do:[:lineNr | |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
225 |
stop := aString indexOf:delimiter startingAt:start. |
6176 | 226 |
stop == 0 ifTrue:[ |
227 |
self at:lineNr put:(aString copyFrom:start). |
|
228 |
self from:lineNr+1 to:numberOfLines put:''. |
|
22354 | 229 |
^ self |
6176 | 230 |
]. |
10 | 231 |
|
6176 | 232 |
self at:lineNr put:(aString copyFrom:start to:stop-1). |
233 |
start := stop + 1 |
|
1 | 234 |
] |
6176 | 235 |
|
14245 | 236 |
"Modified: / 25-07-2012 / 18:52:58 / cg" |
21439 | 237 |
"Modified (format): / 13-02-2017 / 20:31:40 / cg" |
22354 | 238 |
"Modified (format): / 14-11-2017 / 16:26:27 / mawalch" |
968 | 239 |
! ! |
240 |
||
241 |
!StringCollection methodsFor:'copying'! |
|
242 |
||
243 |
copyEmpty:size |
|
14111 | 244 |
"we have to redefine this, since 'self class new:size' |
245 |
does allocate size nil lines (it does an implicit #grow: size). |
|
246 |
In order to get #collect: working, we should not perform this implicit grow" |
|
968 | 247 |
|
22278 | 248 |
^ self class newWithCapacity:size. |
968 | 249 |
|
22278 | 250 |
"Created: / 14-02-1996 / 11:05:47 / stefan" |
251 |
"Modified: / 21-09-2017 / 15:43:49 / stefan" |
|
10452 | 252 |
! |
253 |
||
14111 | 254 |
copyEmptyAndGrow:size |
255 |
"performance optimization: |
|
22278 | 256 |
StringCollections are always grown to size after #new: (containing nil elements)" |
257 |
||
258 |
^ self species new:size. |
|
14111 | 259 |
|
22278 | 260 |
" |
261 |
#('some' 'text' 'bla') asStringCollection copyEmptyAndGrow:20 |
|
262 |
" |
|
263 |
||
264 |
"Modified: / 21-09-2017 / 15:46:18 / stefan" |
|
14111 | 265 |
! |
266 |
||
23515 | 267 |
removeTrailingBlankLines |
268 |
"destructive removal of trailing blank lines" |
|
269 |
||
270 |
|indexOfLastNonBlankLine| |
|
271 |
||
272 |
indexOfLastNonBlankLine := self findLast:[:line | line notNil and:[line isBlank not]]. |
|
273 |
indexOfLastNonBlankLine == self size ifFalse:[ |
|
274 |
self removeFromIndex:indexOfLastNonBlankLine+1 |
|
275 |
]. |
|
276 |
||
277 |
" |
|
278 |
'1 |
|
279 |
2 |
|
280 |
3' asStringCollection removeTrailingBlankLines |
|
281 |
" |
|
282 |
||
283 |
" |
|
284 |
' |
|
285 |
2 |
|
286 |
3 |
|
287 |
' asStringCollection removeTrailingBlankLines |
|
288 |
" |
|
289 |
||
290 |
" |
|
291 |
' |
|
292 |
2 |
|
293 |
3 |
|
294 |
||
295 |
||
296 |
' asStringCollection removeTrailingBlankLines |
|
297 |
" |
|
298 |
" |
|
299 |
' |
|
300 |
||
301 |
' asStringCollection removeTrailingBlankLines |
|
302 |
" |
|
303 |
||
304 |
"Created: / 07-11-2018 / 08:20:39 / Claus Gittinger" |
|
305 |
! |
|
306 |
||
19873 | 307 |
withoutLeadingAndTrailingBlankLines |
308 |
"return a copy of the receiver with leading and trailing blank lines removed. |
|
309 |
If there are no trailing blank lines, the original receiver is returned. |
|
310 |
If all lines are blank, an empty string collection is returned." |
|
311 |
||
312 |
^ self withoutLeadingBlankLines withoutTrailingBlankLines. |
|
313 |
||
314 |
" |
|
315 |
'1 |
|
316 |
2 |
|
317 |
3' asStringCollection withoutLeadingAndTrailingBlankLines |
|
318 |
" |
|
319 |
||
320 |
" |
|
321 |
' |
|
322 |
||
323 |
2 |
|
324 |
3 |
|
325 |
||
326 |
' asStringCollection withoutLeadingAndTrailingBlankLines |
|
327 |
" |
|
328 |
! |
|
329 |
||
10452 | 330 |
withoutLeadingBlankLines |
331 |
"return a copy of the receiver with leading blank lines removed. |
|
332 |
If there are no leading blank lines, the original receiver is returned. |
|
333 |
If all lines are blank, an empty string collection is returned." |
|
334 |
||
335 |
|indexOfFirstNonBlankLine| |
|
336 |
||
23336 | 337 |
indexOfFirstNonBlankLine := self findFirst:[:line | line notNil and:[line isBlank not]]. |
11702 | 338 |
indexOfFirstNonBlankLine == 0 ifTrue:[ ^ self copyEmpty ]. |
339 |
indexOfFirstNonBlankLine == 1 ifTrue:[ ^ self ]. |
|
340 |
^ self copyFrom:indexOfFirstNonBlankLine |
|
10452 | 341 |
|
342 |
" |
|
343 |
'1 |
|
344 |
2 |
|
345 |
3' asStringCollection withoutLeadingBlankLines |
|
346 |
" |
|
347 |
||
348 |
" |
|
349 |
' |
|
350 |
2 |
|
351 |
3' asStringCollection withoutLeadingBlankLines |
|
352 |
" |
|
353 |
||
354 |
" |
|
355 |
' |
|
356 |
||
11702 | 357 |
' asStringCollection withoutLeadingBlankLines |
358 |
" |
|
23336 | 359 |
|
360 |
"Modified: / 14-09-2018 / 10:08:01 / Stefan Vogel" |
|
11702 | 361 |
! |
362 |
||
363 |
withoutTrailingBlankLines |
|
364 |
"return a copy of the receiver with trailing blank lines removed. |
|
365 |
If there are no trailing blank lines, the original receiver is returned. |
|
366 |
If all lines are blank, an empty string collection is returned." |
|
367 |
||
368 |
|indexOfLastNonBlankLine| |
|
369 |
||
23336 | 370 |
indexOfLastNonBlankLine := self findLast:[:line | line notNil and:[line isBlank not]]. |
11702 | 371 |
indexOfLastNonBlankLine == 0 ifTrue:[ ^ self copyEmpty ]. |
372 |
indexOfLastNonBlankLine == self size ifTrue:[ ^ self ]. |
|
373 |
^ self copyTo:indexOfLastNonBlankLine |
|
374 |
||
375 |
" |
|
376 |
'1 |
|
377 |
2 |
|
17083 | 378 |
3' asStringCollection withoutTrailingBlankLines |
11702 | 379 |
" |
380 |
||
381 |
" |
|
382 |
' |
|
383 |
2 |
|
384 |
3 |
|
385 |
' asStringCollection withoutTrailingBlankLines |
|
386 |
" |
|
387 |
||
388 |
" |
|
389 |
' |
|
17083 | 390 |
2 |
391 |
3 |
|
392 |
||
393 |
||
394 |
' asStringCollection withoutTrailingBlankLines |
|
395 |
" |
|
396 |
" |
|
397 |
' |
|
11702 | 398 |
|
399 |
' asStringCollection withoutTrailingBlankLines |
|
10452 | 400 |
" |
23336 | 401 |
|
402 |
"Modified: / 14-09-2018 / 10:08:12 / Stefan Vogel" |
|
608 | 403 |
! ! |
404 |
||
22278 | 405 |
!StringCollection methodsFor:'enumerating'! |
406 |
||
407 |
collect:aBlock |
|
408 |
"evaluate the argument, aBlock for every element in the collection |
|
409 |
and return a collection of the results. |
|
410 |
||
411 |
Redefined, to change the result collection to an OrderedCollection if |
|
412 |
not all elements are valid for a StringCollection (must be string or nil)." |
|
413 |
||
414 |
|newCollection newElement onlyStrings |
|
415 |
start "{ Class:SmallInteger }" |
|
416 |
stop "{ Class:SmallInteger }" | |
|
417 |
||
418 |
onlyStrings := true. "/ ... until proven otherwise |
|
419 |
newCollection := self class newWithCapacity:(self size). |
|
420 |
stop := lastIndex. |
|
421 |
start := firstIndex. |
|
422 |
start to:stop do:[:index | |
|
423 |
newElement := aBlock value:(contentsArray at:index). |
|
424 |
(onlyStrings and:[newElement notNil and:[newElement isString not]]) ifTrue:[ |
|
425 |
onlyStrings := false. |
|
426 |
newCollection := newCollection asOrderedCollection. |
|
427 |
]. |
|
428 |
newCollection add:newElement. |
|
429 |
]. |
|
430 |
^ newCollection |
|
431 |
||
432 |
" |
|
433 |
#('this' 'is' 'some' nil 'text') asStringCollection collect:[:i | i] |
|
434 |
#('this' true 'some' nil 'text') asStringCollection collect:[:i | i ] |
|
435 |
" |
|
436 |
||
437 |
"Created: / 21-09-2017 / 14:31:31 / stefan" |
|
438 |
"Modified: / 21-09-2017 / 15:55:21 / stefan" |
|
439 |
! |
|
440 |
||
441 |
collect:collectBlock thenSelect:selectBlock |
|
442 |
"combination of collect followed by select; |
|
443 |
redefined to avoid the creation of an intermediate (garbage) collection. |
|
444 |
||
445 |
Redefined, to change the result collection to an OrderedCollection if |
|
446 |
not all elements are valid for a StringCollection (must be string or nil)." |
|
447 |
||
448 |
|newCollection newElement onlyStrings |
|
449 |
start "{ Class:SmallInteger }" |
|
450 |
stop "{ Class:SmallInteger }" | |
|
451 |
||
452 |
onlyStrings := true. "/ ... until proven otherwise |
|
453 |
newCollection := self class newWithCapacity:(self size). |
|
454 |
stop := lastIndex. |
|
455 |
start := firstIndex. |
|
456 |
start to:stop do:[:index | |
|
457 |
newElement := collectBlock value:(contentsArray at:index). |
|
458 |
(selectBlock value:newElement) ifTrue:[ |
|
459 |
(onlyStrings and:[newElement notNil and:[newElement isString not]]) ifTrue:[ |
|
460 |
onlyStrings := false. |
|
461 |
newCollection := newCollection asOrderedCollection. |
|
462 |
]. |
|
463 |
newCollection add:newElement. |
|
464 |
] |
|
465 |
]. |
|
466 |
^ newCollection |
|
467 |
||
468 |
" |
|
469 |
#('this' 'is' 'some' nil 'text') asStringCollection collect:[:i | i] thenSelect:[:e| e notNil] |
|
470 |
#('this' true 'some' nil 'text') asStringCollection collect:[:i | i ] thenSelect:[:e| e notNil] |
|
471 |
" |
|
472 |
||
473 |
"Created: / 21-09-2017 / 14:34:46 / stefan" |
|
474 |
"Modified: / 21-09-2017 / 15:55:05 / stefan" |
|
475 |
! |
|
476 |
||
477 |
select:selectBlock thenCollect:collectBlock |
|
478 |
"combination of select followed by collect; |
|
479 |
redefined to avoid the creation of an intermediate (garbage) collection. |
|
480 |
||
481 |
Redefined, to change the result collection to an OrderedCollection if |
|
482 |
not all elements are valid for a StringCollection (must be string or nil)." |
|
483 |
||
484 |
|newCollection element newElement onlyStrings |
|
485 |
start "{ Class:SmallInteger }" |
|
486 |
stop "{ Class:SmallInteger }" | |
|
487 |
||
488 |
onlyStrings := true. "/ ... until proven otherwise |
|
489 |
newCollection := self class newWithCapacity:(self size). |
|
490 |
stop := lastIndex. |
|
491 |
start := firstIndex. |
|
492 |
start to:stop do:[:index | |
|
493 |
element := contentsArray at:index. |
|
494 |
(selectBlock value:element) ifTrue:[ |
|
495 |
newElement := collectBlock value:element. |
|
496 |
(onlyStrings and:[newElement notNil and:[newElement isString not]]) ifTrue:[ |
|
497 |
onlyStrings := false. |
|
498 |
newCollection := newCollection asOrderedCollection. |
|
499 |
]. |
|
500 |
newCollection add:newElement. |
|
501 |
] |
|
502 |
]. |
|
503 |
^ newCollection |
|
504 |
||
505 |
" |
|
506 |
#('this' 'is' 'some' nil 'text') asStringCollection select:[:e| e notNil] thenCollect:[:i | i] |
|
507 |
#('this' true 'some' nil 'text') asStringCollection select:[:e| e notNil] thenCollect:[:i | i] |
|
508 |
" |
|
509 |
||
510 |
"Created: / 21-09-2017 / 14:42:20 / stefan" |
|
511 |
"Modified: / 21-09-2017 / 15:54:33 / stefan" |
|
512 |
! ! |
|
513 |
||
14111 | 514 |
|
5251 | 515 |
!StringCollection methodsFor:'printing & storing'! |
1 | 516 |
|
10909
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
517 |
printOn:aStream |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
518 |
"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
|
519 |
|
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
520 |
self do:[:eachString| |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
521 |
eachString printOn:aStream. |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
522 |
aStream cr. |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
523 |
]. |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
524 |
! |
5f5e63c29ae2
Define #printOn: for each method defining #printString
Stefan Vogel <sv@exept.de>
parents:
10452
diff
changeset
|
525 |
|
1 | 526 |
printString |
19466 | 527 |
"return the receiver's printString" |
284 | 528 |
|
1 | 529 |
^ self asString |
530 |
! ! |
|
531 |
||
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
532 |
!StringCollection methodsFor:'queries'! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
533 |
|
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
534 |
encoding |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
535 |
"the encoding; ask my first line" |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
536 |
|
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
537 |
self do:[:l | l notNil ifTrue:[^ l encoding]]. |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
538 |
"/ sigh |
14173 | 539 |
^ #unicode |
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
540 |
|
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
541 |
"Modified (comment): / 25-07-2012 / 18:09:15 / cg" |
13331 | 542 |
! |
543 |
||
544 |
stringSize |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
545 |
"the size of the string if I was converted" |
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
546 |
|
13331 | 547 |
^ self inject:0 into:[:sizeSoFar :eachLine | |
548 |
sizeSoFar + eachLine size + 1 |
|
549 |
] |
|
14244
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
550 |
|
d107839eb01b
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14204
diff
changeset
|
551 |
"Modified (comment): / 25-07-2012 / 18:02:19 / cg" |
7899
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
552 |
! ! |
7577df77ba95
character encodings - first attempt
Claus Gittinger <cg@exept.de>
parents:
7829
diff
changeset
|
553 |
|
246 | 554 |
!StringCollection methodsFor:'searching'! |
1 | 555 |
|
556 |
indexOfLineStartingWith:aString |
|
557 |
"return the index of the first line starting with the argument, aString" |
|
558 |
||
16460 | 559 |
^ self findFirst:[:l | l notNil and:[l startsWith:aString]] ifNone:0. |
1 | 560 |
|
16460 | 561 |
"/ |sz "{ Class:SmallInteger }" |
562 |
"/ l| |
|
563 |
"/ |
|
564 |
"/ sz := self size. |
|
565 |
"/ 1 to:sz do:[:index | |
|
566 |
"/ l := self at:index. |
|
567 |
"/ (l notNil and:[l startsWith:aString]) ifTrue:[ |
|
568 |
"/ ^ index |
|
569 |
"/ ]. |
|
570 |
"/ ]. |
|
571 |
"/ ^ 0 |
|
1009
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
572 |
|
a0354f2fea61
care for nil-lines when searching
Claus Gittinger <cg@exept.de>
parents:
968
diff
changeset
|
573 |
"Modified: 24.2.1996 / 19:08:47 / cg" |
1 | 574 |
! ! |
608 | 575 |
|
1064 | 576 |
!StringCollection methodsFor:'special converting'! |
577 |
||
14204 | 578 |
decodeFrom: encodingSymbol |
579 |
"given the receiver encoded as described by encodingSymbol, |
|
580 |
convert it into internal ST/X (unicode) encoding and return a |
|
581 |
corresponding StringCollection." |
|
582 |
||
583 |
|myEncoding encoder| |
|
584 |
||
585 |
encodingSymbol isNil ifTrue:[^ self]. |
|
586 |
||
587 |
encoder := CharacterEncoder encoderToEncodeFrom:#unicode into:encodingSymbol. |
|
588 |
encoder isNil ifTrue:[^ self]. |
|
589 |
||
590 |
^ self |
|
591 |
collect:[:eachLineOrNil | |
|
592 |
eachLineOrNil isNil ifTrue:[ |
|
593 |
eachLineOrNil |
|
594 |
] ifFalse:[ |
|
595 |
encoder decodeString: eachLineOrNil |
|
596 |
] |
|
597 |
] |
|
598 |
||
599 |
"Created: / 12-07-2012 / 14:30:25 / cg" |
|
600 |
! |
|
601 |
||
1064 | 602 |
withTabs |
19466 | 603 |
"return a new stringCollection consisting of the receiver's lines, |
1064 | 604 |
where leading spaces are replaced by tabulator characters (assuming 8-col tabs). |
605 |
Notice: lines which do not contain leading spaces, are copied by reference to the |
|
606 |
new stringCollection (i.e. shared); |
|
607 |
otherwise new strings is created. |
|
608 |
Limitation: only the very first spaces are replaced" |
|
609 |
||
610 |
^ self collect:[:string| |
|
611 |
string notNil ifTrue:[ |
|
612 |
string withTabs |
|
613 |
] ifFalse:[ |
|
614 |
string |
|
615 |
] |
|
616 |
] |
|
617 |
||
618 |
" |
|
619 |
(' abcd ') asStringCollection withTabs |
|
620 |
" |
|
621 |
||
622 |
"Created: 4.3.1996 / 17:09:07 / cg" |
|
623 |
"Modified: 4.3.1996 / 17:10:37 / cg" |
|
624 |
! |
|
625 |
||
626 |
withTabsExpanded |
|
19466 | 627 |
"return a new stringCollection consisting of the receiver's lines, |
1064 | 628 |
where tabs are replaced by space characters (assuming 8-col tabs). |
629 |
Notice: lines which do not contain any tab, are copied by reference to the |
|
630 |
new stringCollection (i.e. shared); |
|
631 |
otherwise new strings is created." |
|
632 |
||
15752 | 633 |
^ self withTabsExpanded:8 |
634 |
||
635 |
" |
|
636 |
|tab| |
|
637 |
tab := String with:Character tab. |
|
638 |
('abcd', tab, 'tef', tab, 'tgh') asStringCollection withTabsExpanded |
|
639 |
" |
|
640 |
||
641 |
"Created: 12.2.1996 / 22:25:56 / stefan" |
|
642 |
"Modified: 14.2.1996 / 11:13:01 / stefan" |
|
643 |
"Modified: 4.3.1996 / 17:10:22 / cg" |
|
644 |
! |
|
645 |
||
646 |
withTabsExpanded:n |
|
19466 | 647 |
"return a new stringCollection consisting of the receiver's lines, |
15753 | 648 |
where tabs are replaced by space characters (assuming n-col tabs). |
15752 | 649 |
Notice: lines which do not contain any tab, are copied by reference to the |
650 |
new stringCollection (i.e. shared); |
|
651 |
otherwise new strings is created." |
|
652 |
||
1064 | 653 |
^ self collect:[:string| |
654 |
string notNil ifTrue:[ |
|
15752 | 655 |
string withTabsExpanded:n |
1064 | 656 |
] ifFalse:[ |
657 |
string |
|
658 |
] |
|
659 |
] |
|
660 |
||
661 |
" |
|
662 |
|tab| |
|
663 |
tab := String with:Character tab. |
|
664 |
('abcd', tab, 'tef', tab, 'tgh') asStringCollection withTabsExpanded |
|
665 |
" |
|
666 |
||
667 |
"Created: 12.2.1996 / 22:25:56 / stefan" |
|
668 |
"Modified: 14.2.1996 / 11:13:01 / stefan" |
|
669 |
"Modified: 4.3.1996 / 17:10:22 / cg" |
|
670 |
! ! |
|
671 |
||
4007 | 672 |
!StringCollection methodsFor:'testing'! |
673 |
||
674 |
isStringCollection |
|
675 |
"return true, if the receiver is some kind of stringCollection; |
|
676 |
true is returned here - the method is redefined from Object." |
|
677 |
||
678 |
^ true |
|
679 |
||
680 |
||
681 |
! ! |
|
682 |
||
633 | 683 |
!StringCollection class methodsFor:'documentation'! |
684 |
||
685 |
version |
|
19466 | 686 |
^ '$Header$' |
13331 | 687 |
! |
688 |
||
689 |
version_CVS |
|
19466 | 690 |
^ '$Header$' |
633 | 691 |
! ! |
15752 | 692 |