From 60154c02171c61c7998cd3b89945c5ad3ab08bf8 Mon Sep 17 00:00:00 2001 From: Marko Semet Date: Wed, 27 May 2020 11:30:58 +0200 Subject: [PATCH] Add base project data --- .editorconfig | 23 +++++++ .gitignore | 2 + dub.json | 10 +++ source/structs/endian.d | 131 +++++++++++++++++++++++++++++++++++++++ source/structs/package.d | 56 +++++++++++++++++ 5 files changed, 222 insertions(+) create mode 100644 .editorconfig create mode 100644 dub.json create mode 100644 source/structs/endian.d create mode 100644 source/structs/package.d diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..99e5190 --- /dev/null +++ b/.editorconfig @@ -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 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 2758450..c6edef2 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,5 @@ docs/ # Code coverage *.lst +/dub.selections.json +/structs-test-library \ No newline at end of file diff --git a/dub.json b/dub.json new file mode 100644 index 0000000..5092e92 --- /dev/null +++ b/dub.json @@ -0,0 +1,10 @@ +{ + "authors": [ + "Marko Semet " + ], + "copyright": "Copyright © 2020, Marko Semet ", + "description": "Binary formating like the python struct module.", + "license": "LGPL-3.0", + "name": "structs", + "targetType": "sourceLibrary" +} \ No newline at end of file diff --git a/source/structs/endian.d b/source/structs/endian.d new file mode 100644 index 0000000..1ed3558 --- /dev/null +++ b/source/structs/endian.d @@ -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]); + } +} diff --git a/source/structs/package.d b/source/structs/package.d new file mode 100644 index 0000000..bbfc664 --- /dev/null +++ b/source/structs/package.d @@ -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 +{ + +}