Skip to content

Commit f34aa5d

Browse files
Merge pull request #1 from HuibertJan/master
Add Compiled option to regexes
2 parents 6ea87ff + af0e7af commit f34aa5d

File tree

5 files changed

+98
-6
lines changed

5 files changed

+98
-6
lines changed

Serilog.Enrichers.Sensitive.sln

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26124.0
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29613.14
55
MinimumVisualStudioVersion = 15.0.26124.0
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4F6C5943-A8FF-4625-97ED-5325B9CAF2E4}"
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.Sensitive", "src\Serilog.Enrichers.Sensitive\Serilog.Enrichers.Sensitive.csproj", "{24D87D92-F2C9-449A-80F3-CD90AF105D26}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Enrichers.Sensitive.Demo", "src\Serilog.Enrichers.Sensitive.Demo\Serilog.Enrichers.Sensitive.Demo.csproj", "{86680D69-63DF-4211-845D-7C45DAB92A36}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.Sensitive.Demo", "src\Serilog.Enrichers.Sensitive.Demo\Serilog.Enrichers.Sensitive.Demo.csproj", "{86680D69-63DF-4211-845D-7C45DAB92A36}"
1111
EndProject
1212
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{5953C623-05D9-4967-A65B-01AD83955946}"
1313
EndProject
14-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Enrichers.Sensitive.Tests.Unit", "test\Serilog.Enrichers.Sensitive.Tests.Unit\Serilog.Enrichers.Sensitive.Tests.Unit.csproj", "{8784B51D-900B-44E5-A032-CEF6027CD178}"
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.Sensitive.Tests.Unit", "test\Serilog.Enrichers.Sensitive.Tests.Unit\Serilog.Enrichers.Sensitive.Tests.Unit.csproj", "{8784B51D-900B-44E5-A032-CEF6027CD178}"
15+
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Enrichers.Sensitive.Tests.Benchmark", "test\Serilog.Enrichers.Sensitive.Tests.Benchmark\Serilog.Enrichers.Sensitive.Tests.Benchmark.csproj", "{1C6B2313-3EBB-482F-8A78-2BB7C6FCFA06}"
1517
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -59,6 +61,18 @@ Global
5961
{8784B51D-900B-44E5-A032-CEF6027CD178}.Release|x64.Build.0 = Release|Any CPU
6062
{8784B51D-900B-44E5-A032-CEF6027CD178}.Release|x86.ActiveCfg = Release|Any CPU
6163
{8784B51D-900B-44E5-A032-CEF6027CD178}.Release|x86.Build.0 = Release|Any CPU
64+
{1C6B2313-3EBB-482F-8A78-2BB7C6FCFA06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
65+
{1C6B2313-3EBB-482F-8A78-2BB7C6FCFA06}.Debug|Any CPU.Build.0 = Debug|Any CPU
66+
{1C6B2313-3EBB-482F-8A78-2BB7C6FCFA06}.Debug|x64.ActiveCfg = Debug|Any CPU
67+
{1C6B2313-3EBB-482F-8A78-2BB7C6FCFA06}.Debug|x64.Build.0 = Debug|Any CPU
68+
{1C6B2313-3EBB-482F-8A78-2BB7C6FCFA06}.Debug|x86.ActiveCfg = Debug|Any CPU
69+
{1C6B2313-3EBB-482F-8A78-2BB7C6FCFA06}.Debug|x86.Build.0 = Debug|Any CPU
70+
{1C6B2313-3EBB-482F-8A78-2BB7C6FCFA06}.Release|Any CPU.ActiveCfg = Release|Any CPU
71+
{1C6B2313-3EBB-482F-8A78-2BB7C6FCFA06}.Release|Any CPU.Build.0 = Release|Any CPU
72+
{1C6B2313-3EBB-482F-8A78-2BB7C6FCFA06}.Release|x64.ActiveCfg = Release|Any CPU
73+
{1C6B2313-3EBB-482F-8A78-2BB7C6FCFA06}.Release|x64.Build.0 = Release|Any CPU
74+
{1C6B2313-3EBB-482F-8A78-2BB7C6FCFA06}.Release|x86.ActiveCfg = Release|Any CPU
75+
{1C6B2313-3EBB-482F-8A78-2BB7C6FCFA06}.Release|x86.Build.0 = Release|Any CPU
6276
EndGlobalSection
6377
GlobalSection(SolutionProperties) = preSolution
6478
HideSolutionNode = FALSE
@@ -67,6 +81,7 @@ Global
6781
{24D87D92-F2C9-449A-80F3-CD90AF105D26} = {4F6C5943-A8FF-4625-97ED-5325B9CAF2E4}
6882
{86680D69-63DF-4211-845D-7C45DAB92A36} = {4F6C5943-A8FF-4625-97ED-5325B9CAF2E4}
6983
{8784B51D-900B-44E5-A032-CEF6027CD178} = {5953C623-05D9-4967-A65B-01AD83955946}
84+
{1C6B2313-3EBB-482F-8A78-2BB7C6FCFA06} = {5953C623-05D9-4967-A65B-01AD83955946}
7085
EndGlobalSection
7186
GlobalSection(ExtensibilityGlobals) = postSolution
7287
SolutionGuid = {E5198D5F-2EF2-411D-8569-3E473CFFB6B7}

src/Serilog.Enrichers.Sensitive/EmailAddressMaskingOperator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class EmailAddressMaskingOperator : IMaskingOperator
66
{
77
private static readonly Regex EmailReplaceRegex = new Regex(
88
"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])",
9-
RegexOptions.IgnoreCase);
9+
RegexOptions.IgnoreCase | RegexOptions.Compiled);
1010

1111
public MaskingResult Mask(string input, string mask)
1212
{

src/Serilog.Enrichers.Sensitive/IbanMaskingOperator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Serilog.Enrichers.Sensitive
44
{
55
public class IbanMaskingOperator : IMaskingOperator
66
{
7-
private static readonly Regex IbanReplaceRegex = new Regex("[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}");
7+
private static readonly Regex IbanReplaceRegex = new Regex("[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}", RegexOptions.Compiled);
88

99
public MaskingResult Mask(string input, string mask)
1010
{
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Text.RegularExpressions;
2+
using BenchmarkDotNet.Attributes;
3+
using BenchmarkDotNet.Running;
4+
5+
namespace Serilog.Enrichers.Sensitive.Tests.Benchmark
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
var summary = BenchmarkRunner.Run<BenchmarkCompiledRegex>();
12+
}
13+
}
14+
15+
public class BenchmarkCompiledRegex
16+
{
17+
private readonly Regex EmailRegex = new Regex("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])");
18+
private readonly Regex EmailRegexCompiled = new Regex("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])", RegexOptions.Compiled);
19+
private readonly Regex IbanRegex = new Regex("[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}");
20+
private readonly Regex IbanRegexCompiled = new Regex("[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}", RegexOptions.Compiled);
21+
private const string MaskValue = "***MASKED***";
22+
private const string EmailInput = "[email protected]";
23+
private const string IbanInput = "NL02ABNA0123456789";
24+
25+
[Benchmark]
26+
public string EmailRegexReplace()
27+
{
28+
string result = null;
29+
for (int i = 0; i < 10000; i++)
30+
result = EmailRegex.Replace(EmailInput, MaskValue);
31+
32+
return result;
33+
}
34+
35+
[Benchmark]
36+
public string EmailRegexCompiledReplace()
37+
{
38+
string result = null;
39+
for (int i = 0; i < 10000; i++)
40+
result = EmailRegexCompiled.Replace(EmailInput, MaskValue);
41+
42+
return result;
43+
}
44+
45+
[Benchmark]
46+
public string IbanRegexReplace()
47+
{
48+
string result = null;
49+
for (int i = 0; i < 10000; i++)
50+
result = IbanRegex.Replace(IbanInput, MaskValue);
51+
52+
return result;
53+
}
54+
55+
[Benchmark]
56+
public string IbanRegexCompiledReplace()
57+
{
58+
string result = null;
59+
for (int i = 0; i < 10000; i++)
60+
result = IbanRegexCompiled.Replace(IbanInput, MaskValue);
61+
62+
return result;
63+
}
64+
}
65+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
10+
</ItemGroup>
11+
12+
</Project>

0 commit comments

Comments
 (0)