On Thursday, 8 November 2018 at 13:58:55 UTC, Vinay Sajip wrote:
Excuse my ignorance, but from looking at the documentation on std.range and a quick skim of the guides mentioned there near the top, I can't see what the simple way is of creating an InputRange!(ubyte) from strings, files etc. I would have expected to find something in the DLang Tour about this, but couldn't find anything. Please can someone tell me how to do it? Just to be clear, I want to create an object which is an InputRange!(ubyte) and pass that around, rather than e.g. iterate over a string or a file.

You can iterate through a file one ubyte at a time using `byChunk` and `joiner`:

    auto r1 = stdin.byChunk(1024).joiner;
    assert(is(typeof(r1.front) == ubyte));

You can iterate through a string one ubyte at a time using `representation`:

    auto r2 = "To be or not to be".representation;
    assert(is(typeof(r2.front) == immutable(ubyte)));

To pass these ranges around using the `InputRange` interface, use `inputRangeObject` to wrap them:

    InputRange!ubyte r3 = inputRangeObject(r1);
    InputRange!(immutable(ubyte)) r4 = inputRangeObject(r2);

Reply via email to