Пример #1
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;
            }
        }
 public static string ToEscapedJavaScriptString(string value)
 {
     return(JavaScriptUtils.ToEscapedJavaScriptString(value, '"', true));
 }
Пример #3
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_10D;

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

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

                                default:
                                    if (c != '\\')
                                    {
                                        goto IL_10D;
                                    }
                                    text = "\\\\";
                                    break;
                                }
                            }
                            else if (c != '\u0085')
                            {
                                if (c != '\u2028')
                                {
                                    if (c != '\u2029')
                                    {
                                        goto IL_10D;
                                    }
                                    text = "\\u2029";
                                }
                                else
                                {
                                    text = "\\u2028";
                                }
                            }
                            else
                            {
                                text = "\\u0085";
                            }
IL_185:
                            if (text == null)
                            {
                                goto IL_229;
                            }
                            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_229;
                            }
                            writer.Write(writeBuffer, 0, 6);
                            goto IL_229;
IL_10D:
                            if ((int)c >= charEscapeFlags.Length)
                            {
                                if (stringEscapeHandling != StringEscapeHandling.EscapeNonAscii)
                                {
                                    text = null;
                                    goto IL_185;
                                }
                            }
                            if (c == '\'' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
                            {
                                text = "\\'";
                                goto IL_185;
                            }
                            if (c == '"' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
                            {
                                text = "\\\"";
                                goto IL_185;
                            }
                            if (writeBuffer == null || writeBuffer.Length < 6)
                            {
                                writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, 6, writeBuffer);
                            }
                            StringUtils.ToCharAsUnicode(c, writeBuffer);
                            text = "!";
                            goto IL_185;
                        }
                        IL_229 :;
                    }
                    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);
            }
        }