Skip to content

Commit e16ce50

Browse files
stephentoubrzikm
authored andcommitted
Improve a couple of regex source generator oddities (dotnet#103851)
* Avoiding generating a useless IndexOfAny When we can't efficiently enumerate a character class and it's not just a single range, we fall back to outputting a helper IndexOfXx method. This method does an optimized search for either any ASCII member of the set or anything non-ASCII, and then falls back to walking the items one-by-one. This makes sense, unless all of ASCII is in the set, in which case this is a meaningless IndexOfAny call because it's just going to always return 0 or -1. We should avoid emitting an IndexOfAny call in such a case. * This helper is emitting a call to IndexOfAnyExceptInRange, passing in the full ASCII range. The intent was for this to literally output the text `IndexOfAnyExceptInRange('\0', '\u007f')` into the generated C#, but because those are single slashes, it's actually outputting the characters (char)0x0 and (char(0x7F. That's functionally correct, but it's not what was intended and makes the code harder to read. The fix is just to put in the missing slashes.
1 parent 0a3dc53 commit e16ce50

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -547,16 +547,19 @@ private static string EmitIndexOfAnyCustomHelper(string set, Dictionary<string,
547547
lines.Add($"internal static int {helperName}(this ReadOnlySpan<char> span)");
548548
lines.Add($"{{");
549549
int uncheckedStart = lines.Count;
550-
lines.Add(excludedAsciiChars.Count == 128 ?
551-
$" int i = span.IndexOfAnyExceptInRange('\0', '\u007f');" :
550+
lines.Add(excludedAsciiChars.Count == 128 ? $" int i = span.IndexOfAnyExceptInRange('\\0', '\\u007f');" : // no ASCII is in the set
551+
excludedAsciiChars.Count == 0 ? $" int i = 0;" : // all ASCII is in the set
552552
$" int i = span.IndexOfAnyExcept({EmitSearchValues(excludedAsciiChars.ToArray(), requiredHelpers)});");
553553
lines.Add($" if ((uint)i < (uint)span.Length)");
554554
lines.Add($" {{");
555-
lines.Add($" if (char.IsAscii(span[i]))");
556-
lines.Add($" {{");
557-
lines.Add($" return i;");
558-
lines.Add($" }}");
559-
lines.Add($"");
555+
if (excludedAsciiChars.Count is not (0 or 128))
556+
{
557+
lines.Add($" if (char.IsAscii(span[i]))");
558+
lines.Add($" {{");
559+
lines.Add($" return i;");
560+
lines.Add($" }}");
561+
lines.Add($"");
562+
}
560563
if (additionalDeclarations.Count > 0)
561564
{
562565
lines.AddRange(additionalDeclarations.Select(s => $" {s}"));

0 commit comments

Comments
 (0)