Пример #1
0
 public void Clear(IJsonBufferPool<char> bufferPool)
 {
     if (_buffer != null)
     {
         BufferUtils.ReturnBuffer(bufferPool, ref _buffer);
         _buffer = null;
     }
     _position = 0;
 }
Пример #2
0
 public void Clear(IJsonBufferPool <char> bufferPool)
 {
     if (_buffer != null)
     {
         BufferUtils.ReturnBuffer(bufferPool, ref _buffer);
         _buffer = null;
     }
     _position = 0;
 }
Пример #3
0
        public static char[] RentBuffer(IJsonBufferPool<char> bufferPool, int minSize)
        {
            if (bufferPool == null)
            {
                return new char[minSize];
            }

            return bufferPool.RentBuffer(minSize);
        }
Пример #4
0
        public static void ReturnBuffer(IJsonBufferPool<char> bufferPool, ref char[] buffer)
        {
            if (bufferPool == null)
            {
                buffer = null;
                return;
            }

            bufferPool.ReturnBuffer(ref buffer);
        }
        public static char[] RentBuffer(IJsonBufferPool <char> bufferPool, int minSize)
        {
            if (bufferPool == null)
            {
                return(new char[minSize]);
            }

            char[] buffer = bufferPool.RentBuffer(minSize);
            return(buffer);
        }
        public static void ReturnBuffer(IJsonBufferPool <char> bufferPool, ref char[] buffer)
        {
            if (bufferPool == null)
            {
                buffer = null;
                return;
            }

            bufferPool.ReturnBuffer(ref buffer);
        }
Пример #7
0
        public void Append(IJsonBufferPool<char> bufferPool, char value)
        {
            // test if the buffer array is large enough to take the value
            if (_position == _buffer.Length)
            {
                EnsureSize(bufferPool, 1);
            }

            // set value and increment poisition
            _buffer[_position++] = value;
        }
Пример #8
0
        public void Append(IJsonBufferPool <char> bufferPool, char[] buffer, int startIndex, int count)
        {
            if (_position + count >= _buffer.Length)
            {
                EnsureSize(bufferPool, count);
            }

            Array.Copy(buffer, startIndex, _buffer, _position, count);

            _position += count;
        }
Пример #9
0
        public void Append(IJsonBufferPool <char> bufferPool, char value)
        {
            // test if the buffer array is large enough to take the value
            if (_position == _buffer.Length)
            {
                EnsureSize(bufferPool, 1);
            }

            // set value and increment poisition
            _buffer[_position++] = value;
        }
Пример #10
0
        public void Append(IJsonBufferPool<char> bufferPool, char[] buffer, int startIndex, int count)
        {
            if (_position + count >= _buffer.Length)
            {
                EnsureSize(bufferPool, count);
            }

            Array.Copy(buffer, startIndex, _buffer, _position, count);

            _position += count;
        }
Пример #11
0
        private void EnsureSize(IJsonBufferPool <char> bufferPool, int appendLength)
        {
            char[] newBuffer = BufferUtils.RentBuffer(bufferPool, (_position + appendLength) * 2);

            if (_buffer != null)
            {
                Array.Copy(_buffer, newBuffer, _position);
                BufferUtils.ReturnBuffer(bufferPool, ref _buffer);
            }

            _buffer = newBuffer;
        }
        public static void EnsureBufferSize(IJsonBufferPool <char> bufferPool, int size, ref char[] buffer)
        {
            if (bufferPool == null)
            {
                buffer = new char[size];
                return;
            }

            if (buffer != null)
            {
                bufferPool.ReturnBuffer(ref buffer);
            }

            buffer = bufferPool.RentBuffer(size);
        }
Пример #13
0
        public static void EnsureBufferSize(IJsonBufferPool<char> bufferPool, int size, ref char[] buffer)
        {
            if (bufferPool == null)
            {
                buffer = new char[size];
                return;
            }

            if (buffer != null)
            {
                bufferPool.ReturnBuffer(ref buffer);
            }

            buffer = bufferPool.RentBuffer(size);
        }
Пример #14
0
 public StringBuffer(IJsonBufferPool<char> bufferPool, int initalSize) : this(BufferUtils.RentBuffer(bufferPool, initalSize))
 {
 }
