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

Extend RegexCharClass.Canonicalize range inversion optimization #61562

Merged
merged 3 commits into from
Nov 17, 2021
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 @@ -1390,23 +1390,29 @@ private void Canonicalize(bool isNonBacktracking)
rangelist.RemoveRange(j, rangelist.Count - j);
}

// If the class now represents a single negated character, but does so by including every
// other character, invert it to produce a normalized form recognized by IsSingletonInverse.
if (!isNonBacktracking && // do not produce the IsSingletonInverse transformation in NonBacktracking mode
// If the class now represents a single negated range, but does so by including every
// other character, invert it to produce a normalized form with a single range. This
// is valuable for subsequent optimizations in most of the engines.
// TODO: https://github.com/dotnet/runtime/issues/61048. The special-casing for NonBacktracking
// can be deleted once this issue is addressed. The special-casing exists because NonBacktracking
// is on a different casing plan than the other engines and doesn't use ToLower on each input
// character at match time; this in turn can highlight differences between sets and their inverted
// versions of themselves, e.g. a difference between [0-AC-\uFFFF] and [^B].
if (!isNonBacktracking &&
!_negate &&
_subtractor is null &&
(_categories is null || _categories.Length == 0))
{
if (rangelist.Count == 2)
{
// There are two ranges in the list. See if there's one missing element between them.
// There are two ranges in the list. See if there's one missing range between them.
// Such a range might be as small as a single character.
if (rangelist[0].First == 0 &&
rangelist[0].Last == (char)(rangelist[1].First - 2) &&
rangelist[1].Last == LastChar)
rangelist[1].Last == LastChar &&
rangelist[0].Last < rangelist[1].First - 1)
{
char ch = (char)(rangelist[0].Last + 1);
rangelist[0] = new SingleRange((char)(rangelist[0].Last + 1), (char)(rangelist[1].First - 1));
rangelist.RemoveAt(1);
rangelist[0] = new SingleRange(ch, ch);
_negate = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@ private static int GetMinRequiredLength(Regex r)
[InlineData("[^\n]*", ".*")]
[InlineData("(?>[^\n]*)", "(?>.*)")]
[InlineData("[^\n]*?", ".*?")]
// Set reduction
[InlineData("[\u0001-\uFFFF]", "[^\u0000]")]
[InlineData("[\u0000-\uFFFE]", "[^\uFFFF]")]
[InlineData("[\u0000-AB-\uFFFF]", "[\u0000-\uFFFF]")]
[InlineData("[ABC-EG-J]", "[A-EG-J]")]
[InlineData("[\u0000-AC-\uFFFF]", "[^B]")]
[InlineData("[\u0000-AF-\uFFFF]", "[^B-E]")]
// Large loop patterns
[InlineData("a*a*a*a*a*a*a*b*b*?a+a*", "a*b*b*?a+")]
[InlineData("a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "a{0,30}aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")]
Expand Down