Creating InputRanges from strings, files etc.

Vinay Sajip vinay_sajip at yahoo.co.uk
Thu Nov 8 15:33:24 UTC 2018


On Thursday, 8 November 2018 at 14:38:37 UTC, Paul Backus wrote:
> 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);

Aha - inputRangeObject was the thing I was missing. Thanks!


More information about the Digitalmars-d-learn mailing list