Skip to content

Commit 722f709

Browse files
authored
Merge pull request #2 from rboy1/patch-1
Fix for handling Inline Comments
2 parents 845aa64 + 9f850d5 commit 722f709

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

Src/ConfigurationReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private static void Parse(StringReader reader, Configuration config)
5656
}
5757

5858
var lineWithoutComment = line;
59-
if (commentIndex > 0)
59+
if (!Configuration.IgnoreInlineComments && commentIndex > 0)
6060
{
6161
lineWithoutComment = line.Remove(commentIndex).Trim(); // remove inline comment
6262
}

Src/Setting.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public Setting(string name, object value) : base(name)
5252
/// </summary>
5353
public string StringValue
5454
{
55-
get => GetValue<string>().Trim('\"');
56-
set => SetValue(value.Trim('\"'));
55+
get => Configuration.OutputRawStringValues ? GetValue<string>() : GetValue<string>().Trim('\"');
56+
set => SetValue(Configuration.OutputRawStringValues ? value : value.Trim('\"'));
5757
}
5858

5959
/// <summary>

Src/StockStringConverters.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,11 @@ public override object TryConvertFromString(string value, Type hint)
325325

326326
internal sealed class StringStringConverter : TypeStringConverter<string>
327327
{
328-
public override string ConvertToString(object value) => value.ToString().Trim('\"');
328+
public override string ConvertToString(object value)
329+
=> Configuration.OutputRawStringValues ? value.ToString() : value.ToString().Trim('\"');
329330

330-
public override object TryConvertFromString(string value, Type hint) => value.Trim('\"');
331+
public override object TryConvertFromString(string value, Type hint)
332+
=> Configuration.OutputRawStringValues ? value : value.Trim('\"');
331333
}
332334

333335
internal sealed class UInt16StringConverter : TypeStringConverter<ushort>

Tests/SimpleConfigTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,32 @@ public void GetValueOrDefault()
713713
Assert.AreEqual(setting.GetValue(typeof(ulong)), 7654321);
714714
}
715715

716+
[Test]
717+
public void TestCommentsInSectionNames()
718+
{
719+
var previous = Configuration.IgnoreInlineComments;
720+
Configuration.IgnoreInlineComments = true;
721+
722+
var cfg = new Configuration();
723+
cfg.Add("SomeSection1#NotAComment");
724+
cfg.Add(new Section("SomeSection2# also part of the section's name"));
725+
726+
cfg["SomeSection1#NotAComment"].Add("Setting", 1);
727+
cfg["SomeSection2# also part of the section's name"].Add("Setting", 2);
728+
729+
TestWithFile(filename =>
730+
{
731+
cfg.SaveToFile(filename);
732+
733+
var parsedCfg = Configuration.LoadFromFile(filename);
734+
735+
Assert.AreEqual("SomeSection1#NotAComment", parsedCfg[0].Name);
736+
Assert.AreEqual("SomeSection2# also part of the section's name", parsedCfg[1].Name);
737+
});
738+
739+
Configuration.IgnoreInlineComments = previous;
740+
}
741+
716742
private static void TestWithFile(Action<string> action)
717743
{
718744
string filename = Path.GetTempFileName();

0 commit comments

Comments
 (0)