Skip to content
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 @@ -1832,6 +1832,16 @@ static bool CanCombineCounts(int nodeMin, int nodeMax, int nextMin, int nextMax)
currentNode.MakeRep(RegexNodeKind.Oneloop, 2, 2);
next++;
continue;

// Coalescing identical anchors (e.g. \b\b). These don't need to become loops, as they collapse to a single anchor.
case RegexNodeKind.Beginning or RegexNodeKind.Start or
RegexNodeKind.End or RegexNodeKind.EndZ or
RegexNodeKind.Bol or RegexNodeKind.Eol or
RegexNodeKind.Boundary or RegexNodeKind.NonBoundary or
RegexNodeKind.ECMABoundary or RegexNodeKind.NonECMABoundary
when nextNode.Kind == currentNode.Kind:
next++;
continue;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,16 @@ public class RegexReductionTests
[InlineData("(abc?)*?d", "(ab(?>c?))*?d")]
[InlineData("(ab*c)*d", "(?>(a(?>b*)c)*)d")]
[InlineData("(aba)?d", "(?>(aba)?)d")]
// Anchors
[InlineData(@"\b\b", @"\b")]
[InlineData(@"\b\b\b\b\b", @"\b")]
[InlineData(@"\B\B", @"\B")]
[InlineData(@"^^", @"^")]
[InlineData(@"$", @"$")]
[InlineData(@"\Z\Z", @"\Z")]
[InlineData(@"\z\z", @"\z")]
[InlineData(@"\G\G", @"\G")]
[InlineData(@"\A\A", @"\A")]
// Nothing handling
[InlineData(@"\wabc(?!)def", "(?!)")]
[InlineData(@"\wabc(?!)def|ghi(?!)", "(?!)")]
Expand Down Expand Up @@ -532,6 +542,10 @@ public void PatternsReduceIdentically(string actual, string expected)
[InlineData("a*(?(xyz)bcd)", "(?>a*)(?(xyz)bcd)")]
// Different prefixes on alternation branches
[InlineData("^abcd|$abce", "^abcd|^abce")]
// Anchors
[InlineData(@"\b\B", "\b")]
[InlineData(@"^$", "^")]
[InlineData(@"^$", "$")]
public void PatternsReduceDifferently(string actual, string expected)
{
// NOTE: RegexNode.ToString is only compiled into debug builds, so DEBUG is currently set on the unit tests project.
Expand Down
Loading