Example #1
0
        private static void ReadUnihanCharacterDataEntry(BinaryReader reader, out UnihanCharacterData value)
        {
            var fields = (UnihanFields)reader.ReadUInt16();

            int codePoint = Unicode.UnihanCharacterData.UnpackCodePoint(ReadCodePoint(reader));

            var  numericType  = (UnihanNumericType)(int)(fields & UnihanFields.OtherNumeric);
            long numericValue = numericType != UnihanNumericType.None ?
                                reader.ReadInt64() :
                                0;

            UnicodeRadicalStrokeCount[] unicodeRadicalStrokeCounts = (fields & UnihanFields.UnicodeRadicalStrokeCountMore) != 0 ?
                                                                     new UnicodeRadicalStrokeCount
                                                                     [
                (fields & UnihanFields.UnicodeRadicalStrokeCountMore) == UnihanFields.UnicodeRadicalStrokeCountMore ?
                reader.ReadByte() + 3 :
                ((byte)(fields & UnihanFields.UnicodeRadicalStrokeCountMore) >> 2)
                                                                     ] :
#if NETSTANDARD1_1 || NET45
                                                                     UnicodeRadicalStrokeCount.EmptyArray;
#else
                                                                     Array.Empty <UnicodeRadicalStrokeCount>();
#endif

            for (int i = 0; i < unicodeRadicalStrokeCounts.Length; ++i)
            {
                unicodeRadicalStrokeCounts[i] = new UnicodeRadicalStrokeCount(reader.ReadByte(), reader.ReadByte());
            }

            string definition         = (fields & UnihanFields.Definition) != 0 ? reader.ReadString() : null;
            string mandarinReading    = (fields & UnihanFields.MandarinReading) != 0 ? reader.ReadString() : null;
            string cantoneseReading   = (fields & UnihanFields.CantoneseReading) != 0 ? reader.ReadString() : null;
            string japaneseKunReading = (fields & UnihanFields.JapaneseKunReading) != 0 ? reader.ReadString() : null;
            string japaneseOnReading  = (fields & UnihanFields.JapaneseOnReading) != 0 ? reader.ReadString() : null;
            string koreanReading      = (fields & UnihanFields.KoreanReading) != 0 ? reader.ReadString() : null;
            string hangulReading      = (fields & UnihanFields.HangulReading) != 0 ? reader.ReadString() : null;
            string vietnameseReading  = (fields & UnihanFields.VietnameseReading) != 0 ? reader.ReadString() : null;
            string simplifiedVariant  = (fields & UnihanFields.SimplifiedVariant) != 0 ? reader.ReadString() : null;
            string traditionalVariant = (fields & UnihanFields.TraditionalVariant) != 0 ? reader.ReadString() : null;

            value = new UnihanCharacterData
                    (
                codePoint,
                numericType,
                numericValue,
                unicodeRadicalStrokeCounts,
                definition,
                mandarinReading,
                cantoneseReading,
                japaneseKunReading,
                japaneseOnReading,
                koreanReading,
                hangulReading,
                vietnameseReading,
                simplifiedVariant,
                traditionalVariant
                    );
        }
