Repository: dotnet/runtime
File: src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexFindOptimizations.cs (C#)

Code before:
                default:
                    Debug.Assert(FindMode == FindNextStartingPositionMode.NoSearch, $"Unexpected FindMode {FindMode}");
                    return true;
            }
        }
#endif

        /// <summary>
        /// Determines whether the characters in the best <see cref="FixedDistanceSet"/> are frequent enough in typical text
        /// that IndexOfAny with those characters would be a poor filter. When the best set's characters are common,
        /// multi-string search via SearchValues is preferred because IndexOfAny would match too many
        /// positions. When the characters are rare, IndexOfAny is an excellent filter and is preferred.
        /// </summary>
        private static bool HasHighFrequencyChars(in FixedDistanceSet set)
        {
            if (set.Negated || set.Chars is not { Length: > 0 } chars)
            {
                return false;
            }

            ReadOnlySpan<float> frequency = RegexPrefixAnalyzer.Frequency;
            float totalFrequency = 0;

            foreach (char c in chars)
            {
                if (c >= 128)
                {
                    return false;
                }

                totalFrequency += frequency[c];
            }

            // If the average frequency of the set's chars exceeds this threshold, the characters
            // are common enough that IndexOfAny would match many positions, making SearchValues<string>
            // the better choice. Common characters like lowercase letters have frequencies above this
            // threshold, while less frequent characters like uppercase letters fall below it, so
            // IndexOfAny with rare characters is already an effective filter and is preferred.
            const float HighFrequencyThreshold = 0.6f;
            return totalFrequency >= HighFrequencyThreshold * chars.Length;
        }
    }

    /// <summary>Mode to use for searching for the next location of a possible match.</summary>
    internal enum FindNextStartingPositionMode
    {
        /// <summary>A "beginning" anchor at the beginning of the pattern.</summary>
        LeadingAnchor_LeftToRight_Beginning,
        /// <summary>A "start" anchor at the beginning of the pattern.</summary>
        LeadingAnchor_LeftToRight_Start,
        /// <summary>An "endz" anchor at the beginning of the pattern.  This is rare.</summary>

Review comment (suggestion):
This can now be
```suggestion
                if (c >= frequency.Length)
```

Code after:
#endif

        /// <summary>
        /// Determines whether the characters in the best <see cref="FixedDistanceSet"/> are frequent enough in typical text
        /// that IndexOfAny with those characters would be a poor filter. When the best set's characters are common,
        /// multi-string search via SearchValues is preferred because IndexOfAny would match too many
        /// positions. When the characters are rare, IndexOfAny is an excellent filter and is preferred.
        /// </summary>
        private static bool HasHighFrequencyChars(FixedDistanceSet set)
        {
            // Negated sets match most characters (e.g. [^a]), so they are inherently high frequency.
            if (set.Negated)
            {
                return true;
            }

            // Sets without extracted chars can't be frequency-analyzed.
            // Single-char sets use IndexOf which has much higher throughput than multi-string search,
            // so we never consider them "high frequency" regardless of which character it is.
            if (set.Chars is not { Length: > 1 } chars)
            {
                return false;
            }

            ReadOnlySpan<float> frequency = RegexPrefixAnalyzer.Frequency;
            float totalFrequency = 0;

            foreach (char c in chars)
            {
                if (c >= frequency.Length)
                {
                    return false;
                }

                totalFrequency += frequency[c];
            }

            // If the average frequency of the set's chars exceeds this threshold, the characters
            // are common enough that IndexOfAny would match many positions, making SearchValues<string>
            // the better choice. Common characters like lowercase letters have frequencies above this
            // threshold, while less frequent characters like uppercase letters fall below it, so
            // IndexOfAny with rare characters is already an effective filter and is preferred.
            const float HighFrequencyThreshold = 0.6f;
            return totalFrequency >= HighFrequencyThreshold * chars.Length;
        }
    }

    /// <summary>Mode to use for searching for the next location of a possible match.</summary>
    internal enum FindNextStartingPositionMode
    {
        /// <summary>A "beginning" anchor at the beginning of the pattern.</summary>