Skip to content

Commit

Permalink
Add a few missing char.IsXx helpers usage to RegexGenerator.Emitter.cs (
Browse files Browse the repository at this point in the history
#75101)

Just pretties up the generated code if these ranges are used.  With this, all of the char.IsXx helpers can be emitted.
  • Loading branch information
stephentoub committed Sep 6, 2022
1 parent ae44947 commit 733a51b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
Expand Up @@ -4404,9 +4404,13 @@ private static string MatchCharacterClass(RegexOptions options, string chExpr, s
negate ^= RegexCharClass.IsNegated(charClass);
return (lowInclusive, highInclusive) switch
{
('\0', '\u007F') => $"{(negate ? "!" : "")}char.IsAscii({chExpr})",
('0', '9') => $"{(negate ? "!" : "")}char.IsAsciiDigit({chExpr})",
('a', 'z') => $"{(negate ? "!" : "")}char.IsAsciiLetterLower({chExpr})",
('A', 'Z') => $"{(negate ? "!" : "")}char.IsAsciiLetterUpper({chExpr})",
('\ud800', '\udfff') => $"{(negate ? "!" : "")}char.IsSurrogate({chExpr})",
('\ud800', '\udbff') => $"{(negate ? "!" : "")}char.IsHighSurrogate({chExpr})",
('\udc00', '\udfff') => $"{(negate ? "!" : "")}char.IsLowSurrogate({chExpr})",
_ when lowInclusive == highInclusive => $"({chExpr} {(negate ? "!=" : "==")} {Literal(lowInclusive)})",
_ => $"{(negate ? "!" : "")}char.IsBetween({chExpr}, {Literal(lowInclusive)}, {Literal(highInclusive)})",
};
Expand Down
Expand Up @@ -85,7 +85,7 @@ internal abstract class RegexCompiler
private static readonly MethodInfo s_arrayResize = typeof(Array).GetMethod("Resize")!.MakeGenericMethod(typeof(int));
private static readonly MethodInfo s_mathMinIntInt = typeof(Math).GetMethod("Min", new Type[] { typeof(int), typeof(int) })!;
// Note:
// IsAsciiLetterLower, IsAsciiLetterUpper, IsAsciiDigit, and IsBetween aren't used here, as the IL generated for those
// Single-range helpers like IsAsciiLetterLower, IsAsciiLetterUpper, IsAsciiDigit, and IsBetween aren't used here, as the IL generated for those
// single-range checks is as cheap as the method call, and there's no readability issue as with the source generator.

/// <summary>The ILGenerator currently in use.</summary>
Expand Down

0 comments on commit 733a51b

Please sign in to comment.