From 8d5b75b879693bfd19d5a4cc5d7d91493934bd5d Mon Sep 17 00:00:00 2001 From: ZombineDev Date: Fri, 4 Nov 2016 02:17:52 +0200 Subject: [PATCH 01/13] Fix compilation with DMD 2.072.0 The following DMD PRs added more rigorous safety checks directly affecting this project: * dlang/dmd#5852 (fix Issue 15399 - unaligned pointers are not `@safe`) - triggered at line: https://github.com/kiith-sa/D-YAML/blob/v0.5.3/source/dyaml/emitter.d#L1011 * dlang/dmd#5940 (Unions may break immutability / unions with pointers are un-`@safe` ) - triggered at line: https://github.com/kiith-sa/D-YAML/blob/v0.5.3/source/dyaml/event.d#L230 * dlang/dmd#5876 (Casting from `void[]` to `T[]` is erroneously considered `@safe`) - triggered at line: https://github.com/kiith-sa/D-YAML/blob/v0.5.3/source/dyaml/loader.d#L186 * dlang/dmd#5860 (array.ptr in @safe code may point past end of array) - triggered at line: https://github.com/kiith-sa/D-YAML/blob/v0.5.3/source/dyaml/zerostring.d#L35 --- source/dyaml/emitter.d | 2 +- source/dyaml/event.d | 8 ++++++-- source/dyaml/loader.d | 2 +- source/dyaml/zerostring.d | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/source/dyaml/emitter.d b/source/dyaml/emitter.d index 9f8e991..fe5d997 100644 --- a/source/dyaml/emitter.d +++ b/source/dyaml/emitter.d @@ -54,7 +54,7 @@ class EmitterException : YAMLException private alias EmitterException Error; //Stores results of analysis of a scalar, determining e.g. what scalar style to use. -align(4) struct ScalarAnalysis +struct ScalarAnalysis { //Scalar itself. string scalar; diff --git a/source/dyaml/event.d b/source/dyaml/event.d index 22c5903..40afe2f 100644 --- a/source/dyaml/event.d +++ b/source/dyaml/event.d @@ -227,8 +227,12 @@ Event scalarEvent(const Mark start, const Mark end, const Anchor anchor, const T result.value = value; result.startMark = start; result.endMark = end; - result.anchor = anchor; - result.tag = tag; + + () @trusted { + result.anchor = anchor; + result.tag = tag; + }(); + result.id = EventID.Scalar; result.scalarStyle = style; result.implicit = implicit[0]; diff --git a/source/dyaml/loader.d b/source/dyaml/loader.d index 1839c82..6d0b811 100644 --- a/source/dyaml/loader.d +++ b/source/dyaml/loader.d @@ -179,7 +179,7 @@ struct Loader * * Throws: YAMLException if yamlData contains data illegal in YAML. */ - this(void[] yamlData) @safe + this(void[] yamlData) @trusted { try { diff --git a/source/dyaml/zerostring.d b/source/dyaml/zerostring.d index 841a097..55f2a32 100644 --- a/source/dyaml/zerostring.d +++ b/source/dyaml/zerostring.d @@ -24,7 +24,7 @@ struct ZeroString(string TypeName) @disable int opCmp(ref ZeroString); ///Construct a string. - this(const string str) pure nothrow @safe + this(const string str) pure nothrow @trusted { if(str is null || str == "") { From bc1a2c2d0f56edaec777298361797dd582f3d7aa Mon Sep 17 00:00:00 2001 From: ZombineDev Date: Fri, 4 Nov 2016 02:50:32 +0200 Subject: [PATCH 02/13] Stop using implicit string literal concatenation Implicit string litaral concatenation was deprecated with this DMD PR: dlang/dmd/pull/6155. --- source/dyaml/composer.d | 16 ++++++++-------- source/dyaml/constructor.d | 2 +- source/dyaml/dumper.d | 2 +- source/dyaml/emitter.d | 4 ++-- source/dyaml/flags.d | 22 +++++++++++----------- source/dyaml/node.d | 2 +- source/dyaml/reader.d | 2 +- source/dyaml/resolver.d | 30 +++++++++++++++--------------- source/dyaml/scanner.d | 10 +++++----- 9 files changed, 45 insertions(+), 45 deletions(-) diff --git a/source/dyaml/composer.d b/source/dyaml/composer.d index e9bba34..70f08f6 100644 --- a/source/dyaml/composer.d +++ b/source/dyaml/composer.d @@ -110,7 +110,7 @@ final class Composer { //Get the root node of the next document. assert(!parser_.checkEvent(EventID.StreamEnd), - "Trying to get a node from Composer when there is no node to " + "Trying to get a node from Composer when there is no node to " ~ "get. use checkNode() to determine if there is a node."); return composeDocument(); @@ -120,14 +120,14 @@ final class Composer Node getSingleNode() @trusted { assert(!parser_.checkEvent(EventID.StreamEnd), - "Trying to get a node from Composer when there is no node to " + "Trying to get a node from Composer when there is no node to " ~ "get. use checkNode() to determine if there is a node."); Node document = composeDocument(); //Ensure that the stream contains no more documents. enforce(parser_.checkEvent(EventID.StreamEnd), - new ComposerException("Expected single document in the stream, " + new ComposerException("Expected single document in the stream, " ~ "but found another document.", parser_.getEvent().startMark)); @@ -292,11 +292,11 @@ final class Composer void error(Node node) { //this is Composer, but the code is related to Constructor. - throw new ConstructorException("While constructing a mapping, " - "expected a mapping or a list of " - "mappings for merging, but found: " - ~ node.type.toString() ~ - " NOTE: line/column shows topmost parent " + throw new ConstructorException("While constructing a mapping, " ~ + "expected a mapping or a list of " ~ + "mappings for merging, but found: " ~ + node.type.toString() ~ + " NOTE: line/column shows topmost parent " ~ "to which the content is being merged", startMark, endMark); } diff --git a/source/dyaml/constructor.d b/source/dyaml/constructor.d index e18025d..301c665 100644 --- a/source/dyaml/constructor.d +++ b/source/dyaml/constructor.d @@ -352,7 +352,7 @@ final class Constructor assert((tag in fromScalar_) is null && (tag in fromSequence_) is null && (tag in fromMapping_) is null, - "Constructor function for tag " ~ tag.get ~ " is already " + "Constructor function for tag " ~ tag.get ~ " is already " ~ "specified. Can't specify another one."); diff --git a/source/dyaml/dumper.d b/source/dyaml/dumper.d index 54c57e1..46bb8c1 100644 --- a/source/dyaml/dumper.d +++ b/source/dyaml/dumper.d @@ -297,7 +297,7 @@ struct Dumper foreach(handle, prefix; tags) { assert(handle.length >= 1 && handle[0] == '!' && handle[$ - 1] == '!', - "A tag handle is empty or does not start and end with a " + "A tag handle is empty or does not start and end with a " ~ "'!' character : " ~ handle); assert(prefix.length >= 1, "A tag prefix is empty"); t ~= TagDirective(handle, prefix); diff --git a/source/dyaml/emitter.d b/source/dyaml/emitter.d index fe5d997..5a047b4 100644 --- a/source/dyaml/emitter.d +++ b/source/dyaml/emitter.d @@ -228,7 +228,7 @@ struct Emitter int popIndent() @trusted { enforce(indents_.length > 0, - new YAMLException("Emitter: Need to pop an indent level but there" + new YAMLException("Emitter: Need to pop an indent level but there" ~ " are no indent levels left")); const result = indents_.back; indents_.length = indents_.length - 1; @@ -490,7 +490,7 @@ struct Emitter } break; default: - throw new Error("Expected Alias, Scalar, SequenceStart or " + throw new Error("Expected Alias, Scalar, SequenceStart or " ~ "MappingStart, but got: " ~ event_.idString); } } diff --git a/source/dyaml/flags.d b/source/dyaml/flags.d index 19e86ca..7d03bb1 100644 --- a/source/dyaml/flags.d +++ b/source/dyaml/flags.d @@ -52,17 +52,17 @@ struct Flags(names ...) if(names.length <= 8) foreach(index, name; names) { string istr = to!string(index); - result ~= "\n" - "@property bool " ~ name ~ "(bool value) pure @safe nothrow\n" - "{\n" - " flags_ = value ? flags_ | (1 <<" ~ istr ~ ")\n" - " : flags_ & (0xFF ^ (1 << " ~ istr ~"));\n" - " return value;\n" - "}\n" - "\n" - "@property bool " ~ name ~ "() const pure @safe nothrow\n" - "{\n" - " return (flags_ >> " ~ istr ~ ") & 1;\n" + result ~= "\n" ~ + "@property bool " ~ name ~ "(bool value) pure @safe nothrow\n" ~ + "{\n" ~ + " flags_ = value ? flags_ | (1 <<" ~ istr ~ ")\n" ~ + " : flags_ & (0xFF ^ (1 << " ~ istr ~"));\n" ~ + " return value;\n" ~ + "}\n" ~ + "\n" ~ + "@property bool " ~ name ~ "() const pure @safe nothrow\n" ~ + "{\n" ~ + " return (flags_ >> " ~ istr ~ ") & 1;\n" ~ "}\n"; } return result; diff --git a/source/dyaml/node.d b/source/dyaml/node.d index 03d4d4d..57e102e 100644 --- a/source/dyaml/node.d +++ b/source/dyaml/node.d @@ -454,7 +454,7 @@ struct Node in { assert(keys.length == values.length, - "Lengths of keys and values arrays to construct " + "Lengths of keys and values arrays to construct " ~ "a YAML node from don't match"); } body diff --git a/source/dyaml/reader.d b/source/dyaml/reader.d index b2a25a4..3cfe69a 100644 --- a/source/dyaml/reader.d +++ b/source/dyaml/reader.d @@ -99,7 +99,7 @@ final class Reader auto endianResult = fixUTFByteOrder(buffer); if(endianResult.bytesStripped > 0) { - throw new ReaderException("Size of UTF-16 or UTF-32 input not aligned " + throw new ReaderException("Size of UTF-16 or UTF-32 input not aligned " ~ "to 2 or 4 bytes, respectively"); } diff --git a/source/dyaml/resolver.d b/source/dyaml/resolver.d index fca505e..9344c7e 100644 --- a/source/dyaml/resolver.d +++ b/source/dyaml/resolver.d @@ -226,33 +226,33 @@ final class Resolver void addImplicitResolvers() @safe { addImplicitResolver("tag:yaml.org,2002:bool", - regex(r"^(?:yes|Yes|YES|no|No|NO|true|True|TRUE" + regex(r"^(?:yes|Yes|YES|no|No|NO|true|True|TRUE" ~ "|false|False|FALSE|on|On|ON|off|Off|OFF)$"), "yYnNtTfFoO"); addImplicitResolver("tag:yaml.org,2002:float", - regex(r"^(?:[-+]?([0-9][0-9_]*)\\.[0-9_]*" - "(?:[eE][-+][0-9]+)?|[-+]?(?:[0-9][0-9_]" - "*)?\\.[0-9_]+(?:[eE][-+][0-9]+)?|[-+]?" - "[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]" - "*|[-+]?\\.(?:inf|Inf|INF)|\\." + regex(r"^(?:[-+]?([0-9][0-9_]*)\\.[0-9_]*" ~ + "(?:[eE][-+][0-9]+)?|[-+]?(?:[0-9][0-9_]" ~ + "*)?\\.[0-9_]+(?:[eE][-+][0-9]+)?|[-+]?" ~ + "[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]" ~ + "*|[-+]?\\.(?:inf|Inf|INF)|\\." ~ "(?:nan|NaN|NAN))$"), "-+0123456789."); addImplicitResolver("tag:yaml.org,2002:int", - regex(r"^(?:[-+]?0b[0-1_]+" - "|[-+]?0[0-7_]+" - "|[-+]?(?:0|[1-9][0-9_]*)" - "|[-+]?0x[0-9a-fA-F_]+" + regex(r"^(?:[-+]?0b[0-1_]+" ~ + "|[-+]?0[0-7_]+" ~ + "|[-+]?(?:0|[1-9][0-9_]*)" ~ + "|[-+]?0x[0-9a-fA-F_]+" ~ "|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$"), "-+0123456789"); addImplicitResolver("tag:yaml.org,2002:merge", regex(r"^<<$"), "<"); addImplicitResolver("tag:yaml.org,2002:null", regex(r"^$|^(?:~|null|Null|NULL)$"), "~nN\0"); addImplicitResolver("tag:yaml.org,2002:timestamp", - regex(r"^[0-9][0-9][0-9][0-9]-[0-9][0-9]-" - "[0-9][0-9]|[0-9][0-9][0-9][0-9]-[0-9]" - "[0-9]?-[0-9][0-9]?[Tt]|[ \t]+[0-9]" - "[0-9]?:[0-9][0-9]:[0-9][0-9]" - "(?:\\.[0-9]*)?(?:[ \t]*Z|[-+][0-9]" + regex(r"^[0-9][0-9][0-9][0-9]-[0-9][0-9]-" ~ + "[0-9][0-9]|[0-9][0-9][0-9][0-9]-[0-9]" ~ + "[0-9]?-[0-9][0-9]?[Tt]|[ \t]+[0-9]" ~ + "[0-9]?:[0-9][0-9]:[0-9][0-9]" ~ + "(?:\\.[0-9]*)?(?:[ \t]*Z|[-+][0-9]" ~ "[0-9]?(?::[0-9][0-9])?)?$"), "0123456789"); addImplicitResolver("tag:yaml.org,2002:value", regex(r"^=$"), "="); diff --git a/source/dyaml/scanner.d b/source/dyaml/scanner.d index 9de772a..fb3d696 100644 --- a/source/dyaml/scanner.d +++ b/source/dyaml/scanner.d @@ -320,7 +320,7 @@ final class Scanner default: if(checkPlain()) { return fetchPlain(); } } - throw new ScannerException("While scanning for the next token, found character " + throw new ScannerException("While scanning for the next token, found character " ~ "\'%s\', index %s that cannot start any token" .format(c, to!int(c)), reader_.mark); } @@ -368,7 +368,7 @@ final class Scanner { // Check if a simple key is required at the current position. const required = (flowLevel_ == 0 && indent_ == reader_.column); - assert(allowSimpleKey_ || !required, "A simple key is required only if it is " + assert(allowSimpleKey_ || !required, "A simple key is required only if it is " ~ "the first token in the current line. Therefore it is always allowed."); if(!allowSimpleKey_) { return; } @@ -1606,7 +1606,7 @@ final class Scanner if(overflow) { error("While scanning a double quoted scalar", startMark, - "overflow when parsing an escape sequence of " + "overflow when parsing an escape sequence of " ~ "hexadecimal numbers.", reader_.mark); return; } @@ -1773,7 +1773,7 @@ final class Scanner reader_.sliceBuilder.finish(); reader_.forward(length); error("While scanning a plain scalar", startMark, - "found unexpected ':' . Please check " + "found unexpected ':' . Please check " ~ "http://pyyaml.org/wiki/YAMLColonInFlowContext for details.", reader_.mark); return Token.init; @@ -2015,7 +2015,7 @@ final class Scanner const dchar c = reader_.peek(k); if(!c.isHexDigit) { - auto msg = expected("URI escape sequence of 2 hexadecimal " + auto msg = expected("URI escape sequence of 2 hexadecimal " ~ "numbers", c); error(contextMsg, startMark, msg, reader_.mark); return; From 2a2429f2e36dba34a33393cf95619abd710ff421 Mon Sep 17 00:00:00 2001 From: ZombineDev Date: Sun, 6 Nov 2016 20:42:00 +0200 Subject: [PATCH 03/13] Update README --- README.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.rst b/README.rst index 3adc177..fe44e65 100644 --- a/README.rst +++ b/README.rst @@ -2,6 +2,10 @@ D:YAML 0.5 ========== +This is a fork of the D:YAML library by kiith-sa (https://github.com/kiith-sa/D-YAML). +The intent of this fork is to provide an version suitable for use by dlang-tour, until +fixes are accepted upstream. + .. image:: https://travis-ci.org/kiith-sa/D-YAML.svg?branch=master .. image:: https://raw.githubusercontent.com/kiith-sa/D-YAML/master/code.dlang.org-shield.png :target: http://code.dlang.org From 388a8e9c947bac35aba04684a59e143834cc4b72 Mon Sep 17 00:00:00 2001 From: ZombineDev Date: Tue, 15 Nov 2016 16:32:05 +0200 Subject: [PATCH 04/13] Update dub.json Rename package.json to dub.json and update the package name, so it can be registered on code.dlang.org. --- package.json => dub.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename package.json => dub.json (95%) diff --git a/package.json b/dub.json similarity index 95% rename from package.json rename to dub.json index 4af35a1..aff9cdf 100644 --- a/package.json +++ b/dub.json @@ -1,5 +1,5 @@ { - "name": "dyaml", + "name": "dyaml-dlang-tour", "description": "YAML parser and emitter", "authors": [ "Ferdinand Majerech" ], "libs": [], From 5a9a71ad0011aac02a06de3b10fef055a992f2a1 Mon Sep 17 00:00:00 2001 From: ZombineDev Date: Tue, 15 Nov 2016 17:07:42 +0200 Subject: [PATCH 05/13] Update readme --- README.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index fe44e65..5a30c2a 100644 --- a/README.rst +++ b/README.rst @@ -3,12 +3,12 @@ D:YAML 0.5 ========== This is a fork of the D:YAML library by kiith-sa (https://github.com/kiith-sa/D-YAML). -The intent of this fork is to provide an version suitable for use by dlang-tour, until +The intent of this fork is to provide a version suitable for use by dlang-tour, until fixes are accepted upstream. -.. image:: https://travis-ci.org/kiith-sa/D-YAML.svg?branch=master -.. image:: https://raw.githubusercontent.com/kiith-sa/D-YAML/master/code.dlang.org-shield.png - :target: http://code.dlang.org +.. image:: https://travis-ci.org/dlang-tour/d-yaml-dlang-tour.svg?branch=master +.. image:: https://img.shields.io/dub/v/dyaml-dlang-tour.svg + :target: http://code.dlang.org/packages/dyaml-dlang-tour **Note**: D:YAML 0.5 brings some **breaking changes**. See the `changelog `_. From 503357e3ea9bf52aeba2da4c62926b36679ce3a8 Mon Sep 17 00:00:00 2001 From: ZombineDev Date: Tue, 15 Nov 2016 20:07:48 +0200 Subject: [PATCH 06/13] Stop using implicit string literal concatenation in one more place Implicit string litaral concatenation was deprecated with this DMD PR: dlang/dmd/pull/6155. --- source/dyaml/loader.d | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/dyaml/loader.d b/source/dyaml/loader.d index 6d0b811..33ae4db 100644 --- a/source/dyaml/loader.d +++ b/source/dyaml/loader.d @@ -380,9 +380,9 @@ struct Loader unittest { - char[] yaml_input = "red: '#ff0000'\n" - "green: '#00ff00'\n" - "blue: '#0000ff'".dup; + char[] yaml_input = ("red: '#ff0000'\n" ~ + "green: '#00ff00'\n" ~ + "blue: '#0000ff'").dup; auto colors = Loader.fromString(yaml_input).load(); From 22c432fb52b4dfdc9a0872e9a9ae2a0ec7e2e2b5 Mon Sep 17 00:00:00 2001 From: "Forb.Jok" Date: Thu, 8 Dec 2016 17:59:22 +0100 Subject: [PATCH 07/13] Make Node type identification properties public --- source/dyaml/node.d | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/dyaml/node.d b/source/dyaml/node.d index 57e102e..861339e 100644 --- a/source/dyaml/node.d +++ b/source/dyaml/node.d @@ -1631,6 +1631,7 @@ struct Node return (cast(nothrowType)&value_.type)(); } + public: // Determine if the value stored by the node is of specified type. // // This only works for default YAML types, not for user defined types. @@ -1639,7 +1640,6 @@ struct Node return this.type is typeid(Unqual!T); } - private: // Is the value a bool? alias isType!bool isBool; @@ -1685,6 +1685,7 @@ struct Node else {return false;} } + private: // Implementation of contains() and containsKey(). bool contains_(T, Flag!"key" key, string func)(T rhs) const @trusted { From 6e0ef1aae038ceb239785a2a17a466eb03477f6f Mon Sep 17 00:00:00 2001 From: forbjok Date: Sat, 10 Dec 2016 18:45:43 +0100 Subject: [PATCH 08/13] Fix dumping to file producing garbage (#3) * Fix dumping to file producing garbage * Use rawWrite instead of casting to char[] --- source/dyaml/stream.d | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/dyaml/stream.d b/source/dyaml/stream.d index 01fd4af..b8cf4a0 100644 --- a/source/dyaml/stream.d +++ b/source/dyaml/stream.d @@ -58,11 +58,11 @@ class YFile : YStream { } void writeExact(const void* buffer, size_t size) { - this.file.write(cast(const ubyte[])buffer[0 .. size]); + this.file.rawWrite(cast(const) buffer[0 .. size]); } size_t write(const(ubyte)[] buffer) { - this.file.write(buffer); + this.file.rawWrite(buffer); return buffer.length; } From 655a0aadd22213ca29808aca4eb078280811036e Mon Sep 17 00:00:00 2001 From: ZombineDev Date: Sat, 10 Dec 2016 19:56:44 +0200 Subject: [PATCH 09/13] Add test for file dumping --- source/dyaml/stream.d | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/source/dyaml/stream.d b/source/dyaml/stream.d index b8cf4a0..d788dcb 100644 --- a/source/dyaml/stream.d +++ b/source/dyaml/stream.d @@ -72,3 +72,29 @@ class YFile : YStream { @property bool writeable() { return true; } } + +unittest { + import dyaml.dumper, dyaml.loader, dyaml.node; + import std.file : readText, remove; + + char[] test = ("Hello World : [Hello, World]\n" ~ + "Answer: 42").dup; + //Read the input. + Node expected = Loader.fromString(test).load(); + assert(expected["Hello World"][0] == "Hello"); + assert(expected["Hello World"][1] == "World"); + assert(expected["Answer"].as!int == 42); + + //Dump the loaded document to output.yaml. + Dumper("output.yaml").dump(expected); + + // Load the file and verify that it was saved correctly. + Node actual = Loader("output.yaml").load(); + assert(actual["Hello World"][0] == "Hello"); + assert(actual["Hello World"][1] == "World"); + assert(actual["Answer"].as!int == 42); + assert(actual == expected); + + // Clean up. + remove("output.yaml"); +} From 1ce9935a06680eec0d6ed77cff30c256c6abdb18 Mon Sep 17 00:00:00 2001 From: ZombineDev Date: Sat, 10 Dec 2016 19:57:37 +0200 Subject: [PATCH 10/13] Update Travis-CI badge in the readme --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 5a30c2a..7d9f3d3 100644 --- a/README.rst +++ b/README.rst @@ -6,7 +6,7 @@ This is a fork of the D:YAML library by kiith-sa (https://github.com/kiith-sa/D- The intent of this fork is to provide a version suitable for use by dlang-tour, until fixes are accepted upstream. -.. image:: https://travis-ci.org/dlang-tour/d-yaml-dlang-tour.svg?branch=master +.. image:: https://travis-ci.org/dlang-tour/dyaml-dlang-tour.svg?branch=master .. image:: https://img.shields.io/dub/v/dyaml-dlang-tour.svg :target: http://code.dlang.org/packages/dyaml-dlang-tour From 21c2f11a4f3b56fa3ffec2d3295a79faec3268f7 Mon Sep 17 00:00:00 2001 From: ZombineDev Date: Sat, 10 Dec 2016 19:58:51 +0200 Subject: [PATCH 11/13] Add .editorconfig to enforce existing style --- .editorconfig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..aa1b122 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,14 @@ +# EditorConfig file: http://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +end_of_line = lf +insert_final_newline = true + +[*.d] +charset = utf-8 +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true From b01e99d8e624b3c376eeb52c0418a64924bb911f Mon Sep 17 00:00:00 2001 From: ZombineDev Date: Sat, 10 Dec 2016 20:07:14 +0200 Subject: [PATCH 12/13] Update source/dyaml/stream.d to conform to the existing coding style --- source/dyaml/stream.d | 151 +++++++++++++++++++++++------------------- 1 file changed, 82 insertions(+), 69 deletions(-) diff --git a/source/dyaml/stream.d b/source/dyaml/stream.d index d788dcb..6e3985c 100644 --- a/source/dyaml/stream.d +++ b/source/dyaml/stream.d @@ -1,100 +1,113 @@ module dyaml.stream; -enum BOM { - UTF8, /// UTF-8 - UTF16LE, /// UTF-16 Little Endian - UTF16BE, /// UTF-16 Big Endian - UTF32LE, /// UTF-32 Little Endian - UTF32BE, /// UTF-32 Big Endian +enum BOM +{ + UTF8, /// UTF-8 + UTF16LE, /// UTF-16 Little Endian + UTF16BE, /// UTF-16 Big Endian + UTF32LE, /// UTF-32 Little Endian + UTF32BE, /// UTF-32 Big Endian } import std.system; private enum int NBOMS = 5; immutable Endian[NBOMS] BOMEndian = -[ std.system.endian, - Endian.littleEndian, Endian.bigEndian, - Endian.littleEndian, Endian.bigEndian - ]; +[ + std.system.endian, + Endian.littleEndian, Endian.bigEndian, + Endian.littleEndian, Endian.bigEndian +]; immutable ubyte[][NBOMS] ByteOrderMarks = -[ [0xEF, 0xBB, 0xBF], - [0xFF, 0xFE], - [0xFE, 0xFF], - [0xFF, 0xFE, 0x00, 0x00], - [0x00, 0x00, 0xFE, 0xFF] - ]; +[ + [0xEF, 0xBB, 0xBF], + [0xFF, 0xFE], + [0xFE, 0xFF], + [0xFF, 0xFE, 0x00, 0x00], + [0x00, 0x00, 0xFE, 0xFF] +]; -interface YStream { - void writeExact(const void* buffer, size_t size); - size_t write(const(ubyte)[] buffer); - void flush(); - @property bool writeable(); +interface YStream +{ + void writeExact(const void* buffer, size_t size); + size_t write(const(ubyte)[] buffer); + void flush(); + @property bool writeable(); } -class YMemoryStream : YStream { - ubyte[] data; +class YMemoryStream : YStream +{ + ubyte[] data; - void writeExact(const void* buffer, size_t size) { - data ~= cast(ubyte[])buffer[0 .. size]; - } + void writeExact(const void* buffer, size_t size) + { + data ~= cast(ubyte[])buffer[0 .. size]; + } - size_t write(const(ubyte)[] buffer) { - data ~= buffer; - return buffer.length; - } + size_t write(const(ubyte)[] buffer) + { + data ~= buffer; + return buffer.length; + } - void flush() {} + void flush() {} - @property bool writeable() { return true; } + @property bool writeable() { return true; } } -class YFile : YStream { - static import std.stdio; - std.stdio.File file; +class YFile : YStream +{ + static import std.stdio; + std.stdio.File file; - this(string fn) { - this.file = std.stdio.File(fn, "w"); - } + this(string fn) + { + this.file = std.stdio.File(fn, "w"); + } - void writeExact(const void* buffer, size_t size) { - this.file.rawWrite(cast(const) buffer[0 .. size]); - } + void writeExact(const void* buffer, size_t size) + { + this.file.rawWrite(cast(const) buffer[0 .. size]); + } - size_t write(const(ubyte)[] buffer) { - this.file.rawWrite(buffer); - return buffer.length; - } + size_t write(const(ubyte)[] buffer) + { + this.file.rawWrite(buffer); + return buffer.length; + } - void flush() { - this.file.flush(); - } + void flush() + { + this.file.flush(); + } - @property bool writeable() { return true; } + @property bool writeable() { return true; } } -unittest { - import dyaml.dumper, dyaml.loader, dyaml.node; - import std.file : readText, remove; +unittest +{ + import dyaml.dumper, dyaml.loader, dyaml.node; + import std.file : readText, remove; - char[] test = ("Hello World : [Hello, World]\n" ~ - "Answer: 42").dup; - //Read the input. - Node expected = Loader.fromString(test).load(); - assert(expected["Hello World"][0] == "Hello"); - assert(expected["Hello World"][1] == "World"); - assert(expected["Answer"].as!int == 42); + char[] test = ("Hello World : [Hello, World]\n" ~ + "Answer: 42").dup; + //Read the input. + Node expected = Loader.fromString(test).load(); + assert(expected["Hello World"][0] == "Hello"); + assert(expected["Hello World"][1] == "World"); + assert(expected["Answer"].as!int == 42); - //Dump the loaded document to output.yaml. - Dumper("output.yaml").dump(expected); + //Dump the loaded document to output.yaml. + Dumper("output.yaml").dump(expected); - // Load the file and verify that it was saved correctly. - Node actual = Loader("output.yaml").load(); - assert(actual["Hello World"][0] == "Hello"); - assert(actual["Hello World"][1] == "World"); - assert(actual["Answer"].as!int == 42); - assert(actual == expected); + // Load the file and verify that it was saved correctly. + Node actual = Loader("output.yaml").load(); + assert(actual["Hello World"][0] == "Hello"); + assert(actual["Hello World"][1] == "World"); + assert(actual["Answer"].as!int == 42); + assert(actual == expected); - // Clean up. - remove("output.yaml"); + // Clean up. + remove("output.yaml"); } From 38f1ba3900ba8bae78b43f696b65e52705c7fdb4 Mon Sep 17 00:00:00 2001 From: ZombineDev Date: Sat, 10 Dec 2016 20:33:43 +0200 Subject: [PATCH 13/13] Add support for writing strings to existing files through YFile stream --- source/dyaml/stream.d | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/source/dyaml/stream.d b/source/dyaml/stream.d index 6e3985c..6a5ac00 100644 --- a/source/dyaml/stream.d +++ b/source/dyaml/stream.d @@ -32,6 +32,7 @@ interface YStream { void writeExact(const void* buffer, size_t size); size_t write(const(ubyte)[] buffer); + size_t write(const(char)[] str); void flush(); @property bool writeable(); } @@ -51,6 +52,11 @@ class YMemoryStream : YStream return buffer.length; } + size_t write(const(char)[] str) + { + return write(cast(const(ubyte)[])str); + } + void flush() {} @property bool writeable() { return true; } @@ -66,6 +72,18 @@ class YFile : YStream this.file = std.stdio.File(fn, "w"); } + this(std.stdio.File file) + { + this.file = file; + } + + unittest + { + import std.stdio : stdout; + auto stream = new YFile(stdout); + stream.write("Test writing to stdout through YFile stream\n"); + } + void writeExact(const void* buffer, size_t size) { this.file.rawWrite(cast(const) buffer[0 .. size]); @@ -77,6 +95,11 @@ class YFile : YStream return buffer.length; } + size_t write(const(char)[] str) + { + return write(cast(const(ubyte)[])str); + } + void flush() { this.file.flush();