We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- ////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////
- namespace System.Globalization
- {
-
-
-
- using System;
- using System.Threading;
- using System.Runtime.InteropServices;
- using System.Runtime.CompilerServices;
- using System.Reflection;
-
-
-
-
- public sealed class CharUnicodeInfo : object
- {
-
-
-
-
-
-
-
- internal const char HIGH_SURROGATE_START = '�';
- internal const char HIGH_SURROGATE_END = '�';
- internal const char LOW_SURROGATE_START = '�';
- internal const char LOW_SURROGATE_END = '�';
-
- internal const int UNICODE_CATEGORY_OFFSET = 0;
- internal const int BIDI_CATEGORY_OFFSET = 1;
-
-
- unsafe static byte* m_pDataTable;
-
-
- unsafe static ushort* m_pCategoryLevel1Index;
- unsafe static byte* m_pCategoriesValue;
-
-
-
- unsafe static ushort* m_pNumericLevel1Index;
-
-
-
-
-
-
- unsafe static byte* m_pNumericValues;
-
-
-
- unsafe static DigitValues* m_pDigitValues;
-
- internal const string UNICODE_INFO_FILE_NAME = "charinfo.nlp";
-
- internal const int UNICODE_PLANE01_START = 65536;
-
-
-
-
-
-
- [StructLayout(LayoutKind.Explicit)]
- unsafe internal struct UnicodeDataHeader
- {
- [FieldOffset(0)]
- internal char TableName;
-
- [FieldOffset(32)]
- internal ushort version;
-
- [FieldOffset(40)]
- internal uint OffsetToCategoriesIndex;
-
- [FieldOffset(44)]
- internal uint OffsetToCategoriesValue;
-
- [FieldOffset(48)]
- internal uint OffsetToNumbericIndex;
-
- [FieldOffset(52)]
- internal uint OffsetToDigitValue;
-
- [FieldOffset(56)]
- internal uint OffsetToNumbericValue;
-
- }
-
-
-
-
- [StructLayout(LayoutKind.Sequential, Pack = 2)]
- internal struct DigitValues
- {
- internal sbyte decimalDigit;
- internal sbyte digit;
- }
-
-
-
-
-
-
- unsafe static CharUnicodeInfo()
- {
- m_pDataTable = GlobalizationAssembly.GetGlobalizationResourceBytePtr(typeof(CharUnicodeInfo).Assembly, UNICODE_INFO_FILE_NAME);
- UnicodeDataHeader* mainHeader = (UnicodeDataHeader*)m_pDataTable;
-
-
- m_pCategoryLevel1Index = (ushort*)(m_pDataTable + mainHeader->OffsetToCategoriesIndex);
- m_pCategoriesValue = (byte*)(m_pDataTable + mainHeader->OffsetToCategoriesValue);
- m_pNumericLevel1Index = (ushort*)(m_pDataTable + mainHeader->OffsetToNumbericIndex);
- m_pNumericValues = (byte*)(m_pDataTable + mainHeader->OffsetToNumbericValue);
- m_pDigitValues = (DigitValues*)(m_pDataTable + mainHeader->OffsetToDigitValue);
-
-
- nativeInitTable(m_pDataTable);
- }
-
-
- ////////////////////////////////////////////////////////////////////////
-
-
-
- ////////////////////////////////////////////////////////////////////////
- private CharUnicodeInfo()
- {
- }
-
-
- ////////////////////////////////////////////////////////////////////////
-
-
-
-
-
-
-
-
-
- ////////////////////////////////////////////////////////////////////////
-
- static internal int InternalConvertToUtf32(string s, int index)
- {
- BCLDebug.Assert(s != null, "s != null");
- BCLDebug.Assert(index >= 0 && index < s.Length, "index < s.Length");
- if (index < s.Length - 1) {
- int temp1 = (int)s[index] - HIGH_SURROGATE_START;
- if (temp1 >= 0 && temp1 <= 1023) {
- int temp2 = (int)s[index + 1] - LOW_SURROGATE_START;
- if (temp2 >= 0 && temp2 <= 1023) {
-
- return ((temp1 * 1024) + temp2 + UNICODE_PLANE01_START);
- }
- }
- }
- return ((int)s[index]);
- }
-
- ////////////////////////////////////////////////////////////////////////
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ////////////////////////////////////////////////////////////////////////
-
- static internal int InternalConvertToUtf32(string s, int index, out int charLength)
- {
- BCLDebug.Assert(s != null, "s != null");
- BCLDebug.Assert(s.Length > 0, "s.Length > 0");
- BCLDebug.Assert(index >= 0 && index < s.Length, "index >= 0 && index < s.Length");
- charLength = 1;
- if (index < s.Length - 1) {
- int temp1 = (int)s[index] - HIGH_SURROGATE_START;
- if (temp1 >= 0 && temp1 <= 1023) {
- int temp2 = (int)s[index + 1] - LOW_SURROGATE_START;
- if (temp2 >= 0 && temp2 <= 1023) {
-
- charLength++;
- return ((temp1 * 1024) + temp2 + UNICODE_PLANE01_START);
- }
- }
- }
- return ((int)s[index]);
- }
-
- ////////////////////////////////////////////////////////////////////////
-
-
-
-
-
- ////////////////////////////////////////////////////////////////////////
-
- static internal bool IsWhiteSpace(string s, int index)
- {
- BCLDebug.Assert(s != null, "s!=null");
- BCLDebug.Assert(index >= 0 && index < s.Length, "index >= 0 && index < s.Length");
-
- UnicodeCategory uc = GetUnicodeCategory(s, index);
-
-
- switch (uc) {
- case (UnicodeCategory.SpaceSeparator):
- case (UnicodeCategory.LineSeparator):
- case (UnicodeCategory.ParagraphSeparator):
- return (true);
- }
- return (false);
- }
-
-
- static internal bool IsWhiteSpace(char c)
- {
- UnicodeCategory uc = GetUnicodeCategory(c);
-
-
- switch (uc) {
- case (UnicodeCategory.SpaceSeparator):
- case (UnicodeCategory.LineSeparator):
- case (UnicodeCategory.ParagraphSeparator):
- return (true);
- }
-
- return (false);
- }
-
-
-
-
-
-
- unsafe static internal double InternalGetNumericValue(int ch)
- {
- BCLDebug.Assert(ch >= 0 && ch <= 1114111, "ch is not in valid Unicode range.");
-
- ushort index = m_pNumericLevel1Index[ch >> 8];
-
-
-
- index = m_pNumericLevel1Index[index + ((ch >> 4) & 15)];
- byte* pBytePtr = (byte*)&(m_pNumericLevel1Index[index]);
-
- return (((double*)m_pNumericValues)[pBytePtr[(ch & 15)]]);
- }
-
-
-
-
-
-
- unsafe static internal DigitValues* InternalGetDigitValues(int ch)
- {
- BCLDebug.Assert(ch >= 0 && ch <= 1114111, "ch is not in valid Unicode range.");
-
- ushort index = m_pNumericLevel1Index[ch >> 8];
-
-
-
- index = m_pNumericLevel1Index[index + ((ch >> 4) & 15)];
- byte* pBytePtr = (byte*)&(m_pNumericLevel1Index[index]);
-
- return &(m_pDigitValues[pBytePtr[(ch & 15)]]);
- }
-
-
- unsafe static internal sbyte InternalGetDecimalDigitValue(int ch)
- {
- return (InternalGetDigitValues(ch)->decimalDigit);
- }
-
- unsafe static internal sbyte InternalGetDigitValue(int ch)
- {
- return (InternalGetDigitValues(ch)->digit);
- }
-
-
- ////////////////////////////////////////////////////////////////////////
-
-
-
-
-
-
-
-
-
-
-
-
- ////////////////////////////////////////////////////////////////////////
-
-
- public static double GetNumericValue(char ch)
- {
- return (InternalGetNumericValue(ch));
- }
-
-
- public static double GetNumericValue(string s, int index)
- {
- if (s == null) {
- throw new ArgumentNullException("s");
- }
- if (index < 0 || index >= s.Length) {
- throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
- }
- return (InternalGetNumericValue(InternalConvertToUtf32(s, index)));
-
- }
-
- ////////////////////////////////////////////////////////////////////////
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ////////////////////////////////////////////////////////////////////////
-
-
- public static int GetDecimalDigitValue(char ch)
- {
- return (InternalGetDecimalDigitValue(ch));
- }
-
-
- public static int GetDecimalDigitValue(string s, int index)
- {
- if (s == null) {
- throw new ArgumentNullException("s");
- }
- if (index < 0 || index >= s.Length) {
- throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
- }
-
- return (InternalGetDecimalDigitValue(InternalConvertToUtf32(s, index)));
- }
-
- ////////////////////////////////////////////////////////////////////////
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ////////////////////////////////////////////////////////////////////////
-
-
- public static int GetDigitValue(char ch)
- {
- return (InternalGetDigitValue(ch));
- }
-
-
- public static int GetDigitValue(string s, int index)
- {
- if (s == null) {
- throw new ArgumentNullException("s");
- }
- if (index < 0 || index >= s.Length) {
- throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
- }
- return (InternalGetDigitValue(InternalConvertToUtf32(s, index)));
- }
-
- public static UnicodeCategory GetUnicodeCategory(char ch)
- {
- return (InternalGetUnicodeCategory(ch));
- }
-
- public static UnicodeCategory GetUnicodeCategory(string s, int index)
- {
- if (s == null)
- throw new ArgumentNullException("s");
- if (((uint)index) >= ((uint)s.Length)) {
- throw new ArgumentOutOfRangeException("index");
- }
- return InternalGetUnicodeCategory(s, index);
- }
-
- unsafe static internal UnicodeCategory InternalGetUnicodeCategory(int ch)
- {
- return ((UnicodeCategory)InternalGetCategoryValue(ch, UNICODE_CATEGORY_OFFSET));
- }
-
- ////////////////////////////////////////////////////////////////////////
-
-
-
-
-
-
-
-
-
-
-
- ////////////////////////////////////////////////////////////////////////
-
- unsafe static internal byte InternalGetCategoryValue(int ch, int offset)
- {
- BCLDebug.Assert(ch >= 0 && ch <= 1114111, "ch is not in valid Unicode range.");
-
- ushort index = m_pCategoryLevel1Index[ch >> 8];
-
-
- index = m_pCategoryLevel1Index[index + ((ch >> 4) & 15)];
- byte* pBytePtr = (byte*)&(m_pCategoryLevel1Index[index]);
-
- byte valueIndex = pBytePtr[(ch & 15)];
- byte uc = m_pCategoriesValue[valueIndex * 2 + offset];
-
-
-
-
-
- return (uc);
- }
-
-
-
-
-
- static internal BidiCategory GetBidiCategory(string s, int index)
- {
- if (s == null)
- throw new ArgumentNullException("s");
- if (((uint)index) >= ((uint)s.Length)) {
- throw new ArgumentOutOfRangeException("index");
- }
- return ((BidiCategory)InternalGetCategoryValue(InternalConvertToUtf32(s, index), BIDI_CATEGORY_OFFSET));
- }
-
- ////////////////////////////////////////////////////////////////////////
-
-
-
-
-
-
-
-
-
-
- ////////////////////////////////////////////////////////////////////////
-
- static internal UnicodeCategory InternalGetUnicodeCategory(string value, int index)
- {
- BCLDebug.Assert(value != null, "value can not be null");
- BCLDebug.Assert(index < value.Length, "index < value.Length");
-
- return (InternalGetUnicodeCategory(InternalConvertToUtf32(value, index)));
- }
-
- ////////////////////////////////////////////////////////////////////////
-
-
-
-
- ////////////////////////////////////////////////////////////////////////
-
- static internal UnicodeCategory InternalGetUnicodeCategory(string str, int index, out int charLength)
- {
- BCLDebug.Assert(str != null, "str can not be null");
- BCLDebug.Assert(str.Length > 0, "str.Length > 0");
- ;
- BCLDebug.Assert(index >= 0 && index < str.Length, "index >= 0 && index < str.Length");
-
- return (InternalGetUnicodeCategory(InternalConvertToUtf32(str, index, out charLength)));
- }
-
- static internal bool IsCombiningCategory(UnicodeCategory uc)
- {
- BCLDebug.Assert(uc >= 0, "uc >= 0");
- return (uc == UnicodeCategory.NonSpacingMark || uc == UnicodeCategory.SpacingCombiningMark || uc == UnicodeCategory.EnclosingMark);
- }
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- unsafe private static extern void nativeInitTable(byte* bytePtr);
-
- }
- }