Exemple #1
0
 public static CodePoint Decode(ArrayAccessor buffer, Index index)
 {
     var i = index.index;
     uint x = buffer[i++];
     uint code = 0;
     switch (index.count)
     {
         case 1:
             return new CodePoint(x);
         case 2:
             code = x & 0b1_1111;
             code = (code << 6) | (uint)(buffer[i++] & 0b0011_1111);
             return new CodePoint(code);
         case 3:
             code = x & 0b1111;
             code = (code << 6) | (uint)(buffer[i++] & 0b0011_1111);
             code = (code << 6) | (uint)(buffer[i++] & 0b0011_1111);
             return new CodePoint(code);
         case 4:
             code = x & 0b0111;
             code = (code << 6) | (uint)(buffer[i++] & 0b0011_1111);
             code = (code << 6) | (uint)(buffer[i++] & 0b0011_1111);
             code = (code << 6) | (uint)(buffer[i++] & 0b0011_1111);
             return new CodePoint(code);
         default:
             throw new IndexOutOfRangeException();
     }
 }
Exemple #2
0
        public static CodePoint Decode(ArrayAccessor buffer, Index index)
        {
            var  i    = index.index;
            uint x    = buffer[i++];
            uint code = 0;

            switch (index.count)
            {
            case 1:
                return(new CodePoint(x));

            case 2:
                code = x & 0b1_1111;
                code = (code << 6) | (uint)(buffer[i++] & 0b0011_1111);
                return(new CodePoint(code));

            case 3:
                code = x & 0b1111;
                code = (code << 6) | (uint)(buffer[i++] & 0b0011_1111);
                code = (code << 6) | (uint)(buffer[i++] & 0b0011_1111);
                return(new CodePoint(code));

            case 4:
                code = x & 0b0111;
                code = (code << 6) | (uint)(buffer[i++] & 0b0011_1111);
                code = (code << 6) | (uint)(buffer[i++] & 0b0011_1111);
                code = (code << 6) | (uint)(buffer[i++] & 0b0011_1111);
                return(new CodePoint(code));

            default:
                throw new IndexOutOfRangeException();
            }
        }
Exemple #3
0
 public static int GetLength(ArrayAccessor buffer)
 {
     var count = 0;
     for (int i = 0; i < buffer.Length; i++)
     {
         var x = buffer[i];
         if ((x & 0b1100_0000) != 0b1000_0000)
             count++;
     }
     return count;
 }
Exemple #4
0
        public static int GetLength(ArrayAccessor buffer)
        {
            var count = 0;

            for (int i = 0; i < buffer.Length; i++)
            {
                var x = buffer[i];
                if ((x & 0b1100_0000) != 0b1000_0000)
                {
                    count++;
                }
            }
            return(count);
        }
Exemple #5
0
        public static (CodePoint cp, byte count) TryDecode(ArrayAccessor buffer, int index)
        {
            if (index >= buffer.Length)
            {
                return(Constants.End);
            }

            uint code = buffer[index];

            if (code < 0b1100_0000)
            {
                // ASCII 文字
                return(new CodePoint(code), 1);
            }
            if (code < 0b1110_0000)
            {
                // 2バイト文字
                if (index + 1 >= buffer.Length)
                {
                    return(Constants.End);
                }
                code &= 0b1_1111;
                code  = (code << 6) | (uint)(buffer[++index] & 0b0011_1111);
                return(new CodePoint(code), 2);
            }
            if (code < 0b1111_0000)
            {
                // 3バイト文字
                if (index + 2 >= buffer.Length)
                {
                    return(Constants.End);
                }
                code &= 0b1111;
                code  = (code << 6) | (uint)(buffer[++index] & 0b0011_1111);
                code  = (code << 6) | (uint)(buffer[++index] & 0b0011_1111);
                return(new CodePoint(code), 3);
            }

            // 4バイト文字
            if (index + 3 >= buffer.Length)
            {
                return(Constants.End);
            }
            code &= 0b0111;
            code  = (code << 6) | (uint)(buffer[++index] & 0b0011_1111);
            code  = (code << 6) | (uint)(buffer[++index] & 0b0011_1111);
            code  = (code << 6) | (uint)(buffer[++index] & 0b0011_1111);
            return(new CodePoint(code), 4);
        }
