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