First tentative of name alias encoding.

This commit is contained in:
GoldenCrystal
2014-11-21 15:41:27 +01:00
parent 83fb677dc2
commit 4119824ffc
8 changed files with 165 additions and 24 deletions
+3 -1
View File
@@ -14,7 +14,7 @@ namespace System.Unicode
// Not really a field, just here to indicate that the entry is a range
CodePointRange = 1,
Name = 2,
Name = 2, // Will stand in for official name as well as related names.
Category = 4,
CanonicalCombiningClass = 8,
BidirectionalClass = 16,
@@ -35,5 +35,7 @@ namespace System.Unicode
ContributoryProperties = 8192,
CoreProperties = 16384,
RelatedCodePoints = 32768,
}
}
@@ -11,6 +11,7 @@ namespace System.Unicode
{
public readonly UnicodeCharacterRange CodePointRange;
public readonly string Name;
public readonly UnicodeNameAlias[] NameAliases;
public readonly UnicodeCategory Category;
public readonly CanonicalCombiningClass CanonicalCombiningClass;
public readonly BidirectionalClass BidirectionalClass;
@@ -32,6 +33,7 @@ namespace System.Unicode
(
UnicodeCharacterRange codePointRange,
string name,
UnicodeNameAlias[] nameAliases,
UnicodeCategory category,
CanonicalCombiningClass canonicalCombiningClass,
BidirectionalClass bidirectionalClass,
@@ -51,6 +53,7 @@ namespace System.Unicode
{
this.CodePointRange = codePointRange;
this.Name = name;
this.NameAliases = nameAliases;
this.Category = category;
this.CanonicalCombiningClass = canonicalCombiningClass;
this.BidirectionalClass = bidirectionalClass;
+43 -4
View File
@@ -31,7 +31,7 @@ namespace System.Unicode
{
using (var reader = new BinaryReader(stream, Encoding.UTF8))
{
if (reader.ReadByte() != 'U'
if (reader.ReadByte() != 'U'
| reader.ReadByte() != 'C'
| reader.ReadByte() != 'D')
throw new InvalidDataException();
@@ -43,10 +43,11 @@ namespace System.Unicode
var fileUnicodeVersion = new Version(reader.ReadUInt16(), reader.ReadByte());
var unicodeCharacterDataEntries = new UnicodeCharacterData[ReadCodePoint(reader)];
byte[] nameBuffer = new byte[64];
for (int i = 0; i < unicodeCharacterDataEntries.Length; ++i)
{
unicodeCharacterDataEntries[i] = ReadUnicodeCharacterDataEntry(reader);
unicodeCharacterDataEntries[i] = ReadUnicodeCharacterDataEntry(reader, nameBuffer);
}
var blockEntries = new UnicodeBlock[reader.ReadByte()];
@@ -70,13 +71,50 @@ namespace System.Unicode
}
}
private static UnicodeCharacterData ReadUnicodeCharacterDataEntry(BinaryReader reader)
private static UnicodeCharacterData ReadUnicodeCharacterDataEntry(BinaryReader reader, byte[] nameBuffer)
{
var fields = (UcdFields)reader.ReadUInt16();
var codePointRange = (fields & UcdFields.CodePointRange) != 0 ? new UnicodeCharacterRange(ReadCodePoint(reader), ReadCodePoint(reader)) : new UnicodeCharacterRange(ReadCodePoint(reader));
string name = (fields & UcdFields.Name) != 0 ? reader.ReadString() : null;
string name = null;
UnicodeNameAlias[] nameAliases = UnicodeNameAlias.EmptyArray;
// Read all the official names of the character.
if ((fields & UcdFields.Name) != 0)
{
int length = reader.ReadByte();
byte @case = (byte)(length & 0xC0);
length = (length & 0x3F) + 1;
if (@case < 0xC0) // These cases have an official name.
{
if (@case != 0)
{
}
if (reader.Read(nameBuffer, 0, length) != length) throw new EndOfStreamException();
name = Encoding.UTF8.GetString(nameBuffer, 0, length);
if (@case == 2) length = (reader.ReadByte() & 0x3F) + 1;
else length = @case;
}
if (length > 0) // These cases have official name aliases.
{
nameAliases = new UnicodeNameAlias[length];
for (int i = 0; i < nameAliases.Length; ++i)
{
length = reader.ReadByte();
UnicodeNameAliasKind aliasKind = (UnicodeNameAliasKind)((length >> 6) + 1);
length = (length & 0x3F) + 1;
if (reader.Read(nameBuffer, 0, length) != length) throw new EndOfStreamException();
nameAliases[i] = new UnicodeNameAlias(Encoding.UTF8.GetString(nameBuffer, 0, length), aliasKind);
}
}
}
var category = (fields & UcdFields.Category) != 0 ? (UnicodeCategory)reader.ReadByte() : UnicodeCategory.OtherNotAssigned;
var canonicalCombiningClass = (fields & UcdFields.CanonicalCombiningClass) != 0 ? (CanonicalCombiningClass)reader.ReadByte() : CanonicalCombiningClass.NotReordered;
var bidirectionalClass = (fields & UcdFields.BidirectionalClass) != 0 ? (BidirectionalClass)reader.ReadByte() : 0;
@@ -97,6 +135,7 @@ namespace System.Unicode
(
codePointRange,
name,
nameAliases,
category,
canonicalCombiningClass,
bidirectionalClass,
@@ -77,6 +77,8 @@
<Compile Include="UnicodeCharacterRange.cs" />
<Compile Include="UnicodeCharInfo.cs" />
<Compile Include="UnicodeInfo.cs" />
<Compile Include="UnicodeNameAlias.cs" />
<Compile Include="UnicodeNameAliasKind.cs" />
<Compile Include="UnihanNumericType.cs" />
<Compile Include="UnicodeNumericType.cs" />
<Compile Include="UnihanCharacterData.cs" />
+16
View File
@@ -0,0 +1,16 @@
namespace System.Unicode
{
public struct UnicodeNameAlias
{
internal static readonly UnicodeNameAlias[] EmptyArray = new UnicodeNameAlias[0];
public string Name { get; }
public UnicodeNameAliasKind Kind { get; }
internal UnicodeNameAlias(string name, UnicodeNameAliasKind kind)
{
Name = name;
Kind = kind;
}
}
}
@@ -0,0 +1,11 @@
namespace System.Unicode
{
public enum UnicodeNameAliasKind : byte
{
Correction = 1,
Control = 2,
Alternate = 3,
Figment = 4,
Abbreviation = 5
}
}