Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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,24 @@ 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.
// Compute this lazily to avoid overhead in the interpreter.
{
RegexNodeKind trailingAnchor = RegexPrefixAnalyzer.FindTrailingAnchor(regexTree.Root);
int minRequiredLength = regexTree.FindOptimizations.MinRequiredLength;
if (trailingAnchor == RegexNodeKind.End &&
regexTree.Root.ComputeMaxLength() is int maxLength &&
minRequiredLength == maxLength)
{
writer.WriteLine($"// The pattern has both a leading beginning (\\A) and a trailing end (\\z) anchor, and any possible match is exactly {minRequiredLength} characters.");
using (EmitBlock(writer, $"if (pos == 0 && inputSpan.Length == {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 @@ -507,6 +507,33 @@ 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.
// Compute this lazily to avoid overhead in the interpreter.
{
RegexNodeKind trailingAnchor = RegexPrefixAnalyzer.FindTrailingAnchor(_regexTree.Root);
int minRequiredLength = _regexTree.FindOptimizations.MinRequiredLength;
if (trailingAnchor == RegexNodeKind.End &&
_regexTree.Root.ComputeMaxLength() is int maxLength &&
minRequiredLength == maxLength)
{
// if (pos != 0 || inputSpan.Length != exactLength) goto returnFalse;
// return true;
Label success = DefineLabel();
Ldloc(pos);
Ldc(0);
Bne(returnFalse);
Ldloca(inputSpan);
Call(SpanGetLengthMethod);
Ldc(minRequiredLength);
Beq(success);
Br(returnFalse);
MarkLabel(success);
Ldc(1);
Ret();
return true;
}
}

// if (pos != 0) goto returnFalse;
// return true;
Ldloc(pos);
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