I want a version of splitter that doesn't eat the sentinels.
I want to split AT the sentinels, but the sentinel should be the first
element of the bucket.
eg: assert(equal(splitter("hello world", ' '), [ "hello", " ", " world" ]));
Note the weird behaviour since there are 2 spaces. More useful when
the data is not strings.
Comment #1 by jack — 2016-07-18T20:19:29Z
Does std.regex.splitter!(Yes.keepSeparators) suffice?
Comment #2 by turkeyman — 2016-07-19T00:40:24Z
That's fine.
Does that already exist? I couldn't see anything on dlang.org.
Obviously the Pred function should remain as the first template arg, it can be second...
Comment #3 by jack — 2016-07-19T04:00:14Z
(In reply to Manu from comment #2)
> That's fine.
> Does that already exist? I couldn't see anything on dlang.org.
>
> Obviously the Pred function should remain as the first template arg, it can
> be second...
It's in the nightlies and on the prerelease docs.
Comment #4 by turkeyman — 2016-07-21T09:35:55Z
Wait up. I misread... you say std.regex.splitter.
No, that's not what I'm asking for. I'm interested in std.algorithm.iterator.splitter. It should be in those.
> Doesn't implement desired behaviour.
Fair enough - I tried to make it similar to `splitter` in `std.regex`, but it seems that even this didn't work out:
"a..b.c".splitter!(Yes.keepSeparators)(regex("[.]")).writeln
> ["a", ".", "", ".", "b", ".", "c"]
"a..b.c".splitter!(Yes.keepSeparators)('.').writeln;
> ["a", ".", ".", "b", ".", "c"]
From the example you posted, you want it to yield sth. like this, right?
> ["a", ".", ".b", ".c"]
And there's another common use case - though that one is simply splitter.filter!`a.empty`:
> ["a", "b", "c"]
However, at least the existing behavior for No.keepSeparators is the same:
"a..b.c".splitter!(No.keepSeparators)(regex("[.]")).writeln
> ["a", "", "b", "c"]
"a..b.c".splitter!(No.keepSeparators)('.').writeln;
> ["a", "", "b", "c"]
Comment #8 by turkeyman — 2020-07-18T11:40:21Z
They are all interesting cases. I think splitter should be configurable like this.
Comment #9 by robert.schadek — 2024-12-01T16:27:33Z