diff --git a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs index 33755cfd62c8a..12014b3fd15e9 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs @@ -547,16 +547,19 @@ private static string EmitIndexOfAnyCustomHelper(string set, Dictionary span)"); lines.Add($"{{"); int uncheckedStart = lines.Count; - lines.Add(excludedAsciiChars.Count == 128 ? - $" int i = span.IndexOfAnyExceptInRange('\0', '\u007f');" : + lines.Add(excludedAsciiChars.Count == 128 ? $" int i = span.IndexOfAnyExceptInRange('\\0', '\\u007f');" : // no ASCII is in the set + excludedAsciiChars.Count == 0 ? $" int i = 0;" : // all ASCII is in the set $" int i = span.IndexOfAnyExcept({EmitSearchValues(excludedAsciiChars.ToArray(), requiredHelpers)});"); lines.Add($" if ((uint)i < (uint)span.Length)"); lines.Add($" {{"); - lines.Add($" if (char.IsAscii(span[i]))"); - lines.Add($" {{"); - lines.Add($" return i;"); - lines.Add($" }}"); - lines.Add($""); + if (excludedAsciiChars.Count is not (0 or 128)) + { + lines.Add($" if (char.IsAscii(span[i]))"); + lines.Add($" {{"); + lines.Add($" return i;"); + lines.Add($" }}"); + lines.Add($""); + } if (additionalDeclarations.Count > 0) { lines.AddRange(additionalDeclarations.Select(s => $" {s}"));