Exemple #1
0
        /// <summary>
        /// Checks if the string contains special char and returns the first index
        /// of special char if present.
        /// </summary>
        /// <param name="inputString">string that might contain special characters.</param>
        /// <param name="stringEscapeOption">The string escape option.</param>
        /// <param name="firstIndex">first index of the special char</param>
        /// <returns>A value indicating whether the string contains special character</returns>
        private static bool CheckIfStringHasSpecialChars(string inputString, ODataStringEscapeOption stringEscapeOption, out int firstIndex)
        {
            Debug.Assert(inputString != null, "The string value must not be null.");

            firstIndex = -1;
            int inputStringLength = inputString.Length;

            for (int currentIndex = 0; currentIndex < inputStringLength; currentIndex++)
            {
                char c = inputString[currentIndex];

                if (stringEscapeOption == ODataStringEscapeOption.EscapeOnlyControls && c >= 0x7F)
                {
                    continue;
                }

                // Append the un-handled characters (that do not require special treatment)
                // to the string builder when special characters are detected.
                if (JsonValueUtils.SpecialCharToEscapedStringMap[c] != null)
                {
                    firstIndex = currentIndex;
                    return(true);
                }
            }

            return(false);
        }
Exemple #2
0
 /// <summary>
 /// Creates a new instance of Json writer.
 /// </summary>
 /// <param name="writer">Writer to which text needs to be written.</param>
 /// <param name="isIeee754Compatible">if it is IEEE754Compatible.</param>
 /// <param name="stringEscapeOption">Specifies how to escape string.</param>
 internal JsonWriter(TextWriter writer, bool isIeee754Compatible, ODataStringEscapeOption stringEscapeOption)
 {
     this.writer = new NonIndentedTextWriter(writer);
     this.scopes = new Stack <Scope>();
     this.isIeee754Compatible = isIeee754Compatible;
     this.stringEscapeOption  = stringEscapeOption;
 }
        /// <summary>
        /// Writes the string value with special characters escaped.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="inputString">Input string value.</param>
        /// <param name="stringEscapeOption">The string escape option.</param>
        /// <param name="buffer">Char buffer to use for streaming data.</param>
        /// <param name="bufferPool">Array pool for renting a buffer.</param>
        internal static void WriteEscapedJsonStringValue(TextWriter writer, string inputString,
                                                         ODataStringEscapeOption stringEscapeOption, ref char[] buffer, ICharArrayPool bufferPool)
        {
            Debug.Assert(writer != null, "writer != null");
            Debug.Assert(inputString != null, "The string value must not be null.");

            int firstIndex;

            if (!JsonValueUtils.CheckIfStringHasSpecialChars(inputString, stringEscapeOption, out firstIndex))
            {
                writer.Write(inputString);
            }
            else
            {
                int inputStringLength = inputString.Length;

                Debug.Assert(firstIndex < inputStringLength, "First index of the special character should be within the string");
                buffer = BufferUtils.InitializeBufferIfRequired(bufferPool, buffer);
                int bufferLength = buffer.Length;
                int bufferIndex  = 0;
                int currentIndex = 0;

                // Let's copy and flush strings up to the first index of the special char
                while (currentIndex < firstIndex)
                {
                    int subStrLength = firstIndex - currentIndex;

                    Debug.Assert(subStrLength > 0, "SubStrLength should be greater than 0 always");

                    // If the first index of the special character is larger than the buffer length,
                    // flush everything to the buffer first and reset the buffer to the next chunk.
                    // Otherwise copy to the buffer and go on from there.
                    if (subStrLength >= bufferLength)
                    {
                        inputString.CopyTo(currentIndex, buffer, 0, bufferLength);
                        writer.Write(buffer, 0, bufferLength);
                        currentIndex += bufferLength;
                    }
                    else
                    {
                        inputString.CopyTo(currentIndex, buffer, 0, subStrLength);
                        bufferIndex   = subStrLength;
                        currentIndex += subStrLength;
                    }
                }

                // start writing escaped strings
                for (; currentIndex < inputStringLength; currentIndex++)
                {
                    bufferIndex = EscapeAndWriteCharToBuffer(writer, inputString[currentIndex], buffer, bufferIndex, stringEscapeOption);
                }

                // write any remaining chars to the writer
                if (bufferIndex > 0)
                {
                    writer.Write(buffer, 0, bufferIndex);
                }
            }
        }
