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

Factor common prefix text out of Regex alternations #2171

Merged
merged 3 commits into from
Jan 27, 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 @@ -105,16 +105,7 @@ private void Init(string pattern, RegexOptions options, TimeSpan matchTimeout, C
#if DEBUG
if (IsDebug)
{
Debug.Write($"Pattern: {pattern}");
RegexOptions displayOptions = options & ~RegexOptions.Debug;
if (displayOptions != RegexOptions.None)
{
Debug.Write($"Options: {displayOptions}");
}
if (matchTimeout != InfiniteMatchTimeout)
{
Debug.Write($"Timeout: {matchTimeout}");
}
Debug.WriteLine($"Pattern: {pattern} Options: {options & ~RegexOptions.Debug} Timeout: {(matchTimeout == InfiniteMatchTimeout ? "infinite" : matchTimeout.ToString())}");
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,37 +334,35 @@ public int Scan(string text, int index, int beglimit, int endlimit)
}

#if DEBUG
/// <summary>
/// Used when dumping for debugging.
/// </summary>
/// <summary>Used when dumping for debugging.</summary>
[ExcludeFromCodeCoverage]
public override string ToString() => Pattern;

[ExcludeFromCodeCoverage]
public string Dump(string indent)
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();

sb.AppendLine($"{indent}BM Pattern: {Pattern}");
sb.Append(indent + "Positive: ");
for (int i = 0; i < Positive.Length; i++)

sb.Append($"{indent}Positive: ");
foreach (int i in Positive)
{
sb.Append(Positive[i].ToString(CultureInfo.InvariantCulture) + " ");
sb.Append($"{i} ");
}
sb.AppendLine();

if (NegativeASCII != null)
{
sb.Append(indent + "Negative table: ");
sb.Append($"{indent}Negative table: ");
for (int i = 0; i < NegativeASCII.Length; i++)
{
if (NegativeASCII[i] != Pattern.Length)
{
sb.Append(" {" + Regex.Escape(Convert.ToString((char)i, CultureInfo.InvariantCulture)) + " " + NegativeASCII[i].ToString(CultureInfo.InvariantCulture) + "}");
sb.Append($" {{{Regex.Escape(((char)i).ToString())} {NegativeASCII[i]}}}");
}
}
}

sb.AppendLine();

return sb.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ public static char SingletonChar(string set)
return set[SetStartIndex];
}

public static bool IsMergeable(string? charClass) =>
public static bool IsMergeable(string charClass) =>
charClass != null &&
!IsNegated(charClass) &&
!IsSubtraction(charClass);
Expand Down Expand Up @@ -1541,7 +1541,7 @@ public static string SetDescription(string set)
int categoryLength = set[CategoryLengthIndex];
int endPosition = SetStartIndex + setLength + categoryLength;

StringBuilder desc = new StringBuilder();
var desc = new StringBuilder();

desc.Append('[');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ private static string OperatorDescription(int Opcode)
[ExcludeFromCodeCoverage]
public string OpcodeDescription(int offset)
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
int opcode = Codes[offset];

sb.AppendFormat("{0:D6} ", offset);
Expand All @@ -307,7 +307,7 @@ public string OpcodeDescription(int offset)
case Notoneloopatomic:
case Onelazy:
case Notonelazy:
sb.Append(RegexCharClass.CharDescription((char)Codes[offset + 1]));
sb.Append("'").Append(RegexCharClass.CharDescription((char)Codes[offset + 1])).Append("'");
break;

case Set:
Expand All @@ -319,7 +319,7 @@ public string OpcodeDescription(int offset)
break;

case Multi:
sb.Append(Strings[Codes[offset + 1]]);
sb.Append('"').Append(Strings[Codes[offset + 1]]).Append('"');
break;

case Ref:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1772,7 +1772,7 @@ static bool NodeSupportsNonBacktrackingImplementation(RegexNode node, int maxDep
// Its children must all also be supported.
case RegexNode.Alternate:
if (node.Next != null &&
(node.Next.Type == RegexNode.Atomic || // atomic alternate
(node.IsAtomicByParent() || // atomic alternate
(node.Next.Type == RegexNode.Capture && node.Next.Next is null))) // root alternate
{
goto case RegexNode.Concatenate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,29 +227,20 @@ private static int AnchorFromType(int type) =>
[ExcludeFromCodeCoverage]
public static string AnchorDescription(int anchors)
{
StringBuilder sb = new StringBuilder();

if (0 != (anchors & Beginning))
sb.Append(", Beginning");
if (0 != (anchors & Start))
sb.Append(", Start");
if (0 != (anchors & Bol))
sb.Append(", Bol");
if (0 != (anchors & Boundary))
sb.Append(", Boundary");
if (0 != (anchors & ECMABoundary))
sb.Append(", ECMABoundary");
if (0 != (anchors & Eol))
sb.Append(", Eol");
if (0 != (anchors & End))
sb.Append(", End");
if (0 != (anchors & EndZ))
sb.Append(", EndZ");

if (sb.Length >= 2)
return (sb.ToString(2, sb.Length - 2));

return "None";
var sb = new StringBuilder();

if ((anchors & Beginning) != 0) sb.Append(", Beginning");
if ((anchors & Start) != 0) sb.Append(", Start");
if ((anchors & Bol) != 0) sb.Append(", Bol");
if ((anchors & Boundary) != 0) sb.Append(", Boundary");
if ((anchors & ECMABoundary) != 0) sb.Append(", ECMABoundary");
if ((anchors & Eol) != 0) sb.Append(", Eol");
if ((anchors & End) != 0) sb.Append(", End");
if ((anchors & EndZ) != 0) sb.Append(", EndZ");

return sb.Length >= 2 ?
sb.ToString(2, sb.Length - 2) :
"None";
}
#endif

Expand Down
Loading