std.algorithm.splitter on a string not always bidirectional
Jon Degenhardt
jond at noreply.com
Fri Jan 22 05:51:38 UTC 2021
On Thursday, 21 January 2021 at 22:43:37 UTC, Steven
Schveighoffer wrote:
> auto sp1 = "a|b|c".splitter('|');
>
> writeln(sp1.back); // ok
>
> auto sp2 = "a.b|c".splitter!(v => !isAlphaNum(v));
>
> writeln(sp2.back); // error, not bidirectional
>
> Why? is it an oversight, or is there a good reason for it?
>
> -Steve
I believe the reason is two-fold. First, splitter is lazy.
Second, the range splitting is defined in the forward direction,
not the reverse direction. A bidirectional range is only
supported if it is guaranteed that the splits will occur at the
same points in the range when run in either direction. That's why
the single element delimiter is supported. Its clearly the case
for the predicate function in your example. If that's known to be
always true then perhaps it would make sense to enhance splitter
to generate bidirectional results in this case.
--Jon
More information about the Digitalmars-d-learn
mailing list