Range for files by character

Stephan Schiffels stephan_schiffels at mac.com
Mon May 20 14:36:39 PDT 2013


Hi,

I need an Input Range that iterates a file character by 
character. In bioinformatics this is often important, and having 
a D-range is of course preferable than any foreach-byLine 
combination, since we can apply filters and other goodies from 
std.algorithm. In this implementation, I am simply filtering out 
new-lines, as an example.

import std.stdio;
import std.conv;
import std.algorithm;

void main() {
   auto f = File("someFile.txt", "r");
   foreach(c; f.byChunk(1).filter!(a => to!char(a[0]) != '\n'))
     write(to!char(c[0]));
}

Is this the right way to do it? I was a bit surprised that 
std.stdio doesn't provide a "byChar" or "byByte" range. Is there 
a reason for this, or is this a too special need?

Stephan


More information about the Digitalmars-d mailing list