Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use source-generated regex instead of runtime compilation (SYSLIB1045) #10303

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/Microsoft.DotNet.Wpf/src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,6 @@ dotnet_diagnostic.IDE1006.severity = suggestion
# SA1400: Member should declare an access modifier
dotnet_diagnostic.SA1400.severity = suggestion

# SYSLIB1045: Convert to 'GeneratedRegexAttribute'.
dotnet_diagnostic.SYSLIB1045.severity = suggestion

# ---------------------
# Ref specific C# files
# ---------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ private string GeneratePrefix()


// writing to a file, removing or updating uid
internal sealed class UidWriter
internal sealed partial class UidWriter
{
internal UidWriter(UidCollector collector, Stream source, Stream target)
{
Expand Down Expand Up @@ -1098,7 +1098,7 @@ private void WriteNewUid(Uid uid)
// escape all the Xml entities in the value
string attributeValue = EscapedXmlEntities.Replace(
uid.Value,
EscapeMatchEvaluator
s_escapeMatchEvaluator
);

string clause = string.Format(
Expand All @@ -1125,11 +1125,6 @@ private void WriteNewNamespace()

private void WriteNewAttributeValue(string value)
{
string attributeValue = EscapedXmlEntities.Replace(
value,
EscapeMatchEvaluator
);

_targetWriter.Write(
string.Format(
TypeConverterHelper.InvariantEnglishUS,
Expand Down Expand Up @@ -1305,8 +1300,13 @@ private enum WriterAction
Skip = 1, // skip the content
}

private static Regex EscapedXmlEntities = new Regex("(<|>|\"|'|&)", RegexOptions.CultureInvariant | RegexOptions.Compiled);
private static MatchEvaluator EscapeMatchEvaluator = new MatchEvaluator(EscapeMatch);
#if !NETFX
[GeneratedRegex("(<|>|\"|'|&)", RegexOptions.CultureInvariant)]
private static partial Regex EscapedXmlEntities { get; }
#else
private static readonly Regex EscapedXmlEntities = new("(<|>|\"|'|&)", RegexOptions.CultureInvariant | RegexOptions.Compiled);
#endif
private static readonly MatchEvaluator s_escapeMatchEvaluator = new MatchEvaluator(EscapeMatch);

/// <summary>
/// the delegate to escape the matched pattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ private void ClearDictionaries(bool disposing = false)
}
}

[GeneratedRegex(@"\s*\#LID\s+(\d+)\s*", RegexOptions.Singleline | RegexOptions.CultureInvariant)]
private static partial Regex LexiconCultureRegex { get; }

/// <summary>
/// Detect whether the <paramref name="line"/> is of the form #LID nnnn,
/// and if it is, try to instantiate a CultureInfo object with LCID nnnn.
Expand All @@ -554,17 +557,14 @@ private void ClearDictionaries(bool disposing = false)
/// </returns>
private static CultureInfo TryParseLexiconCulture(string line)
{
const string regexPattern = @"\s*\#LID\s+(\d+)\s*";
RegexOptions regexOptions = RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.Compiled;

CultureInfo result = CultureInfo.InvariantCulture;

if (line == null)
{
return result;
}

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

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