diff --git a/Src/ConfigurationReader.cs b/Src/ConfigurationReader.cs index 3ad36fc..b23a086 100644 --- a/Src/ConfigurationReader.cs +++ b/Src/ConfigurationReader.cs @@ -56,7 +56,7 @@ private static void Parse(StringReader reader, Configuration config) } var lineWithoutComment = line; - if (commentIndex > 0) + if (!Configuration.IgnoreInlineComments && commentIndex > 0) { lineWithoutComment = line.Remove(commentIndex).Trim(); // remove inline comment } diff --git a/Src/Setting.cs b/Src/Setting.cs index 93a6ce3..d3d2c82 100644 --- a/Src/Setting.cs +++ b/Src/Setting.cs @@ -52,8 +52,8 @@ public Setting(string name, object value) : base(name) /// public string StringValue { - get => GetValue().Trim('\"'); - set => SetValue(value.Trim('\"')); + get => Configuration.OutputRawStringValues ? GetValue() : GetValue().Trim('\"'); + set => SetValue(Configuration.OutputRawStringValues ? value : value.Trim('\"')); } /// diff --git a/Src/StockStringConverters.cs b/Src/StockStringConverters.cs index b448fcc..612480f 100644 --- a/Src/StockStringConverters.cs +++ b/Src/StockStringConverters.cs @@ -325,9 +325,11 @@ public override object TryConvertFromString(string value, Type hint) internal sealed class StringStringConverter : TypeStringConverter { - public override string ConvertToString(object value) => value.ToString().Trim('\"'); + public override string ConvertToString(object value) + => Configuration.OutputRawStringValues ? value.ToString() : value.ToString().Trim('\"'); - public override object TryConvertFromString(string value, Type hint) => value.Trim('\"'); + public override object TryConvertFromString(string value, Type hint) + => Configuration.OutputRawStringValues ? value : value.Trim('\"'); } internal sealed class UInt16StringConverter : TypeStringConverter diff --git a/Tests/SimpleConfigTest.cs b/Tests/SimpleConfigTest.cs index d1055b3..6e7e0fd 100644 --- a/Tests/SimpleConfigTest.cs +++ b/Tests/SimpleConfigTest.cs @@ -713,6 +713,32 @@ public void GetValueOrDefault() Assert.AreEqual(setting.GetValue(typeof(ulong)), 7654321); } + [Test] + public void TestCommentsInSectionNames() + { + var previous = Configuration.IgnoreInlineComments; + Configuration.IgnoreInlineComments = true; + + var cfg = new Configuration(); + cfg.Add("SomeSection1#NotAComment"); + cfg.Add(new Section("SomeSection2# also part of the section's name")); + + cfg["SomeSection1#NotAComment"].Add("Setting", 1); + cfg["SomeSection2# also part of the section's name"].Add("Setting", 2); + + TestWithFile(filename => + { + cfg.SaveToFile(filename); + + var parsedCfg = Configuration.LoadFromFile(filename); + + Assert.AreEqual("SomeSection1#NotAComment", parsedCfg[0].Name); + Assert.AreEqual("SomeSection2# also part of the section's name", parsedCfg[1].Name); + }); + + Configuration.IgnoreInlineComments = previous; + } + private static void TestWithFile(Action action) { string filename = Path.GetTempFileName();