/// <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 #2
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);
        }