示例#1
0
        public static void WriteEscapedJavaScriptString(TextWriter writer, string value, char delimiter, bool appendDelimiters)
        {
            // leading delimiter
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }

            if (value != null)
            {
                int    lastWritePosition = 0;
                int    skipped           = 0;
                char[] chars             = null;

                for (int i = 0; i < value.Length; i++)
                {
                    char   c = value[i];
                    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;

                    case '\'':
                        // only escape if this charater is being used as the delimiter
                        escapedValue = (delimiter == '\'') ? @"\'" : null;
                        break;

                    case '"':
                        // only escape if this charater is being used as the delimiter
                        escapedValue = (delimiter == '"') ? "\\\"" : null;
                        break;

                    default:
                        escapedValue = (c <= '\u001f') ? StringUtils.ToCharAsUnicode(c) : null;
                        break;
                    }

                    if (escapedValue != null)
                    {
                        if (chars == null)
                        {
                            chars = value.ToCharArray();
                        }

                        // write skipped text
                        if (skipped > 0)
                        {
                            writer.Write(chars, lastWritePosition, skipped);
                            skipped = 0;
                        }

                        // write escaped value and note position
                        writer.Write(escapedValue);
                        lastWritePosition = i + 1;
                    }
                    else
                    {
                        skipped++;
                    }
                }

                // write any remaining skipped text
                if (skipped > 0)
                {
                    if (lastWritePosition == 0)
                    {
                        writer.Write(value);
                    }
                    else
                    {
                        writer.Write(chars, lastWritePosition, skipped);
                    }
                }
            }

            // trailing delimiter
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
        }
        public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling)
        {
            // leading delimiter
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }

            if (s != null)
            {
                char[] chars             = null;
                char[] unicodeBuffer     = 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 (unicodeBuffer == null)
                                {
                                    unicodeBuffer = new char[6];
                                }

                                StringUtils.ToCharAsUnicode(c, unicodeBuffer);

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

                    if (escapedValue == null)
                    {
                        continue;
                    }

                    if (i > lastWritePosition)
                    {
                        if (chars == null)
                        {
                            chars = s.ToCharArray();
                        }

                        // write unchanged chars before writing escaped text
                        writer.Write(chars, lastWritePosition, i - lastWritePosition);
                    }

                    lastWritePosition = i + 1;
                    if (!string.Equals(escapedValue, EscapedUnicodeText))
                    {
                        writer.Write(escapedValue);
                    }
                    else
                    {
                        writer.Write(unicodeBuffer);
                    }
                }

                if (lastWritePosition == 0)
                {
                    // no escaped text, write entire string
                    writer.Write(s);
                }
                else
                {
                    if (chars == null)
                    {
                        chars = s.ToCharArray();
                    }

                    // write remaining text
                    writer.Write(chars, lastWritePosition, s.Length - lastWritePosition);
                }
            }

            // trailing delimiter
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
        }
        public static void WriteEscapedJavaScriptString(
            TextWriter writer,
            string s,
            char delimiter,
            bool appendDelimiters,
            bool[] charEscapeFlags,
            StringEscapeHandling stringEscapeHandling,
            IArrayPool <char> bufferPool,
            ref char[] writeBuffer)
        {
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
            if (s != null)
            {
                int sourceIndex = 0;
                for (int index = 0; index < s.Length; ++index)
                {
                    char c = s[index];
                    if ((int)c >= charEscapeFlags.Length || charEscapeFlags[(int)c])
                    {
                        string a;
                        switch (c)
                        {
                        case '\b':
                            a = "\\b";
                            break;

                        case '\t':
                            a = "\\t";
                            break;

                        case '\n':
                            a = "\\n";
                            break;

                        case '\f':
                            a = "\\f";
                            break;

                        case '\r':
                            a = "\\r";
                            break;

                        case '\\':
                            a = "\\\\";
                            break;

                        case '\x0085':
                            a = "\\u0085";
                            break;

                        case '\x2028':
                            a = "\\u2028";
                            break;

                        case '\x2029':
                            a = "\\u2029";
                            break;

                        default:
                            if ((int)c < charEscapeFlags.Length || stringEscapeHandling == StringEscapeHandling.EscapeNonAscii)
                            {
                                if (c == '\'' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
                                {
                                    a = "\\'";
                                    break;
                                }
                                if (c == '"' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
                                {
                                    a = "\\\"";
                                    break;
                                }
                                if (writeBuffer == null || writeBuffer.Length < 6)
                                {
                                    writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, 6, writeBuffer);
                                }
                                StringUtils.ToCharAsUnicode(c, writeBuffer);
                                a = "!";
                                break;
                            }
                            a = (string)null;
                            break;
                        }
                        if (a != null)
                        {
                            bool flag = string.Equals(a, "!");
                            if (index > sourceIndex)
                            {
                                int minSize = index - sourceIndex + (flag ? 6 : 0);
                                int num     = flag ? 6 : 0;
                                if (writeBuffer == null || writeBuffer.Length < minSize)
                                {
                                    char[] chArray = BufferUtils.RentBuffer(bufferPool, minSize);
                                    if (flag)
                                    {
                                        Array.Copy((Array)writeBuffer, (Array)chArray, 6);
                                    }
                                    BufferUtils.ReturnBuffer(bufferPool, writeBuffer);
                                    writeBuffer = chArray;
                                }
                                s.CopyTo(sourceIndex, writeBuffer, num, minSize - num);
                                writer.Write(writeBuffer, num, minSize - num);
                            }
                            sourceIndex = index + 1;
                            if (!flag)
                            {
                                writer.Write(a);
                            }
                            else
                            {
                                writer.Write(writeBuffer, 0, 6);
                            }
                        }
                    }
                }
                if (sourceIndex == 0)
                {
                    writer.Write(s);
                }
                else
                {
                    int num = s.Length - sourceIndex;
                    if (writeBuffer == null || writeBuffer.Length < num)
                    {
                        writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, num, writeBuffer);
                    }
                    s.CopyTo(sourceIndex, writeBuffer, 0, num);
                    writer.Write(writeBuffer, 0, num);
                }
            }
            if (!appendDelimiters)
            {
                return;
            }
            writer.Write(delimiter);
        }
        public static void WriteEscapedJavaScriptString(TextWriter writer, string value, char delimiter, bool appendDelimiters)
        {
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
            if (value != null)
            {
                int    num   = 0;
                int    num2  = 0;
                char[] array = null;
                for (int i = 0; i < value.Length; i++)
                {
                    char   c = value[i];
                    string text;
                    switch (c)
                    {
                    default:
                        text = ((c == '\u2028') ? "\\u2028" : ((c == '\u2029') ? "\\u2029" : ((c == '"') ? ((delimiter != '"') ? null : "\\\"") : ((c == '\'') ? ((delimiter != '\'') ? null : "\\'") : ((c == '\\') ? "\\\\" : ((c != '\u0085') ? ((c > '\u001f') ? null : StringUtils.ToCharAsUnicode(c)) : "\\u0085"))))));
                        break;

                    case '\t':
                        text = "\\t";
                        break;

                    case '\n':
                        text = "\\n";
                        break;

                    case '\r':
                        text = "\\r";
                        break;

                    case '\f':
                        text = "\\f";
                        break;

                    case '\b':
                        text = "\\b";
                        break;
                    }
                    if (text != null)
                    {
                        if (array == null)
                        {
                            array = value.ToCharArray();
                        }
                        if (num2 > 0)
                        {
                            writer.Write(array, num, num2);
                            num2 = 0;
                        }
                        writer.Write(text);
                        num = i + 1;
                    }
                    else
                    {
                        num2++;
                    }
                }
                if (num2 > 0)
                {
                    if (num == 0)
                    {
                        writer.Write(value);
                    }
                    else
                    {
                        writer.Write(array, num, num2);
                    }
                }
            }
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
        }
