Skip to content

Commit

Permalink
Use RegexRunner.CharInClass directly from source-generated regex code (
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Apr 14, 2023
1 parent 510e9fb commit ed0f1c5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4932,19 +4932,6 @@ private static string MatchCharacterClass(string chExpr, string charClass, bool
$"({range0Clause} | {range1Clause})";
}

const string Base = nameof(Base);
if (!requiredHelpers.ContainsKey(Base))
{
requiredHelpers.Add(Base, new string[]
{
$"internal class {Base} : RegexRunner",
$"{{",
$" /// <summary>Determines whether the specified character in is in the specified character class.</summary>",
$" internal static new bool CharInClass(char ch, string charClass) => RegexRunner.CharInClass(ch, charClass);",
$"}}",
});
}

if (analysis.ContainsNoAscii)
{
// We determined that the character class contains only non-ASCII,
Expand Down Expand Up @@ -5041,20 +5028,20 @@ private static string MatchCharacterClass(string chExpr, string charClass, bool

_ => $"({Literal(bitVectorString)}[ch >> 4] & (1 << (ch & 0xF))) {(negate ? "=" : "!")}= 0",
};
return $"((ch = {chExpr}) < 128 ? {asciiExpr} : {(negate ? "!" : "")}{HelpersTypeName}.{Base}.CharInClass((char)ch, {Literal(charClass)}))";
return $"((ch = {chExpr}) < 128 ? {asciiExpr} : {(negate ? "!" : "")}RegexRunner.CharInClass((char)ch, {Literal(charClass)}))";

string EmitContainsNoAscii()
{
return negate ?
$"((ch = {chExpr}) < 128 || !{HelpersTypeName}.{Base}.CharInClass((char)ch, {Literal(charClass)}))" :
$"((ch = {chExpr}) >= 128 && {HelpersTypeName}.{Base}.CharInClass((char)ch, {Literal(charClass)}))";
$"((ch = {chExpr}) < 128 || !RegexRunner.CharInClass((char)ch, {Literal(charClass)}))" :
$"((ch = {chExpr}) >= 128 && RegexRunner.CharInClass((char)ch, {Literal(charClass)}))";
}

string EmitAllAsciiContained()
{
return negate ?
$"((ch = {chExpr}) >= 128 && !{HelpersTypeName}.{Base}.CharInClass((char)ch, {Literal(charClass)}))" :
$"((ch = {chExpr}) < 128 || {HelpersTypeName}.{Base}.CharInClass((char)ch, {Literal(charClass)}))";
$"((ch = {chExpr}) >= 128 && !RegexRunner.CharInClass((char)ch, {Literal(charClass)}))" :
$"((ch = {chExpr}) < 128 || RegexRunner.CharInClass((char)ch, {Literal(charClass)}))";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public abstract partial class RegexRunner
protected internal int runtrackpos;
protected internal RegexRunner() { }
protected void Capture(int capnum, int start, int end) { }
protected static bool CharInClass(char ch, string charClass) { throw null; }
public static bool CharInClass(char ch, string charClass) { throw null; }
protected static bool CharInSet(char ch, string @set, string category) { throw null; }
protected void CheckTimeout() { }
protected void Crawl(int i) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ protected static bool CharInSet(char ch, string set, string category)
return RegexCharClass.CharInClass(ch, charClass);
}

protected static bool CharInClass(char ch, string charClass)
public static bool CharInClass(char ch, string charClass)
{
return RegexCharClass.CharInClass(ch, charClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ private bool TryMatchAtCurrentPosition(ReadOnlySpan<char> inputSpan)
// Match a character in the set [^>\s] atomically at least once.
{
int iteration3 = 0;
while ((uint)iteration3 < (uint)slice.Length && ((ch = slice[iteration3]) < 128 ? ("쇿\uffff\ufffe뿿\uffff\uffff\uffff\uffff"[ch >> 4] & (1 << (ch & 0xF))) != 0 : Utilities.Base.CharInClass((char)ch, "\u0001\u0002\u0001>?d")))
while ((uint)iteration3 < (uint)slice.Length && ((ch = slice[iteration3]) < 128 ? ("쇿\uffff\ufffe뿿\uffff\uffff\uffff\uffff"[ch >> 4] & (1 << (ch & 0xF))) != 0 : RegexRunner.CharInClass((char)ch, "\u0001\u0002\u0001>?d")))
{
iteration3++;
}
Expand Down Expand Up @@ -702,16 +702,6 @@ void UncaptureUntil(int capturePosition)

}

/// <summary>Helper methods used by generated <see cref="Regex"/>-derived implementations.</summary>
[GeneratedCodeAttribute("System.Text.RegularExpressions.Generator", "42.42.42.42")]
file static class Utilities
{
internal class Base : RegexRunner
{
/// <summary>Determines whether the specified character in is in the specified character class.</summary>
internal static new bool CharInClass(char ch, string charClass) => RegexRunner.CharInClass(ch, charClass);
}
}
}
"""
};
Expand Down

0 comments on commit ed0f1c5

Please sign in to comment.