range simple toy problem
ag0aep6g
anonymous at example.com
Fri Jun 1 18:40:45 UTC 2018
On 06/01/2018 07:00 PM, Xiaoxi wrote:
> import std.range;
> import std.algorithm;
> import std.string;
> import std.stdio;
>
> void main()
> {
> auto s = "1 2 3 4 5 6 7 8 9";
> auto iter = s.split(" ").drop(2);
>
> // How to find the unconsumed/not-split part of s here?
> // i.e. "3 4 5 6 7 8 9" NOT ["3", "4", "5", "6", "7", "8", "9"]
> // s[??? .. $] <- what goes here?
> }
This prints "3 4 5 6 7 8 9":
----
import std.range;
import std.algorithm;
import std.stdio;
void main()
{
auto s = "1 2 3 4 5 6 7 8 9";
auto iter = refRange(&s).splitter!(c => c == ' ').drop(2);
writeln(s); /* "3 4 5 6 7 8 9" */
}
----
Arguably, `.splitter(' ')` should work as well, but it doesn't.
Warning: Large parts of Phobos, including `splitter`, have problems
handling a `RefRange`. So this might break down when you take it beyond
the toy stage.
https://issues.dlang.org/show_bug.cgi?id=18657
> split is just an example, it's a generic question if you chain multiple
> lazy functions and then consume a part of the data...
Nitpick: `split` is not lazy. `splitter` is.
> how do you know
> how to slice the original buffer to point to the unconsumed data?
In my opinion, `refRange` should fit the bill here. But:
1) It's not always clear from the documentation how much a lazy function
actually pops. It might pop more than you expect, or less.
2) As mentioned, `refRange` has compatibility issues with other parts of
Phobos.
More information about the Digitalmars-d-learn
mailing list