示例#5
0
        public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters)
        {
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
            if (s != null)
            {
                char[] buffer1 = (char[])null;
                char[] buffer2 = (char[])null;
                int    index1  = 0;
                for (int index2 = 0; index2 < s.Length; ++index2)
                {
                    char c = s[index2];
                    if ((int)c < 32 || (int)c >= 128 || ((int)c == 92 || (int)c == (int)delimiter))
                    {
                        string a;
                        switch (c)
                        {
                        case '\\':
                            a = "\\\\";
                            break;

                        case '\x0085':
                            a = "\\u0085";
                            break;

                        case '\x2028':
                            a = "\\u2028";
                            break;

                        case '\x2029':
                            a = "\\u2029";
                            break;

                        case '\b':
                            a = "\\b";
                            break;

                        case '\t':
                            a = "\\t";
                            break;

                        case '\n':
                            a = "\\n";
                            break;

                        case '\f':
                            a = "\\f";
                            break;

                        case '\r':
                            a = "\\r";
                            break;

                        case '"':
                            a = "\\\"";
                            break;

                        case '\'':
                            a = "\\'";
                            break;

                        default:
                            if ((int)c <= 31)
                            {
                                if (buffer2 == null)
                                {
                                    buffer2 = new char[6];
                                }
                                StringUtils.ToCharAsUnicode(c, buffer2);
                                a = "!";
                                break;
                            }
                            else
                            {
                                a = (string)null;
                                break;
                            }
                        }
                        if (a != null)
                        {
                            if (index2 > index1)
                            {
                                if (buffer1 == null)
                                {
                                    buffer1 = s.ToCharArray();
                                }
                                writer.Write(buffer1, index1, index2 - index1);
                            }
                            index1 = index2 + 1;
                            if (!string.Equals(a, "!"))
                            {
                                writer.Write(a);
                            }
                            else
                            {
                                writer.Write(buffer2);
                            }
                        }
                    }
                }
                if (index1 == 0)
                {
                    writer.Write(s);
                }
                else
                {
                    if (buffer1 == null)
                    {
                        buffer1 = s.ToCharArray();
                    }
                    writer.Write(buffer1, index1, s.Length - index1);
                }
            }
            if (!appendDelimiters)
            {
                return;
            }
            writer.Write(delimiter);
        }
