-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Expand RegexNode atomicity test to loops at the end of alternation branches #1769
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -731,11 +731,13 @@ private void ReduceConcatenateWithAutoAtomic() | |
Debug.Assert((Options & RegexOptions.RightToLeft) == 0); | ||
Debug.Assert(Children is List<RegexNode>); | ||
|
||
List<RegexNode> children = (List<RegexNode>)Children; | ||
var children = (List<RegexNode>)Children; | ||
for (int i = 0; i < children.Count - 1; i++) | ||
{ | ||
RegexNode node = children[i], subsequent = children[i + 1]; | ||
ProcessNode(children[i], children[i + 1]); | ||
|
||
static void ProcessNode(RegexNode node, RegexNode subsequent, int maxDepth = 20) | ||
{ | ||
// Skip down the node past irrelevant nodes. We don't need to | ||
// skip Groups, as they should have already been reduced away. | ||
// If there's a concatenation, we can jump to the last element of it. | ||
|
@@ -748,15 +750,30 @@ private void ReduceConcatenateWithAutoAtomic() | |
// If the node can be changed to atomic based on what comes after it, do so. | ||
switch (node.Type) | ||
{ | ||
case Oneloop when CanBeMadeAtomic(node, subsequent): | ||
case Oneloop when CanBeMadeAtomic(node, subsequent, maxDepth - 1): | ||
node.Type = Oneloopatomic; | ||
break; | ||
case Notoneloop when CanBeMadeAtomic(node, subsequent): | ||
case Notoneloop when CanBeMadeAtomic(node, subsequent, maxDepth - 1): | ||
node.Type = Notoneloopatomic; | ||
break; | ||
case Setloop when CanBeMadeAtomic(node, subsequent): | ||
case Setloop when CanBeMadeAtomic(node, subsequent, maxDepth - 1): | ||
node.Type = Setloopatomic; | ||
break; | ||
case Alternate: | ||
// In the case of alternation, we can't change the alternation node itself | ||
// based on what comes after it (at least not with more complicated analysis | ||
// that factors in all branches together), but we can look at each individual | ||
// branch, and analyze ending loops in each branch individually to see if they | ||
// can be made atomic. Then if we do end up backtracking into the alternation, | ||
// we at least won't need to backtrack into that loop. | ||
{ | ||
int alternateBranches = node.ChildCount(); | ||
for (int b = 0; b < alternateBranches; b++) | ||
{ | ||
ProcessNode(node.Child(b), subsequent, maxDepth - 1); | ||
} | ||
} | ||
break; | ||
} | ||
|
||
// Determines whether node can be switched to an atomic loop. Subsequent is the node | ||
|
@@ -889,6 +906,7 @@ static bool CanBeMadeAtomic(RegexNode node, RegexNode subsequent, int maxDepth = | |
} | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aside, I noticed on line 832, this
maybe should be clarified to say
since one and notone are node types that are distinct to oneloop/notoneloop. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eh, it's everywhere eg
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. I can see how it might not be clear; in my head I see the grouping, e.g. as if it were {one/notone/set}loop, but I can clean it up. I'll do so in my next PR. |
||
|
||
/// <summary>Computes a min bound on the required length of any string that could possibly match.</summary> | ||
/// <returns>The min computed length. If the result is 0, there is no minimum we can enforce.</returns> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, might be better to pass in 20 above rather than default it here, so any future calls don't forget to decide what to pass in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for CanBeMadeAtomic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I'll address that subsequently.