Exemple #4
0
 public void WriteStringWithNoSpecialCharShouldLeaveBufferUntouched(ODataStringEscapeOption stringEscapeOption)
 {
     this.TestInit();
     char[] charBuffer = null;
     JsonValueUtils.WriteEscapedJsonString(this.writer, "StartMiddleEnd", stringEscapeOption, ref charBuffer);
     Assert.Equal("\"StartMiddleEnd\"", this.StreamToString());
     Assert.Null(charBuffer);
 }
Exemple #5
0
 public void WriteStringWithNoSpecialCharShouldLeaveBufferUntouched(ODataStringEscapeOption stringEscapeOption)
 {
     this.TestInit();
     char[] charBuffer = null;
     JsonValueUtils.WriteEscapedJsonString(this.writer, "StartMiddleEnd", stringEscapeOption, ref charBuffer);
     this.StreamToString().Should().Be("\"StartMiddleEnd\"");
     charBuffer.Should().BeNull("Char Buffer for cases with zero special characters should need to use buffer");
 }
        /// <summary>
        /// Writes the string value with special characters escaped.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="inputString">Input string value.</param>
        /// <param name="stringEscapeOption">The string escape option.</param>
        /// <param name="buffer">Char buffer to use for streaming data.</param>
        /// <param name="bufferPool">Array pool for renting a buffer.</param>
        internal static async Task WriteEscapedJsonStringValueAsync(
            this TextWriter writer,
            string inputString,
            ODataStringEscapeOption stringEscapeOption,
            Ref <char[]> buffer,
            ICharArrayPool bufferPool)
        {
            Debug.Assert(writer != null, "writer != null");
            Debug.Assert(inputString != null, "The string value must not be null.");

            int firstIndex;

            if (!CheckIfStringHasSpecialChars(inputString, stringEscapeOption, out firstIndex))
            {
                await writer.WriteAsync(inputString).ConfigureAwait(false);
            }
            else
            {
                Debug.Assert(firstIndex < inputString.Length, "First index of the special character should be within the string");
                buffer.Value = BufferUtils.InitializeBufferIfRequired(bufferPool, buffer.Value);
                int bufferLength = buffer.Value.Length;
                int bufferIndex  = 0;
                int currentIndex = 0;

                // Let's copy and flush strings up to the first index of the special char
                while (currentIndex < firstIndex)
                {
                    int substrLength = firstIndex - currentIndex;

                    Debug.Assert(substrLength > 0, "SubStrLength should be greater than 0 always");

                    // If the first index of the special character is larger than the buffer length,
                    // flush everything to the buffer first and reset the buffer to the next chunk.
                    // Otherwise copy to the buffer and go on from there.
                    if (substrLength >= bufferLength)
                    {
                        inputString.CopyTo(currentIndex, buffer.Value, 0, bufferLength);
                        await writer.WriteAsync(buffer.Value, 0, bufferLength).ConfigureAwait(false);

                        currentIndex += bufferLength;
                    }
                    else
                    {
                        WriteSubstringToBuffer(inputString, ref currentIndex, buffer.Value, ref bufferIndex, substrLength);
                    }
                }

                // Write escaped string to buffer
                WriteEscapedStringToBuffer(writer, inputString, ref currentIndex, buffer.Value, ref bufferIndex, stringEscapeOption);

                // write any remaining chars to the writer
                if (bufferIndex > 0)
                {
                    await writer.WriteAsync(buffer.Value, 0, bufferIndex).ConfigureAwait(false);
                }
            }
        }
        public async Task WriteStringWithNoSpecialCharShouldLeaveBufferUntouched(ODataStringEscapeOption stringEscapeOption)
        {
            this.Reset();
            Ref <char[]> charBuffer = new Ref <char[]>(null);
            await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, "StartMiddleEnd", stringEscapeOption, charBuffer);

            Assert.Equal("\"StartMiddleEnd\"", this.StreamToString());
            Assert.Null(charBuffer.Value);
        }
        /// <summary>
        /// Returns the string value with special characters escaped.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="inputString">Input string value.</param>
        /// <param name="stringEscapeOption">The string escape option.</param>
        /// <param name="buffer">Char buffer to use for streaming data</param>
        /// <param name="bufferPool">Array pool for renting a buffer.</param>
        internal static void WriteEscapedJsonString(TextWriter writer, string inputString,
                                                    ODataStringEscapeOption stringEscapeOption, ref char[] buffer, ICharArrayPool bufferPool = null)
        {
            Debug.Assert(writer != null, "writer != null");
            Debug.Assert(inputString != null, "The string value must not be null.");

            writer.Write(JsonConstants.QuoteCharacter);
            WriteEscapedJsonStringValue(writer, inputString, stringEscapeOption, ref buffer, bufferPool);
            writer.Write(JsonConstants.QuoteCharacter);
        }
