コード例 #1
0
        private static void EscapeString(ReadOnlySpan <byte> value, Span <byte> destination, JavaScriptEncoder encoder, ref int written)
        {
            Debug.Assert(encoder != null);

            OperationStatus result = encoder.EncodeUtf8(value, destination, out int encoderBytesConsumed, out int encoderBytesWritten);

            Debug.Assert(result != OperationStatus.DestinationTooSmall);
            Debug.Assert(result != OperationStatus.NeedMoreData);
            Debug.Assert(encoderBytesConsumed == value.Length);

            if (result != OperationStatus.Done)
            {
                ThrowHelper.ThrowArgumentException_InvalidUTF8(value.Slice(encoderBytesWritten));
            }

            written += encoderBytesWritten;
        }
コード例 #2
0
        public static void EscapeString(ReadOnlySpan <byte> value, Span <byte> destination, int indexOfFirstByteToEscape, JavaScriptEncoder encoder, out int written)
        {
            Debug.Assert(indexOfFirstByteToEscape >= 0 && indexOfFirstByteToEscape < value.Length);

            value.Slice(0, indexOfFirstByteToEscape).CopyTo(destination);
            written = indexOfFirstByteToEscape;
            int consumed = indexOfFirstByteToEscape;

            if (encoder != null)
            {
                OperationStatus result = encoder.EncodeUtf8(
                    value.Slice(consumed), destination.Slice(written), out int encoderBytesConsumed, out int encoderBytesWritten);

                Debug.Assert(result != OperationStatus.DestinationTooSmall);
                Debug.Assert(result != OperationStatus.NeedMoreData);
                Debug.Assert(encoderBytesConsumed == value.Length - consumed);

                if (result != OperationStatus.Done)
                {
                    ThrowHelper.ThrowArgumentException_InvalidUTF8(value.Slice(encoderBytesWritten));
                }

                written += encoderBytesWritten;
            }
            else
            {
                // For performance when no encoder is specified, perform escaping here for Ascii and on the
                // first occurrence of a non-Ascii character, then call into the default encoder.
                while (consumed < value.Length)
                {
                    byte val = value[consumed];
                    if (IsAsciiValue(val))
                    {
                        if (NeedsEscapingNoBoundsCheck(val))
                        {
                            EscapeNextBytes(val, destination, ref written);
                            consumed++;
                        }
                        else
                        {
                            destination[written] = val;
                            written++;
                            consumed++;
                        }
                    }
                    else
                    {
                        // Fall back to default encoder
                        OperationStatus result = JavaScriptEncoder.Default.EncodeUtf8(
                            value.Slice(consumed), destination.Slice(written), out int encoderBytesConsumed, out int encoderBytesWritten);

                        Debug.Assert(result != OperationStatus.DestinationTooSmall);
                        Debug.Assert(result != OperationStatus.NeedMoreData);
                        Debug.Assert(encoderBytesConsumed == value.Length - consumed);

                        if (result != OperationStatus.Done)
                        {
                            ThrowHelper.ThrowArgumentException_InvalidUTF8(value.Slice(encoderBytesConsumed));
                        }

                        consumed += encoderBytesConsumed;
                        written  += encoderBytesWritten;
                    }
                }
            }
        }