Skip to content

Commit abdd599

Browse files
authored
Use source-generated regex instead of runtime compilation (SYSLIB1045) (#10303)
* Fix immediate SYSLIB1045 warning * Replace inline regex with GeneratedRegex
1 parent 72af9a5 commit abdd599

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

src/Microsoft.DotNet.Wpf/src/.editorconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,6 @@ dotnet_diagnostic.IDE0300.severity = suggestion
224224
# IDE1006: Naming Styles
225225
dotnet_diagnostic.IDE1006.severity = suggestion
226226

227-
# SYSLIB1045: Convert to 'GeneratedRegexAttribute'.
228-
dotnet_diagnostic.SYSLIB1045.severity = suggestion
229-
230227
# ---------------------
231228
# Ref specific C# files
232229
# ---------------------

src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/Microsoft/Build/Tasks/Windows/UidManager.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ private string GeneratePrefix()
925925

926926

927927
// writing to a file, removing or updating uid
928-
internal sealed class UidWriter
928+
internal sealed partial class UidWriter
929929
{
930930
internal UidWriter(UidCollector collector, Stream source, Stream target)
931931
{
@@ -1101,7 +1101,7 @@ private void WriteNewUid(Uid uid)
11011101
// escape all the Xml entities in the value
11021102
string attributeValue = EscapedXmlEntities.Replace(
11031103
uid.Value,
1104-
EscapeMatchEvaluator
1104+
s_escapeMatchEvaluator
11051105
);
11061106

11071107
string clause = string.Format(
@@ -1128,11 +1128,6 @@ private void WriteNewNamespace()
11281128

11291129
private void WriteNewAttributeValue(string value)
11301130
{
1131-
string attributeValue = EscapedXmlEntities.Replace(
1132-
value,
1133-
EscapeMatchEvaluator
1134-
);
1135-
11361131
_targetWriter.Write(
11371132
string.Format(
11381133
TypeConverterHelper.InvariantEnglishUS,
@@ -1308,8 +1303,13 @@ private enum WriterAction
13081303
Skip = 1, // skip the content
13091304
}
13101305

1311-
private static Regex EscapedXmlEntities = new Regex("(<|>|\"|'|&)", RegexOptions.CultureInvariant | RegexOptions.Compiled);
1312-
private static MatchEvaluator EscapeMatchEvaluator = new MatchEvaluator(EscapeMatch);
1306+
#if !NETFX
1307+
[GeneratedRegex("(<|>|\"|'|&)", RegexOptions.CultureInvariant)]
1308+
private static partial Regex EscapedXmlEntities { get; }
1309+
#else
1310+
private static readonly Regex EscapedXmlEntities = new("(<|>|\"|'|&)", RegexOptions.CultureInvariant | RegexOptions.Compiled);
1311+
#endif
1312+
private static readonly MatchEvaluator s_escapeMatchEvaluator = new MatchEvaluator(EscapeMatch);
13131313

13141314
/// <summary>
13151315
/// the delegate to escape the matched pattern

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/WinRTSpellerInterop.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,9 @@ private void ClearDictionaries(bool disposing = false)
543543
}
544544
}
545545

546+
[GeneratedRegex(@"\s*\#LID\s+(\d+)\s*", RegexOptions.Singleline | RegexOptions.CultureInvariant)]
547+
private static partial Regex LexiconCultureRegex { get; }
548+
546549
/// <summary>
547550
/// Detect whether the <paramref name="line"/> is of the form #LID nnnn,
548551
/// and if it is, try to instantiate a CultureInfo object with LCID nnnn.
@@ -553,17 +556,14 @@ private void ClearDictionaries(bool disposing = false)
553556
/// </returns>
554557
private static CultureInfo TryParseLexiconCulture(string line)
555558
{
556-
const string regexPattern = @"\s*\#LID\s+(\d+)\s*";
557-
RegexOptions regexOptions = RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.Compiled;
558-
559559
CultureInfo result = CultureInfo.InvariantCulture;
560560

561561
if (line == null)
562562
{
563563
return result;
564564
}
565565

566-
string[] matches = Regex.Split(line.Trim(), regexPattern, regexOptions);
566+
string[] matches = LexiconCultureRegex.Split(line.Trim());
567567

568568
// We expect 1 exact match, which implies matches.Length == 3 (before, match, after)
569569
if (matches.Length != 3)

0 commit comments

Comments
 (0)