Closed bioinfornatics closed 5 years ago
So it's on purpose that an iopipe is not a range. It's because the basic iopipe represents the entire stream, and it's not clear what you want to be range elements.
There is the asInputRange
constructor which turns an iopipe into a range of window buffers.
I really should add something to convert to an input range of element types. The thing is I don't want to confuse algorithms by projecting a certain range mechanism.
I wrote this for someone at dconf to show how it would work:
import iopipe.bufpipe;
import iopipe.textpipe;
import iopipe.traits;
import std.range;
import std.utf;
struct ByElementRange(Chain)
{
Chain chain;
this(Chain chain)
{
this.chain = chain;
if(chain.window.length == 0)
cast(void)chain.extend(0);
}
auto front() { return chain.window[0]; }
void popFront()
{
chain.release(1);
if(chain.window.length == 0)
cast(void)chain.extend(0);
}
bool empty() { return chain.window.length == 0; }
}
auto byElementRange(Chain)(Chain chain)
{
return ByElementRange!Chain(chain);
}
void main()
{
import std.algorithm;
auto input = "some sentence I want to separate by words";
auto wordrange = input.delimitedText(' ').asInputRange;
assert(wordrange.equal(["some ", "sentence ", "I ", "want ", "to ", "separate ", "by ", "words"]));
auto charRange = input.byElementRange;
assert(charRange.take(6).equal(input[0 .. 6].byCodeUnit));
assert(charRange.front == 's');
}
Oh, and you have an error in your iopipe chain. A file is typed as a stream of ubyte
. You need to buffer in that form, and then wrap as an encoded stream.
e.g.:
File(args[1]).refCounted.bufd.assumeText!Char...
And also, if you wanted to count lines, you could use iopipe.textpipe.byLineRange
@bioinfornatics #25
thanks @schveiguy
It is possible to provide a little example of cycling through the buffer to count a char as a
\n
?I tried this, but BufferedInputSource is not a Range
test 1
test 2
Thanks