Exemple #9
0
 public void WriteControllCharactersWithStringEscapeOptionShouldWork(ODataStringEscapeOption escapeOption)
 {
     foreach (string specialChar in this.controlCharsMap.Keys)
     {
         this.TestInit();
         char[] charBuffer = new char[6];
         JsonValueUtils.WriteEscapedJsonString(this.writer, string.Format("S{0}M{0}E{0}", specialChar),
                                               escapeOption, ref charBuffer);
         Assert.Equal(string.Format("\"S{0}M{0}E{0}\"", this.controlCharsMap[specialChar]), this.StreamToString());
     }
 }
Exemple #10
0
        public void WriteStringShouldIgnoreExistingContentsOfTheBuffer(ODataStringEscapeOption stringEscapeOption)
        {
            this.TestInit();
            char[] charBuffer = new char[128];
            for (int index = 0; index < 128; index++)
            {
                charBuffer[index] = (char)index;
            }

            JsonValueUtils.WriteEscapedJsonString(this.writer, "StartVeryVeryLongMiddleEnd", stringEscapeOption, ref charBuffer);
            Assert.Equal("\"StartVeryVeryLongMiddleEnd\"", this.StreamToString());
        }
Exemple #11
0
        /// <summary>
        /// Returns the string value with special characters escaped.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="inputString">Input string value.</param>
        /// <param name="stringEscapeOption">The string escape option.</param>
        /// <param name="buffer">Char buffer to use for streaming data.</param>
        /// <param name="bufferPool">Array pool for renting a buffer.</param>
        internal static async Task WriteEscapedJsonStringAsync(this TextWriter writer, string inputString,
                                                               ODataStringEscapeOption stringEscapeOption, Ref <char[]> buffer, ICharArrayPool bufferPool = null)
        {
            Debug.Assert(writer != null, "writer != null");
            Debug.Assert(inputString != null, "The string value must not be null.");

            await writer.WriteAsync(JsonConstants.QuoteCharacter).ConfigureAwait(false);

            await writer.WriteEscapedJsonStringValueAsync(inputString, stringEscapeOption, buffer, bufferPool).ConfigureAwait(false);

            await writer.WriteAsync(JsonConstants.QuoteCharacter).ConfigureAwait(false);
        }
        public async Task WriteControllCharactersWithStringEscapeOptionShouldWork(ODataStringEscapeOption escapeOption)
        {
            foreach (string specialChar in this.controlCharsMap.Keys)
            {
                this.Reset();
                Ref <char[]> charBuffer = new Ref <char[]>(new char[6]);
                await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, string.Format("S{0}M{0}E{0}", specialChar),
                                                                 escapeOption, charBuffer);

                Assert.Equal(string.Format("\"S{0}M{0}E{0}\"", this.controlCharsMap[specialChar]), this.StreamToString());
            }
        }
        public async Task WriteStringShouldIgnoreExistingContentsOfTheBuffer(ODataStringEscapeOption stringEscapeOption)
        {
            Ref <char[]> charBuffer = new Ref <char[]>(new char[128]);

            for (int index = 0; index < 128; index++)
            {
                charBuffer.Value[index] = (char)index;
            }

            await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, "StartVeryVeryLongMiddleEnd", stringEscapeOption, charBuffer);

            Assert.Equal("\"StartVeryVeryLongMiddleEnd\"", this.StreamToString());
        }
