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

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

#if !NET
        /// <summary>Polyfill for StringBuilder.Append(ReadOnlySpan&lt;char&gt;) which is not available on .NET Standard 2.0.</summary>
        private static unsafe void Append(this StringBuilder builder, ReadOnlySpan<char> span)
        {
            fixed (char* ptr = span)
            {
                builder.Append(ptr, span.Length);
            }
        }
#endif

        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
                int pos;
                while ((pos = data.IndexOf(JsonConstants.LineFeed)) >= 0)
                {
                    newLines++;
                    data = data.Slice(pos + 1);

Review comment (security):
@copilot, can you instead add this as a polyfill in https://github.com/dotnet/runtime/tree/main/src/libraries/Common/src/System/Text ? Otherwise there's not really any benefit to separating this out from AppendEscapedPropertyName. Once you do that, you might also dedup with other occurrences, like https://github.com/dotnet/runtime/blob/34c3feeb72002da5600dd4de02636042486e1ca8/src/libraries/System.Text.RegularExpressions/gen/Stubs.cs#L17

Code after:
                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);
            }
        }

        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
                int pos;
                while ((pos = data.IndexOf(JsonConstants.LineFeed)) >= 0)
                {
                    newLines++;
                    data = data.Slice(pos + 1);
                }
#endif
            }

            return (newLines, lastLineFeedIndex);
        }

        internal static JsonValueKind ToValueKind(this JsonTokenType tokenType)
        {
            switch (tokenType)
            {