Ejemplo n.º 1
0
        public static String Decode(Utf8PointerLengthSlice text)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder((int)text.length);
            Utf8Pointer next  = text.ptr;
            Utf8Pointer limit = text.LimitPointer;

            while (next < limit)
            {
                UInt32 c = Decode(ref next, limit);
                builder.Append((Char)c);
            }
            return(builder.ToString());
        }
Ejemplo n.º 2
0
        public static UInt32 Decode(ref Utf8Pointer text, Utf8Pointer limit)
        {
            if (text >= limit)
            {
                throw new ArgumentException("Cannot pass an empty data segment to Utf8.Decode");
            }

            UInt32 c = text[0];

            if (c <= 0x7F)
            {
                return(c);
            }
            if ((c & 0x40) == 0)
            {
                throw new Utf8Exception(Utf8ExceptionType.StartedInsideCodePoint);
            }

            text++;
            if ((c & 0x20) == 0)
            {
                if (text >= limit)
                {
                    throw new Utf8Exception(Utf8ExceptionType.MissingBytes);
                }
                return(((c << 6) & 0x7C0U) | (text[0] & 0x3FU));
            }

            if ((c & 0x10) == 0)
            {
                if (text + 1 >= limit)
                {
                    throw new Utf8Exception(Utf8ExceptionType.MissingBytes);
                }
                return(((c << 12) & 0xF000U) | ((UInt32)(text[0] << 6) & 0xFC0U) | (text[1] & 0x3FU));
            }

            if ((c & 0x08) == 0)
            {
                if (text + 2 >= limit)
                {
                    throw new Utf8Exception(Utf8ExceptionType.MissingBytes);
                }
                return(((c << 18) & 0x1C0000U) | ((UInt32)(text[0] << 12) & 0x3F000U) |
                       ((UInt32)(text[1] << 6) & 0xFC0U) | (text[2] & 0x3FU));
            }

            throw new Utf8Exception(Utf8ExceptionType.OutOfRange);
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 4
0
 public Utf8PointerLengthSlice(Utf8Pointer ptr, UInt32 length)
 {
     this.ptr    = ptr;
     this.length = length;
 }
Ejemplo n.º 5
0
 public Utf8PointerLimitSlice(Utf8Pointer ptr, Utf8Pointer limit)
 {
     this.ptr   = ptr;
     this.limit = limit;
 }