public static void AppendUtf8(this StringBuilder builder, Byte[] bytes, UInt32 offset, UInt32 limit)
        {
            // Small optimization to prevent multiple resizes
            builder.EnsureCapacity((int)(builder.Length + (limit - offset)));

            while (true)
            {
                if (offset >= limit)
                {
                    return;
                }
                var c = Utf8.Decode(bytes, ref offset, limit);
                builder.Append((Char)c);
            }
        }
Exemple #2
0
        void TestInvalidUtf8(Utf8ExceptionType expectedError, params Byte[] s)
        {
            UInt32 start = 0;
            UInt32 limit = (UInt32)s.Length;

            try
            {
                var decoded = Utf8.Decode(s, ref start, limit);
                Assert.Fail("expected error '%s' but no error was thrown", expectedError);
            } catch (Utf8Exception e)
            {
                Assert.AreEqual(expectedError, e.type, String.Format("expected error '{0}' but got '{1}'", expectedError, e.type));
            }

            Console.WriteLine("got expected error '{0}'", expectedError);
        }
Exemple #3
0
        unsafe void TestDecodeUtf8(Byte[] s, UInt32 start, UInt32 limit, params UInt32[] expectedChars)
        {
            foreach (var expected in expectedChars)
            {
                if (start >= limit)
                {
                    Assert.Fail("Expected more decoded utf8 chars but input ended");
                }
                UInt32 encodedLength = limit - start;
                {
                    var saveStart = start;
                    var decoded   = Utf8.Decode(s, ref start, limit);
                    if (decoded != expected)
                    {
                        Assert.Fail("decodeUtf8: Expected '{0}' 0x{1} but decoded '{2}' 0x{3}",
                                    expected, expected, decoded, decoded);
                    }
                    Console.WriteLine("decodeUtf8('{0}')", decoded);
                }

                fixed(byte *sPointer = s)
                {
                    Utf8Pointer pointer      = new Utf8Pointer(sPointer);
                    Utf8Pointer pointerLimit = new Utf8Pointer(sPointer + encodedLength);
                    var         decoded      = Utf8.Decode(ref pointer, pointerLimit);

                    if (decoded != expected)
                    {
                        Assert.Fail("decodeUtf8: Expected '{0}' 0x{1} but decoded '{2}' 0x{3}",
                                    expected, expected, decoded, decoded);
                    }
                }
            }

            if (start != limit)
            {
                Assert.Fail("Expected {0} characters but didn't get enough", expectedChars.Length);
            }
        }