示例#6
0
        public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, ref char[] writeBuffer)
        {
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
            if (s != null)
            {
                int num = 0;
                for (int i = 0; i < s.Length; i++)
                {
                    char c = s[i];
                    if ((int)c >= charEscapeFlags.Length || charEscapeFlags[(int)c])
                    {
                        char   c2 = c;
                        string text;
                        if (c2 <= '\\')
                        {
                            switch (c2)
                            {
                            case '\b':
                                text = "\\b";
                                break;

                            case '\t':
                                text = "\\t";
                                break;

                            case '\n':
                                text = "\\n";
                                break;

                            case '\v':
                                goto IL_FE;

                            case '\f':
                                text = "\\f";
                                break;

                            case '\r':
                                text = "\\r";
                                break;

                            default:
                                if (c2 != '\\')
                                {
                                    goto IL_FE;
                                }
                                text = "\\\\";
                                break;
                            }
                        }
                        else if (c2 != '\u0085')
                        {
                            switch (c2)
                            {
                            case '\u2028':
                                text = "\\u2028";
                                break;

                            case '\u2029':
                                text = "\\u2029";
                                break;

                            default:
                                goto IL_FE;
                            }
                        }
                        else
                        {
                            text = "\\u0085";
                        }
IL_171:
                        if (text == null)
                        {
                            goto IL_229;
                        }
                        bool flag = string.Equals(text, "!");
                        if (i > num)
                        {
                            int num2 = i - num + (flag ? 6 : 0);
                            int num3 = flag ? 6 : 0;
                            if (writeBuffer == null || writeBuffer.Length < num2)
                            {
                                char[] array = new char[num2];
                                if (flag)
                                {
                                    Array.Copy(writeBuffer, array, 6);
                                }
                                writeBuffer = array;
                            }
                            s.CopyTo(num, writeBuffer, num3, num2 - num3);
                            writer.Write(writeBuffer, num3, num2 - num3);
                        }
                        num = i + 1;
                        if (!flag)
                        {
                            writer.Write(text);
                            goto IL_229;
                        }
                        writer.Write(writeBuffer, 0, 6);
                        goto IL_229;
IL_FE:
                        if ((int)c >= charEscapeFlags.Length && stringEscapeHandling != StringEscapeHandling.EscapeNonAscii)
                        {
                            text = null;
                            goto IL_171;
                        }
                        if (c == '\'' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
                        {
                            text = "\\'";
                            goto IL_171;
                        }
                        if (c == '"' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
                        {
                            text = "\\\"";
                            goto IL_171;
                        }
                        if (writeBuffer == null)
                        {
                            writeBuffer = new char[6];
                        }
                        StringUtils.ToCharAsUnicode(c, writeBuffer);
                        text = "!";
                        goto IL_171;
                    }
                    IL_229 :;
                }
                if (num == 0)
                {
                    writer.Write(s);
                }
                else
                {
                    int num4 = s.Length - num;
                    if (writeBuffer == null || writeBuffer.Length < num4)
                    {
                        writeBuffer = new char[num4];
                    }
                    s.CopyTo(num, writeBuffer, 0, num4);
                    writer.Write(writeBuffer, 0, num4);
                }
            }
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
        }
示例#7
0
        public static void WriteEscapedJavaScriptString([Nullable(1)] TextWriter writer, string s, char delimiter, bool appendDelimiters, [Nullable(1)] bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IArrayPool <char> bufferPool, ref char[] writeBuffer)
        {
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
            if (!StringUtils.IsNullOrEmpty(s))
            {
                int num = JavaScriptUtils.FirstCharToEscape(s, charEscapeFlags, stringEscapeHandling);
                if (num == -1)
                {
                    writer.Write(s);
                }
                else
                {
                    if (num != 0)
                    {
                        if (writeBuffer == null || writeBuffer.Length < num)
                        {
                            writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, num, writeBuffer);
                        }
                        s.CopyTo(0, writeBuffer, 0, num);
                        writer.Write(writeBuffer, 0, num);
                    }
                    int num2;
                    for (int i = num; i < s.Length; i++)
                    {
                        char c = s[i];
                        if ((int)c >= charEscapeFlags.Length || charEscapeFlags[(int)c])
                        {
                            string text;
                            if (c <= '\\')
                            {
                                switch (c)
                                {
                                case '\b':
                                    text = "\\b";
                                    break;

                                case '\t':
                                    text = "\\t";
                                    break;

                                case '\n':
                                    text = "\\n";
                                    break;

                                case '\v':
                                    goto IL_154;

                                case '\f':
                                    text = "\\f";
                                    break;

                                case '\r':
                                    text = "\\r";
                                    break;

                                default:
                                    if (c != '\\')
                                    {
                                        goto IL_154;
                                    }
                                    text = "\\\\";
                                    break;
                                }
                            }
                            else if (c != '\u0085')
                            {
                                if (c != '\u2028')
                                {
                                    if (c != '\u2029')
                                    {
                                        goto IL_154;
                                    }
                                    text = "\\u2029";
                                }
                                else
                                {
                                    text = "\\u2028";
                                }
                            }
                            else
                            {
                                text = "\\u0085";
                            }
IL_1D3:
                            if (text == null)
                            {
                                goto IL_295;
                            }
                            bool flag = string.Equals(text, "!", StringComparison.Ordinal);
                            if (i > num)
                            {
                                num2 = i - num + (flag ? 6 : 0);
                                int num3 = flag ? 6 : 0;
                                if (writeBuffer == null || writeBuffer.Length < num2)
                                {
                                    char[] array = BufferUtils.RentBuffer(bufferPool, num2);
                                    if (flag)
                                    {
                                        Array.Copy(writeBuffer, array, 6);
                                    }
                                    BufferUtils.ReturnBuffer(bufferPool, writeBuffer);
                                    writeBuffer = array;
                                }
                                s.CopyTo(num, writeBuffer, num3, num2 - num3);
                                writer.Write(writeBuffer, num3, num2 - num3);
                            }
                            num = i + 1;
                            if (!flag)
                            {
                                writer.Write(text);
                                goto IL_295;
                            }
                            writer.Write(writeBuffer, 0, 6);
                            goto IL_295;
IL_154:
                            if ((int)c >= charEscapeFlags.Length && stringEscapeHandling != StringEscapeHandling.EscapeNonAscii)
                            {
                                text = null;
                                goto IL_1D3;
                            }
                            if (c == '\'' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
                            {
                                text = "\\'";
                                goto IL_1D3;
                            }
                            if (c == '"' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
                            {
                                text = "\\\"";
                                goto IL_1D3;
                            }
                            if (writeBuffer == null || writeBuffer.Length < 6)
                            {
                                writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, 6, writeBuffer);
                            }
                            StringUtils.ToCharAsUnicode(c, writeBuffer);
                            text = "!";
                            goto IL_1D3;
                        }
                        IL_295 :;
                    }
                    num2 = s.Length - num;
                    if (num2 > 0)
                    {
                        if (writeBuffer == null || writeBuffer.Length < num2)
                        {
                            writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, num2, writeBuffer);
                        }
                        s.CopyTo(num, writeBuffer, 0, num2);
                        writer.Write(writeBuffer, 0, num2);
                    }
                }
            }
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
        }
示例#8
0
        public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters,
                                                        bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IArrayPool <char> bufferPool, ref char[] writeBuffer)
        {
            // leading delimiter
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }

            if (!StringUtils.IsNullOrEmpty(s))
            {
                int lastWritePosition = FirstCharToEscape(s, charEscapeFlags, stringEscapeHandling);
                if (lastWritePosition == -1)
                {
                    writer.Write(s);
                }
                else
                {
                    if (lastWritePosition != 0)
                    {
                        if (writeBuffer == null || writeBuffer.Length < lastWritePosition)
                        {
                            writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, lastWritePosition, writeBuffer);
                        }

                        // write unchanged chars at start of text.
                        s.CopyTo(0, writeBuffer, 0, lastWritePosition);
                        writer.Write(writeBuffer, 0, lastWritePosition);
                    }

                    int length;
                    for (int i = lastWritePosition; i < s.Length; i++)
                    {
                        char 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)
                                    {
                                        writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, UnicodeTextLength, 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, StringComparison.Ordinal);

                        if (i > lastWritePosition)
                        {
                            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)
                                {
                                    MiscellaneousUtils.Assert(writeBuffer != null, "Write buffer should never be null because it is set when the escaped unicode text is encountered.");

                                    Array.Copy(writeBuffer, newBuffer, UnicodeTextLength);
                                }

                                BufferUtils.ReturnBuffer(bufferPool, 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);
                        }
                    }

                    MiscellaneousUtils.Assert(lastWritePosition != 0);
                    length = s.Length - lastWritePosition;
                    if (length > 0)
                    {
                        if (writeBuffer == null || writeBuffer.Length < length)
                        {
                            writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, length, writeBuffer);
                        }

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

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

            // trailing delimiter
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
        }
        public static void WriteEscapedJavaScriptString(TextWriter writer, string value, char delimiter, bool appendDelimiters)
        {
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
            if (value != null)
            {
                int    num   = 0;
                int    num2  = 0;
                char[] array = null;
                for (int i = 0; i < value.get_Length(); i++)
                {
                    char   c  = value.get_Chars(i);
                    char   c2 = c;
                    string text;
                    switch (c2)
                    {
                    case '\b':
                        text = "\\b";
                        goto IL_14F;

                    case '\t':
                        text = "\\t";
                        goto IL_14F;

                    case '\n':
                        text = "\\n";
                        goto IL_14F;

                    case '\v':
IL_4E:
                        if (c2 == '\u2028')
                        {
                            text = "\\u2028";
                            goto IL_14F;
                        }
                        if (c2 == '\u2029')
                        {
                            text = "\\u2029";
                            goto IL_14F;
                        }
                        if (c2 == '"')
                        {
                            text = ((delimiter != '"') ? null : "\\\"");
                            goto IL_14F;
                        }
                        if (c2 == '\'')
                        {
                            text = ((delimiter != '\'') ? null : "\\'");
                            goto IL_14F;
                        }
                        if (c2 == '\\')
                        {
                            text = "\\\\";
                            goto IL_14F;
                        }
                        if (c2 != '\u0085')
                        {
                            text = ((c > '\u001f') ? null : StringUtils.ToCharAsUnicode(c));
                            goto IL_14F;
                        }
                        text = "\\u0085";
                        goto IL_14F;

                    case '\f':
                        text = "\\f";
                        goto IL_14F;

                    case '\r':
                        text = "\\r";
                        goto IL_14F;
                    }
                    goto IL_4E;
IL_14F:
                    if (text != null)
                    {
                        if (array == null)
                        {
                            array = value.ToCharArray();
                        }
                        if (num2 > 0)
                        {
                            writer.Write(array, num, num2);
                            num2 = 0;
                        }
                        writer.Write(text);
                        num = i + 1;
                    }
                    else
                    {
                        num2++;
                    }
                }
                if (num2 > 0)
                {
                    if (num == 0)
                    {
                        writer.Write(value);
                    }
                    else
                    {
                        writer.Write(array, num, num2);
                    }
                }
            }
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
        }