Exemple #14
0
        /// <summary>
        /// Write a string value.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="value">String value to be written.</param>
        /// <param name="stringEscapeOption">The string escape option.</param>
        /// <param name="buffer">Char buffer to use for streaming data</param>
        internal static void WriteValue(TextWriter writer, string value, ODataStringEscapeOption stringEscapeOption, ref char[] buffer, ICharArrayPool arrayPool = null)
        {
            Debug.Assert(writer != null, "writer != null");

            if (value == null)
            {
                writer.Write(JsonConstants.JsonNullLiteral);
            }
            else
            {
                JsonValueUtils.WriteEscapedJsonString(writer, value, stringEscapeOption, ref buffer, arrayPool);
            }
        }
Exemple #15
0
        /// <summary>
        /// Write a char value.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="value">The char value to write.</param>
        /// <param name="stringEscapeOption">The ODataStringEscapeOption to use in escaping the string.</param>
        internal static Task WriteValueAsync(this TextWriter writer, char value, ODataStringEscapeOption stringEscapeOption)
        {
            Debug.Assert(writer != null, "writer != null");

            if (stringEscapeOption == ODataStringEscapeOption.EscapeNonAscii || value <= 0x7F)
            {
                string escapedString = SpecialCharToEscapedStringMap[value];
                if (escapedString != null)
                {
                    return(writer.WriteAsync(escapedString));
                }
            }

            return(writer.WriteAsync(value));
        }
Exemple #16
0
        /// <summary>
        /// Writes an escaped string to the buffer.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="inputString">Input string value.</param>
        /// <param name="currentIndex">The index in the string at which copying should begin.</param>
        /// <param name="buffer">Char buffer to use for streaming data.</param>
        /// <param name="bufferIndex">Current position in the buffer after the string has been written.</param>
        /// <param name="stringEscapeOption">The string escape option.</param>
        /// <remarks>
        /// IMPORTANT: After all characters have been written,
        /// caller is responsible for writing the final buffer contents to the writer.
        /// </remarks>
        private static void WriteEscapedStringToBuffer(
            TextWriter writer,
            string inputString,
            ref int currentIndex,
            char[] buffer,
            ref int bufferIndex,
            ODataStringEscapeOption stringEscapeOption)
        {
            Debug.Assert(inputString != null, "inputString != null");
            Debug.Assert(buffer != null, "buffer != null");

            for (; currentIndex < inputString.Length; currentIndex++)
            {
                bufferIndex = EscapeAndWriteCharToBuffer(writer, inputString[currentIndex], buffer, bufferIndex, stringEscapeOption);
            }
        }
        /// <summary>
        /// Write a char value.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="value">The char value to write.</param>
        /// <param name="stringEscapeOption">The ODataStringEscapeOption to use in escaping the string.</param>
        internal static void WriteValue(TextWriter writer, char value, ODataStringEscapeOption stringEscapeOption)
        {
            Debug.Assert(writer != null, "writer != null");

            if (stringEscapeOption == ODataStringEscapeOption.EscapeNonAscii || value <= 0x7F)
            {
                string escapedString = JsonValueUtils.SpecialCharToEscapedStringMap[value];
                if (escapedString != null)
                {
                    writer.Write(escapedString);
                    return;
                }
            }

            writer.Write(value);
        }
Exemple #18
0
        /// <summary>
        /// Writes an escaped char array to the buffer.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="inputArray">Character array to write.</param>
        /// <param name="inputArrayOffset">How many characters to skip in the input array.</param>
        /// <param name="inputArrayCount">How many characters to write from the input array.</param>
        /// <param name="buffer">Char buffer to use for streaming data.</param>
        /// <param name="bufferIndex">Current position in the buffer after the string has been written.</param>
        /// <param name="stringEscapeOption">The string escape option.</param>
        /// <remarks>
        /// IMPORTANT: After all characters have been written,
        /// caller is responsible for writing the final buffer contents to the writer.
        /// </remarks>
        private static void WriteEscapedCharArrayToBuffer(
            TextWriter writer,
            char[] inputArray,
            ref int inputArrayOffset,
            int inputArrayCount,
            char[] buffer,
            ref int bufferIndex,
            ODataStringEscapeOption stringEscapeOption)
        {
            Debug.Assert(inputArray != null, "inputArray != null");
            Debug.Assert(buffer != null, "buffer != null");

            for (; inputArrayOffset < inputArrayCount; inputArrayOffset++)
            {
                bufferIndex = EscapeAndWriteCharToBuffer(writer, inputArray[inputArrayOffset], buffer, bufferIndex, stringEscapeOption);
            }
        }
