Why is there no std.stream anymore?

Steven Schveighoffer schveiguy at yahoo.com
Tue Dec 12 20:51:30 UTC 2017


On 12/11/17 6:33 PM, Seb wrote:
> Though if you need superb performance, iopipe or similar will be faster.
Since iopipe was mentioned several times, I will say a couple things:

1. iopipe is not made for processing one element at a time, it focuses 
on buffers. The reason for this is because certain tasks (i.e. parsing) 
are much more efficient with buffered data than when using the range 
API. Even with FILE *, using fgetc for every character is going to suck 
when compared to fread, and processing the resulting array in-memory.

2. If you do want to process by element, I recommend the following chain:

// an example that uses iopipe's file stream and assumes it's UTF8 text.
// other mechanisms are available.
auto mypipe = openDev("somefile") // open a file
     .bufd                         // buffer it
     .assumeText                   // assume it's utf-8 text
     .ensureDecodeable;            // ensure there are no partial 
code-points in the window

// convert to range of "chunks", and then join into one large range
foreach(c; mypipe.asInputRange.joiner) {
     process(c);
}

Note, due to Phobos's auto-decoding, joiner is going to auto-decode all 
of the data. This means typeof(c) is going to be dchar, and not char, 
and everything needs to be proper utf-8. If you want to process the 
bytes raw, you can omit the .assumeText.ensureDecodeable part, and the 
data will be ubytes.

-Steve


More information about the Digitalmars-d-learn mailing list