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

Add tighter bound to range check for matching Regex char classes #67133

Merged
merged 1 commit into from
Mar 25, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4006,8 +4006,8 @@ private static string MatchCharacterClass(bool hasTextInfo, RegexOptions options
// character class were [A-Za-z0-9], so since the ch is now known to be >= 128, we
// can just fail the comparison.
return negate ?
$"((ch = {chExpr}) >= 128 || ({Literal(bitVectorString)}[ch >> 4] & (1 << (ch & 0xF))) == 0)" :
$"((ch = {chExpr}) < 128 && ({Literal(bitVectorString)}[ch >> 4] & (1 << (ch & 0xF))) != 0)";
$"((ch = {chExpr}) >= {Literal((char)analysis.UpperBoundExclusiveIfContainsOnlyAscii)} || ({Literal(bitVectorString)}[ch >> 4] & (1 << (ch & 0xF))) == 0)" :
$"((ch = {chExpr}) < {Literal((char)analysis.UpperBoundExclusiveIfContainsOnlyAscii)} && ({Literal(bitVectorString)}[ch >> 4] & (1 << (ch & 0xF))) != 0)";
}

if (analysis.AllNonAsciiContained)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,8 @@ internal struct CharClassAnalysisResults
public bool AllAsciiContained;
/// <summary>true if we know for sure that all non-ASCII values are in the set; otherwise, false.</summary>
public bool AllNonAsciiContained;
/// <summary>The exclusive upper bound. Only valid if <see cref="ContainsOnlyAscii"/> is true.</summary>
public int UpperBoundExclusiveIfContainsOnlyAscii;
}

/// <summary>Analyzes the set to determine some basic properties that can be used to optimize usage.</summary>
Expand Down Expand Up @@ -962,6 +964,7 @@ internal static CharClassAnalysisResults Analyze(string set)
AllAsciiContained = false,
ContainsOnlyAscii = set[set.Length - 1] <= 128,
ContainsNoAscii = set[SetStartIndex] >= 128,
UpperBoundExclusiveIfContainsOnlyAscii = set[set.Length - 1],
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5021,7 +5021,7 @@ void EmitCharInClass()

// ch < 128 ? (bitVectorString[ch >> 4] & (1 << (ch & 0xF))) != 0 :
Ldloc(tempLocal);
Ldc(128);
Ldc(analysis.ContainsOnlyAscii ? analysis.UpperBoundExclusiveIfContainsOnlyAscii : 128);
Bge(comparisonLabel);
Ldstr(bitVectorString);
Ldloc(tempLocal);
Expand Down