Added hangul syllable naming algorithm from Unicode 7.0.

This commit is contained in:
GoldenCrystal
2014-11-20 21:26:50 +01:00
parent 5c47c98b18
commit 694a749046
5 changed files with 93 additions and 14 deletions
+62
View File
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace System.Unicode
{
internal static class HangulInfo
{
// Constants defined at page 144 of the Unicode 7.0 Standard (3.12)
private const ushort SBase = 0xAC00;
private const ushort LBase = 0x1100;
private const ushort VBase = 0x1161;
private const ushort TBase = 0x11A7;
private const int LCount = 19;
private const int VCount = 21;
private const int TCount = 28;
private const int NCount = VCount * TCount;
private const int SCount = LCount * NCount;
private static readonly string[] JamoLTable =
{
"G", "GG", "N", "D", "DD", "R", "M", "B", "BB",
"S", "SS", "", "J", "JJ", "C", "K", "T", "P", "H"
};
private static readonly string[] JamoVTable =
{
"A", "AE", "YA", "YAE", "EO", "E", "YEO", "YE", "O",
"WA", "WAE", "OE", "YO", "U", "WEO", "WE", "WI",
"YU", "EU", "YI", "I"
};
private static readonly string[] JamoTTable =
{
"", "G", "GG", "GS", "N", "NJ", "NH", "D", "L", "LG", "LM",
"LB", "LS", "LT", "LP", "LH", "M", "B", "BS",
"S", "SS", "NG", "J", "C", "K", "T", "P", "H"
};
private static int lIndex;
// Algorithm defined on page 150 of the Unicode 7.0 Standard (3.12)
internal static string GetHangulName(char codePoint)
{
int sIndex = codePoint - SBase;
if (sIndex < 0 || sIndex >= SCount) throw new ArgumentOutOfRangeException("codePoint");
int lIndex = sIndex / NCount;
int vIndex = (sIndex % NCount) / TCount;
int tIndex = sIndex % TCount;
return "HANGUL SYLLABLE " + JamoLTable[lIndex] + JamoVTable[vIndex] + JamoTTable[tIndex];
}
internal static bool IsHangul(int codePoint)
{
return codePoint >= SBase && codePoint <= SBase + SCount;
}
}
}
+4 -12
View File
@@ -10,23 +10,14 @@ namespace System.Unicode
public struct UnicodeCharInfo
{
private readonly int codePoint;
private readonly string name;
private readonly UnicodeCharacterData unicodeCharacterData;
private readonly UnihanCharacterData unihanCharacterData;
private readonly string block;
public int CodePoint { get { return codePoint; } }
public string Name
{
get
{
return unicodeCharacterData != null ?
unicodeCharacterData.Name == null || unicodeCharacterData.CodePointRange.IsSingleCodePoint ?
unicodeCharacterData.Name :
unicodeCharacterData.Name + "-" + codePoint.ToString("X4") :
null;
}
}
public string Name { get { return name; } }
public UnicodeCategory Category { get { return unicodeCharacterData?.Category ?? UnicodeCategory.OtherNotAssigned; } }
public string Block { get { return block ?? "No_Block"; } }
@@ -60,7 +51,8 @@ namespace System.Unicode
internal UnicodeCharInfo(int codePoint, UnicodeCharacterData unicodeCharacterData, UnihanCharacterData unihanCharacterData, string block)
{
this.codePoint = codePoint;
this.unicodeCharacterData = unicodeCharacterData;
this.name = UnicodeInfo.GetName(codePoint, unicodeCharacterData);
this.unicodeCharacterData = unicodeCharacterData;
this.unihanCharacterData = unihanCharacterData;
this.block = block;
}
+16 -2
View File
@@ -264,9 +264,9 @@ namespace System.Unicode
public static UnicodeCategory GetCategory(int codePoint)
{
var charInfo = FindUnicodeCodePoint(codePoint);
var charData = FindUnicodeCodePoint(codePoint);
return charInfo != null ? charInfo.Category : UnicodeCategory.OtherNotAssigned;
return charData != null ? charData.Category : UnicodeCategory.OtherNotAssigned;
}
public static string GetDisplayText(UnicodeCharInfo charInfo)
@@ -283,6 +283,20 @@ namespace System.Unicode
else return char.ConvertFromUtf32(codePoint);
}
public static string GetName(int codePoint)
{
if (HangulInfo.IsHangul(codePoint)) return HangulInfo.GetHangulName((char)codePoint);
else return GetName(codePoint, FindUnicodeCodePoint(codePoint));
}
internal static string GetName(int codePoint, UnicodeCharacterData characterData)
{
if (characterData.CodePointRange.IsSingleCodePoint) return characterData.Name;
else if (HangulInfo.IsHangul(codePoint)) return HangulInfo.GetHangulName((char)codePoint);
else if (characterData.Name != null) return characterData.Name + "-" + codePoint.ToString("X4");
else return null;
}
public static UnicodeBlock[] GetBlocks()
{
return (UnicodeBlock[])blocks.Clone();
@@ -65,6 +65,7 @@
<Compile Include="CodePointEnumerable.cs" />
<Compile Include="CodePointEnumerator.cs" />
<Compile Include="CoreProperties.cs" />
<Compile Include="HangulInfo.cs" />
<Compile Include="UcdFields.cs" />
<Compile Include="EnumHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />