Skip to content

Commit ee60daf

Browse files
committed
fixes #16 - when key value contains itself a valid key and a value
1 parent 47353cf commit ee60daf

File tree

5 files changed

+60
-10
lines changed

5 files changed

+60
-10
lines changed

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Operating system (build VM template)
2-
os: Windows Server 2016
2+
os: Windows Server 2019
33

44
# If the build configuration does not specify build worker image
55
# then Visual Studio 2015 image is used.
@@ -93,7 +93,7 @@ deploy:
9393
-
9494
provider: NuGet
9595
api_key:
96-
secure: i6oWn60J7ZOM4UuYcvxbuk9OAEp6or+Wq7izyJDPNlcLIhG2UKsxz7G/8erhdY3M
96+
secure: fLRrY6iKFQhPFD5fHraib6Ot+0x3eKiVWoBCiKT5zPRCqxWxLAycIO8lJWg3o43r
9797
artifact: NuGet_Files
9898
server: # remove to push to NuGet.org
9999
skip_symbols: false

src/ConfigParser.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,10 @@ private void Read(string configContent)
665665
break;
666666

667667
case var _ when Settings.KeyMatcher.IsMatch(lineRaw):
668-
ReadKeyAndValue(ref currentSection, ref currentLine, lineRaw, lineNumber);
668+
if (!Settings.MultiLineValues.HasFlag(MultiLineValues.NotAllowed) && IsKeyLikeArrayValue(currentLine?.Content, lineRaw))
669+
ReadKeyAndValue(ref currentSection, ref currentLine, lineRaw, lineNumber, append: true, forceIncludeKey: true);
670+
else
671+
ReadKeyAndValue(ref currentSection, ref currentLine, lineRaw, lineNumber);
669672
break;
670673

671674
// Multi-line + allow value-less option on
@@ -697,6 +700,21 @@ private void Read(string configContent)
697700
sections.Add(currentSection.SectionName, currentSection);
698701
}
699702

