Skip to content

Commit ed0f1c5

Browse files
authored
Use RegexRunner.CharInClass directly from source-generated regex code (#84814)
1 parent 510e9fb commit ed0f1c5

File tree

4 files changed

+8
-31
lines changed

4 files changed

+8
-31
lines changed

src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4932,19 +4932,6 @@ private static string MatchCharacterClass(string chExpr, string charClass, bool
49324932
$"({range0Clause} | {range1Clause})";
49334933
}
49344934

4935-
const string Base = nameof(Base);
4936-
if (!requiredHelpers.ContainsKey(Base))
4937-
{
4938-
requiredHelpers.Add(Base, new string[]
4939-
{
4940-
$"internal class {Base} : RegexRunner",
4941-
$"{{",
4942-
$" /// <summary>Determines whether the specified character in is in the specified character class.</summary>",
4943-
$" internal static new bool CharInClass(char ch, string charClass) => RegexRunner.CharInClass(ch, charClass);",
4944-
$"}}",
4945-
});
4946-
}
4947-
49484935
if (analysis.ContainsNoAscii)
49494936
{
49504937
// We determined that the character class contains only non-ASCII,
@@ -5041,20 +5028,20 @@ private static string MatchCharacterClass(string chExpr, string charClass, bool
50415028

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

50465033
string EmitContainsNoAscii()
50475034
{
50485035
return negate ?
5049-
$"((ch = {chExpr}) < 128 || !{HelpersTypeName}.{Base}.CharInClass((char)ch, {Literal(charClass)}))" :
5050-
$"((ch = {chExpr}) >= 128 && {HelpersTypeName}.{Base}.CharInClass((char)ch, {Literal(charClass)}))";
5036+
$"((ch = {chExpr}) < 128 || !RegexRunner.CharInClass((char)ch, {Literal(charClass)}))" :
5037+
$"((ch = {chExpr}) >= 128 && RegexRunner.CharInClass((char)ch, {Literal(charClass)}))";
50515038
}
50525039

50535040
string EmitAllAsciiContained()
50545041
{
50555042
return negate ?
5056-
$"((ch = {chExpr}) >= 128 && !{HelpersTypeName}.{Base}.CharInClass((char)ch, {Literal(charClass)}))" :
5057-
$"((ch = {chExpr}) < 128 || {HelpersTypeName}.{Base}.CharInClass((char)ch, {Literal(charClass)}))";
5043+
$"((ch = {chExpr}) >= 128 && !RegexRunner.CharInClass((char)ch, {Literal(charClass)}))" :
5044+
$"((ch = {chExpr}) < 128 || RegexRunner.CharInClass((char)ch, {Literal(charClass)}))";
50585045
}
50595046
}
50605047

src/libraries/System.Text.RegularExpressions/ref/System.Text.RegularExpressions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public abstract partial class RegexRunner
354354
protected internal int runtrackpos;
355355
protected internal RegexRunner() { }
356356
protected void Capture(int capnum, int start, int end) { }
357-
protected static bool CharInClass(char ch, string charClass) { throw null; }
357+
public static bool CharInClass(char ch, string charClass) { throw null; }
358358
protected static bool CharInSet(char ch, string @set, string category) { throw null; }
359359
protected void CheckTimeout() { }
360360
protected void Crawl(int i) { }

src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ protected static bool CharInSet(char ch, string set, string category)
399399
return RegexCharClass.CharInClass(ch, charClass);
400400
}
401401

402-
protected static bool CharInClass(char ch, string charClass)
402+
public static bool CharInClass(char ch, string charClass)
403403
{
404404
return RegexCharClass.CharInClass(ch, charClass);
405405
}

src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/RegexGeneratorOutputTests.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ private bool TryMatchAtCurrentPosition(ReadOnlySpan<char> inputSpan)
660660
// Match a character in the set [^>\s] atomically at least once.
661661
{
662662
int iteration3 = 0;
663-
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")))
663+
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")))
664664
{
665665
iteration3++;
666666
}
@@ -702,16 +702,6 @@ void UncaptureUntil(int capturePosition)
702702
703703
}
704704
705-
/// <summary>Helper methods used by generated <see cref="Regex"/>-derived implementations.</summary>
706-
[GeneratedCodeAttribute("System.Text.RegularExpressions.Generator", "42.42.42.42")]
707-
file static class Utilities
708-
{
709-
internal class Base : RegexRunner
710-
{
711-
/// <summary>Determines whether the specified character in is in the specified character class.</summary>
712-
internal static new bool CharInClass(char ch, string charClass) => RegexRunner.CharInClass(ch, charClass);
713-
}
714-
}
715705
}
716706
"""
717707
};

0 commit comments

Comments
 (0)