Exemple #19
0
        /// <summary>
        /// Write a string value.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="value">String value to be written.</param>
        /// <param name="stringEscapeOption">The string escape option.</param>
        /// <param name="buffer">Char buffer to use for streaming data.</param>
        /// <param name="arrayPool">Array pool for renting a buffer.</param>
        internal static Task WriteValueAsync(
            this TextWriter writer,
            string value,
            ODataStringEscapeOption stringEscapeOption,
            Ref <char[]> buffer,
            ICharArrayPool arrayPool = null)
        {
            Debug.Assert(writer != null, "writer != null");

            if (value == null)
            {
                return(writer.WriteAsync(JsonConstants.JsonNullLiteral));
            }
            else
            {
                return(writer.WriteEscapedJsonStringAsync(value, stringEscapeOption, buffer, arrayPool));
            }
        }
Exemple #20
0
        /// <summary>
        /// Escapes and writes a character array to a writer.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="inputArray">Character array to write.</param>
        /// <param name="inputArrayOffset">How many characters to skip in the input array.</param>
        /// <param name="inputArrayCount">How many characters to write from the input array.</param>
        /// <param name="stringEscapeOption">The string escape option.</param>
        /// <param name="buffer">Char buffer to use for streaming data.</param>
        /// <param name="bufferPool">Character buffer pool.</param>
        internal static void WriteEscapedCharArray(
            TextWriter writer,
            char[] inputArray,
            int inputArrayOffset,
            int inputArrayCount,
            ODataStringEscapeOption stringEscapeOption,
            Ref <char[]> buffer, ICharArrayPool bufferPool)
        {
            int bufferIndex = 0;

            buffer.Value = BufferUtils.InitializeBufferIfRequired(bufferPool, buffer.Value);

            WriteEscapedCharArrayToBuffer(writer, inputArray, ref inputArrayOffset, inputArrayCount, buffer.Value, ref bufferIndex, stringEscapeOption);

            // write remaining bytes in buffer
            if (bufferIndex > 0)
            {
                writer.Write(buffer.Value, 0, bufferIndex);
            }
        }
Exemple #21
0
        /// <summary>
        /// Escapes and writes a character array to a writer.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="inputArray">Character array to write.</param>
        /// <param name="inputArrayOffset">How many characters to skip in the input array.</param>
        /// <param name="inputArrayCount">How many characters to write from the input array.</param>
        /// <param name="stringEscapeOption">The string escape option.</param>
        /// <param name="buffer">Char buffer to use for streaming data.</param>
        /// <param name="bufferPool">Character buffer pool.</param>
        internal static async Task WriteEscapedCharArrayAsync(
            this TextWriter writer,
            char[] inputArray,
            int inputArrayOffset,
            int inputArrayCount,
            ODataStringEscapeOption stringEscapeOption,
            Ref <char[]> buffer,
            ICharArrayPool bufferPool)
        {
            int bufferIndex = 0;

            buffer.Value = BufferUtils.InitializeBufferIfRequired(bufferPool, buffer.Value);

            WriteEscapedCharArrayToBuffer(writer, inputArray, ref inputArrayOffset, inputArrayCount, buffer.Value, ref bufferIndex, stringEscapeOption);

            // write remaining bytes in buffer
            if (bufferIndex > 0)
            {
                await writer.WriteAsync(buffer.Value, 0, bufferIndex).ConfigureAwait(false);
            }
        }
Exemple #22
0
        public async Task WriteEmptyStringShouldWork(ODataStringEscapeOption stringEscapeOption)
        {
            await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, string.Empty, stringEscapeOption, this.buffer);

            Assert.Equal("\"\"", this.StreamToString());
        }
Exemple #23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultJsonWriterFactory" />.
 /// </summary>
 /// <param name="stringEscapeOption">The string escape option.</param>
 public DefaultJsonWriterFactory(ODataStringEscapeOption stringEscapeOption)
 {
     this.stringEscapeOption = stringEscapeOption;
 }
Exemple #24
0
        public async Task WriteNonSpecialCharactersShouldWork(ODataStringEscapeOption stringEscapeOption)
        {
            await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, "abcdefg123", stringEscapeOption, this.buffer);

            Assert.Equal("\"abcdefg123\"", this.StreamToString());
        }
