Repository: dotnet/runtime
File: src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs (C#)

Code before:
    {
        // Characters that require the bracketed property name syntax in JSON Path.
        private static readonly SearchValues<char> s_specialCharacters = SearchValues.Create("$. '/\"[]()\t\n\r\f\b\\\u0085\u2028\u2029");

        // Characters that need to be escaped in the single-quoted bracket notation.
        private static readonly SearchValues<char> s_charactersToEscape = SearchValues.Create("'\\");

        public static bool ContainsSpecialCharacters(this ReadOnlySpan<char> text) =>
            text.ContainsAny(s_specialCharacters);

        /// <summary>
        /// Appends a property name escaped for use in JSON Path single-quoted bracket notation.
        /// Escapes single quotes as \' and backslashes as \\.
        /// </summary>
        public static void AppendEscapedPropertyName(this ref ValueStringBuilder builder, string propertyName)
        {
            ReadOnlySpan<char> span = propertyName.AsSpan();

            // Fast path: if no characters need escaping, append directly.
            if (!span.ContainsAny(s_charactersToEscape))
            {
                builder.Append(propertyName);
                return;
            }

            // Slow path: escape single quotes and backslashes.
            foreach (char c in span)
            {
                if (c is '\\' or '\'')
                {
                    builder.Append('\\');
                }

                builder.Append(c);
            }
        }

        /// <summary>
        /// Appends a property name escaped for use in JSON Path single-quoted bracket notation.
        /// Escapes single quotes as \' and backslashes as \\.
        /// </summary>
        public static void AppendEscapedPropertyName(this StringBuilder builder, string propertyName)
        {
            ReadOnlySpan<char> span = propertyName.AsSpan();

            // Fast path: if no characters need escaping, append directly.
            if (!span.ContainsAny(s_charactersToEscape))
            {
                builder.Append(propertyName);
                return;
            }

Review comment (suggestion):
@copilot, instead of using ContainsAny, this can use IndexOfAny. If the result is < 0, then it can just append the propertyName and return. If it's > 0, it can append propertyName.Slice(0, i). And then it can walk character by character starting from i as is done below. That way, if the character that needs escaping is later rather than earlier, we don't have to spend time individually appending each individual character up until that point.

Code after:
    {
        // Characters that require the bracketed property name syntax in JSON Path.
        private static readonly SearchValues<char> s_specialCharacters = SearchValues.Create("$. '/\"[]()\t\n\r\f\b\\\u0085\u2028\u2029");

        // Characters that need to be escaped in the single-quoted bracket notation.
        private static readonly SearchValues<char> s_charactersToEscape = SearchValues.Create("'\\");

        public static bool ContainsSpecialCharacters(this ReadOnlySpan<char> text) =>
            text.ContainsAny(s_specialCharacters);

        /// <summary>
        /// Appends a property name escaped for use in JSON Path single-quoted bracket notation.
        /// Escapes single quotes as \' and backslashes as \\.
        /// </summary>
        public static void AppendEscapedPropertyName(this ref ValueStringBuilder builder, string propertyName)
        {
            ReadOnlySpan<char> span = propertyName.AsSpan();

            int i = span.IndexOfAny(s_charactersToEscape);

            // Fast path: if no characters need escaping, append directly.
            if (i < 0)
            {
                builder.Append(propertyName);
                return;
            }

            // Append the portion before the first character needing escaping.
            if (i > 0)
            {
                builder.Append(span.Slice(0, i));
            }

            // Escape characters from position i onward.
            for (; i < span.Length; i++)
            {
                char c = span[i];
                if (c is '\\' or '\'')
                {
                    builder.Append('\\');
                }

                builder.Append(c);
            }
        }

        /// <summary>
        /// Appends a property name escaped for use in JSON Path single-quoted bracket notation.
        /// Escapes single quotes as \' and backslashes as \\.
        /// </summary>
        public static void AppendEscapedPropertyName(this StringBuilder builder, string propertyName)