703+
private bool IsKeyLikeArrayValue(string currentLine, string lineRaw)
704+
{
705+
if (string.IsNullOrWhiteSpace(currentLine) || string.IsNullOrWhiteSpace(lineRaw))
706+
return false;
707+
708+
var lastLine = currentLine?.Split(new string[] { Settings.NewLine }, StringSplitOptions.None)?.LastOrDefault();
709+
var prevLineMatch = Settings.ArrayStartMatcher.Match(lastLine);
710+
var curLineMatch = Settings.ArrayStartMatcher.Match(lineRaw);
711+
712+
if (!prevLineMatch.Success || !curLineMatch.Success)
713+
return false;
714+
715+
return prevLineMatch.Groups[0].Value.Length == curLineMatch.Groups[0].Value.Length;
716+
}
717+
700718
/// <summary>
701719
/// Reads the valueless key.
702720
/// </summary>
@@ -726,9 +744,11 @@ private void AppendValueToKey(ref ConfigSection currentSection, ref ConfigLine c
726744
{
727745
if (MultiLineValues.NotAllowed == Settings.MultiLineValues ||
728746
Settings.MultiLineValues.HasFlag(MultiLineValues.NotAllowed))
747+
{
729748
throw new ConfigParserException(
730749
"Multi-line values are explicitly disallowed by parser settings. Please consider changing them.",
731750
lineNumber);
751+
}
732752

733753
ReadKeyAndValue(ref currentSection, ref currentLine, lineRaw, lineNumber, true);
734754
}
@@ -745,7 +765,7 @@ private void AppendValueToKey(ref ConfigSection currentSection, ref ConfigLine c
745765
/// or</exception>
746766
/// <exception cref="NotImplementedException"></exception>
747767
private void ReadKeyAndValue(ref ConfigSection currentSection, ref ConfigLine currentLine, string lineRaw,
748-
int lineNumber, bool append = false)
768+
int lineNumber, bool append = false, bool forceIncludeKey = false)
749769
{
750770
if (null != currentLine && !append)
751771
BackupCurrentLine(ref currentSection, ref currentLine, lineNumber);
@@ -758,7 +778,8 @@ private void ReadKeyAndValue(ref ConfigSection currentSection, ref ConfigLine cu
758778
var separator = (string.IsNullOrWhiteSpace(keyMatch.Groups["separator"]?.Value))
759779
? Settings.KeyValueSeparator
760780
: keyMatch.Groups["separator"]?.Value;
761-
if (keyMatch.Success && keyMatch.Captures.Count > 0)
781+
782+
if (!forceIncludeKey && keyMatch.Success && keyMatch.Captures.Count > 0)
762783
lineRaw = lineRaw.Substring(keyMatch.Captures[0].Value.Length);
763784

764785
var valueMatch = Settings.ValueMatcher.Match(lineRaw);

src/ConfigParser.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
<Copyright>Copyright 2015 (c) Salaros</Copyright>
1919
<Title>ConfigParser</Title>
2020
<Description>A slim, cross-platform, fully managed C# library for reading/writing .ini, .conf, .cfg etc configuration files.</Description>
21-
<Version>1.0.0</Version>
22-
<AssemblyVersion>1.0.0</AssemblyVersion>
23-
<FileVersion>1.0.0</FileVersion>
21+
<Version>0.3.7</Version>
22+
<AssemblyVersion>0.3.7</AssemblyVersion>
23+
<FileVersion>0.3.7</FileVersion>
2424
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
2525
<PackageLicenseUrl>https://raw.githubusercontent.com/salaros/config-parser/master/LICENSE</PackageLicenseUrl>
2626
<PackageProjectUrl>https://github.com/salaros/config-parser</PackageProjectUrl>
@@ -67,5 +67,5 @@
6767
<Delete Files="@(NuGetFilesBin)" />
6868
<Delete Files="@(NuGetFilesProject)" />
6969
</Target>
70-
70+
7171
</Project>

src/ConfigParserSettings.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Salaros.Configuration
1010
{
1111
public class ConfigParserSettings
1212
{
13-
protected Regex keyMatcher, commentMatcher, valueMatcher;
13+
protected Regex keyMatcher, commentMatcher, valueMatcher, arrayStartMatcher;
1414

1515
/// <summary>
1616
/// Initializes the <see cref="ConfigParserSettings"/> class.
@@ -84,6 +84,11 @@ static ConfigParserSettings()
8484
/// </value>
8585
internal static Regex SectionMatcher { get; set; }
8686

87+
/// <summary>Gets the array value line matcher (matches the whitespaces at the beggining of each array value).</summary>
88+
/// <value>The array start matcher.</value>
89+
internal Regex ArrayStartMatcher => arrayStartMatcher ?? (arrayStartMatcher =
90+
new Regex(@"^(\s{1,})", RegexOptions.Compiled));
91+
8792
/// <summary>
8893
/// Gets the comment matcher.
8994
/// </summary>

tests/ConfigParserTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,29 @@ public void FileCanBeSavedToNewPath()
5757

5858
Assert.True(File.Exists(configFilePathTmpNew));
5959
}
60+
61+
[Fact]
62+
public void ArrayIsReadCorrectly()
63+
{
64+
// Set up
65+
var settings = new ConfigParserSettings { MultiLineValues = MultiLineValues.Simple | MultiLineValues.QuoteDelimitedValues };
66+
var configFile = new ConfigParser(
67+
@"[Advanced]
68+
Select =
69+
select * from
70+
from table
71+
where ID = '5'
72+
",
73+
settings);
74+
75+
// Act
76+
var arrayValues = configFile.GetArrayValue("Advanced", "Select");
77+
78+
// Assert
79+
Assert.Equal(3, arrayValues?.Length ?? 0);
80+
Assert.Equal("select * from", arrayValues[0]);
81+
Assert.Equal("from table", arrayValues[1]);
82+
Assert.Equal("where ID = '5'", arrayValues[2]);
83+
}
6084
}
6185
}

0 commit comments

Comments
 (0)