示例#10
0
        public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters)
        {
            // leading delimiter
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }

            if (s != null)
            {
                char[] chars             = null;
                char[] unicodeBuffer     = null;
                int    lastWritePosition = 0;

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

                    // don't escape standard text/numbers except '\' and the text delimiter
                    if (c >= ' ' && c < 128 && c != '\\' && c != delimiter)
                    {
                        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;

                    case '\'':
                        // this charater is being used as the delimiter
                        escapedValue = @"\'";
                        break;

                    case '"':
                        // this charater is being used as the delimiter
                        escapedValue = "\\\"";
                        break;

                    default:
                        if (c <= '\u001f')
                        {
                            if (unicodeBuffer == null)
                            {
                                unicodeBuffer = new char[6];
                            }

                            StringUtils.ToCharAsUnicode(c, unicodeBuffer);

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

                    if (escapedValue == null)
                    {
                        continue;
                    }

                    if (i > lastWritePosition)
                    {
                        if (chars == null)
                        {
                            chars = s.ToCharArray();
                        }

                        // write unchanged chars before writing escaped text
                        writer.Write(chars, lastWritePosition, i - lastWritePosition);
                    }

                    lastWritePosition = i + 1;
                    if (!string.Equals(escapedValue, EscapedUnicodeText))
                    {
                        writer.Write(escapedValue);
                    }
                    else
                    {
                        writer.Write(unicodeBuffer);
                    }
                }

                if (lastWritePosition == 0)
                {
                    // no escaped text, write entire string
                    writer.Write(s);
                }
                else
                {
                    if (chars == null)
                    {
                        chars = s.ToCharArray();
                    }

                    // write remaining text
                    writer.Write(chars, lastWritePosition, s.Length - lastWritePosition);
                }
            }

            // trailing delimiter
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
        }
