Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -954,6 +954,18 @@ bool EmitAnchors()
switch (regexTree.FindOptimizations.FindMode)
{
case FindNextStartingPositionMode.LeadingAnchor_LeftToRight_Beginning:
// If we also have a trailing End anchor with fixed length, we can check for exact length match.
if (regexTree.FindOptimizations.TrailingAnchor == RegexNodeKind.End &&
regexTree.FindOptimizations.MinRequiredLength == regexTree.FindOptimizations.MaxPossibleLength)
{
writer.WriteLine($"// The pattern has both a leading beginning (\\A) and a trailing end (\\z) anchor, and any possible match is exactly {regexTree.FindOptimizations.MinRequiredLength} characters.");
using (EmitBlock(writer, $"if (pos == 0 && inputSpan.Length == {regexTree.FindOptimizations.MinRequiredLength})"))
{
writer.WriteLine("return true;");
}
return true;
}

writer.WriteLine("// The pattern leads with a beginning (\\A) anchor.");
using (EmitBlock(writer, "if (pos == 0)"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ private RegexFindOptimizations(RegexNode root, RegexOptions options, bool isLead
(_, false) => FindNextStartingPositionMode.LeadingAnchor_LeftToRight_EndZ,
(_, true) => FindNextStartingPositionMode.LeadingAnchor_RightToLeft_EndZ,
};

// Even when we have a leading anchor, compute the trailing anchor for potential additional optimizations.
// For example, if we have both a leading Beginning anchor and a trailing End anchor with fixed length,
// we can fail fast if the input isn't exactly the right length.
if (!rightToLeft && !isLeadingPartial && LeadingAnchor == RegexNodeKind.Beginning)
{
TrailingAnchor = RegexPrefixAnalyzer.FindTrailingAnchor(root);
if (TrailingAnchor == RegexNodeKind.End && root.ComputeMaxLength() is int maxLength)
{
Debug.Assert(maxLength >= MinRequiredLength, $"{maxLength} should have been greater than {MinRequiredLength} minimum");
MaxPossibleLength = maxLength;
}
}

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,14 @@ public static IEnumerable<object[]> Match_MemberData()
yield return (@"a?(\b|c)", "ac", RegexOptions.None, 0, 2, true, "ac");
yield return (@"(a|())*(\b|c)", "ac", RegexOptions.None, 0, 2, true, "ac");
yield return (@"(\b|a)*", "a", RegexOptions.None, 0, 1, true, "");

// Tests for patterns with both beginning and end anchors with fixed length (optimization for early fail-fast)
yield return (@"^1234\z", "1234", RegexOptions.None, 0, 4, true, "1234");
yield return (@"^1234\z", "12345", RegexOptions.None, 0, 5, false, "");
yield return (@"^1234\z", "123", RegexOptions.None, 0, 3, false, "");
yield return (@"^1234\z", "x1234", RegexOptions.None, 0, 5, false, "");
yield return (@"\Aabc\z", "abc", RegexOptions.None, 0, 3, true, "abc");
yield return (@"\Aabc\z", "abcd", RegexOptions.None, 0, 4, false, "");
}

[OuterLoop("Takes several seconds to run")]
Expand Down
Loading