Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more support for whitespace configuration #211

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/IniParser.Tests/Integration/ConfigurationOptionTests.cs
Original file line number Diff line number Diff line change
@@ -157,7 +157,7 @@ public void check_skip_invalid_lines()
}

[Test]
public void check_trim_properties()
public void check_trim_properties_and_values()
{
var ini = @"prop1=0
prop2 = value2
@@ -172,6 +172,7 @@ public void check_trim_properties()
Assert.That(iniData.Global["prop2"], Is.EqualTo("value2"));

parser.Configuration.TrimProperties = false;
parser.Configuration.TrimValues = false;
iniData = parser.Parse(ini);

Assert.That(iniData.Global.Contains("prop1"), Is.True);
2 changes: 2 additions & 0 deletions src/IniParser.Tests/Integration/SchemeOptionTests.cs
Original file line number Diff line number Diff line change
@@ -106,6 +106,8 @@ public void check_property_assignment_string()
var ini2 = @"key1 <== 1";
parser.Scheme.PropertyAssigmentString = "<==";
iniData = parser.Parse(ini2);

Assert.That(iniData.Global["key1"], Is.EqualTo("1"));
}
}
}
38 changes: 38 additions & 0 deletions src/IniParser.Tests/Unit/Parser/ParserTests.cs
Original file line number Diff line number Diff line change
@@ -502,5 +502,43 @@ [W103 0.5' wc]
Assert.That(parsedData.Sections["W101 0.5\" wc"], Is.Not.Empty);
Assert.That(parsedData.Sections["W103 0.5' wc"], Is.Not.Empty);
}

[Test, Description("Allow leading spaces in property values")]
public void can_preserve_leading_space_in_value()
{
var parser = new IniDataParser();
parser.Configuration.TrimValues = false;
parser.Scheme.PropertyAssigmentString = " = ";

var iniDataString = @"[Section 1]
key1 = value1
key2 = value2
key3 = value3 ";
IniData parsedData = parser.Parse(iniDataString);

Assert.That(parsedData.Sections["Section 1"]["key1"], Is.EqualTo(" value1"));
Assert.That(parsedData.Sections["Section 1"]["key2"], Is.EqualTo("value2"));
Assert.That(parsedData.Sections["Section 1"]["key3"], Is.EqualTo("value3 "));
}

[Test, Description("Allow leading and railing space in the assignment string. Useful for reading properties with trailing space and values with leading space")]
public void allow_leading_and_trailing_space_in_assignment_string()
{
var parser = new IniDataParser();
parser.Scheme.PropertyAssigmentString = " = ";
parser.Configuration.TrimValues = false;
parser.Configuration.TrimProperties = false;

var iniDataString = @"[Section 1]
key1 = value1
key2 = value2
key3 = value3";
IniData parsedData = parser.Parse(iniDataString);

Assert.That(parsedData.Sections["Section 1"]["key1"], Is.EqualTo("value1"));
Assert.That(parsedData.Sections["Section 1"]["key2 "], Is.EqualTo(" value2"));
Assert.That(parsedData.Sections["Section 1"]["key3"], Is.EqualTo("value3"));
Assert.That(parsedData.Sections["Section 1"]["key2"], Is.Null);
}
}
}
11 changes: 11 additions & 0 deletions src/IniParser/Configuration/IniParserConfiguration.cs
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ public IniParserConfiguration()
SkipInvalidLines = ori.SkipInvalidLines;
TrimSections = ori.TrimSections;
TrimProperties = ori.TrimProperties;
TrimValues = ori.TrimValues;
}

/// <summary>
@@ -152,6 +153,16 @@ public enum EDuplicatePropertiesBehaviour
/// </remarks>
public bool TrimProperties { get; set; } = true;

/// <summary>
/// If set to true, it will trim the whitespace out of the value when parsing.
/// If set to false, it will consider all the whitespace in the line as part of the
/// value when extracting the key and values.
/// </summary>
/// <remarks>
/// Defaults to true.
/// </remarks>
public bool TrimValues { get; set; } = true;

/// <summary>
/// If set to true, it will trim the whitespace out of the section name when parsing.
/// If set to false, it will consider all the whitespace in the line as part of the
2 changes: 1 addition & 1 deletion src/IniParser/Configuration/IniScheme.cs
Original file line number Diff line number Diff line change
@@ -103,7 +103,7 @@ public string SectionEndString
public string PropertyAssigmentString
{
get => string.IsNullOrWhiteSpace(_propertyAssigmentString) ? "=" : _propertyAssigmentString;
set => _propertyAssigmentString = value?.Trim();
set => _propertyAssigmentString = value;
}

#region IDeepCloneable<T> Members
4 changes: 4 additions & 0 deletions src/IniParser/IniDataParser.cs
Original file line number Diff line number Diff line change
@@ -356,6 +356,10 @@ protected virtual bool ProcessProperty(StringBuffer currentLine, IniData iniData
if (Configuration.TrimProperties)
{
key.Trim();
}

if (Configuration.TrimValues)
{
value.Trim();
}