xxxxxxxxxx
import std.range;
alias TestType = ushort;
void main()
{
TestType[] data1 = [ 0, 1, 2, 3, 4, 5 ];
auto data12 = iota(cast(TestType)data1.length);
while(!data12.empty)
{
data12.testRange!uint;
}
}/* readBytes() Results:
(ubyte -> ushort): [1, 515, 1029]
(ushort -> uint): fail
Error: dlang/dmd/linux/bin64/../../src/phobos/std/bitmanip.d(3721): Candidate is: `read(T, Endian endianness = Endian.bigEndian, R)(ref R range)`
with `T = uint,
endianness = E,
R = Result`
must satisfy the following constraint:
` is(ElementType!R : const(ubyte))`
HATANIN NEDENİ ------------------^
readUnits() Results:
(ubyte -> ushort): [1, 515, 1029]
(ushort -> uint): [1, 131075, 262149]
//*/
template readUnits(T, R)
{
enum ES = ElementType!R.sizeof;
enum size = T.sizeof / ES;
auto readUnits(ref R data)
{
T result;
auto len = size;
while(len--)
{
result <<= ES * 8;
result |= data.front;
data.popFront;
}
return result;
}
}
template readBytes(T, bool big = true, R)
{ // pair endian version 2.0
import bop = std.bitmanip;
static if(big)
enum E = bop.Endian.bigEndian;
else
enum E = bop.Endian.littleEndian;
auto readBytes(ref R dat)
=> bop.read!(T, E)(dat);
}
void testRange(T, R)(ref R r)
{
import std.stdio : writefln;
//r.readBytes!T.writefln!"%s";/*
r.readUnits!T.writefln!"%s";//*/
writefln("Data: %s, Length: %s", r, r.length);
}