Fix MiniYaml parsing of empty comments

This commit is contained in:
Paul Chote
2019-12-19 20:45:27 +00:00
committed by reaperrr
parent aabfd91001
commit 0b8a367867
2 changed files with 5 additions and 2 deletions

View File

@@ -225,7 +225,7 @@ namespace OpenRA
if (commentStart < 0 && line[i] == '#' && (i == 0 || line[i - 1] != '\\')) if (commentStart < 0 && line[i] == '#' && (i == 0 || line[i - 1] != '\\'))
{ {
commentStart = i + 1; commentStart = i + 1;
if (commentStart < keyLength) if (commentStart <= keyLength)
keyLength = i - keyStart; keyLength = i - keyStart;
else else
valueLength = i - valueStart; valueLength = i - valueStart;
@@ -446,7 +446,7 @@ namespace OpenRA
{ {
var hasKey = !string.IsNullOrEmpty(key); var hasKey = !string.IsNullOrEmpty(key);
var hasValue = !string.IsNullOrEmpty(Value); var hasValue = !string.IsNullOrEmpty(Value);
var hasComment = !string.IsNullOrEmpty(comment); var hasComment = comment != null;
yield return (hasKey ? key + ":" : "") yield return (hasKey ? key + ":" : "")
+ (hasValue ? " " + Value.Replace("#", "\\#") : "") + (hasValue ? " " + Value.Replace("#", "\\#") : "")
+ (hasComment ? (hasKey || hasValue ? " " : "") + "#" + comment : ""); + (hasComment ? (hasKey || hasValue ? " " : "") + "#" + comment : "");

View File

@@ -246,10 +246,13 @@ TestB:
{ {
var yaml = @" var yaml = @"
# Top level comment node # Top level comment node
#
Parent: # comment without value Parent: # comment without value
# Indented comment node # Indented comment node
First: value containing a \# character First: value containing a \# character
Second: value # node with inline comment Second: value # node with inline comment
Third: value #
Fourth: #
".Replace("\r\n", "\n"); ".Replace("\r\n", "\n");
var result = MiniYaml.FromString(yaml, discardCommentsAndWhitespace: false).WriteToString(); var result = MiniYaml.FromString(yaml, discardCommentsAndWhitespace: false).WriteToString();