134 lines
3.0 KiB
D
134 lines
3.0 KiB
D
module structs.endian;
|
|
|
|
private
|
|
{
|
|
import std.bitmanip;
|
|
import std.system;
|
|
}
|
|
|
|
public
|
|
{
|
|
/++
|
|
+ Dumps the value with little endian.
|
|
+ Params:
|
|
+ value = Input value to dump
|
|
+ destination = Target output buffer
|
|
+/
|
|
@nogc pure nothrow void dumpBigEndian(TYPE)(immutable TYPE value, ref ubyte[] destination)
|
|
in (destination.length == TYPE.sizeof)
|
|
{
|
|
destination.write!(TYPE, Endian.bigEndian)(value, 0);
|
|
}
|
|
/++
|
|
+ Dumps the value in big endian.
|
|
+ Params:
|
|
+ value = Input value to dump
|
|
+ destination = Target output buffer
|
|
+/
|
|
@nogc pure nothrow void dumpLittleEndian(TYPE)(immutable TYPE value, ref ubyte[] destination)
|
|
in (destination.length == TYPE.sizeof)
|
|
{
|
|
destination.write!(TYPE, Endian.littleEndian)(value, 0);
|
|
}
|
|
|
|
/++
|
|
+ Loads the value with little endian.
|
|
+ Params:
|
|
+ source = Source buffer
|
|
+/
|
|
@nogc pure nothrow TYPE loadBigEndian(TYPE)(immutable(ubyte)[] source)
|
|
in (source.length == TYPE.sizeof)
|
|
{
|
|
return source.read!(TYPE, Endian.bigEndian)();
|
|
}
|
|
/++
|
|
+ Dumps the value in big endian.
|
|
+ Params:
|
|
+ source = Source buffer
|
|
+/
|
|
@nogc pure nothrow TYPE loadLittleEndian(TYPE)(immutable(ubyte)[] source)
|
|
in (source.length == TYPE.sizeof)
|
|
{
|
|
return source.read!(TYPE, Endian.littleEndian)();
|
|
}
|
|
}
|
|
|
|
private unittest
|
|
{
|
|
// Test dump big endian
|
|
{
|
|
// 1 Byte
|
|
ubyte[] target = [0];
|
|
dumpBigEndian!byte(1, target);
|
|
assert(target == [1]);
|
|
|
|
dumpBigEndian!ubyte(2, target);
|
|
assert(target == [2]);
|
|
|
|
dumpBigEndian!char(3, target);
|
|
assert(target == [3]);
|
|
}
|
|
{
|
|
ubyte[] target = [0, 0];
|
|
dumpBigEndian!short(0x0102, target);
|
|
assert(target == [1, 2]);
|
|
|
|
dumpBigEndian!ushort(0x0304, target);
|
|
assert(target == [3, 4]);
|
|
}
|
|
{
|
|
ubyte[] target = [0, 0, 0, 0];
|
|
dumpBigEndian!int(0x01020304, target);
|
|
assert(target == [1, 2, 3, 4]);
|
|
|
|
dumpBigEndian!uint(0x05060708, target);
|
|
assert(target == [5, 6, 7, 8]);
|
|
}
|
|
{
|
|
ubyte[] target = [0, 0, 0, 0, 0, 0, 0, 0];
|
|
dumpBigEndian!long(0x0102030405060708, target);
|
|
assert(target == [1, 2, 3, 4, 5, 6, 7, 8]);
|
|
|
|
dumpBigEndian!ulong(0x090a0b0c0d0e0f10, target);
|
|
assert(target == [9, 10, 11, 12, 13, 14, 15, 16]);
|
|
}
|
|
|
|
// Test dump little endian
|
|
{
|
|
// 1 Byte
|
|
ubyte[] target = [0];
|
|
dumpLittleEndian!byte(1, target);
|
|
assert(target == [1]);
|
|
|
|
dumpLittleEndian!ubyte(2, target);
|
|
assert(target == [2]);
|
|
|
|
dumpLittleEndian!char(3, target);
|
|
assert(target == [3]);
|
|
}
|
|
{
|
|
ubyte[] target = [0, 0];
|
|
dumpLittleEndian!short(0x0102, target);
|
|
assert(target == [2, 1]);
|
|
|
|
dumpLittleEndian!ushort(0x0304, target);
|
|
assert(target == [4, 3]);
|
|
}
|
|
{
|
|
ubyte[] target = [0, 0, 0, 0];
|
|
dumpLittleEndian!int(0x01020304, target);
|
|
assert(target == [4, 3, 2, 1]);
|
|
|
|
dumpLittleEndian!uint(0x05060708, target);
|
|
assert(target == [8, 7, 6, 5]);
|
|
}
|
|
{
|
|
ubyte[] target = [0, 0, 0, 0, 0, 0, 0, 0];
|
|
dumpLittleEndian!long(0x0102030405060708, target);
|
|
assert(target == [8, 7, 6, 5, 4, 3, 2, 1]);
|
|
|
|
dumpLittleEndian!ulong(0x090a0b0c0d0e0f10, target);
|
|
assert(target == [16, 15, 14, 13, 12, 11, 10, 9]);
|
|
}
|
|
}
|