diff options
Diffstat (limited to 'src/mscorlib/src/System/Globalization/CharUnicodeInfo.cs')
-rw-r--r-- | src/mscorlib/src/System/Globalization/CharUnicodeInfo.cs | 173 |
1 files changed, 104 insertions, 69 deletions
diff --git a/src/mscorlib/src/System/Globalization/CharUnicodeInfo.cs b/src/mscorlib/src/System/Globalization/CharUnicodeInfo.cs index 2822b418ef..860b847d36 100644 --- a/src/mscorlib/src/System/Globalization/CharUnicodeInfo.cs +++ b/src/mscorlib/src/System/Globalization/CharUnicodeInfo.cs @@ -12,8 +12,8 @@ // //////////////////////////////////////////////////////////////////////////// -namespace System.Globalization { - +namespace System.Globalization +{ //This class has only static members and therefore doesn't need to be serialized. using System; @@ -36,34 +36,34 @@ namespace System.Globalization { // // Native methods to access the Unicode category data tables in charinfo.nlp. // - internal const char HIGH_SURROGATE_START = '\ud800'; - internal const char HIGH_SURROGATE_END = '\udbff'; - internal const char LOW_SURROGATE_START = '\udc00'; - internal const char LOW_SURROGATE_END = '\udfff'; + internal const char HIGH_SURROGATE_START = '\ud800'; + internal const char HIGH_SURROGATE_END = '\udbff'; + internal const char LOW_SURROGATE_START = '\udc00'; + internal const char LOW_SURROGATE_END = '\udfff'; internal const int UNICODE_CATEGORY_OFFSET = 0; internal const int BIDI_CATEGORY_OFFSET = 1; - static bool s_initialized = InitTable(); + private static bool s_initialized = InitTable(); // The native pointer to the 12:4:4 index table of the Unicode cateogry data. - unsafe static ushort* s_pCategoryLevel1Index; - unsafe static byte* s_pCategoriesValue; + private unsafe static ushort* s_pCategoryLevel1Index; + private unsafe static byte* s_pCategoriesValue; // The native pointer to the 12:4:4 index table of the Unicode numeric data. // The value of this index table is an index into the real value table stored in s_pNumericValues. - unsafe static ushort* s_pNumericLevel1Index; + private unsafe static ushort* s_pNumericLevel1Index; // The numeric value table, which is indexed by s_pNumericLevel1Index. // Every item contains the value for numeric value. // unsafe static double* s_pNumericValues; // To get around the IA64 alignment issue. Our double data is aligned in 8-byte boundary, but loader loads the embeded table starting // at 4-byte boundary. This cause a alignment issue since double is 8-byte. - unsafe static byte* s_pNumericValues; + private unsafe static byte* s_pNumericValues; // The digit value table, which is indexed by s_pNumericLevel1Index. It shares the same indice as s_pNumericValues. // Every item contains the value for decimal digit/digit value. - unsafe static DigitValues* s_pDigitValues; + private unsafe static DigitValues* s_pDigitValues; internal const String UNICODE_INFO_FILE_NAME = "charinfo.nlp"; // The starting codepoint for Unicode plane 1. Plane 1 contains 0x010000 ~ 0x01ffff. @@ -75,7 +75,8 @@ namespace System.Globalization { // // Excplicit layout is used here since a syntax like char[16] can not be used in sequential layout. [StructLayout(LayoutKind.Explicit)] - internal unsafe struct UnicodeDataHeader { + internal unsafe struct UnicodeDataHeader + { [FieldOffset(0)] internal char TableName; // WCHAR[16] [FieldOffset(0x20)] @@ -90,14 +91,14 @@ namespace System.Globalization { internal uint OffsetToDigitValue; // DWORD [FieldOffset(0x38)] internal uint OffsetToNumbericValue; // DWORD - } // NOTE: It's important to specify pack size here, since the size of the structure is 2 bytes. Otherwise, // the default pack size will be 4. - [StructLayout(LayoutKind.Sequential, Pack=2)] - internal struct DigitValues { + [StructLayout(LayoutKind.Sequential, Pack = 2)] + internal struct DigitValues + { internal sbyte decimalDigit; internal sbyte digit; } @@ -107,19 +108,19 @@ namespace System.Globalization { //use. We allocate this once in the class initializer and then we don't need to worry //about it again. // - unsafe static bool InitTable() { - + private unsafe static bool InitTable() + { // Go to native side and get pointer to the native table - byte * pDataTable = GlobalizationAssembly.GetGlobalizationResourceBytePtr(typeof(CharUnicodeInfo).Assembly, UNICODE_INFO_FILE_NAME); + byte* pDataTable = GlobalizationAssembly.GetGlobalizationResourceBytePtr(typeof(CharUnicodeInfo).Assembly, UNICODE_INFO_FILE_NAME); UnicodeDataHeader* mainHeader = (UnicodeDataHeader*)pDataTable; // Set up the native pointer to different part of the tables. - s_pCategoryLevel1Index = (ushort*) (pDataTable + mainHeader->OffsetToCategoriesIndex); - s_pCategoriesValue = (byte*) (pDataTable + mainHeader->OffsetToCategoriesValue); - s_pNumericLevel1Index = (ushort*) (pDataTable + mainHeader->OffsetToNumbericIndex); - s_pNumericValues = (byte*) (pDataTable + mainHeader->OffsetToNumbericValue); - s_pDigitValues = (DigitValues*) (pDataTable + mainHeader->OffsetToDigitValue); + s_pCategoryLevel1Index = (ushort*)(pDataTable + mainHeader->OffsetToCategoriesIndex); + s_pCategoriesValue = (byte*)(pDataTable + mainHeader->OffsetToCategoriesValue); + s_pNumericLevel1Index = (ushort*)(pDataTable + mainHeader->OffsetToNumbericIndex); + s_pNumericValues = (byte*)(pDataTable + mainHeader->OffsetToNumbericValue); + s_pDigitValues = (DigitValues*)(pDataTable + mainHeader->OffsetToDigitValue); return true; } @@ -137,14 +138,18 @@ namespace System.Globalization { // //////////////////////////////////////////////////////////////////////// - internal static int InternalConvertToUtf32(String s, int index) { + internal static int InternalConvertToUtf32(String s, int index) + { Debug.Assert(s != null, "s != null"); Debug.Assert(index >= 0 && index < s.Length, "index < s.Length"); - if (index < s.Length - 1) { + if (index < s.Length - 1) + { int temp1 = (int)s[index] - HIGH_SURROGATE_START; - if (temp1 >= 0 && temp1 <= 0x3ff) { - int temp2 = (int)s[index+1] - LOW_SURROGATE_START; - if (temp2 >= 0 && temp2 <= 0x3ff) { + if (temp1 >= 0 && temp1 <= 0x3ff) + { + int temp2 = (int)s[index + 1] - LOW_SURROGATE_START; + if (temp2 >= 0 && temp2 <= 0x3ff) + { // Convert the surrogate to UTF32 and get the result. return ((temp1 * 0x400) + temp2 + UNICODE_PLANE01_START); } @@ -175,16 +180,20 @@ namespace System.Globalization { // //////////////////////////////////////////////////////////////////////// - internal static int InternalConvertToUtf32(String s, int index, out int charLength) { + internal static int InternalConvertToUtf32(String s, int index, out int charLength) + { Debug.Assert(s != null, "s != null"); Debug.Assert(s.Length > 0, "s.Length > 0"); Debug.Assert(index >= 0 && index < s.Length, "index >= 0 && index < s.Length"); charLength = 1; - if (index < s.Length - 1) { + if (index < s.Length - 1) + { int temp1 = (int)s[index] - HIGH_SURROGATE_START; - if (temp1 >= 0 && temp1 <= 0x3ff) { - int temp2 = (int)s[index+1] - LOW_SURROGATE_START; - if (temp2 >= 0 && temp2 <= 0x3ff) { + if (temp1 >= 0 && temp1 <= 0x3ff) + { + int temp2 = (int)s[index + 1] - LOW_SURROGATE_START; + if (temp2 >= 0 && temp2 <= 0x3ff) + { // Convert the surrogate to UTF32 and get the result. charLength++; return ((temp1 * 0x400) + temp2 + UNICODE_PLANE01_START); @@ -210,7 +219,8 @@ namespace System.Globalization { UnicodeCategory uc = GetUnicodeCategory(s, index); // In Unicode 3.0, U+2028 is the only character which is under the category "LineSeparator". // And U+2029 is th eonly character which is under the category "ParagraphSeparator". - switch (uc) { + switch (uc) + { case (UnicodeCategory.SpaceSeparator): case (UnicodeCategory.LineSeparator): case (UnicodeCategory.ParagraphSeparator): @@ -225,7 +235,8 @@ namespace System.Globalization { UnicodeCategory uc = GetUnicodeCategory(c); // In Unicode 3.0, U+2028 is the only character which is under the category "LineSeparator". // And U+2029 is th eonly character which is under the category "ParagraphSeparator". - switch (uc) { + switch (uc) + { case (UnicodeCategory.SpaceSeparator): case (UnicodeCategory.LineSeparator): case (UnicodeCategory.ParagraphSeparator): @@ -240,7 +251,8 @@ namespace System.Globalization { // // Note that for ch in the range D800-DFFF we just treat it as any other non-numeric character // - internal unsafe static double InternalGetNumericValue(int ch) { + internal unsafe static double InternalGetNumericValue(int ch) + { Debug.Assert(ch >= 0 && ch <= 0x10ffff, "ch is not in valid Unicode range."); // Get the level 2 item from the highest 12 bit (8 - 19) of ch. ushort index = s_pNumericLevel1Index[ch >> 8]; @@ -254,7 +266,8 @@ namespace System.Globalization { // To get around the IA64 alignment issue. Our double data is aligned in 8-byte boundary, but loader loads the embeded table starting // at 4-byte boundary. This cause a alignment issue since double is 8-byte. byte* pSourcePtr = &(s_pNumericValues[pBytePtr[(ch & 0x000f)] * sizeof(double)]); - if (((long)pSourcePtr % 8) != 0) { + if (((long)pSourcePtr % 8) != 0) + { // We are not aligned in 8-byte boundary. Do a copy. double ret; byte* retPtr = (byte*)&ret; @@ -272,7 +285,8 @@ namespace System.Globalization { // // Note that for ch in the range D800-DFFF we just treat it as any other non-numeric character // - internal unsafe static DigitValues* InternalGetDigitValues(int ch) { + internal unsafe static DigitValues* InternalGetDigitValues(int ch) + { Debug.Assert(ch >= 0 && ch <= 0x10ffff, "ch is not in valid Unicode range."); // Get the level 2 item from the highest 12 bit (8 - 19) of ch. ushort index = s_pNumericLevel1Index[ch >> 8]; @@ -285,11 +299,13 @@ namespace System.Globalization { return &(s_pDigitValues[pBytePtr[(ch & 0x000f)]]); } - internal unsafe static sbyte InternalGetDecimalDigitValue(int ch) { + internal unsafe static sbyte InternalGetDecimalDigitValue(int ch) + { return (InternalGetDigitValues(ch)->decimalDigit); } - internal unsafe static sbyte InternalGetDigitValue(int ch) { + internal unsafe static sbyte InternalGetDigitValue(int ch) + { return (InternalGetDigitValues(ch)->digit); } @@ -310,21 +326,24 @@ namespace System.Globalization { //////////////////////////////////////////////////////////////////////// - public static double GetNumericValue(char ch) { + public static double GetNumericValue(char ch) + { return (InternalGetNumericValue(ch)); } - public static double GetNumericValue(String s, int index) { - if (s == null) { + public static double GetNumericValue(String s, int index) + { + if (s == null) + { throw new ArgumentNullException(nameof(s)); } - if (index < 0 || index >= s.Length) { + if (index < 0 || index >= s.Length) + { throw new ArgumentOutOfRangeException(nameof(index), Environment.GetResourceString("ArgumentOutOfRange_Index")); } Contract.EndContractBlock(); return (InternalGetNumericValue(InternalConvertToUtf32(s, index))); - } //////////////////////////////////////////////////////////////////////// @@ -345,16 +364,20 @@ namespace System.Globalization { //////////////////////////////////////////////////////////////////////// - public static int GetDecimalDigitValue(char ch) { + public static int GetDecimalDigitValue(char ch) + { return (InternalGetDecimalDigitValue(ch)); } - public static int GetDecimalDigitValue(String s, int index) { - if (s == null) { + public static int GetDecimalDigitValue(String s, int index) + { + if (s == null) + { throw new ArgumentNullException(nameof(s)); } - if (index < 0 || index >= s.Length) { + if (index < 0 || index >= s.Length) + { throw new ArgumentOutOfRangeException(nameof(index), Environment.GetResourceString("ArgumentOutOfRange_Index")); } Contract.EndContractBlock(); @@ -382,16 +405,20 @@ namespace System.Globalization { //////////////////////////////////////////////////////////////////////// - public static int GetDigitValue(char ch) { + public static int GetDigitValue(char ch) + { return (InternalGetDigitValue(ch)); } - public static int GetDigitValue(String s, int index) { - if (s == null) { + public static int GetDigitValue(String s, int index) + { + if (s == null) + { throw new ArgumentNullException(nameof(s)); } - if (index < 0 || index >= s.Length) { + if (index < 0 || index >= s.Length) + { throw new ArgumentOutOfRangeException(nameof(index), Environment.GetResourceString("ArgumentOutOfRange_Index")); } Contract.EndContractBlock(); @@ -400,21 +427,23 @@ namespace System.Globalization { public static UnicodeCategory GetUnicodeCategory(char ch) { - return (InternalGetUnicodeCategory(ch)) ; + return (InternalGetUnicodeCategory(ch)); } public static UnicodeCategory GetUnicodeCategory(String s, int index) { - if (s==null) + if (s == null) throw new ArgumentNullException(nameof(s)); - if (((uint)index)>=((uint)s.Length)) { + if (((uint)index) >= ((uint)s.Length)) + { throw new ArgumentOutOfRangeException(nameof(index)); } Contract.EndContractBlock(); return InternalGetUnicodeCategory(s, index); } - internal unsafe static UnicodeCategory InternalGetUnicodeCategory(int ch) { + internal unsafe static UnicodeCategory InternalGetUnicodeCategory(int ch) + { return ((UnicodeCategory)InternalGetCategoryValue(ch, UNICODE_CATEGORY_OFFSET)); } @@ -432,7 +461,8 @@ namespace System.Globalization { // //////////////////////////////////////////////////////////////////////// - internal unsafe static byte InternalGetCategoryValue(int ch, int offset) { + internal unsafe static byte InternalGetCategoryValue(int ch, int offset) + { Debug.Assert(ch >= 0 && ch <= 0x10ffff, "ch is not in valid Unicode range."); // Get the level 2 item from the highest 12 bit (8 - 19) of ch. ushort index = s_pCategoryLevel1Index[ch >> 8]; @@ -451,14 +481,16 @@ namespace System.Globalization { return (uc); } -// internal static BidiCategory GetBidiCategory(char ch) { -// return ((BidiCategory)InternalGetCategoryValue(c, BIDI_CATEGORY_OFFSET)); -// } + // internal static BidiCategory GetBidiCategory(char ch) { + // return ((BidiCategory)InternalGetCategoryValue(c, BIDI_CATEGORY_OFFSET)); + // } - internal static BidiCategory GetBidiCategory(String s, int index) { - if (s==null) + internal static BidiCategory GetBidiCategory(String s, int index) + { + if (s == null) throw new ArgumentNullException(nameof(s)); - if (((uint)index)>=((uint)s.Length)) { + if (((uint)index) >= ((uint)s.Length)) + { throw new ArgumentOutOfRangeException(nameof(index)); } Contract.EndContractBlock(); @@ -478,7 +510,8 @@ namespace System.Globalization { // //////////////////////////////////////////////////////////////////////// - internal static UnicodeCategory InternalGetUnicodeCategory(String value, int index) { + internal static UnicodeCategory InternalGetUnicodeCategory(String value, int index) + { Debug.Assert(value != null, "value can not be null"); Debug.Assert(index < value.Length, "index < value.Length"); @@ -492,15 +525,17 @@ namespace System.Globalization { // //////////////////////////////////////////////////////////////////////// - internal static UnicodeCategory InternalGetUnicodeCategory(String str, int index, out int charLength) { + internal static UnicodeCategory InternalGetUnicodeCategory(String str, int index, out int charLength) + { Debug.Assert(str != null, "str can not be null"); - Debug.Assert(str.Length > 0, "str.Length > 0");; + Debug.Assert(str.Length > 0, "str.Length > 0"); ; Debug.Assert(index >= 0 && index < str.Length, "index >= 0 && index < str.Length"); return (InternalGetUnicodeCategory(InternalConvertToUtf32(str, index, out charLength))); } - internal static bool IsCombiningCategory(UnicodeCategory uc) { + internal static bool IsCombiningCategory(UnicodeCategory uc) + { Debug.Assert(uc >= 0, "uc >= 0"); return ( uc == UnicodeCategory.NonSpacingMark || |