Пример #15
0
        private void EnsureSize(IJsonBufferPool<char> bufferPool, int appendLength)
        {
            char[] newBuffer = BufferUtils.RentBuffer(bufferPool, (_position + appendLength) * 2);

            if (_buffer != null)
            {
                Array.Copy(_buffer, newBuffer, _position);
                BufferUtils.ReturnBuffer(bufferPool, ref _buffer);
            }

            _buffer = newBuffer;
        }
Пример #16
0
 public StringBuffer(IJsonBufferPool <char> bufferPool, int initalSize) : this(BufferUtils.RentBuffer(bufferPool, initalSize))
 {
 }
        public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters,
                                                        bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IJsonBufferPool <char> bufferPool, ref char[] writeBuffer)
        {
            // leading delimiter
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }

            if (s != null)
            {
                int lastWritePosition = 0;

                for (int i = 0; i < s.Length; i++)
                {
                    var c = s[i];

                    if (c < charEscapeFlags.Length && !charEscapeFlags[c])
                    {
                        continue;
                    }

                    string escapedValue;

                    switch (c)
                    {
                    case '\t':
                        escapedValue = @"\t";
                        break;

                    case '\n':
                        escapedValue = @"\n";
                        break;

                    case '\r':
                        escapedValue = @"\r";
                        break;

                    case '\f':
                        escapedValue = @"\f";
                        break;

                    case '\b':
                        escapedValue = @"\b";
                        break;

                    case '\\':
                        escapedValue = @"\\";
                        break;

                    case '\u0085':     // Next Line
                        escapedValue = @"\u0085";
                        break;

                    case '\u2028':     // Line Separator
                        escapedValue = @"\u2028";
                        break;

                    case '\u2029':     // Paragraph Separator
                        escapedValue = @"\u2029";
                        break;

                    default:
                        if (c < charEscapeFlags.Length || stringEscapeHandling == StringEscapeHandling.EscapeNonAscii)
                        {
                            if (c == '\'' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
                            {
                                escapedValue = @"\'";
                            }
                            else if (c == '"' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
                            {
                                escapedValue = @"\""";
                            }
                            else
                            {
                                if (writeBuffer == null || writeBuffer.Length < UnicodeTextLength)
                                {
                                    BufferUtils.EnsureBufferSize(bufferPool, UnicodeTextLength, ref writeBuffer);
                                }

                                StringUtils.ToCharAsUnicode(c, writeBuffer);

                                // slightly hacky but it saves multiple conditions in if test
                                escapedValue = EscapedUnicodeText;
                            }
                        }
                        else
                        {
                            escapedValue = null;
                        }
                        break;
                    }

                    if (escapedValue == null)
                    {
                        continue;
                    }

                    bool isEscapedUnicodeText = string.Equals(escapedValue, EscapedUnicodeText);

                    if (i > lastWritePosition)
                    {
                        int length = i - lastWritePosition + ((isEscapedUnicodeText) ? UnicodeTextLength : 0);
                        int start  = (isEscapedUnicodeText) ? UnicodeTextLength : 0;

                        if (writeBuffer == null || writeBuffer.Length < length)
                        {
                            char[] newBuffer = BufferUtils.RentBuffer(bufferPool, length);

                            // the unicode text is already in the buffer
                            // copy it over when creating new buffer
                            if (isEscapedUnicodeText)
                            {
                                Array.Copy(writeBuffer, newBuffer, UnicodeTextLength);
                            }

                            BufferUtils.ReturnBuffer(bufferPool, ref writeBuffer);

                            writeBuffer = newBuffer;
                        }

                        s.CopyTo(lastWritePosition, writeBuffer, start, length - start);

                        // write unchanged chars before writing escaped text
                        writer.Write(writeBuffer, start, length - start);
                    }

                    lastWritePosition = i + 1;
                    if (!isEscapedUnicodeText)
                    {
                        writer.Write(escapedValue);
                    }
                    else
                    {
                        writer.Write(writeBuffer, 0, UnicodeTextLength);
                    }
                }

                if (lastWritePosition == 0)
                {
                    // no escaped text, write entire string
                    writer.Write(s);
                }
                else
                {
                    int length = s.Length - lastWritePosition;

                    if (writeBuffer == null || writeBuffer.Length < length)
                    {
                        writeBuffer = new char[length];
                    }

                    s.CopyTo(lastWritePosition, writeBuffer, 0, length);

                    // write remaining text
                    writer.Write(writeBuffer, 0, length);
                }
            }

            // trailing delimiter
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
        }
Пример #18
0
        public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters,
            bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IJsonBufferPool<char> bufferPool, ref char[] writeBuffer)
        {
            // leading delimiter
            if (appendDelimiters)
                writer.Write(delimiter);

            if (s != null)
            {
                int lastWritePosition = 0;

                for (int i = 0; i < s.Length; i++)
                {
                    var c = s[i];

                    if (c < charEscapeFlags.Length && !charEscapeFlags[c])
                        continue;

                    string escapedValue;

                    switch (c)
                    {
                        case '\t':
                            escapedValue = @"\t";
                            break;
                        case '\n':
                            escapedValue = @"\n";
                            break;
                        case '\r':
                            escapedValue = @"\r";
                            break;
                        case '\f':
                            escapedValue = @"\f";
                            break;
                        case '\b':
                            escapedValue = @"\b";
                            break;
                        case '\\':
                            escapedValue = @"\\";
                            break;
                        case '\u0085': // Next Line
                            escapedValue = @"\u0085";
                            break;
                        case '\u2028': // Line Separator
                            escapedValue = @"\u2028";
                            break;
                        case '\u2029': // Paragraph Separator
                            escapedValue = @"\u2029";
                            break;
                        default:
                            if (c < charEscapeFlags.Length || stringEscapeHandling == StringEscapeHandling.EscapeNonAscii)
                            {
                                if (c == '\'' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
                                {
                                    escapedValue = @"\'";
                                }
                                else if (c == '"' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
                                {
                                    escapedValue = @"\""";
                                }
                                else
                                {
                                    if (writeBuffer == null || writeBuffer.Length < UnicodeTextLength)
                                    {
                                        BufferUtils.EnsureBufferSize(bufferPool, UnicodeTextLength, ref writeBuffer);
                                    }

                                    StringUtils.ToCharAsUnicode(c, writeBuffer);

                                    // slightly hacky but it saves multiple conditions in if test
                                    escapedValue = EscapedUnicodeText;
                                }
                            }
                            else
                            {
                                escapedValue = null;
                            }
                            break;
                    }

                    if (escapedValue == null)
                        continue;

                    bool isEscapedUnicodeText = string.Equals(escapedValue, EscapedUnicodeText);

                    if (i > lastWritePosition)
                    {
                        int length = i - lastWritePosition + ((isEscapedUnicodeText) ? UnicodeTextLength : 0);
                        int start = (isEscapedUnicodeText) ? UnicodeTextLength : 0;

                        if (writeBuffer == null || writeBuffer.Length < length)
                        {
                            char[] newBuffer = BufferUtils.RentBuffer(bufferPool, length);

                            // the unicode text is already in the buffer
                            // copy it over when creating new buffer
                            if (isEscapedUnicodeText)
                                Array.Copy(writeBuffer, newBuffer, UnicodeTextLength);

                            BufferUtils.ReturnBuffer(bufferPool, ref writeBuffer);

                            writeBuffer = newBuffer;
                        }

                        s.CopyTo(lastWritePosition, writeBuffer, start, length - start);

                        // write unchanged chars before writing escaped text
                        writer.Write(writeBuffer, start, length - start);
                    }

                    lastWritePosition = i + 1;
                    if (!isEscapedUnicodeText)
                        writer.Write(escapedValue);
                    else
                        writer.Write(writeBuffer, 0, UnicodeTextLength);
                }

                if (lastWritePosition == 0)
                {
                    // no escaped text, write entire string
                    writer.Write(s);
                }
                else
                {
                    int length = s.Length - lastWritePosition;

                    if (writeBuffer == null || writeBuffer.Length < length)
                        writeBuffer = new char[length];

                    s.CopyTo(lastWritePosition, writeBuffer, 0, length);

                    // write remaining text
                    writer.Write(writeBuffer, 0, length);
                }
            }

            // trailing delimiter
            if (appendDelimiters)
                writer.Write(delimiter);
        }