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

Code before:
            // 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;
            }

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

                builder.Append(c);
            }
        }

        public static (int, int) CountNewLines(ReadOnlySpan<byte> data)
        {
            int lastLineFeedIndex = data.LastIndexOf(JsonConstants.LineFeed);
            int newLines = 0;

            if (lastLineFeedIndex >= 0)
            {
                newLines = 1;
                data = data.Slice(0, lastLineFeedIndex);
#if NET
                newLines += data.Count(JsonConstants.LineFeed);
#else

Review comment (suggestion):
@copilot, same comment as above. Under `#if NET`, StringBuilder supports appending a span, so it can append a slice. Otherwise, StringBuilder supports appending a pointer/length, so it can pin the slice and append it that way.

Code after:
            }

            // 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)
        {
            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('\\');