示例#11
0
        public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IArrayPool <char> bufferPool, ref char[] writeBuffer)
        {
            int    length;
            char   chr;
            string str;

            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
            if (!string.IsNullOrEmpty(s))
            {
                int escape = JavaScriptUtils.FirstCharToEscape(s, charEscapeFlags, stringEscapeHandling);
                if (escape != -1)
                {
                    if (escape != 0)
                    {
                        if (writeBuffer == null || (int)writeBuffer.Length < escape)
                        {
                            writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, escape, writeBuffer);
                        }
                        s.CopyTo(0, writeBuffer, 0, escape);
                        writer.Write(writeBuffer, 0, escape);
                    }
                    for (int i = escape; i < s.Length; i++)
                    {
                        chr = s[i];
                        if (chr >= (char)((int)charEscapeFlags.Length) || charEscapeFlags[chr])
                        {
                            if (chr <= '\\')
                            {
                                switch (chr)
                                {
                                case '\b':
                                {
                                    str = "\\b";
                                    break;
                                }

                                case '\t':
                                {
                                    str = "\\t";
                                    break;
                                }

                                case '\n':
                                {
                                    str = "\\n";
                                    break;
                                }

                                case '\v':
                                {
                                    goto Label0;
                                }

                                case '\f':
                                {
                                    str = "\\f";
                                    break;
                                }

                                case '\r':
                                {
                                    str = "\\r";
                                    break;
                                }

                                default:
                                {
                                    if (chr == '\\')
                                    {
                                        str = "\\\\";
                                        break;
                                    }
                                    else
                                    {
                                        goto Label0;
                                    }
                                }
                                }
                            }
                            else if (chr == '\u0085')
                            {
                                str = "\\u0085";
                            }
                            else if (chr == '\u2028')
                            {
                                str = "\\u2028";
                            }
                            else
                            {
                                if (chr != '\u2029')
                                {
                                    goto Label0;
                                }
                                str = "\\u2029";
                            }
Label1:
                            if (str != null)
                            {
                                bool flag = string.Equals(str, "!");
                                if (i > escape)
                                {
                                    length = i - escape + (flag ? 6 : 0);
                                    int num = (flag ? 6 : 0);
                                    if (writeBuffer == null || (int)writeBuffer.Length < length)
                                    {
                                        char[] chrArray = BufferUtils.RentBuffer(bufferPool, length);
                                        if (flag)
                                        {
                                            Array.Copy(writeBuffer, chrArray, 6);
                                        }
                                        BufferUtils.ReturnBuffer(bufferPool, writeBuffer);
                                        writeBuffer = chrArray;
                                    }
                                    s.CopyTo(escape, writeBuffer, num, length - num);
                                    writer.Write(writeBuffer, num, length - num);
                                }
                                escape = i + 1;
                                if (flag)
                                {
                                    writer.Write(writeBuffer, 0, 6);
                                }
                                else
                                {
                                    writer.Write(str);
                                }
                            }
                        }
                    }
                    length = s.Length - escape;
                    if (length > 0)
                    {
                        if (writeBuffer == null || (int)writeBuffer.Length < length)
                        {
                            writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, length, writeBuffer);
                        }
                        s.CopyTo(escape, writeBuffer, 0, length);
                        writer.Write(writeBuffer, 0, length);
                    }
                }
                else
                {
                    writer.Write(s);
                }
            }
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
            return;

Label0:
            if (chr >= (char)((int)charEscapeFlags.Length))
            {
                if (stringEscapeHandling == StringEscapeHandling.EscapeNonAscii)
                {
                    goto Label3;
                }
                str = null;
                goto Label1;
            }
            if (chr == '\'' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
            {
                str = "\\'";
                goto Label1;
            }
            else if (chr != '\"' || stringEscapeHandling == StringEscapeHandling.EscapeHtml)
            {
                if (writeBuffer == null || (int)writeBuffer.Length < 6)
                {
                    writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, 6, writeBuffer);
                }
                StringUtils.ToCharAsUnicode(chr, writeBuffer);
                str = "!";
                goto Label1;
            }
            else
            {
                str = "\\\"";
                goto Label1;
            }
        }