Exemple #25
0
        /// <summary>
        /// Returns the string value with special characters escaped.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="inputString">Input string value.</param>
        /// <param name="stringEscapeOption">The string escape option.</param>
        /// <param name="buffer">Char buffer to use for streaming data</param>
        internal static void WriteEscapedJsonString(TextWriter writer, string inputString,
                                                    ODataStringEscapeOption stringEscapeOption, ref char[] buffer, ICharArrayPool bufferPool = null)
        {
            Debug.Assert(writer != null, "writer != null");
            Debug.Assert(inputString != null, "The string value must not be null.");

            writer.Write(JsonConstants.QuoteCharacter);

            int firstIndex;

            if (!JsonValueUtils.CheckIfStringHasSpecialChars(inputString, stringEscapeOption, out firstIndex))
            {
                writer.Write(inputString);
            }
            else
            {
                int inputStringLength = inputString.Length;

                Debug.Assert(firstIndex < inputStringLength, "First index of the special character should be within the string");
                buffer = BufferUtils.InitializeBufferIfRequired(bufferPool, buffer);
                int bufferLength = buffer.Length;
                int bufferIndex  = 0;
                int currentIndex = 0;

                // Let's copy and flush strings up to the first index of the special char
                while (currentIndex < firstIndex)
                {
                    int subStrLength = firstIndex - currentIndex;

                    Debug.Assert(subStrLength > 0, "SubStrLength should be greater than 0 always");

                    // If the first index of the special character is larger than the buffer length,
                    // flush everything to the buffer first and reset the buffer to the next chunk.
                    // Otherwise copy to the buffer and go on from there.
                    if (subStrLength >= bufferLength)
                    {
                        inputString.CopyTo(currentIndex, buffer, 0, bufferLength);
                        writer.Write(buffer, 0, bufferLength);
                        currentIndex += bufferLength;
                    }
                    else
                    {
                        inputString.CopyTo(currentIndex, buffer, 0, subStrLength);
                        bufferIndex   = subStrLength;
                        currentIndex += subStrLength;
                    }
                }

                for (; currentIndex < inputStringLength; currentIndex++)
                {
                    char   c             = inputString[currentIndex];
                    string escapedString = null;

                    if (stringEscapeOption == ODataStringEscapeOption.EscapeNonAscii || c <= 0x7F)
                    {
                        escapedString = JsonValueUtils.SpecialCharToEscapedStringMap[c];
                    }

                    // Append the unhandled characters (that do not require special treament)
                    // to the buffer.
                    if (escapedString == null)
                    {
                        buffer[bufferIndex] = c;
                        bufferIndex++;
                    }
                    else
                    {
                        // Okay, an unhandled character was deteced.
                        // First lets check if we can fit it in the existing buffer, if not,
                        // flush the current buffer and reset. Add the escaped string to the buffer
                        // and continue.
                        int escapedStringLength = escapedString.Length;
                        Debug.Assert(escapedStringLength <= bufferLength, "Buffer should be larger than the escaped string");

                        if ((bufferIndex + escapedStringLength) > bufferLength)
                        {
                            writer.Write(buffer, 0, bufferIndex);
                            bufferIndex = 0;
                        }

                        escapedString.CopyTo(0, buffer, bufferIndex, escapedStringLength);
                        bufferIndex += escapedStringLength;
                    }

                    if (bufferIndex >= bufferLength)
                    {
                        Debug.Assert(bufferIndex == bufferLength,
                                     "We should never encounter a situation where the buffer index is greater than the buffer length");
                        writer.Write(buffer, 0, bufferIndex);
                        bufferIndex = 0;
                    }
                }

                if (bufferIndex > 0)
                {
                    writer.Write(buffer, 0, bufferIndex);
                }
            }

            writer.Write(JsonConstants.QuoteCharacter);
        }
 public void WriteEmptyStringShouldWork(ODataStringEscapeOption stringEscapeOption)
 {
     this.TestInit();
     JsonValueUtils.WriteEscapedJsonString(this.writer, string.Empty, stringEscapeOption, ref this.buffer);
     this.StreamToString().Should().Be("\"\"");
 }
        /// <summary>
        /// Escapes and writes a character buffer, flushing to the writer as the buffer fills.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="character">The character to write to the buffer.</param>
        /// <param name="buffer">Char buffer to use for streaming data.</param>
        /// <param name="bufferIndex">The index into the buffer in which to write the character.</param>
        /// <param name="stringEscapeOption">The string escape option.</param>
        /// <returns>Current position in the buffer after the character has been written.</returns>
        /// <remarks>
        /// IMPORTANT: After all characters have been written,
        /// caller is responsible for writing the final buffer contents to the writer.
        /// </remarks>
        private static int EscapeAndWriteCharToBuffer(TextWriter writer, char character, char[] buffer, int bufferIndex, ODataStringEscapeOption stringEscapeOption)
        {
            int    bufferLength  = buffer.Length;
            string escapedString = null;

            if (stringEscapeOption == ODataStringEscapeOption.EscapeNonAscii || character <= 0x7F)
            {
                escapedString = JsonValueUtils.SpecialCharToEscapedStringMap[character];
            }

            // Append the unhandled characters (that do not require special treatment)
            // to the buffer.
            if (escapedString == null)
            {
                buffer[bufferIndex] = character;
                bufferIndex++;
            }
            else
            {
                // Okay, an unhandled character was detected.
                // First lets check if we can fit it in the existing buffer, if not,
                // flush the current buffer and reset. Add the escaped string to the buffer
                // and continue.
                int escapedStringLength = escapedString.Length;
                Debug.Assert(escapedStringLength <= bufferLength, "Buffer should be larger than the escaped string");

                if ((bufferIndex + escapedStringLength) > bufferLength)
                {
                    writer.Write(buffer, 0, bufferIndex);
                    bufferIndex = 0;
                }

                escapedString.CopyTo(0, buffer, bufferIndex, escapedStringLength);
                bufferIndex += escapedStringLength;
            }

            if (bufferIndex >= bufferLength)
            {
                Debug.Assert(bufferIndex == bufferLength,
                             "We should never encounter a situation where the buffer index is greater than the buffer length");
                writer.Write(buffer, 0, bufferIndex);
                bufferIndex = 0;
            }

            return(bufferIndex);
        }
        /// <summary>
        /// Escapes and writes a character array to a writer.
        /// </summary>
        /// <param name="writer">The text writer to write the output to.</param>
        /// <param name="inputArray">Character array to write.</param>
        /// <param name="inputArrayOffset">How many characters to skip in the input array.</param>
        /// <param name="inputArrayCount">How many characters to write from the input array.</param>
        /// <param name="stringEscapeOption">The string escape option.</param>
        /// <param name="buffer">Char buffer to use for streaming data.</param>
        /// <param name="bufferPool">Character buffer pool.</param>
        internal static void WriteEscapedCharArray(TextWriter writer, char[] inputArray, int inputArrayOffset, int inputArrayCount, ODataStringEscapeOption stringEscapeOption, ref char[] buffer, ICharArrayPool bufferPool)
        {
            int bufferIndex = 0;

            buffer = BufferUtils.InitializeBufferIfRequired(bufferPool, buffer);

            for (; inputArrayOffset < inputArrayCount; inputArrayOffset++)
            {
                bufferIndex = EscapeAndWriteCharToBuffer(writer, inputArray[inputArrayOffset], buffer, bufferIndex, stringEscapeOption);
            }

            // write remaining bytes in buffer
            if (bufferIndex > 0)
            {
                writer.Write(buffer, 0, bufferIndex);
            }
        }
 public void WriteNonSpecialCharactersShouldWork(ODataStringEscapeOption stringEscapeOption)
 {
     this.TestInit();
     JsonValueUtils.WriteEscapedJsonString(this.writer, "abcdefg123", stringEscapeOption, ref this.buffer);
     this.StreamToString().Should().Be("\"abcdefg123\"");
 }
Exemple #30
0
 public void WriteLowSpecialCharactersShouldWorkForEscapeOption(ODataStringEscapeOption stringEscapeOption)
 {
     this.TestInit();
     JsonValueUtils.WriteEscapedJsonString(this.writer, "cA_\n\r\b", stringEscapeOption, ref this.buffer);
     Assert.Equal("\"cA_\\n\\r\\b\"", this.StreamToString());
 }