Skip to content

Commit 8eb9aa3

Browse files
committed
#724 Fixed splitting of possible glob patterns (contributed by @karl-sjogren)
2 parents ec0903e + 105e3f3 commit 8eb9aa3

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

src/Readme.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ For further details take a look at LICENSE.txt.
6767

6868
CHANGELOG
6969

70+
5.4.5.0
71+
72+
* Fix: #724 Fixed splitting of possible glob patterns (contributed by @karl-sjogren)
73+
7074
5.4.4.0
7175

7276
* New: #714 Improved AoT support

src/ReportGenerator.Core.Test/Common/StringExtensionsTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,15 @@ public void ParseLargeInteger_CorrectResultReturned()
1313
Assert.Equal(1000, "1000".ParseLargeInteger());
1414
Assert.Equal(int.MaxValue, "2147483649".ParseLargeInteger());
1515
}
16+
17+
[Theory]
18+
[InlineData("a,b,c", new[] { "a", "b", "c" })]
19+
[InlineData("a,{b,c};d", new[] { "a", "{b,c}", "d" })]
20+
[InlineData("a,{b,c;d", new[] { "a", "{b", "c", "d" })]
21+
public void SplitThatEnsuresGlobsAreSafe_CorrectResultReturned(string input, string[] expectedParts)
22+
{
23+
var result = input.SplitThatEnsuresGlobsAreSafe(',', ';');
24+
Assert.Equal(expectedParts, result);
25+
}
1626
}
1727
}

src/ReportGenerator.Core/Common/StringExtensions.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System.Collections.Generic;
12
using System.Globalization;
3+
using System.Linq;
24

35
namespace Palmmedia.ReportGenerator.Core.Common
46
{
@@ -24,5 +26,53 @@ public static int ParseLargeInteger(this string input)
2426
return int.MaxValue;
2527
}
2628
}
29+
30+
/// <summary>
31+
/// Splits the string at the specified separator, but ensures that globs are not split.
32+
/// </summary>
33+
/// <param name="input">The input string.</param>
34+
/// <param name="separators">List of separators.</param>
35+
/// <returns>The parts.</returns>
36+
public static string[] SplitThatEnsuresGlobsAreSafe(this string input, params char[] separators)
37+
{
38+
if (separators == null || separators.Length == 0)
39+
{
40+
return new string[] { input };
41+
}
42+
43+
var parts = new List<string>();
44+
var braceCount = 0;
45+
var start = 0;
46+
47+
for (int i = 0; i < input.Length; i++)
48+
{
49+
if (input[i] == '{')
50+
{
51+
braceCount++;
52+
}
53+
else if (input[i] == '}')
54+
{
55+
braceCount--;
56+
}
57+
58+
if (braceCount > 0 && input.IndexOf('}', i + 1) == -1)
59+
{
60+
braceCount = 0;
61+
}
62+
63+
if (separators.Contains(input[i]) && braceCount == 0)
64+
{
65+
parts.Add(input.Substring(start, i - start).Trim());
66+
start = i + 1;
67+
}
68+
}
69+
70+
if (start < input.Length)
71+
{
72+
parts.Add(input.Substring(start).Trim());
73+
}
74+
75+
return parts.ToArray();
76+
}
2777
}
2878
}

src/ReportGenerator.Core/ReportConfigurationBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using DotNetConfig;
5+
using Palmmedia.ReportGenerator.Core.Common;
56
using Palmmedia.ReportGenerator.Core.Logging;
67
using Palmmedia.ReportGenerator.Core.Properties;
78

@@ -53,11 +54,11 @@ public ReportConfiguration Create(Dictionary<string, string> cliArguments)
5354

5455
if (namedArguments.TryGetValue(CommandLineArgumentNames.Reports, out value))
5556
{
56-
reportFilePatterns = value.Split(ArgumentSeparators, StringSplitOptions.RemoveEmptyEntries);
57+
reportFilePatterns = value.SplitThatEnsuresGlobsAreSafe(ArgumentSeparators);
5758
}
5859
else if (config.TryGetString(DotNetConfigSettingNames.Reports, out value))
5960
{
60-
reportFilePatterns = value.Split(ArgumentSeparators, StringSplitOptions.RemoveEmptyEntries);
61+
reportFilePatterns = value.SplitThatEnsuresGlobsAreSafe(ArgumentSeparators);
6162
}
6263
else
6364
{

src/ReportGenerator.MSBuild/ReportGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Build.Framework;
66
using Microsoft.Build.Utilities;
77
using Palmmedia.ReportGenerator.Core;
8+
using Palmmedia.ReportGenerator.Core.Common;
89

910
namespace Palmmedia.ReportGenerator.MSBuild
1011
{
@@ -190,7 +191,7 @@ public override bool Execute()
190191
}
191192
else if (config.TryGetString(DotNetConfigSettingNames.Reports, out value))
192193
{
193-
reportFilePatterns = value.Split(ArgumentSeparators, StringSplitOptions.RemoveEmptyEntries);
194+
reportFilePatterns = value.SplitThatEnsuresGlobsAreSafe(ArgumentSeparators);
194195
}
195196
else
196197
{

0 commit comments

Comments
 (0)