Esempio n. 1
0
        public static Rune FromUtf16(byte[] array, ref int index, ByteOrder endianness = ByteOrder.LittleEndian)
        {
            var decoder = new Utf16.ByteDecoder(endianness);

            if (index + 2 > array.Length)
            {
                throw new ArgumentOutOfRangeException("index", "Need at least 2 bytes to process Utf16");
            }

            decoder.Process(array[index++]);
            var result = decoder.Process(array[index++]);

            if (result == null)
            {
                if (index + 2 > array.Length)
                {
                    throw new ArgumentOutOfRangeException("index", "Need 4 bytes to process Utf16 surrogate pair");
                }

                decoder.Process(array[index++]);
                result = decoder.Process(array[index++]);

                if (result == null)
                {
                    throw new RuneException(Utf16.MissingTrailingSurrogateMessage());
                }
            }

            return(new Rune(result.GetValueOrDefault()));
        }
        // byte.Utf16... -> Rune
        public static IEnumerable <Rune> AsUtf16Runes(this IEnumerable <byte> bytes, ByteOrder endianness = ByteOrder.LittleEndian)
        {
            var decoder = new Utf16.ByteDecoder(endianness);

            return(DecodeValues(bytes, decoder, "Ill-formed Utf16."));
        }