Exemple #6
0
        public static byte TyrGetCount(ArrayAccessor buffer, int index)
        {
            if (index >= buffer.Length) return Constants.InvalidCount;

            uint x = buffer[index];

            var byteCount =
                (x < 0b1100_0000U) ? (byte)1 :
                (x < 0b1110_0000U) ? (byte)2 :
                (x < 0b1111_0000U) ? (byte)3 :
                (byte)4;

            if (index + byteCount > buffer.Length) return Constants.InvalidCount;

            return byteCount;
        }
Exemple #7
0
        private static bool TryNext(ArrayAccessor buffer, ref int index, ref uint code)
        {
            if (index >= buffer.Length)
            {
                return(false);
            }

            var c = buffer[index++];

            if ((c & 0b1100_0000) != 0b1000_0000)
            {
                return(false);
            }

            code = (code << 6) | (uint)(c & 0b0011_1111);
            return(true);
        }
Exemple #8
0
        public static byte TyrGetCount(ArrayAccessor buffer, int index)
        {
            if (index >= buffer.Length)
            {
                return(Constants.InvalidCount);
            }

            uint x = buffer[index];

            var byteCount =
                (x < 0b1100_0000U) ? (byte)1 :
                (x < 0b1110_0000U) ? (byte)2 :
                (x < 0b1111_0000U) ? (byte)3 :
                (byte)4;

            if (index + byteCount > buffer.Length)
            {
                return(Constants.InvalidCount);
            }

            return(byteCount);
        }
Exemple #9
0
        public static (CodePoint cp, byte count) TryDecode(ArrayAccessor buffer, int index)
        {
            if (index >= buffer.Length) return Constants.End;

            uint code = buffer[index];

            if (code < 0b1100_0000)
            {
                // ASCII 文字
                return (new CodePoint(code), 1);
            }
            if (code < 0b1110_0000)
            {
                // 2バイト文字
                if (index + 1 >= buffer.Length) return Constants.End;
                code &= 0b1_1111;
                code = (code << 6) | (uint)(buffer[++index] & 0b0011_1111);
                return (new CodePoint(code), 2);
            }
            if (code < 0b1111_0000)
            {
                // 3バイト文字
                if (index + 2 >= buffer.Length) return Constants.End;
                code &= 0b1111;
                code = (code << 6) | (uint)(buffer[++index] & 0b0011_1111);
                code = (code << 6) | (uint)(buffer[++index] & 0b0011_1111);
                return (new CodePoint(code), 3);
            }

            // 4バイト文字
            if (index + 3 >= buffer.Length) return Constants.End;
            code &= 0b0111;
            code = (code << 6) | (uint)(buffer[++index] & 0b0011_1111);
            code = (code << 6) | (uint)(buffer[++index] & 0b0011_1111);
            code = (code << 6) | (uint)(buffer[++index] & 0b0011_1111);
            return (new CodePoint(code), 4);
        }
Exemple #10
0
 public IndexEnumerable(ArrayAccessor buffer)
 {
     _buffer = buffer;
     _i      = default(Index);
 }
Exemple #11
0
 public StringEnumerator(ArrayAccessor buffer)
 {
     _buffer = buffer;
     _index  = default(Index);
     Current = default(CodePoint);
 }
Exemple #12
0
 public String(byte[] encodedBytes)
 {
     _buffer = new ArrayAccessor(encodedBytes);
 }
Exemple #13
0
        private static bool TryNext(ArrayAccessor buffer, ref int index, ref uint code)
        {
            if (index >= buffer.Length) return false;

            var c = buffer[index++];
            if ((c & 0b1100_0000) != 0b1000_0000) return false;

            code = (code << 6) | (uint)(c & 0b0011_1111);
            return true;
        }