Skip to content

Commit aeb6b7e

Browse files
committed
moves regex cache to bottom of class
moves regex cache to bottom of the class, for better organization.
1 parent 826d8a6 commit aeb6b7e

File tree

1 file changed

+76
-75
lines changed

1 file changed

+76
-75
lines changed

src/GitVersion.Core/Core/RegexPatterns.cs

Lines changed: 76 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,80 +10,6 @@ internal static partial class RegexPatterns
1010
private const RegexOptions Options = RegexOptions.IgnoreCase | RegexOptions.Compiled;
1111
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(2); // unified timeout for non-GeneratedRegex fallbacks
1212

13-
public static class Cache
14-
{
15-
private static readonly ConcurrentDictionary<string, Regex> cache = new();
16-
17-
public static Regex GetOrAdd([StringSyntax(StringSyntaxAttribute.Regex)] string pattern)
18-
{
19-
ArgumentNullException.ThrowIfNull(pattern);
20-
21-
return cache.GetOrAdd(pattern, key =>
22-
KnownRegexes.TryGetValue(key, out var regex)
23-
? regex
24-
: new Regex(key, Options, DefaultTimeout)); // now uses timeout for safety
25-
}
26-
27-
// Descriptor used to centralize pattern + compiled regex instance. Extendable with options/timeout metadata later.
28-
private readonly record struct RegexDescriptor(string Pattern, Regex Regex);
29-
30-
// Central descriptor list – single source of truth for known patterns. Order not significant.
31-
private static readonly RegexDescriptor[] Descriptors =
32-
[
33-
new(Common.SwitchArgumentRegexPattern, Common.SwitchArgumentRegex),
34-
new(Common.ObscurePasswordRegexPattern, Common.ObscurePasswordRegex),
35-
new(Common.ExpandTokensRegexPattern, Common.ExpandTokensRegex),
36-
new(Common.SanitizeEnvVarNameRegexPattern, Common.SanitizeEnvVarNameRegex),
37-
new(Common.SanitizeMemberNameRegexPattern, Common.SanitizeMemberNameRegex),
38-
new(Common.SanitizeNameRegexPattern, Common.SanitizeNameRegex),
39-
new(Configuration.DefaultTagPrefixRegexPattern, Configuration.DefaultTagPrefixRegex),
40-
new(Configuration.DefaultVersionInBranchRegexPattern, Configuration.DefaultVersionInBranchRegex),
41-
new(Configuration.MainBranchRegexPattern, Configuration.MainBranchRegex),
42-
new(Configuration.DevelopBranchRegexPattern, Configuration.DevelopBranchRegex),
43-
new(Configuration.ReleaseBranchRegexPattern, Configuration.ReleaseBranchRegex),
44-
new(Configuration.FeatureBranchRegexPattern, Configuration.FeatureBranchRegex),
45-
new(Configuration.PullRequestBranchRegexPattern, Configuration.PullRequestBranchRegex),
46-
new(Configuration.HotfixBranchRegexPattern, Configuration.HotfixBranchRegex),
47-
new(Configuration.SupportBranchRegexPattern, Configuration.SupportBranchRegex),
48-
new(Configuration.UnknownBranchRegexPattern, Configuration.UnknownBranchRegex),
49-
new(MergeMessage.DefaultMergeMessageRegexPattern, MergeMessage.DefaultMergeMessageRegex),
50-
new(MergeMessage.SmartGitMergeMessageRegexPattern, MergeMessage.SmartGitMergeMessageRegex),
51-
new(MergeMessage.BitBucketPullMergeMessageRegexPattern, MergeMessage.BitBucketPullMergeMessageRegex),
52-
new(MergeMessage.BitBucketPullv7MergeMessageRegexPattern, MergeMessage.BitBucketPullv7MergeMessageRegex),
53-
new(MergeMessage.BitBucketCloudPullMergeMessageRegexPattern, MergeMessage.BitBucketCloudPullMergeMessageRegex),
54-
new(MergeMessage.GitHubPullMergeMessageRegexPattern, MergeMessage.GitHubPullMergeMessageRegex),
55-
new(MergeMessage.RemoteTrackingMergeMessageRegexPattern, MergeMessage.RemoteTrackingMergeMessageRegex),
56-
new(MergeMessage.AzureDevOpsPullMergeMessageRegexPattern, MergeMessage.AzureDevOpsPullMergeMessageRegex),
57-
new(Output.AssemblyVersionRegexPattern, Output.AssemblyVersionRegex),
58-
new(Output.AssemblyInfoVersionRegexPattern, Output.AssemblyInfoVersionRegex),
59-
new(Output.AssemblyFileVersionRegexPattern, Output.AssemblyFileVersionRegex),
60-
new(Output.SanitizeAssemblyInfoRegexPattern, Output.SanitizeAssemblyInfoRegex),
61-
new(Output.CsharpAssemblyAttributeRegexPattern, Output.CsharpAssemblyAttributeRegex),
62-
new(Output.FsharpAssemblyAttributeRegexPattern, Output.FsharpAssemblyAttributeRegex),
63-
new(Output.VisualBasicAssemblyAttributeRegexPattern, Output.VisualBasicAssemblyAttributeRegex),
64-
new(Output.SanitizeParticipantRegexPattern, Output.SanitizeParticipantRegex),
65-
new(VersionCalculation.DefaultMajorRegexPattern, VersionCalculation.DefaultMajorRegex),
66-
new(VersionCalculation.DefaultMinorRegexPattern, VersionCalculation.DefaultMinorRegex),
67-
new(VersionCalculation.DefaultPatchRegexPattern, VersionCalculation.DefaultPatchRegex),
68-
new(VersionCalculation.DefaultNoBumpRegexPattern, VersionCalculation.DefaultNoBumpRegex),
69-
new(SemanticVersion.ParseStrictRegexPattern, SemanticVersion.ParseStrictRegex),
70-
new(SemanticVersion.ParseLooseRegexPattern, SemanticVersion.ParseLooseRegex),
71-
new(SemanticVersion.ParseBuildMetaDataRegexPattern, SemanticVersion.ParseBuildMetaDataRegex),
72-
new(SemanticVersion.FormatBuildMetaDataRegexPattern, SemanticVersion.FormatBuildMetaDataRegex),
73-
new(SemanticVersion.ParsePreReleaseTagRegexPattern, SemanticVersion.ParsePreReleaseTagRegex),
74-
// Trivia pattern unified: C# & F# share same underlying pattern; only map once under C# constant.
75-
new(AssemblyVersion.CSharp.TriviaRegexPattern, AssemblyVersion.CSharp.TriviaRegex),
76-
new(AssemblyVersion.CSharp.AttributeRegexPattern, AssemblyVersion.CSharp.AttributeRegex),
77-
// F# Trivia pattern identical – Attribute differs, so include attribute pattern only.
78-
new(AssemblyVersion.FSharp.AttributeRegexPattern, AssemblyVersion.FSharp.AttributeRegex),
79-
new(AssemblyVersion.VisualBasic.TriviaRegexPattern, AssemblyVersion.VisualBasic.TriviaRegex),
80-
new(AssemblyVersion.VisualBasic.AttributeRegexPattern, AssemblyVersion.VisualBasic.AttributeRegex)
81-
];
82-
83-
private static readonly ImmutableDictionary<string, Regex> KnownRegexes =
84-
Descriptors.ToImmutableDictionary(d => d.Pattern, d => d.Regex);
85-
}
86-
8713
internal static partial class Common
8814
{
8915
[StringSyntax(StringSyntaxAttribute.Regex)]
@@ -93,7 +19,8 @@ internal static partial class Common
9319
internal const string ObscurePasswordRegexPattern = "(https?://)(.+)(:.+@)";
9420

9521
[StringSyntax(StringSyntaxAttribute.Regex)]
96-
internal const string ExpandTokensRegexPattern = """
22+
internal const string ExpandTokensRegexPattern =
23+
"""
9724
\{ # Opening brace
9825
(?: # Start of either env or member expression
9926
env:(?!env:)(?<envvar>[A-Za-z_][A-Za-z0-9_]*) # Only a single env: prefix, not followed by another env:
@@ -190,6 +117,80 @@ internal static partial class Common
190117
#endif
191118
}
192119

120+
public static class Cache
121+
{
122+
private static readonly ConcurrentDictionary<string, Regex> cache = new();
123+
124+
public static Regex GetOrAdd([StringSyntax(StringSyntaxAttribute.Regex)] string pattern)
125+
{
126+
ArgumentNullException.ThrowIfNull(pattern);
127+
128+
return cache.GetOrAdd(pattern, key =>
129+
KnownRegexes.TryGetValue(key, out var regex)
130+
? regex
131+
: new Regex(key, Options, DefaultTimeout)); // now uses timeout for safety
132+
}
133+
134+
// Descriptor used to centralize pattern + compiled regex instance. Extendable with options/timeout metadata later.
135+
private readonly record struct RegexDescriptor(string Pattern, Regex Regex);
136+
137+
// Central descriptor list – single source of truth for known patterns. Order not significant.
138+
private static readonly RegexDescriptor[] Descriptors =
139+
[
140+
new(Common.SwitchArgumentRegexPattern, Common.SwitchArgumentRegex),
141+
new(Common.ObscurePasswordRegexPattern, Common.ObscurePasswordRegex),
142+
new(Common.ExpandTokensRegexPattern, Common.ExpandTokensRegex),
143+
new(Common.SanitizeEnvVarNameRegexPattern, Common.SanitizeEnvVarNameRegex),
144+
new(Common.SanitizeMemberNameRegexPattern, Common.SanitizeMemberNameRegex),
145+
new(Common.SanitizeNameRegexPattern, Common.SanitizeNameRegex),
146+
new(Configuration.DefaultTagPrefixRegexPattern, Configuration.DefaultTagPrefixRegex),
147+
new(Configuration.DefaultVersionInBranchRegexPattern, Configuration.DefaultVersionInBranchRegex),
148+
new(Configuration.MainBranchRegexPattern, Configuration.MainBranchRegex),
149+
new(Configuration.DevelopBranchRegexPattern, Configuration.DevelopBranchRegex),
150+
new(Configuration.ReleaseBranchRegexPattern, Configuration.ReleaseBranchRegex),
151+
new(Configuration.FeatureBranchRegexPattern, Configuration.FeatureBranchRegex),
152+
new(Configuration.PullRequestBranchRegexPattern, Configuration.PullRequestBranchRegex),
153+
new(Configuration.HotfixBranchRegexPattern, Configuration.HotfixBranchRegex),
154+
new(Configuration.SupportBranchRegexPattern, Configuration.SupportBranchRegex),
155+
new(Configuration.UnknownBranchRegexPattern, Configuration.UnknownBranchRegex),
156+
new(MergeMessage.DefaultMergeMessageRegexPattern, MergeMessage.DefaultMergeMessageRegex),
157+
new(MergeMessage.SmartGitMergeMessageRegexPattern, MergeMessage.SmartGitMergeMessageRegex),
158+
new(MergeMessage.BitBucketPullMergeMessageRegexPattern, MergeMessage.BitBucketPullMergeMessageRegex),
159+
new(MergeMessage.BitBucketPullv7MergeMessageRegexPattern, MergeMessage.BitBucketPullv7MergeMessageRegex),
160+
new(MergeMessage.BitBucketCloudPullMergeMessageRegexPattern, MergeMessage.BitBucketCloudPullMergeMessageRegex),
161+
new(MergeMessage.GitHubPullMergeMessageRegexPattern, MergeMessage.GitHubPullMergeMessageRegex),
162+
new(MergeMessage.RemoteTrackingMergeMessageRegexPattern, MergeMessage.RemoteTrackingMergeMessageRegex),
163+
new(MergeMessage.AzureDevOpsPullMergeMessageRegexPattern, MergeMessage.AzureDevOpsPullMergeMessageRegex),
164+
new(Output.AssemblyVersionRegexPattern, Output.AssemblyVersionRegex),
165+
new(Output.AssemblyInfoVersionRegexPattern, Output.AssemblyInfoVersionRegex),
166+
new(Output.AssemblyFileVersionRegexPattern, Output.AssemblyFileVersionRegex),
167+
new(Output.SanitizeAssemblyInfoRegexPattern, Output.SanitizeAssemblyInfoRegex),
168+
new(Output.CsharpAssemblyAttributeRegexPattern, Output.CsharpAssemblyAttributeRegex),
169+
new(Output.FsharpAssemblyAttributeRegexPattern, Output.FsharpAssemblyAttributeRegex),
170+
new(Output.VisualBasicAssemblyAttributeRegexPattern, Output.VisualBasicAssemblyAttributeRegex),
171+
new(Output.SanitizeParticipantRegexPattern, Output.SanitizeParticipantRegex),
172+
new(VersionCalculation.DefaultMajorRegexPattern, VersionCalculation.DefaultMajorRegex),
173+
new(VersionCalculation.DefaultMinorRegexPattern, VersionCalculation.DefaultMinorRegex),
174+
new(VersionCalculation.DefaultPatchRegexPattern, VersionCalculation.DefaultPatchRegex),
175+
new(VersionCalculation.DefaultNoBumpRegexPattern, VersionCalculation.DefaultNoBumpRegex),
176+
new(SemanticVersion.ParseStrictRegexPattern, SemanticVersion.ParseStrictRegex),
177+
new(SemanticVersion.ParseLooseRegexPattern, SemanticVersion.ParseLooseRegex),
178+
new(SemanticVersion.ParseBuildMetaDataRegexPattern, SemanticVersion.ParseBuildMetaDataRegex),
179+
new(SemanticVersion.FormatBuildMetaDataRegexPattern, SemanticVersion.FormatBuildMetaDataRegex),
180+
new(SemanticVersion.ParsePreReleaseTagRegexPattern, SemanticVersion.ParsePreReleaseTagRegex),
181+
// Trivia pattern unified: C# & F# share same underlying pattern; only map once under C# constant.
182+
new(AssemblyVersion.CSharp.TriviaRegexPattern, AssemblyVersion.CSharp.TriviaRegex),
183+
new(AssemblyVersion.CSharp.AttributeRegexPattern, AssemblyVersion.CSharp.AttributeRegex),
184+
// F# Trivia pattern identical – Attribute differs, so include attribute pattern only.
185+
new(AssemblyVersion.FSharp.AttributeRegexPattern, AssemblyVersion.FSharp.AttributeRegex),
186+
new(AssemblyVersion.VisualBasic.TriviaRegexPattern, AssemblyVersion.VisualBasic.TriviaRegex),
187+
new(AssemblyVersion.VisualBasic.AttributeRegexPattern, AssemblyVersion.VisualBasic.AttributeRegex)
188+
];
189+
190+
private static readonly ImmutableDictionary<string, Regex> KnownRegexes =
191+
Descriptors.ToImmutableDictionary(d => d.Pattern, d => d.Regex);
192+
}
193+
193194
internal static partial class Configuration
194195
{
195196
[StringSyntax(StringSyntaxAttribute.Regex)]

0 commit comments

Comments
 (0)