mscorlib(4.0.0.0) API with additions
CharUnicodeInfo.cs
2 using System.Security;
3 
4 namespace System.Globalization
5 {
7  [__DynamicallyInvokable]
8  public static class CharUnicodeInfo
9  {
10  [StructLayout(LayoutKind.Explicit)]
11  internal struct UnicodeDataHeader
12  {
13  [FieldOffset(0)]
14  internal char TableName;
15 
16  [FieldOffset(32)]
17  internal ushort version;
18 
19  [FieldOffset(40)]
20  internal uint OffsetToCategoriesIndex;
21 
22  [FieldOffset(44)]
23  internal uint OffsetToCategoriesValue;
24 
25  [FieldOffset(48)]
26  internal uint OffsetToNumbericIndex;
27 
28  [FieldOffset(52)]
29  internal uint OffsetToDigitValue;
30 
31  [FieldOffset(56)]
32  internal uint OffsetToNumbericValue;
33  }
34 
35  [StructLayout(LayoutKind.Sequential, Pack = 2)]
36  internal struct DigitValues
37  {
38  internal sbyte decimalDigit;
39 
40  internal sbyte digit;
41  }
42 
43  internal const char HIGH_SURROGATE_START = '\ud800';
44 
45  internal const char HIGH_SURROGATE_END = '\udbff';
46 
47  internal const char LOW_SURROGATE_START = '\udc00';
48 
49  internal const char LOW_SURROGATE_END = '\udfff';
50 
51  internal const int UNICODE_CATEGORY_OFFSET = 0;
52 
53  internal const int BIDI_CATEGORY_OFFSET = 1;
54 
55  private static bool s_initialized = InitTable();
56 
57  [SecurityCritical]
58  private unsafe static ushort* s_pCategoryLevel1Index;
59 
60  [SecurityCritical]
61  private unsafe static byte* s_pCategoriesValue;
62 
63  [SecurityCritical]
64  private unsafe static ushort* s_pNumericLevel1Index;
65 
66  [SecurityCritical]
67  private unsafe static byte* s_pNumericValues;
68 
69  [SecurityCritical]
70  private unsafe static DigitValues* s_pDigitValues;
71 
72  internal const string UNICODE_INFO_FILE_NAME = "charinfo.nlp";
73 
74  internal const int UNICODE_PLANE01_START = 65536;
75 
76  [SecuritySafeCritical]
77  private unsafe static bool InitTable()
78  {
79  byte* globalizationResourceBytePtr = GlobalizationAssembly.GetGlobalizationResourceBytePtr(typeof(CharUnicodeInfo).Assembly, "charinfo.nlp");
80  UnicodeDataHeader* ptr = (UnicodeDataHeader*)globalizationResourceBytePtr;
81  s_pCategoryLevel1Index = (ushort*)(globalizationResourceBytePtr + ptr->OffsetToCategoriesIndex);
82  s_pCategoriesValue = globalizationResourceBytePtr + ptr->OffsetToCategoriesValue;
83  s_pNumericLevel1Index = (ushort*)(globalizationResourceBytePtr + ptr->OffsetToNumbericIndex);
84  s_pNumericValues = globalizationResourceBytePtr + ptr->OffsetToNumbericValue;
85  s_pDigitValues = (DigitValues*)(globalizationResourceBytePtr + ptr->OffsetToDigitValue);
86  return true;
87  }
88 
89  internal static int InternalConvertToUtf32(string s, int index)
90  {
91  if (index < s.Length - 1)
92  {
93  int num = s[index] - 55296;
94  if (num >= 0 && num <= 1023)
95  {
96  int num2 = s[index + 1] - 56320;
97  if (num2 >= 0 && num2 <= 1023)
98  {
99  return num * 1024 + num2 + 65536;
100  }
101  }
102  }
103  return s[index];
104  }
105 
106  internal static int InternalConvertToUtf32(string s, int index, out int charLength)
107  {
108  charLength = 1;
109  if (index < s.Length - 1)
110  {
111  int num = s[index] - 55296;
112  if (num >= 0 && num <= 1023)
113  {
114  int num2 = s[index + 1] - 56320;
115  if (num2 >= 0 && num2 <= 1023)
116  {
117  charLength++;
118  return num * 1024 + num2 + 65536;
119  }
120  }
121  }
122  return s[index];
123  }
124 
125  internal static bool IsWhiteSpace(string s, int index)
126  {
127  switch (GetUnicodeCategory(s, index))
128  {
129  case UnicodeCategory.SpaceSeparator:
130  case UnicodeCategory.LineSeparator:
131  case UnicodeCategory.ParagraphSeparator:
132  return true;
133  default:
134  return false;
135  }
136  }
137 
138  internal static bool IsWhiteSpace(char c)
139  {
140  switch (GetUnicodeCategory(c))
141  {
142  case UnicodeCategory.SpaceSeparator:
143  case UnicodeCategory.LineSeparator:
144  case UnicodeCategory.ParagraphSeparator:
145  return true;
146  default:
147  return false;
148  }
149  }
150 
151  [SecuritySafeCritical]
152  internal unsafe static double InternalGetNumericValue(int ch)
153  {
154  ushort num = s_pNumericLevel1Index[ch >> 8];
155  num = s_pNumericLevel1Index[num + ((ch >> 4) & 0xF)];
156  byte* ptr = (byte*)(s_pNumericLevel1Index + (int)num);
157  byte* ptr2 = s_pNumericValues + ptr[ch & 0xF] * 8;
158  if ((long)ptr2 % 8 != 0L)
159  {
160  double result = default(double);
161  byte* dest = (byte*)(&result);
162  Buffer.Memcpy(dest, ptr2, 8);
163  return result;
164  }
165  return *(double*)(s_pNumericValues + (long)(int)ptr[ch & 0xF] * 8L);
166  }
167 
168  [SecuritySafeCritical]
169  internal unsafe static DigitValues* InternalGetDigitValues(int ch)
170  {
171  ushort num = s_pNumericLevel1Index[ch >> 8];
172  num = s_pNumericLevel1Index[num + ((ch >> 4) & 0xF)];
173  byte* ptr = (byte*)(s_pNumericLevel1Index + (int)num);
174  return s_pDigitValues + (int)ptr[ch & 0xF];
175  }
176 
177  [SecuritySafeCritical]
178  internal unsafe static sbyte InternalGetDecimalDigitValue(int ch)
179  {
180  return InternalGetDigitValues(ch)->decimalDigit;
181  }
182 
183  [SecuritySafeCritical]
184  internal unsafe static sbyte InternalGetDigitValue(int ch)
185  {
186  return InternalGetDigitValues(ch)->digit;
187  }
188 
192  [__DynamicallyInvokable]
193  public static double GetNumericValue(char ch)
194  {
195  return InternalGetNumericValue(ch);
196  }
197 
206  [__DynamicallyInvokable]
207  public static double GetNumericValue(string s, int index)
208  {
209  if (s == null)
210  {
211  throw new ArgumentNullException("s");
212  }
213  if (index < 0 || index >= s.Length)
214  {
215  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
216  }
217  return InternalGetNumericValue(InternalConvertToUtf32(s, index));
218  }
219 
223  public static int GetDecimalDigitValue(char ch)
224  {
225  return InternalGetDecimalDigitValue(ch);
226  }
227 
236  public static int GetDecimalDigitValue(string s, int index)
237  {
238  if (s == null)
239  {
240  throw new ArgumentNullException("s");
241  }
242  if (index < 0 || index >= s.Length)
243  {
244  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
245  }
246  return InternalGetDecimalDigitValue(InternalConvertToUtf32(s, index));
247  }
248 
252  public static int GetDigitValue(char ch)
253  {
254  return InternalGetDigitValue(ch);
255  }
256 
265  public static int GetDigitValue(string s, int index)
266  {
267  if (s == null)
268  {
269  throw new ArgumentNullException("s");
270  }
271  if (index < 0 || index >= s.Length)
272  {
273  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
274  }
275  return InternalGetDigitValue(InternalConvertToUtf32(s, index));
276  }
277 
281  [__DynamicallyInvokable]
282  public static UnicodeCategory GetUnicodeCategory(char ch)
283  {
284  return InternalGetUnicodeCategory(ch);
285  }
286 
295  [__DynamicallyInvokable]
296  public static UnicodeCategory GetUnicodeCategory(string s, int index)
297  {
298  if (s == null)
299  {
300  throw new ArgumentNullException("s");
301  }
302  if ((uint)index >= (uint)s.Length)
303  {
304  throw new ArgumentOutOfRangeException("index");
305  }
306  return InternalGetUnicodeCategory(s, index);
307  }
308 
309  internal static UnicodeCategory InternalGetUnicodeCategory(int ch)
310  {
311  return (UnicodeCategory)InternalGetCategoryValue(ch, 0);
312  }
313 
314  [SecuritySafeCritical]
315  internal unsafe static byte InternalGetCategoryValue(int ch, int offset)
316  {
317  ushort num = s_pCategoryLevel1Index[ch >> 8];
318  num = s_pCategoryLevel1Index[num + ((ch >> 4) & 0xF)];
319  byte* ptr = (byte*)(s_pCategoryLevel1Index + (int)num);
320  byte b = ptr[ch & 0xF];
321  return s_pCategoriesValue[b * 2 + offset];
322  }
323 
324  internal static BidiCategory GetBidiCategory(string s, int index)
325  {
326  if (s == null)
327  {
328  throw new ArgumentNullException("s");
329  }
330  if ((uint)index >= (uint)s.Length)
331  {
332  throw new ArgumentOutOfRangeException("index");
333  }
334  return (BidiCategory)InternalGetCategoryValue(InternalConvertToUtf32(s, index), 1);
335  }
336 
337  internal static UnicodeCategory InternalGetUnicodeCategory(string value, int index)
338  {
339  return InternalGetUnicodeCategory(InternalConvertToUtf32(value, index));
340  }
341 
342  internal static UnicodeCategory InternalGetUnicodeCategory(string str, int index, out int charLength)
343  {
344  return InternalGetUnicodeCategory(InternalConvertToUtf32(str, index, out charLength));
345  }
346 
347  internal static bool IsCombiningCategory(UnicodeCategory uc)
348  {
349  if (uc != UnicodeCategory.NonSpacingMark && uc != UnicodeCategory.SpacingCombiningMark)
350  {
351  return uc == UnicodeCategory.EnclosingMark;
352  }
353  return true;
354  }
355  }
356 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static int GetDigitValue(string s, int index)
Gets the digit value of the numeric character at the specified index of the specified string.
LayoutKind
Controls the layout of an object when exported to unmanaged code.
Definition: LayoutKind.cs:7
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
static UnicodeCategory GetUnicodeCategory(string s, int index)
Gets the Unicode category of the character at the specified index of the specified string.
static int GetDecimalDigitValue(char ch)
Gets the decimal digit value of the specified numeric character.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static UnicodeCategory GetUnicodeCategory(char ch)
Gets the Unicode category of the specified character.
static int GetDecimalDigitValue(string s, int index)
Gets the decimal digit value of the numeric character at the specified index of the specified string.
static int GetDigitValue(char ch)
Gets the digit value of the specified numeric character.
Attribute can be applied to an assembly.
Retrieves information about a Unicode character. This class cannot be inherited.
UnicodeCategory
Defines the Unicode category of a character.
static double GetNumericValue(char ch)
Gets the numeric value associated with the specified character.
static double GetNumericValue(string s, int index)
Gets the numeric value associated with the character at the specified index of the specified string.
Manipulates arrays of primitive types.
Definition: Buffer.cs:11