Exemple #1
0
        private static ReadOnlySpan <byte> GetUnescapedString(ReadOnlySpan <byte> utf8Source, int idx)
        {
            // The escaped name is always longer than the unescaped, so it is safe to use escaped name for the buffer length.
            int length = utf8Source.Length;

            byte[] pooledName = null;

            Span <byte> unescapedName = length <= JsonConstants.StackallocThreshold ?
                                        stackalloc byte[length] :
                                        (pooledName = ArrayPool <byte> .Shared.Rent(length));

            JsonReaderHelper.Unescape(utf8Source, unescapedName, idx, out int written);
            ReadOnlySpan <byte> propertyName = unescapedName.Slice(0, written).ToArray();

            if (pooledName != null)
            {
                // We clear the array because it is "user data" (although a property name).
                new Span <byte>(pooledName, 0, written).Clear();
                ArrayPool <byte> .Shared.Return(pooledName);
            }

            return(propertyName);
        }