www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: buffered input

Dang, you beat me to my post on what I have run into trying to provide a
slice-able, assignable, buffered Forward Range.

I was doing some work on a CSV parser. It is rather simple to build a proper
parser from an input range. But providing the ability to use custom separators
which could be of any length did not work well with a forward range. It was no
longer a look-ahead of one. So I started examining how Splitter[1] work with
slice-able ranges. Ok enough of the background.

So basically I tried to make a range that would provide everything I needed for
the new CSV design[2] and the result[3] didn't work. It actually works better
with my CSV parser than it does splitter.

The main issue I was having is, if you save the range and move forward, how do
you keep the buffer of all instances in sync. Can we turn an input range into a
forward range? If not, how would you get splitter working on an input range? (I
probably need to file a bug, but my InputFileRange[3, bottom] didn't work with
splitter either)

The next issue is with slicing. If we can't get an input range to become a
forward range then we can't have slicing either. A slice of [0..$] should give
me a copy of the range. But even if you could do this, how would you know that
the slice should be made of the entire range, or of just what is available in
the buffer?

So I guess the question is, with the proposal. Can a hasSlicing!R be created
from an InputRange!R such that 

    auto range = "Hello, World";
    auto len = countUntil(range, ",");
    assert(range[0..len] == "Hello");

where range is replaced by a buffered Input Range. And as an added bonus:

    range = range[len..$];
    assert(range == ",World");

You can of course use the Range for equality, instead of strings like "Hello".

1. https://github.com/D-Programming-Language/phobos/blob/master/std/algorithm.d#L1317

2. https://github.com/he-the-great/JPDLibs/blob/csvoptimize/csv/csv.d

3. https://gist.github.com/812681
Feb 05 2011