Skip to content

Commit 559d181

Browse files
authored
Fix argument escaping (#1841)
* add a failing test * fix the argument escaping
1 parent 8a88b4c commit 559d181

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/BenchmarkDotNet/Extensions/StringAndTextExtensions.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Text;
6+
using System.Text.RegularExpressions;
67

78
namespace BenchmarkDotNet.Extensions
89
{
@@ -14,7 +15,17 @@ private static readonly Lazy<Dictionary<string, string>> InvalidFileNameCharacte
1415

1516
internal static string ToLowerCase(this bool value) => value ? "true" : "false"; // to avoid .ToString().ToLower() allocation
1617

17-
internal static string Escape(this string path) => "\"" + path + "\"";
18+
// source: https://stackoverflow.com/a/12364234/5852046
19+
internal static string Escape(this string cliArg)
20+
{
21+
if (string.IsNullOrEmpty(cliArg))
22+
return cliArg;
23+
24+
string value = Regex.Replace(cliArg, @"(\\*)" + "\"", @"$1\$0");
25+
value = Regex.Replace(value, @"^(.*\s.*?)(\\*)$", "\"$1$2$2\"", RegexOptions.Singleline);
26+
27+
return value;
28+
}
1829

1930
/// <summary>
2031
/// replaces all invalid file name chars with their number representation

tests/BenchmarkDotNet.IntegrationTests/ArgumentsTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,18 @@ public void Test(string first, string second)
770770
}
771771
}
772772

773+
[Theory, MemberData(nameof(GetToolchains))]
774+
public void ComplexStringPattersAreSupported(IToolchain toolchain) => CanExecute<Perf_Regex_Industry_RustLang_Sherlock>(toolchain);
775+
776+
public class Perf_Regex_Industry_RustLang_Sherlock
777+
{
778+
[Params(@"[""'][^""']{0,30}[?!.][""']")]
779+
public string Pattern { get; set; }
780+
781+
[Benchmark]
782+
public int Consume() => Pattern.Length;
783+
}
784+
773785
[Fact]
774786
public void UnusedDisposableParamsAreDisposed() => CanExecute<WithDisposableArguments>(Job.Default.GetToolchain());
775787

0 commit comments

Comments
 (0)