示例#12
0
        private static async Task WriteDefinitelyEscapedJavaScriptStringWithoutDelimitersAsync(TextWriter writer, string s, int lastWritePosition, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, JsonTextWriter client, char[] writeBuffer, CancellationToken cancellationToken)
        {
            int length;
            ConfiguredTaskAwaitable configuredTaskAwaitable;
            char chr;

            if (writeBuffer == null || (int)writeBuffer.Length < lastWritePosition)
            {
                writeBuffer = client.EnsureWriteBuffer(lastWritePosition, 6);
            }
            if (lastWritePosition != 0)
            {
                s.CopyTo(0, writeBuffer, 0, lastWritePosition);
                configuredTaskAwaitable = writer.WriteAsync(writeBuffer, 0, lastWritePosition, cancellationToken).ConfigureAwait(false);
                await configuredTaskAwaitable;
            }
            bool   flag = false;
            string str  = null;

            for (int i = lastWritePosition; i < s.Length; i++)
            {
                chr = s[i];
                if (chr >= (char)((int)charEscapeFlags.Length) || charEscapeFlags[chr])
                {
                    if (chr <= '\\')
                    {
                        switch (chr)
                        {
                        case '\b':
                        {
                            str = "\\b";
                            break;
                        }

                        case '\t':
                        {
                            str = "\\t";
                            break;
                        }

                        case '\n':
                        {
                            str = "\\n";
                            break;
                        }

                        case '\v':
                        {
                            goto Label0;
                        }

                        case '\f':
                        {
                            str = "\\f";
                            break;
                        }

                        case '\r':
                        {
                            str = "\\r";
                            break;
                        }

                        default:
                        {
                            if (chr == '\\')
                            {
                                str = "\\\\";
                                break;
                            }
                            else
                            {
                                goto Label0;
                            }
                        }
                        }
                    }
                    else if (chr == '\u0085')
                    {
                        str = "\\u0085";
                    }
                    else if (chr == '\u2028')
                    {
                        str = "\\u2028";
                    }
                    else
                    {
                        if (chr != '\u2029')
                        {
                            goto Label0;
                        }
                        str = "\\u2029";
                    }
Label2:
                    if (i > lastWritePosition)
                    {
                        int num = i - lastWritePosition;
                        length = num + (flag ? 6 : 0);
                        int num1 = (flag ? 6 : 0);
                        if ((int)writeBuffer.Length < length)
                        {
                            writeBuffer = client.EnsureWriteBuffer(length, 6);
                        }
                        s.CopyTo(lastWritePosition, writeBuffer, num1, length - num1);
                        configuredTaskAwaitable = writer.WriteAsync(writeBuffer, num1, length - num1, cancellationToken).ConfigureAwait(false);
                        await configuredTaskAwaitable;
                    }
                    lastWritePosition = i + 1;
                    if (flag)
                    {
                        configuredTaskAwaitable = writer.WriteAsync(writeBuffer, 0, 6, cancellationToken).ConfigureAwait(false);
                        await configuredTaskAwaitable;
                        flag = false;
                    }
                    else
                    {
                        configuredTaskAwaitable = writer.WriteAsync(str, cancellationToken).ConfigureAwait(false);
                        await configuredTaskAwaitable;
                    }
                }
Label1:
            }
            length = s.Length - lastWritePosition;
            if (length != 0)
            {
                if ((int)writeBuffer.Length < length)
                {
                    writeBuffer = client.EnsureWriteBuffer(length, 0);
                }
                s.CopyTo(lastWritePosition, writeBuffer, 0, length);
                configuredTaskAwaitable = writer.WriteAsync(writeBuffer, 0, length, cancellationToken).ConfigureAwait(false);
                await configuredTaskAwaitable;
            }
            return;

Label0:
            if (chr >= (char)((int)charEscapeFlags.Length) && stringEscapeHandling != StringEscapeHandling.EscapeNonAscii)
            {
                goto Label1;
            }
            if (chr == '\'' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
            {
                str = "\\'";
                goto Label2;
            }
            else if (chr != '\"' || stringEscapeHandling == StringEscapeHandling.EscapeHtml)
            {
                if ((int)writeBuffer.Length < 6)
                {
                    writeBuffer = client.EnsureWriteBuffer(6, 0);
                }
                StringUtils.ToCharAsUnicode(chr, writeBuffer);
                flag = true;
                goto Label2;
            }
            else
            {
                str = "\\\"";
                goto Label2;
            }
        }
示例#13
0
        public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IArrayPool <char> bufferPool, ref char[] writeBuffer)
        {
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
            if (s != null)
            {
                int sourceIndex = 0;
                for (int i = 0; i < s.Length; i++)
                {
                    char index = s[i];
                    if ((index >= charEscapeFlags.Length) || charEscapeFlags[index])
                    {
                        string str;
                        switch (index)
                        {
                        case '\x0085':
                            str = @"\u0085";
                            break;

                        case '\u2028':
                            str = @"\u2028";
                            break;

                        case '\u2029':
                            str = @"\u2029";
                            break;

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

                        case '\t':
                            str = @"\t";
                            break;

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

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

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

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

                        default:
                            if ((index < charEscapeFlags.Length) || (stringEscapeHandling == StringEscapeHandling.EscapeNonAscii))
                            {
                                if ((index == '\'') && (stringEscapeHandling != StringEscapeHandling.EscapeHtml))
                                {
                                    str = @"\'";
                                }
                                else if ((index == '"') && (stringEscapeHandling != StringEscapeHandling.EscapeHtml))
                                {
                                    str = "\\\"";
                                }
                                else
                                {
                                    if ((writeBuffer == null) || (writeBuffer.Length < 6))
                                    {
                                        writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, 6, writeBuffer);
                                    }
                                    StringUtils.ToCharAsUnicode(index, writeBuffer);
                                    str = "!";
                                }
                            }
                            else
                            {
                                str = null;
                            }
                            break;
                        }
                        if (str != null)
                        {
                            bool flag = string.Equals(str, "!");
                            if (i > sourceIndex)
                            {
                                int minSize          = (i - sourceIndex) + (flag ? 6 : 0);
                                int destinationIndex = flag ? 6 : 0;
                                if ((writeBuffer == null) || (writeBuffer.Length < minSize))
                                {
                                    char[] destinationArray = BufferUtils.RentBuffer(bufferPool, minSize);
                                    if (flag)
                                    {
                                        Array.Copy(writeBuffer, destinationArray, 6);
                                    }
                                    BufferUtils.ReturnBuffer(bufferPool, writeBuffer);
                                    writeBuffer = destinationArray;
                                }
                                s.CopyTo(sourceIndex, writeBuffer, destinationIndex, minSize - destinationIndex);
                                writer.Write(writeBuffer, destinationIndex, minSize - destinationIndex);
                            }
                            sourceIndex = i + 1;
                            if (!flag)
                            {
                                writer.Write(str);
                            }
                            else
                            {
                                writer.Write(writeBuffer, 0, 6);
                            }
                        }
                    }
                }
                if (sourceIndex == 0)
                {
                    writer.Write(s);
                }
                else
                {
                    int size = s.Length - sourceIndex;
                    if ((writeBuffer == null) || (writeBuffer.Length < size))
                    {
                        writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, size, writeBuffer);
                    }
                    s.CopyTo(sourceIndex, writeBuffer, 0, size);
                    writer.Write(writeBuffer, 0, size);
                }
            }
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
        }
