public void TestUtfStringBuffer2()
        {
            string source = "abcd012345\n\r\u0000\u00a4\u10fb\ufffdnjetty";
            Utf8StringBuffer buffer = new Utf8StringBuffer();
            buffer.Append(source);

            for (int i = 0; i < 100; i++)
            {
                source += "abcd012345\n\r\u0000\u00a4\u10fb\ufffdnjetty";
                buffer.Append("abcd012345\n\r\u0000\u00a4\u10fb\ufffdnjetty");
            }

            Assert.AreEqual(source, buffer.ToString());
            Assert.IsTrue(buffer.ToString().EndsWith("njetty"));
        }
        public void TestUtfStringBuffer1()
        {
            string source="abcd012345\n\r\u0000\u00a4\u10fb\ufffdnjetty";

            for (int i = 0; i < 100; i++)
            {
                source += "abcd012345\n\r\u0000\u00a4\u10fb\ufffdnjetty";
            }

            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(source);
            Utf8StringBuffer buffer = new Utf8StringBuffer();
            for (int i = 0; i < bytes.Length; i++)
            {
                buffer.Append(bytes[i]);
            }

            Assert.AreEqual(source, buffer.ToString());
            Assert.IsTrue(buffer.ToString().EndsWith("njetty"));
        }
Exemple #3
0
        /// <summary>
        /// Decode string with % encoding.
        /// This method makes the assumption that the majority of calls
        /// will need no decoding.
        /// </summary>
        /// <param name="encoded"></param>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        /// <param name="charset"></param>
        /// <returns></returns>
        public static string DecodeString(string encoded, int offset, int length, string charset)
        {
            if (charset == null || StringUtil.IsUTF8(charset))
            {
                Utf8StringBuffer buffer = null;

                for (int i = 0; i < length; i++)
                {
                    char c = encoded[offset + i];
                    if (c < 0 || c > 0xff)
                    {
                        if (buffer == null)
                        {
                            buffer = new Utf8StringBuffer(length);
                            buffer.Append(encoded, offset, i + 1);
                        }
                        else
                        {
                            buffer.Append((byte)c);
                        }
                    }
                    else if (c == '+')
                    {
                        if (buffer == null)
                        {
                            buffer = new Utf8StringBuffer(length);
                            buffer.Append(encoded, offset, i);
                        }

                        buffer.Append((byte)' ');
                    }
                    else if (c == '%' && (i + 2) < length)
                    {
                        if (buffer == null)
                        {
                            buffer = new Utf8StringBuffer(length);
                            buffer.Append(encoded, offset, i);
                        }

                        while (c == '%' && (i + 2) < length)
                        {
                            try
                            {
                                byte b = (byte)TypeUtil.ParseInt(encoded, offset + i + 1, 2, 16);
                                buffer.Append(b);
                                i += 3;
                            }
                            catch (FormatException)
                            {
                                buffer.Append('%');
                                for (char next; ((next = encoded[++i + offset]) != '%');)
                                {
                                    buffer.Append((next == '+' ? ' ' : next));
                                }
                            }

                            if (i < length)
                            {
                                c = encoded[offset + i];
                            }
                        }
                        i--;
                    }
                    else if (buffer != null)
                    {
                        buffer.Append((byte)c);
                    }
                }

                if (buffer == null)
                {
                    if (offset == 0 && encoded.Length == length)
                    {
                        return(encoded);
                    }
                    return(encoded.Substring(offset, length));
                }

                return(buffer.ToString());
            }
            else
            {
                StringBuilder buffer = null;

                try
                {
                    for (int i = 0; i < length; i++)
                    {
                        char c = encoded[offset + i];
                        if (c < 0 || c > 0xff)
                        {
                            if (buffer == null)
                            {
                                buffer = new StringBuilder(length);
                                buffer.Append(encoded, offset, i + 1);
                            }
                            else
                            {
                                buffer.Append(c);
                            }
                        }
                        else if (c == '+')
                        {
                            if (buffer == null)
                            {
                                buffer = new StringBuilder(length);
                                buffer.Append(encoded, offset, i);
                            }

                            buffer.Append(' ');
                        }
                        else if (c == '%' && (i + 2) < length)
                        {
                            if (buffer == null)
                            {
                                buffer = new StringBuilder(length);
                                buffer.Append(encoded, offset, i);
                            }

                            byte[] ba = new byte[length];
                            int    n  = 0;
                            while (c >= 0 && c <= 0xff)
                            {
                                if (c == '%')
                                {
                                    if (i + 2 < length)
                                    {
                                        try
                                        {
                                            ba[n++] = (byte)TypeUtil.ParseInt(encoded, offset + i + 1, 2, 16);
                                            i      += 3;
                                        }
                                        catch (FormatException)
                                        {
                                            ba[n - 1] = (byte)'%';
                                            for (char next; ((next = encoded[++i + offset]) != '%');)
                                            {
                                                ba[n++] = (byte)(next == '+' ? ' ' : next);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        ba[n++] = (byte)'%';
                                        i++;
                                    }
                                }
                                else if (c == '+')
                                {
                                    ba[n++] = (byte)' ';
                                    i++;
                                }
                                else
                                {
                                    ba[n++] = (byte)c;
                                    i++;
                                }

                                if (i >= length)
                                {
                                    break;
                                }
                                c = encoded[offset + i];
                            }

                            i--;
                            string str = Encoding.GetEncoding(charset).GetString(ba, 0, n);
                            buffer.Append(str);
                        }
                        else if (buffer != null)
                        {
                            buffer.Append(c);
                        }
                    }

                    if (buffer == null)
                    {
                        if (offset == 0 && encoded.Length == length)
                        {
                            return(encoded);
                        }
                        return(encoded.Substring(offset, length));
                    }

                    return(buffer.ToString());
                }
                catch (ArgumentException e)
                {
                    throw new SystemException(e.Message, e);
                }
            }
        }