Skip to content

Commit 0281f05

Browse files
danmoseleyCopilot
andcommitted
Rename ReReduceTree to FinalReduce
Better name that pairs with FinalOptimize which calls it. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4f2b287 commit 0281f05

2 files changed

Lines changed: 13 additions & 13 deletions

File tree

src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexNode.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,11 @@ internal RegexNode FinalOptimize()
394394
// FinalOptimize can create patterns like Atomic(Alternate(X, Empty)) that ReduceAtomic
395395
// would simplify to Loop?(X), or Concatenate(X, Empty) that ReduceConcatenation would
396396
// simplify to X. A single re-reduce pass catches all such cases.
397-
rootNode.ReReduceTree();
397+
rootNode.FinalReduce();
398398

399399
// Optimization: unnecessary re-processing of starting loops.
400-
// This runs after ReReduceTree so it operates on the final tree structure, since
401-
// ReReduceTree may restructure alternations into concatenations with a leading loop.
400+
// This runs after FinalReduce so it operates on the final tree structure, since
401+
// FinalReduce may restructure alternations into concatenations with a leading loop.
402402
// If an expression is guaranteed to begin with a single-character unbounded loop that isn't part of an alternation (in which case it
403403
// wouldn't be guaranteed to be at the beginning) or a capture (in which case a back reference could be influenced by its length), then we
404404
// can update the tree with a temporary node to indicate that the implementation should use that node's ending position in the input text
@@ -456,7 +456,7 @@ internal RegexNode FinalOptimize()
456456
/// created by the <see cref="FinalOptimize"/> passes, e.g. Concatenate(X, Empty)
457457
/// or Atomic wrappers that became redundant.
458458
/// </summary>
459-
private void ReReduceTree()
459+
private void FinalReduce()
460460
{
461461
if (!StackHelper.TryEnsureSufficientExecutionStack())
462462
{
@@ -465,7 +465,7 @@ private void ReReduceTree()
465465

466466
for (int i = 0, childCount = ChildCount(); i < childCount; i++)
467467
{
468-
Child(i).ReReduceTree();
468+
Child(i).FinalReduce();
469469
ReplaceChild(i, Child(i)); // ReplaceChild reduces the node in place
470470
}
471471
}

src/libraries/System.Text.RegularExpressions/tests/UnitTests/RegexReductionTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,14 @@ public class RegexReductionTests
475475
[InlineData("(?:http|https)://foo", "http(?>s?)://foo")]
476476
[InlineData("(?:ab|abc)d", "ab(?>c?)d")]
477477
[InlineData("(?:abc|abcd|abce|abcfg)h", "abc(?:|[de]|fg)h")]
478-
// ReReduceTree: post-FinalOptimize cleanup. Each case shows the equivalent tree without re-reduce.
479-
[InlineData("a|ab", "a")] // Without re-reduce: a(?:) — prefix extraction leaves Concat(a, Empty); re-reduce strips Empty
480-
[InlineData(@"\n|\n\r|\r\n", @"(?>\n|\r\n)")] // Without re-reduce: (?>\n(?:)|\r\n) — shared prefix \n leaves Concat(\n, Empty) in branch; re-reduce collapses it
481-
[InlineData(@"[ab]+c[ab]+|[ab]+", @"(?>(?>[ab]+)(?:c(?>[ab]+))?)")] // Without re-reduce: (?>[ab]+c[ab]+|[ab]+) — quantified set prefix [ab]+ not extracted until re-reduce
482-
[InlineData("ab|a|ac", "ab?")] // Without re-reduce: a(?>b?) — prefix extraction + Atomic context creates redundant Atomic(Oneloopatomic); re-reduce strips Atomic
483-
[InlineData("ab|a|ac|d", "(?>ab?|d)")] // Without re-reduce: (?>a(?>b?)|d) — same redundant Atomic removal, within a larger Alternate
484-
[InlineData("a?b|a??b", "(?>a?(?>b))")] // Without re-reduce: (?>a?(?>[b])) — greedy/lazy branches merge after atomic promotion; re-reduce converts single-char [b] to b
485-
[InlineData("[ab]?c|[ab]??c", "(?>[ab]?(?>c))")] // Without re-reduce: (?>[ab]?(?>[c])) — same single-char class simplification with set loop prefix
478+
// FinalReduce: post-FinalOptimize re-reduction. Each case shows the equivalent tree without this pass.
479+
[InlineData("a|ab", "a")] // Without FinalReduce: a(?:) — prefix extraction leaves Concat(a, Empty); FinalReduce strips Empty
480+
[InlineData(@"\n|\n\r|\r\n", @"(?>\n|\r\n)")] // Without FinalReduce: (?>\n(?:)|\r\n) — shared prefix \n leaves Concat(\n, Empty) in branch; FinalReduce collapses it
481+
[InlineData(@"[ab]+c[ab]+|[ab]+", @"(?>(?>[ab]+)(?:c(?>[ab]+))?)")] // Without FinalReduce: (?>[ab]+c[ab]+|[ab]+) — quantified set prefix [ab]+ not extracted until FinalReduce
482+
[InlineData("ab|a|ac", "ab?")] // Without FinalReduce: a(?>b?) — prefix extraction + Atomic context creates redundant Atomic(Oneloopatomic); FinalReduce strips Atomic
483+
[InlineData("ab|a|ac|d", "(?>ab?|d)")] // Without FinalReduce: (?>a(?>b?)|d) — same redundant Atomic removal, within a larger Alternate
484+
[InlineData("a?b|a??b", "(?>a?(?>b))")] // Without FinalReduce: (?>a?(?>[b])) — greedy/lazy branches merge after atomic promotion; FinalReduce converts single-char [b] to b
485+
[InlineData("[ab]?c|[ab]??c", "(?>[ab]?(?>c))")] // Without FinalReduce: (?>[ab]?(?>[c])) — same single-char class simplification with set loop prefix
486486
public void PatternsReduceIdentically(string actual, string expected)
487487
{
488488
// NOTE: RegexNode.ToString is only compiled into debug builds, so DEBUG is currently set on the unit tests project.

0 commit comments

Comments
 (0)