[英]How does .NET determine the Unicode category of a character?
我正在使用.NET Reflector查找mscorelib.dll,并偶然发现了Char类。 我总是想知道Char.isLetter这样的方法是如何完成的。 我期待一个巨大的测试列表,但是,买一点点,我找到了一个确定Unicode类别的真正短代码。 但是,这段代码使用某种表格和一些比特变换伏都教。 任何人都可以向我解释这是如何完成的,或者指向一些资源?
编辑:这是代码。 它位于System.Globalization.CharUnicodeInfo中。
internal static unsafe byte InternalGetCategoryValue(int ch, int offset)
{
ushort num = s_pCategoryLevel1Index[ch >> 8];
num = s_pCategoryLevel1Index[num + ((ch >> 4) & 15)];
byte* numPtr = (byte*) (s_pCategoryLevel1Index + num);
byte num2 = numPtr[ch & 15];
return s_pCategoriesValue[(num2 * 2) + offset];
}
s_pCategoryLevel1Index
是一个short*
, s_pCategoryValues
是一个byte*
两者都是在CharUnicodeInfo静态构造函数中创建的:
static unsafe CharUnicodeInfo()
{
s_pDataTable = GlobalizationAssembly.GetGlobalizationResourceBytePtr(typeof(CharUnicodeInfo).Assembly, "charinfo.nlp");
UnicodeDataHeader* headerPtr = (UnicodeDataHeader*) s_pDataTable;
s_pCategoryLevel1Index = (ushort*) (s_pDataTable + headerPtr->OffsetToCategoriesIndex);
s_pCategoriesValue = s_pDataTable + ((byte*) headerPtr->OffsetToCategoriesValue);
s_pNumericLevel1Index = (ushort*) (s_pDataTable + headerPtr->OffsetToNumbericIndex);
s_pNumericValues = s_pDataTable + ((byte*) headerPtr->OffsetToNumbericValue);
s_pDigitValues = (DigitValues*) (s_pDataTable + headerPtr->OffsetToDigitValue);
nativeInitTable(s_pDataTable);
}
这是UnicodeDataHeader。
internal struct UnicodeDataHeader
{
// Fields
[FieldOffset(40)]
internal uint OffsetToCategoriesIndex;
[FieldOffset(0x2c)]
internal uint OffsetToCategoriesValue;
[FieldOffset(0x34)]
internal uint OffsetToDigitValue;
[FieldOffset(0x30)]
internal uint OffsetToNumbericIndex;
[FieldOffset(0x38)]
internal uint OffsetToNumbericValue;
[FieldOffset(0)]
internal char TableName;
[FieldOffset(0x20)]
internal ushort version;
}
注意:我希望这不会破坏任何许可证。 如果是这样,我将删除代码。
这看起来像是各种各样的b树。
优点是一堆区域都可以指向相同的“字符未知”块,而不是在每个可能的Char值中需要数组中的唯一元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.