示例#14
0
        private static async Task WriteDefinitelyEscapedJavaScriptStringWithoutDelimitersAsync(
            TextWriter writer, string s, int lastWritePosition, bool[] charEscapeFlags,
            StringEscapeHandling stringEscapeHandling, JsonTextWriter client, char[] writeBuffer,
            CancellationToken cancellationToken)
        {
            if (writeBuffer == null || writeBuffer.Length < lastWritePosition)
            {
                writeBuffer = client.EnsureWriteBuffer(lastWritePosition, UnicodeTextLength);
            }

            if (lastWritePosition != 0)
            {
                s.CopyTo(0, writeBuffer, 0, lastWritePosition);

                // write unchanged chars at start of text.
                await writer.WriteAsync(writeBuffer, 0, lastWritePosition, cancellationToken).ConfigureAwait(false);
            }

            int    length;
            bool   isEscapedUnicodeText = false;
            string?escapedValue         = null;

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

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

                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.Length < UnicodeTextLength)
                            {
                                writeBuffer = client.EnsureWriteBuffer(UnicodeTextLength, 0);
                            }

                            StringUtils.ToCharAsUnicode(c, writeBuffer);

                            isEscapedUnicodeText = true;
                        }
                    }
                    else
                    {
                        continue;
                    }
                    break;
                }

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

                    if (writeBuffer.Length < length)
                    {
                        writeBuffer = client.EnsureWriteBuffer(length, UnicodeTextLength);
                    }

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

                    // write unchanged chars before writing escaped text
                    await writer.WriteAsync(writeBuffer, start, length - start, cancellationToken).ConfigureAwait(false);
                }

                lastWritePosition = i + 1;
                if (!isEscapedUnicodeText)
                {
                    await writer.WriteAsync(escapedValue !, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    await writer.WriteAsync(writeBuffer, 0, UnicodeTextLength, cancellationToken).ConfigureAwait(false);

                    isEscapedUnicodeText = false;
                }
            }

            length = s.Length - lastWritePosition;

            if (length != 0)
            {
                if (writeBuffer.Length < length)
                {
                    writeBuffer = client.EnsureWriteBuffer(length, 0);
                }

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

                // write remaining text
                await writer.WriteAsync(writeBuffer, 0, length, cancellationToken).ConfigureAwait(false);
            }
        }
        public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IArrayPool <char> bufferPool, ref char[] writeBuffer)
        {
            char   chr;
            string str;

            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
            if (s != null)
            {
                int num = 0;
                for (int i = 0; i < s.Length; i++)
                {
                    chr = s[i];
                    if (chr >= (char)((int)charEscapeFlags.Length) || charEscapeFlags[chr])
                    {
                        if (chr <= '\\')
                        {
                            switch (chr)
                            {
                            case '\b':
                            {
                                str = "\\b";
                                break;
                            }

                            case '\t':
                            {
                                str = "\\t";
                                break;
                            }

                            case '\n':
                            {
                                str = "\\n";
                                break;
                            }

                            case '\v':
                            {
                                goto Label0;
                            }

                            case '\f':
                            {
                                str = "\\f";
                                break;
                            }

                            case '\r':
                            {
                                str = "\\r";
                                break;
                            }

                            default:
                            {
                                if (chr == '\\')
                                {
                                    str = "\\\\";
                                    break;
                                }
                                else
                                {
                                    goto Label0;
                                }
                            }
                            }
                        }
                        else if (chr == '\u0085')
                        {
                            str = "\\u0085";
                        }
                        else if (chr == '\u2028')
                        {
                            str = "\\u2028";
                        }
                        else
                        {
                            if (chr != '\u2029')
                            {
                                goto Label0;
                            }
                            str = "\\u2029";
                        }
Label2:
                        if (str != null)
                        {
                            bool flag = string.Equals(str, "!");
                            if (i > num)
                            {
                                int num1 = i - num + (flag ? 6 : 0);
                                int num2 = (flag ? 6 : 0);
                                if (writeBuffer == null || (int)writeBuffer.Length < num1)
                                {
                                    char[] chrArray = BufferUtils.RentBuffer(bufferPool, num1);
                                    if (flag)
                                    {
                                        Array.Copy(writeBuffer, chrArray, 6);
                                    }
                                    BufferUtils.ReturnBuffer(bufferPool, writeBuffer);
                                    writeBuffer = chrArray;
                                }
                                s.CopyTo(num, writeBuffer, num2, num1 - num2);
                                writer.Write(writeBuffer, num2, num1 - num2);
                            }
                            num = i + 1;
                            if (flag)
                            {
                                writer.Write(writeBuffer, 0, 6);
                            }
                            else
                            {
                                writer.Write(str);
                            }
                        }
                    }
                }
                if (num != 0)
                {
                    int length = s.Length - num;
                    if (writeBuffer == null || (int)writeBuffer.Length < length)
                    {
                        writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, length, writeBuffer);
                    }
                    s.CopyTo(num, writeBuffer, 0, length);
                    writer.Write(writeBuffer, 0, length);
                }
                else
                {
                    writer.Write(s);
                }
            }
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
            return;

            if (chr >= (char)((int)charEscapeFlags.Length) && stringEscapeHandling != StringEscapeHandling.EscapeNonAscii)
            {
                str = null;
                goto Label2;
            }
            else if (chr == '\'' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
            {
                str = "\\'";
                goto Label2;
            }
            else if (chr != '\"' || stringEscapeHandling == StringEscapeHandling.EscapeHtml)
            {
                if (writeBuffer == null || (int)writeBuffer.Length < 6)
                {
                    writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, 6, writeBuffer);
                }
                StringUtils.ToCharAsUnicode(chr, writeBuffer);
                str = "!";
                goto Label2;
            }
            else
            {
                str = "\\\"";
                goto Label2;
            }
        }
        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);
            }
        }
