Make endians with dynamic arrays

master
Marko Semet 2020-06-15 13:42:41 +02:00
parent a2b0da0a57
commit 3407361468
1 changed files with 15 additions and 13 deletions

View File

@ -14,9 +14,10 @@ public
+ value = Input value to dump
+ destination = Target output buffer
+/
@nogc pure nothrow void dumpBigEndian(TYPE)(immutable TYPE value, ref ubyte[TYPE.sizeof] destination)
@nogc pure nothrow void dumpBigEndian(TYPE)(immutable TYPE value, ref ubyte[] destination)
in (destination.length == TYPE.sizeof)
{
(cast(ubyte[]) destination).write!(TYPE, Endian.bigEndian)(value, 0);
destination.write!(TYPE, Endian.bigEndian)(value, 0);
}
/++
+ Dumps the value in big endian.
@ -24,9 +25,10 @@ public
+ value = Input value to dump
+ destination = Target output buffer
+/
@nogc pure nothrow void dumpLittleEndian(TYPE)(immutable TYPE value, ref ubyte[TYPE.sizeof] destination)
@nogc pure nothrow void dumpLittleEndian(TYPE)(immutable TYPE value, ref ubyte[] destination)
in (destination.length == TYPE.sizeof)
{
(cast(ubyte[]) destination).write!(TYPE, Endian.littleEndian)(value, 0);
destination.write!(TYPE, Endian.littleEndian)(value, 0);
}
/++
+ Dumps the value in the system's endian.
@ -34,7 +36,7 @@ public
+ value = Input value to dump
+ destination = Target output buffer
+/
@nogc pure nothrow void dumpNative(TYPE)(immutable TYPE value, ubyte[TYPE.sizeof] destination)
@nogc pure nothrow void dumpNative(TYPE)(immutable TYPE value, ubyte[] destination)
{
static if (endian == Endian.bigEndian)
{
@ -56,7 +58,7 @@ private unittest
// Test dump big endian
{
// 1 Byte
ubyte[1] target = [0];
ubyte[] target = [0];
dumpBigEndian!byte(1, target);
assert(target == [1]);
@ -67,7 +69,7 @@ private unittest
assert(target == [3]);
}
{
ubyte[2] target = [0, 0];
ubyte[] target = [0, 0];
dumpBigEndian!short(0x0102, target);
assert(target == [1, 2]);
@ -75,7 +77,7 @@ private unittest
assert(target == [3, 4]);
}
{
ubyte[4] target = [0, 0, 0, 0];
ubyte[] target = [0, 0, 0, 0];
dumpBigEndian!int(0x01020304, target);
assert(target == [1, 2, 3, 4]);
@ -83,7 +85,7 @@ private unittest
assert(target == [5, 6, 7, 8]);
}
{
ubyte[8] target = [0, 0, 0, 0, 0, 0, 0, 0];
ubyte[] target = [0, 0, 0, 0, 0, 0, 0, 0];
dumpBigEndian!long(0x0102030405060708, target);
assert(target == [1, 2, 3, 4, 5, 6, 7, 8]);
@ -94,7 +96,7 @@ private unittest
// Test dump little endian
{
// 1 Byte
ubyte[1] target = [0];
ubyte[] target = [0];
dumpLittleEndian!byte(1, target);
assert(target == [1]);
@ -105,7 +107,7 @@ private unittest
assert(target == [3]);
}
{
ubyte[2] target = [0, 0];
ubyte[] target = [0, 0];
dumpLittleEndian!short(0x0102, target);
assert(target == [2, 1]);
@ -113,7 +115,7 @@ private unittest
assert(target == [4, 3]);
}
{
ubyte[4] target = [0, 0, 0, 0];
ubyte[] target = [0, 0, 0, 0];
dumpLittleEndian!int(0x01020304, target);
assert(target == [4, 3, 2, 1]);
@ -121,7 +123,7 @@ private unittest
assert(target == [8, 7, 6, 5]);
}
{
ubyte[8] target = [0, 0, 0, 0, 0, 0, 0, 0];
ubyte[] target = [0, 0, 0, 0, 0, 0, 0, 0];
dumpLittleEndian!long(0x0102030405060708, target);
assert(target == [8, 7, 6, 5, 4, 3, 2, 1]);