Skip to content

Commit 733a51b

Browse files
authored
Add a few missing char.IsXx helpers usage to RegexGenerator.Emitter.cs (#75101)
Just pretties up the generated code if these ranges are used. With this, all of the char.IsXx helpers can be emitted.
1 parent ae44947 commit 733a51b

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4404,9 +4404,13 @@ private static string MatchCharacterClass(RegexOptions options, string chExpr, s
44044404
negate ^= RegexCharClass.IsNegated(charClass);
44054405
return (lowInclusive, highInclusive) switch
44064406
{
4407+
('\0', '\u007F') => $"{(negate ? "!" : "")}char.IsAscii({chExpr})",
44074408
('0', '9') => $"{(negate ? "!" : "")}char.IsAsciiDigit({chExpr})",
44084409
('a', 'z') => $"{(negate ? "!" : "")}char.IsAsciiLetterLower({chExpr})",
44094410
('A', 'Z') => $"{(negate ? "!" : "")}char.IsAsciiLetterUpper({chExpr})",
4411+
('\ud800', '\udfff') => $"{(negate ? "!" : "")}char.IsSurrogate({chExpr})",
4412+
('\ud800', '\udbff') => $"{(negate ? "!" : "")}char.IsHighSurrogate({chExpr})",
4413+
('\udc00', '\udfff') => $"{(negate ? "!" : "")}char.IsLowSurrogate({chExpr})",
44104414
_ when lowInclusive == highInclusive => $"({chExpr} {(negate ? "!=" : "==")} {Literal(lowInclusive)})",
44114415
_ => $"{(negate ? "!" : "")}char.IsBetween({chExpr}, {Literal(lowInclusive)}, {Literal(highInclusive)})",
44124416
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ internal abstract class RegexCompiler
8585
private static readonly MethodInfo s_arrayResize = typeof(Array).GetMethod("Resize")!.MakeGenericMethod(typeof(int));
8686
private static readonly MethodInfo s_mathMinIntInt = typeof(Math).GetMethod("Min", new Type[] { typeof(int), typeof(int) })!;
8787
// Note:
88-
// IsAsciiLetterLower, IsAsciiLetterUpper, IsAsciiDigit, and IsBetween aren't used here, as the IL generated for those
88+
// Single-range helpers like IsAsciiLetterLower, IsAsciiLetterUpper, IsAsciiDigit, and IsBetween aren't used here, as the IL generated for those
8989
// single-range checks is as cheap as the method call, and there's no readability issue as with the source generator.
9090

9191
/// <summary>The ILGenerator currently in use.</summary>

0 commit comments

Comments
 (0)