示例#17
0
        public static void WriteEscapedJavaScriptString(TextWriter writer, string value, char delimiter, bool appendDelimiters)
        {
            string str;
            string str1;
            string str2;
            string charAsUnicode;

            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
            if (value != null)
            {
                int    num       = 0;
                int    num1      = 0;
                char[] charArray = null;
                for (int i = 0; i < value.Length; i++)
                {
                    char chr = value[i];
                    switch (chr)
                    {
                    case '\b':
                    {
                        str = "\\b";
                        break;
                    }

                    case '\t':
                    {
                        str = "\\t";
                        break;
                    }

                    case '\n':
                    {
                        str = "\\n";
                        break;
                    }

                    case '\f':
                    {
                        str = "\\f";
                        break;
                    }

                    case '\r':
                    {
                        str = "\\r";
                        break;
                    }

                    default:
                    {
                        if (chr == '\u2028')
                        {
                            str = "\\u2028";
                            break;
                        }
                        else if (chr == '\u2029')
                        {
                            str = "\\u2029";
                            break;
                        }
                        else if (chr == '\"')
                        {
                            if (delimiter != '\"')
                            {
                                str1 = null;
                            }
                            else
                            {
                                str1 = "\\\"";
                            }
                            str = str1;
                            break;
                        }
                        else if (chr == '\'')
                        {
                            if (delimiter != '\'')
                            {
                                str2 = null;
                            }
                            else
                            {
                                str2 = "\\'";
                            }
                            str = str2;
                            break;
                        }
                        else if (chr == '\\')
                        {
                            str = "\\\\";
                            break;
                        }
                        else if (chr == '\u0085')
                        {
                            str = "\\u0085";
                            break;
                        }
                        else
                        {
                            if (chr > '\u001F')
                            {
                                charAsUnicode = null;
                            }
                            else
                            {
                                charAsUnicode = StringUtils.ToCharAsUnicode(chr);
                            }
                            str = charAsUnicode;
                            break;
                        }
                    }
                    }
                    if (str == null)
                    {
                        num1++;
                    }
                    else
                    {
                        if (charArray == null)
                        {
                            charArray = value.ToCharArray();
                        }
                        if (num1 > 0)
                        {
                            writer.Write(charArray, num, num1);
                            num1 = 0;
                        }
                        writer.Write(str);
                        num = i + 1;
                    }
                }
                if (num1 > 0)
                {
                    if (num != 0)
                    {
                        writer.Write(charArray, num, num1);
                    }
                    else
                    {
                        writer.Write(value);
                    }
                }
            }
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
        }