Example #2
0
        internal static void ReadFromStream(Stream stream, out Version unicodeVersion, out UnicodeCharacterData[] unicodeCharacterData, out UnihanCharacterData[] unihanCharacterData, out CjkRadicalData[] radicals, out UnicodeBlock[] blocks, out int maxContiguousIndex)
        {
            using (var reader = new BinaryReader(stream, Encoding.UTF8))
            {
                int i;

                if (reader.ReadByte() != 'U'
                    | reader.ReadByte() != 'C'
                    | reader.ReadByte() != 'D')
                {
                    throw new InvalidDataException();
                }

                byte formatVersion = reader.ReadByte();

                if (formatVersion != 2)
                {
                    throw new InvalidDataException();
                }

                var fileUnicodeVersion = new Version(reader.ReadUInt16(), reader.ReadByte(), reader.ReadByte());

                var    unicodeCharacterDataEntries = new UnicodeCharacterData[ReadCodePoint(reader)];              // Allocate one extra entry to act as a dummy entry.
                byte[] nameBuffer = new byte[128];
                int    mci        = 0;

                for (i = 0; i < unicodeCharacterDataEntries.Length; ++i)
                {
                    ReadUnicodeCharacterDataEntry(reader, nameBuffer, out unicodeCharacterDataEntries[i]);
                    if (unicodeCharacterDataEntries[i].CodePointRange.Contains(i))
                    {
                        mci = i;
                    }
                    else
                    {
                        ++i;
                        break;
                    }
                }

                maxContiguousIndex = mci;

                for (; i < unicodeCharacterDataEntries.Length; ++i)
                {
                    ReadUnicodeCharacterDataEntry(reader, nameBuffer, out unicodeCharacterDataEntries[i]);
                }

                var blockEntries = new UnicodeBlock[reader.ReadUInt16()];

                for (i = 0; i < blockEntries.Length; ++i)
                {
                    ReadBlockEntry(reader, out blockEntries[i]);
                }

                var cjkRadicalEntries = new CjkRadicalData[reader.ReadByte()];

                for (i = 0; i < cjkRadicalEntries.Length; ++i)
                {
                    ReadCjkRadicalInfo(reader, out cjkRadicalEntries[i]);
                }

                var unihanCharacterDataEntries = new UnihanCharacterData[ReadCodePoint(reader)];

                for (i = 0; i < unihanCharacterDataEntries.Length; ++i)
                {
                    ReadUnihanCharacterDataEntry(reader, out unihanCharacterDataEntries[i]);
                }

                unicodeVersion       = fileUnicodeVersion;
                unicodeCharacterData = unicodeCharacterDataEntries;
                unihanCharacterData  = unihanCharacterDataEntries;
                radicals             = cjkRadicalEntries;
                blocks = blockEntries;
            }
        }
Example #3
0
        public static UnicodeData ReadFromStream(Stream stream)
        {
            using (var reader = new BinaryReader(stream, Encoding.UTF8))
            {
                int i;

                if (reader.ReadByte() != 'U'
                    | reader.ReadByte() != 'C'
                    | reader.ReadByte() != 'D')
                {
                    throw new InvalidDataException();
                }

                byte formatVersion = reader.ReadByte();

                if (formatVersion != 2)
                {
                    throw new InvalidDataException();
                }

                var fileUnicodeVersion = new Version(reader.ReadUInt16(), reader.ReadByte(), reader.ReadByte());

                var    unicodeCharacterDataEntries = new UnicodeCharacterData[ReadCodePoint(reader)];              // Allocate one extra entry to act as a dummy entry.
                byte[] nameBuffer         = new byte[128];
                int    maxContiguousIndex = 0;

                for (i = 0; i < unicodeCharacterDataEntries.Length; ++i)
                {
                    ReadUnicodeCharacterDataEntry(reader, nameBuffer, out unicodeCharacterDataEntries[i]);
                    if (unicodeCharacterDataEntries[i].CodePointRange.Contains(i))
                    {
                        maxContiguousIndex = i;
                    }
                    else
                    {
                        ++i;
                        break;
                    }
                }

                for (; i < unicodeCharacterDataEntries.Length; ++i)
                {
                    ReadUnicodeCharacterDataEntry(reader, nameBuffer, out unicodeCharacterDataEntries[i]);
                }

                var blockEntries = new UnicodeBlock[reader.ReadUInt16()];

                for (i = 0; i < blockEntries.Length; ++i)
                {
                    ReadBlockEntry(reader, out blockEntries[i]);
                }

                var cjkRadicalEntries = new CjkRadicalData[reader.ReadByte()];

                for (i = 0; i < cjkRadicalEntries.Length; ++i)
                {
                    ReadCjkRadicalInfo(reader, out cjkRadicalEntries[i]);
                }

                var unihanCharacterDataEntries = new UnihanCharacterData[ReadCodePoint(reader)];

                for (i = 0; i < unihanCharacterDataEntries.Length; ++i)
                {
                    ReadUnihanCharacterDataEntry(reader, out unihanCharacterDataEntries[i]);
                }

                return(new UnicodeData
                       (
                           fileUnicodeVersion,
                           unicodeCharacterDataEntries,
                           unihanCharacterDataEntries,
                           blockEntries,
                           cjkRadicalEntries,
                           maxContiguousIndex
                       ));
            }
        }