Skip to content
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 @@ -181,6 +181,8 @@ private static string Syntax(string pattern)
[InlineData(@"\u0041\x41\cA", @"\u0041 \x41 \cA")]
[InlineData("[a-z][^0-9]", "[a-z] [^0-9]")]
[InlineData(@"[]a][\]][a-z-[aeiou]]x", @"[]a] [\]] [a-z-[aeiou]]")]
[InlineData("[a[b]x+", "[a[b] +")]
[InlineData("[[]x[a[-[a]]y", "[[] [a[-[a]]")]
[InlineData("(a)(?:b)(?=c)(?!d)(?<=e)(?<!f)(?>g)", "( ) (?: ) (?= ) (?! ) (?<= ) (?<! ) (?> )")]
[InlineData("(?<n>x)(?'m'y)(?i)(?i-m:z)(?#note)", "(?<n> ) (?'m' ) (?i ) (?i-m: ) (?#note)")]
[InlineData("a*b+c?d*?e+?f??", "* + ? *? +? ??")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private static int DigitsEnd(string pattern, int at)
return end;
}

/// <summary>A whole <c>[...]</c> class: a leading <c>]</c> is a member, an escape hides its character, a subtraction nests.</summary>
/// <summary>A whole <c>[...]</c> class: a leading <c>]</c> is a member, an escape hides its character, only a subtraction nests.</summary>
private static int ClassEnd(string pattern, int at)
{
var index = Follows(pattern, at + 1, '^') ? at + 2 : at + 1;
Expand All @@ -95,7 +95,7 @@ private static int ClassEnd(string pattern, int at)
{
depth += pattern[index] switch
{
'[' => 1,
'[' when OpensSubtraction(pattern, index) => 1,
']' => -1,
_ => 0,
};
Expand All @@ -104,6 +104,15 @@ private static int ClassEnd(string pattern, int at)
return Math.Min(index, pattern.Length);
}

/// <summary>
/// True when the <c>[</c> at <paramref name="at"/> opens a subtraction (<c>-[…]</c>). Any
/// other <c>[</c> inside a class is a literal member: <c>[a[b]</c> ends at its first <c>]</c>.
/// </summary>
private static bool OpensSubtraction(string pattern, int at)
{
return at > 0 && pattern[at - 1] == '-' && (at < 2 || pattern[at - 2] != '\\');
}

/// <summary><c>(</c> with its prefix: <c>(?:</c>, <c>(?=</c>, <c>(?&lt;name&gt;</c>, inline options; a comment group whole.</summary>
private static int GroupOpenEnd(string pattern, int at)
{
Expand Down