Add base project data
parent
451f13ce32
commit
60154c0217
|
@ -0,0 +1,23 @@
|
||||||
|
[*.d]
|
||||||
|
charset = UTF-8
|
||||||
|
dfmt_align_switch_statements = true
|
||||||
|
dfmt_brace_style = allman
|
||||||
|
dfmt_compact_labeled_statements = true
|
||||||
|
dfmt_keep_line_breaks = false
|
||||||
|
dfmt_outdent_attributes = true
|
||||||
|
dfmt_outdent_labels = true
|
||||||
|
dfmt_selective_import_space = true
|
||||||
|
dfmt_single_template_constraint_indent = false
|
||||||
|
dfmt_soft_max_line_length = 120
|
||||||
|
dfmt_space_after_cast = true
|
||||||
|
dfmt_space_after_keywords = false
|
||||||
|
dfmt_space_before_aa_colon = false
|
||||||
|
dfmt_space_before_function_parameters = false
|
||||||
|
dfmt_split_operator_at_line_end = false
|
||||||
|
dfmt_template_constraint_style = conditional_newline_indent
|
||||||
|
end_of_line = lf
|
||||||
|
indent_size = 4
|
||||||
|
indent_style = tab
|
||||||
|
insert_final_newline = true
|
||||||
|
max_line_length = 120
|
||||||
|
tab_width = 4
|
|
@ -24,3 +24,5 @@ docs/
|
||||||
# Code coverage
|
# Code coverage
|
||||||
*.lst
|
*.lst
|
||||||
|
|
||||||
|
/dub.selections.json
|
||||||
|
/structs-test-library
|
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"Marko Semet <marko@marko10-000.de>"
|
||||||
|
],
|
||||||
|
"copyright": "Copyright © 2020, Marko Semet <marko@marko10-000.de>",
|
||||||
|
"description": "Binary formating like the python struct module.",
|
||||||
|
"license": "LGPL-3.0",
|
||||||
|
"name": "structs",
|
||||||
|
"targetType": "sourceLibrary"
|
||||||
|
}
|
|
@ -0,0 +1,131 @@
|
||||||
|
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[TYPE.sizeof] destination)
|
||||||
|
{
|
||||||
|
(cast(ubyte[]) 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[TYPE.sizeof] destination)
|
||||||
|
{
|
||||||
|
(cast(ubyte[]) destination).write!(TYPE, Endian.littleEndian)(value, 0);
|
||||||
|
}
|
||||||
|
/++
|
||||||
|
+ Dumps the value in the system's endian.
|
||||||
|
+ Params:
|
||||||
|
+ value = Input value to dump
|
||||||
|
+ destination = Target output buffer
|
||||||
|
+/
|
||||||
|
@nogc pure nothrow void dumpNative(TYPE)(immutable TYPE value, ubyte[TYPE.sizeof] destination)
|
||||||
|
{
|
||||||
|
static if (endian == Endian.bigEndian)
|
||||||
|
{
|
||||||
|
dumpBigEndian(value, destination);
|
||||||
|
}
|
||||||
|
else static if (endian == Endian.littleEndian)
|
||||||
|
{
|
||||||
|
dumpLittleEndian(value, destination);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
static assert(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private unittest
|
||||||
|
{
|
||||||
|
// Test dump big endian
|
||||||
|
{
|
||||||
|
// 1 Byte
|
||||||
|
ubyte[1] target = [0];
|
||||||
|
dumpBigEndian!byte(1, target);
|
||||||
|
assert(target == [1]);
|
||||||
|
|
||||||
|
dumpBigEndian!ubyte(2, target);
|
||||||
|
assert(target == [2]);
|
||||||
|
|
||||||
|
dumpBigEndian!char(3, target);
|
||||||
|
assert(target == [3]);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
ubyte[2] target = [0, 0];
|
||||||
|
dumpBigEndian!short(0x0102, target);
|
||||||
|
assert(target == [1, 2]);
|
||||||
|
|
||||||
|
dumpBigEndian!ushort(0x0304, target);
|
||||||
|
assert(target == [3, 4]);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
ubyte[4] 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[8] 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[1] target = [0];
|
||||||
|
dumpLittleEndian!byte(1, target);
|
||||||
|
assert(target == [1]);
|
||||||
|
|
||||||
|
dumpLittleEndian!ubyte(2, target);
|
||||||
|
assert(target == [2]);
|
||||||
|
|
||||||
|
dumpLittleEndian!char(3, target);
|
||||||
|
assert(target == [3]);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
ubyte[2] target = [0, 0];
|
||||||
|
dumpLittleEndian!short(0x0102, target);
|
||||||
|
assert(target == [2, 1]);
|
||||||
|
|
||||||
|
dumpLittleEndian!ushort(0x0304, target);
|
||||||
|
assert(target == [4, 3]);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
ubyte[4] 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[8] 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]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
module structs;
|
||||||
|
|
||||||
|
private
|
||||||
|
{
|
||||||
|
import std.array;
|
||||||
|
import std.bitmanip;
|
||||||
|
import std.system;
|
||||||
|
|
||||||
|
/++
|
||||||
|
+ Supported format types
|
||||||
|
+/
|
||||||
|
enum FormatType
|
||||||
|
{
|
||||||
|
INT_8, /// Signed integer 8 bit
|
||||||
|
INT_16, /// Signed integer 16 bit
|
||||||
|
INT_32, /// Signed integer 32 bit
|
||||||
|
INT_64, /// Signed integer 64 bit
|
||||||
|
UINT_8, /// Unsigned integer 8 bit
|
||||||
|
UINT_16, /// Unsigned integer 16 bit
|
||||||
|
UINT_32, /// Unsigned integer 32 bit
|
||||||
|
UINT_64 /// Unsigned integer 64 bit
|
||||||
|
}
|
||||||
|
|
||||||
|
/++
|
||||||
|
+ A element of a package
|
||||||
|
+/
|
||||||
|
struct Element
|
||||||
|
{
|
||||||
|
Endian endian; /// The endian to use
|
||||||
|
bool isArray; /// If it's an array
|
||||||
|
size_t arraySize; /// Array size
|
||||||
|
FormatType formatType; /// The formated type
|
||||||
|
}
|
||||||
|
|
||||||
|
/++
|
||||||
|
+ Removes the whitespaces of the string.
|
||||||
|
+ Params:
|
||||||
|
+ source = The source string to format
|
||||||
|
+ Returns: String without whitespaces
|
||||||
|
+/
|
||||||
|
pure string remove_whitespaces(string source)
|
||||||
|
{
|
||||||
|
return source.replace(" ", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test remove whitespaces.
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
assert(remove_whitespaces(" a b c ") == "abc");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue