File.byChunk returns a range which returns a ubyte[] via front.
A quick Google search seemed to indicate that UTF-8 uses 1 to 6 bytes to encode data so just make sure you always have 6 bytes of data and you can use std.encoding's decode to convert it a dchar character. You can then use std.utf's toUFT8 to convert to a regular string instead of a dstring.
The convert function below will convert any unsigned array range to a string.
import std.encoding, std.stdio, std.traits, std.utf;
void main()
{
File input = File("test.txt");
string data = convert(input.byChunk(512));
writeln("Data: ", data);
}
string convert(R)(R chunkRange)
in
{
assert(isArray!(typeof(chunkRange.front)) && isUnsigned!(typeof(chunkRange.front[0])));
}
body
{
ubyte[] inbuffer;
dchar[] outbuffer;
while(inbuffer.length > 0 || !chunkRange.empty)
{
while((inbuffer.length < 6) && !chunkRange.empty)// Max UTF-8 byte length is 6
{
inbuffer ~= chunkRange.front;
chunkRange.popFront();
}
outbuffer ~= decode(inbuffer);
}
return toUTF8(outbuffer); // Convert to string instead of dstring
}