join
Yanni Chiu
yanni at rogers.com
Fri Sep 15 17:12:40 UTC 2006
stephane ducasse wrote:
> Oscar and Damien did an implementation of join and split and I would
> really like to ease
> perl people and include such protocols in the next release (with tests)
> Now if would be nice to have a discussion on the implementations
I've also had a brush with Perl, so was looking for join/split too.
Here's what I have. Notice that split: expects a charater argument.
IIRC, the reason I did that was to make it easier to implement the
equivalent method on VAST. Also, the joinWith: doesn't use
nextPutAll:separatedBy: so I could easily implement an equivalent
method in VAST and VW.
SequenceableCollection joinWith: separator
"Answer a string with elements converted to strings and separated by
separator."
| ws |
ws := WriteStream on: (String new: 100).
self withIndexDo: [:each :i |
ws nextPutAll: each asString.
i < self size ifTrue: [ ws nextPutAll: separator ].
].
^ws contents
String>>split: ch
| pieces i prev |
pieces := OrderedCollection new.
i := 0.
[(i := self indexOf: ch startingAt: (prev := i + 1)) == 0] whileFalse:
[prev < i ifTrue: [pieces add: (self copyFrom: prev to: i - 1)].
prev == i ifTrue: [pieces add: String new]].
prev <= self size
ifTrue: [pieces add: (self copyFrom: prev to: self size)].
(self isEmpty not and: [self last = ch]) ifTrue: [pieces add: String new].
^pieces
More information about the Squeak-dev
mailing list
|