Search code examples
d

How to use InputRange!(dchar) with Stdin in D 2.0?


I'm trying to write generic code that can lex any stream of characters (dchars) into anywhere... whether it's from a file or from stdin into another file or stdout.

How do I do this?

It seems like stdin and stdout are painful to use with InputRange and OutputRange (since I have to wrap them manuallly every time), and I can't use std.stdio.Stream because it gives me bytes, not strings... and I can't find any adapter to convert from one into the other easily.

So effectively, I can't find a good way to use stdin/stdout in my programs.

Is there something e.g. like .NET's TextReader that can abstractly read text as an InputRange!dchar regardless of whether its input is a string, the console, or a file?


Solution

  • The proper function which takes a dchar input range is:

    void func(Range)(Range input)
        if(isInputRange!Range && is(ElementType!Range == dchar))
    {}
    

    However, you must have an input range that gets its input from stdin. There is talk of needing to rework stdio/streaming which has not been done yet. There is an undocumented range of dchar for stdin.