[Cuis-dev] Split string
Mariano Montone
marianomontone at gmail.com
Tue Nov 30 05:06:37 PST 2021
Hello,
I have a small proposal.
Splitting of strings is a very common operation, but there's no
implemention in Cuis core. And so I often need to depend on
SqueakCompatibility that implements String>>subStrings: , just because
of that.
I'd like to move that to Cuis core. I would rename it to String>>split:,
but that's not a requirement.
I attach the source (I'm not the author, though!).
This is the info of the SqueakCompatibility method: 'eem 2/3/2015 12:04
° 18 lines of code ° *squeakCompatibility ° 1 implementor ° 20 senders °
in package SqueakCompatibility ° in change sets:
4974-CuisCore-AuthorName-2021Nov19-11h59m, Install-SqueakCompatibility'.
Cheers,
Mariano
-------------- next part --------------
'From Cuis 5.0 [latest update: #4973] on 30 November 2021 at 9:55:52 am'!
!String methodsFor: 'converting' stamp: 'MM 11/30/2021 09:54:34'!
split: separators
"Answer an array containing the substrings in the receiver separated
by the elements of separators."
| char result sourceStream subString |
(separators isString or:[separators allSatisfy: [:element | element isCharacter]]) ifFalse:
[^ self error: 'separators must be Characters.'].
sourceStream := ReadStream on: self.
result := OrderedCollection new.
subString := String new.
[sourceStream atEnd]
whileFalse:
[char := sourceStream next.
(separators includes: char)
ifTrue: [subString notEmpty
ifTrue:
[result add: subString copy.
subString := String new]]
ifFalse: [subString := subString , (String with: char)]].
subString notEmpty ifTrue: [result add: subString copy].
^ result asArray! !
More information about the Cuis-dev
mailing list