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

Expand RegexNode atomicity test to loops at the end of alternation branches #1769

Merged
merged 1 commit into from
Jan 17, 2020
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 @@ -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)
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for CanBeMadeAtomic

Copy link
Member Author

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.

{
// 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.
Expand All @@ -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
Expand Down Expand Up @@ -889,6 +906,7 @@ static bool CanBeMadeAtomic(RegexNode node, RegexNode subsequent, int maxDepth =
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside, I noticed on line 832, this

                    // If this node is a one/notone/setloop, see if it overlaps with its successor in the concatenation.
                    // If it doesn't, then we can upgrade it to being a one/notone/setloopatomic.

maybe should be clarified to say

                    // If this node is a oneloop/notoneloop/setloop, see if it overlaps with its successor in the concatenation.
                    // If it doesn't, then we can upgrade it to being a oneloopatomic/notoneloopatomic/setloopatomic.

since one and notone are node types that are distinct to oneloop/notoneloop.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, it's everywhere eg

        /// Simple optimization. If an atomic subexpression contains only a one/notone/set loop,
        /// change it to be an atomic one/notone/set loop and remove the atomic node.

Copy link
Member Author

Choose a reason for hiding this comment

The 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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ public static IEnumerable<object[]> Groups_Basic_TestData()
yield return new object[] { null, @"(cat){cat,dog}?", "cat{cat,dog}?", RegexOptions.None, new string[] { "cat{cat,dog}", "cat" } };

// Atomic subexpressions
// Implicitly upgrading oneloop to be atomic
// Implicitly upgrading (or not) oneloop to be atomic
yield return new object[] { null, @"a*", "aaa", RegexOptions.None, new string[] { "aaa" } };
yield return new object[] { null, @"a*b", "aaab", RegexOptions.None, new string[] { "aaab" } };
yield return new object[] { null, @"a*b+", "aaab", RegexOptions.None, new string[] { "aaab" } };
Expand All @@ -669,14 +669,22 @@ public static IEnumerable<object[]> Groups_Basic_TestData()
yield return new object[] { null, @"a*\b", "aaa bbb", RegexOptions.ECMAScript, new string[] { "aaa" } };
yield return new object[] { null, @"@*\B", "@@@", RegexOptions.None, new string[] { "@@@" } };
yield return new object[] { null, @"@*\B", "@@@", RegexOptions.ECMAScript, new string[] { "@@@" } };
// Implicitly upgrading notoneloop to be atomic
yield return new object[] { null, @"(?:abcd*|efgh)i", "efghi", RegexOptions.None, new string[] { "efghi" } };
yield return new object[] { null, @"(?:abcd|efgh*)i", "efgi", RegexOptions.None, new string[] { "efgi" } };
yield return new object[] { null, @"(?:abcd|efghj{2,}|j[klm]o+)i", "efghjjjjji", RegexOptions.None, new string[] { "efghjjjjji" } };
yield return new object[] { null, @"(?:abcd|efghi{2,}|j[klm]o+)i", "efghiii", RegexOptions.None, new string[] { "efghiii" } };
yield return new object[] { null, @"(?:abcd|efghi{2,}|j[klm]o+)i", "efghiiiiiiii", RegexOptions.None, new string[] { "efghiiiiiiii" } };
// Implicitly upgrading (or not) notoneloop to be atomic
yield return new object[] { null, @"[^b]*b", "aaab", RegexOptions.None, new string[] { "aaab" } };
yield return new object[] { null, @"[^b]*b+", "aaab", RegexOptions.None, new string[] { "aaab" } };
yield return new object[] { null, @"[^b]*b+?", "aaab", RegexOptions.None, new string[] { "aaab" } };
yield return new object[] { null, @"[^b]*(?>b+)", "aaab", RegexOptions.None, new string[] { "aaab" } };
yield return new object[] { null, @"[^b]*bac", "aaabac", RegexOptions.None, new string[] { "aaabac" } };
yield return new object[] { null, @"[^b]*", "aaa", RegexOptions.None, new string[] { "aaa" } };
// Implicitly upgrading setloop to be atomic
yield return new object[] { null, @"(?:abc[^b]*|efgh)i", "efghi", RegexOptions.None, new string[] { "efghi" } }; // can't upgrade
yield return new object[] { null, @"(?:abcd|efg[^b]*)b", "efgb", RegexOptions.None, new string[] { "efgb" } };
yield return new object[] { null, @"(?:abcd|efg[^b]*)i", "efgi", RegexOptions.None, new string[] { "efgi" } }; // can't upgrade
// Implicitly upgrading (or not) setloop to be atomic
yield return new object[] { null, @"[ac]*", "aaa", RegexOptions.None, new string[] { "aaa" } };
yield return new object[] { null, @"[ac]*b", "aaab", RegexOptions.None, new string[] { "aaab" } };
yield return new object[] { null, @"[ac]*b+", "aaab", RegexOptions.None, new string[] { "aaab" } };
Expand All @@ -699,14 +707,17 @@ public static IEnumerable<object[]> Groups_Basic_TestData()
yield return new object[] { null, @"[@']*\B", "@@@", RegexOptions.None, new string[] { "@@@" } };
yield return new object[] { null, @"[@']*\B", "@@@", RegexOptions.ECMAScript, new string[] { "@@@" } };
yield return new object[] { null, @".*.", "@@@", RegexOptions.Singleline, new string[] { "@@@" } };
// Implicitly upgrading concat loops to be atomic
yield return new object[] { null, @"(?:abcd|efg[hij]*)h", "efgh", RegexOptions.None, new string[] { "efgh" } }; // can't upgrade
yield return new object[] { null, @"(?:abcd|efg[hij]*)ih", "efgjih", RegexOptions.None, new string[] { "efgjih" } }; // can't upgrade
yield return new object[] { null, @"(?:abcd|efg[hij]*)k", "efgjk", RegexOptions.None, new string[] { "efgjk" } };
// Implicitly upgrading (or not) concat loops to be atomic
yield return new object[] { null, @"(?:[ab]c[de]f)*", "", RegexOptions.None, new string[] { "" } };
yield return new object[] { null, @"(?:[ab]c[de]f)*", "acdf", RegexOptions.None, new string[] { "acdf" } };
yield return new object[] { null, @"(?:[ab]c[de]f)*", "acdfbcef", RegexOptions.None, new string[] { "acdfbcef" } };
yield return new object[] { null, @"(?:[ab]c[de]f)*", "cdfbcef", RegexOptions.None, new string[] { "" } };
yield return new object[] { null, @"(?:[ab]c[de]f)+", "cdfbcef", RegexOptions.None, new string[] { "bcef" } };
yield return new object[] { null, @"(?:[ab]c[de]f)*", "bcefbcdfacfe", RegexOptions.None, new string[] { "bcefbcdf" } };
// Implicitly upgrading nested loops to be atomic
// Implicitly upgrading (or not) nested loops to be atomic
yield return new object[] { null, @"(?:a){3}", "aaaaaaaaa", RegexOptions.None, new string[] { "aaa" } };
yield return new object[] { null, @"(?:a){3}?", "aaaaaaaaa", RegexOptions.None, new string[] { "aaa" } };
yield return new object[] { null, @"(?:a{2}){3}", "aaaaaaaaa", RegexOptions.None, new string[] { "aaaaaa" } };
Expand Down