When I write small command line utilities in Python, I often take advantage of the fileinput module that makes working with text files very convenient: the library permits to write quickly and easily a loop over standard input or a list of files, something like perl -a or awk line processing.

Then the size of input data grew, and also for a language comparison, I wanted to port my utility in the D programming language, but I cannot find an equivalent module, so I decided to write one myself.

The usage is pretty similar to Python:

import fileinput;
import std.stdio;

void main(in string[] args)
    foreach (line; fileinput.input(args))
        writeln(line); // do something with line

Once compiled, this will lead to an executable that can accept any number of text file as input, or read from stdin and seamlessly iterate on all of the lines contained in every file.

Thanks to the range interface; every struct / class implementing these methods can be used in a foreach statement.

interface InputRange(E)
        bool empty();
        E front();
        void popFront();

You can find the full implementation on github


25 January 2021