116 // Convert the BMP character or surrogate pointed by index to a UTF32 value.
117 // This is similar to Char.ConvertToUTF32, but the difference is that
118 // it does not throw exceptions when invalid surrogate characters are passed in.
120 // WARNING: since it doesn't throw an exception it CAN return a value
121 // in the surrogate range D800-DFFF, which are not legal unicode values.
125 internal static int InternalConvertToUtf32(String s, int index) {
126 Contract.Assert(s != null, "s != null");
127 Contract.Assert(index >= 0 && index < s.Length, "index < s.Length");
128 if (index < s.Length - 1) {
129 int temp1 = (int)s[index] - HIGH_SURROGATE_START;
130 if (temp1 >= 0 && temp1 <= 0x3ff) {
131 int temp2 = (int)s[index + 1] - LOW_SURROGATE_START;
132 if (temp2 >= 0 && temp2 <= 0x3ff) {
133 // Convert the surrogate to UTF32 and get the result.
134 return ((temp1 * 0x400) + temp2 + UNICODE_PLANE01_START);
138 return ((int)s[index]);
143 // Convert a character or a surrogate pair starting at index of string s
148 // index The starting index. It can point to a BMP character or
150 // len The length of the string.
151 // charLength [out] If the index points to a BMP char, charLength
152 // will be 1. If the index points to a surrogate pair,
153 // charLength will be 2.
155 // WARNING: since it doesn't throw an exception it CAN return a value
156 // in the surrogate range D800-DFFF, which are not legal unicode values.
163 internal static int InternalConvertToUtf32(String s, int index, out int charLength) {
164 Contract.Assert(s != null, "s != null");
165 Contract.Assert(s.Length > 0, "s.Length > 0");
166 Contract.Assert(index >= 0 && index < s.Length, "index >= 0 && index < s.Length");
168 if (index < s.Length - 1) {
169 int temp1 = (int)s[index] - HIGH_SURROGATE_START;
170 if (temp1 >= 0 && temp1 <= 0x3ff) {
171 int temp2 = (int)s[index + 1] - LOW_SURROGATE_START;
172 if (temp2 >= 0 && temp2 <= 0x3ff) {
173 // Convert the surrogate to UTF32 and get the result.
175 return ((temp1 * 0x400) + temp2 + UNICODE_PLANE01_START);
179 return ((int)s[index]);
186 // Determines if the given character is a white space character.
190 internal static bool IsWhiteSpace(String s, int index)
192 Contract.Assert(s != null, "s!=null");
193 Contract.Assert(index >= 0 && index < s.Length, "index >= 0 && index < s.Length");
195 UnicodeCategory uc = GetUnicodeCategory(s, index);
196 // In Unicode 3.0, U+2028 is the only character which is under the category "LineSeparator".
197 // And U+2029 is th eonly character which is under the category "ParagraphSeparator".
199 case (UnicodeCategory.SpaceSeparator):
200 case (UnicodeCategory.LineSeparator):
201 case (UnicodeCategory.ParagraphSeparator):
208 internal static bool IsWhiteSpace(char c)
210 UnicodeCategory uc = GetUnicodeCategory(c);
211 // In Unicode 3.0, U+2028 is the only character which is under the category "LineSeparator".
212 // And U+2029 is th eonly character which is under the category "ParagraphSeparator".
214 case (UnicodeCategory.SpaceSeparator):
215 case (UnicodeCategory.LineSeparator):
216 case (UnicodeCategory.ParagraphSeparator):
224 // This is called by the public char and string, index versions
226 // Note that for ch in the range D800-DFFF we just treat it as any other non-numeric character
228 [System.Security.SecuritySafeCritical] // auto-generated
229 internal unsafe static double InternalGetNumericValue(int ch) {
230 Contract.Assert(ch >= 0 && ch <= 0x10ffff, "ch is not in valid Unicode range.");
231 // Get the level 2 item from the highest 12 bit (8 - 19) of ch.
232 ushort index = s_pNumericLevel1Index[ch >> 8];
233 // Get the level 2 WORD offset from the 4 - 7 bit of ch. This provides the base offset of the level 3 table.
234 // The offset is referred to an float item in m_pNumericFloatData.
235 // Note that & has the lower precedence than addition, so don't forget the parathesis.
236 index = s_pNumericLevel1Index[index + ((ch >> 4) & 0x000f)];
237 byte* pBytePtr = (byte*)&(s_pNumericLevel1Index[index]);
238 // Get the result from the 0 -3 bit of ch.
240 // To get around the IA64 alignment issue. Our double data is aligned in 8-byte boundary, but loader loads the embeded table starting
241 // at 4-byte boundary. This cause a alignment issue since double is 8-byte.
242 byte* pSourcePtr = &(s_pNumericValues[pBytePtr[(ch & 0x000f)] * sizeof(double)]);
243 if (((long)pSourcePtr % 8) != 0) {
244 // We are not aligned in 8-byte boundary. Do a copy.
246 byte* retPtr = (byte*)&ret;
247 Buffer.Memcpy(retPtr, pSourcePtr, sizeof(double));
250 return (((double*)s_pNumericValues)[pBytePtr[(ch & 0x000f)]]);
252 return (((double*)s_pNumericValues)[pBytePtr[(ch & 0x000f)]]);
257 // This is called by the public char and string, index versions
259 // Note that for ch in the range D800-DFFF we just treat it as any other non-numeric character
261 [System.Security.SecuritySafeCritical] // auto-generated
262 internal unsafe static DigitValues* InternalGetDigitValues(int ch) {
263 Contract.Assert(ch >= 0 && ch <= 0x10ffff, "ch is not in valid Unicode range.");
264 // Get the level 2 item from the highest 12 bit (8 - 19) of ch.
265 ushort index = s_pNumericLevel1Index[ch >> 8];
266 // Get the level 2 WORD offset from the 4 - 7 bit of ch. This provides the base offset of the level 3 table.
267 // The offset is referred to an float item in m_pNumericFloatData.
268 // Note that & has the lower precedence than addition, so don't forget the parathesis.
269 index = s_pNumericLevel1Index[index + ((ch >> 4) & 0x000f)];
270 byte* pBytePtr = (byte*)&(s_pNumericLevel1Index[index]);
271 // Get the result from the 0 -3 bit of ch.
272 return &(s_pDigitValues[pBytePtr[(ch & 0x000f)]]);
275 [System.Security.SecuritySafeCritical] // auto-generated
276 internal unsafe static sbyte InternalGetDecimalDigitValue(int ch) {
277 return (InternalGetDigitValues(ch)->decimalDigit);
280 [System.Security.SecuritySafeCritical] // auto-generated
281 internal unsafe static sbyte InternalGetDigitValue(int ch) {
282 return (InternalGetDigitValues(ch)->digit);
288 //Returns the numeric value associated with the character c. If the character is a fraction,
289 // the return value will not be an integer. If the character does not have a numeric value, the return value is -1.
292 // the numeric value for the specified Unicode character. If the character does not have a numeric value, the return value is -1.
294 // ch a Unicode character
296 // ArgumentNullException
297 // ArgumentOutOfRangeException
302 public static double GetNumericValue(char ch) {
303 return (InternalGetNumericValue(ch));
307 public static double GetNumericValue(String s, int index) {
309 throw new ArgumentNullException("s");
311 if (index < 0 || index >= s.Length) {
312 throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
314 Contract.EndContractBlock();
315 return (InternalGetNumericValue(InternalConvertToUtf32(s, index)));
321 //Returns the decimal digit value associated with the character c.
323 // The value should be from 0 ~ 9.
324 // If the character does not have a numeric value, the return value is -1.
325 // From Unicode.org: Decimal Digits. Digits that can be used to form decimal-radix numbers.
327 // the decimal digit value for the specified Unicode character. If the character does not have a decimal digit value, the return value is -1.
329 // ch a Unicode character
331 // ArgumentNullException
332 // ArgumentOutOfRangeException
337 public static int GetDecimalDigitValue(char ch) {
338 return (InternalGetDecimalDigitValue(ch));
342 public static int GetDecimalDigitValue(String s, int index) {
344 throw new ArgumentNullException("s");
346 if (index < 0 || index >= s.Length) {
347 throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
349 Contract.EndContractBlock();
351 return (InternalGetDecimalDigitValue(InternalConvertToUtf32(s, index)));
356 //Action: Returns the digit value associated with the character c.
357 // If the character does not have a numeric value, the return value is -1.
358 // From Unicode.org: If the character represents a digit, not necessarily a decimal digit,
359 // the value is here. This covers digits which do not form decimal radix forms, such as the compatibility superscript digits.
361 // An example is: U+2460 IRCLED DIGIT ONE. This character has digit value 1, but does not have associcated decimal digit value.
364 // the digit value for the specified Unicode character. If the character does not have a digit value, the return value is -1.
366 // ch a Unicode character
368 // ArgumentNullException
369 // ArgumentOutOfRangeException
374 public static int GetDigitValue(char ch) {
375 return (InternalGetDigitValue(ch));
379 public static int GetDigitValue(String s, int index) {
381 throw new ArgumentNullException("s");
383 if (index < 0 || index >= s.Length) {
384 throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
386 Contract.EndContractBlock();
387 return (InternalGetDigitValue(InternalConvertToUtf32(s, index)));
390 public static UnicodeCategory GetUnicodeCategory(char ch)
392 return (InternalGetUnicodeCategory(ch));
395 public static UnicodeCategory GetUnicodeCategory(String s, int index)
398 throw new ArgumentNullException("s");
399 if (((uint)index) >= ((uint)s.Length)) {
400 throw new ArgumentOutOfRangeException("index");
402 Contract.EndContractBlock();
403 return InternalGetUnicodeCategory(s, index);
406 internal unsafe static UnicodeCategory InternalGetUnicodeCategory(int ch) {
407 return ((UnicodeCategory)InternalGetCategoryValue(ch, UNICODE_CATEGORY_OFFSET));
412 //Action: Returns the Unicode Category property for the character c.
414 // an value in UnicodeCategory enum
416 // ch a Unicode character
420 //Note that this API will return values for D800-DF00 surrogate halves.
424 [System.Security.SecuritySafeCritical] // auto-generated
425 internal unsafe static byte InternalGetCategoryValue(int ch, int offset) {
426 Contract.Assert(ch >= 0 && ch <= 0x10ffff, "ch is not in valid Unicode range.");
427 // Get the level 2 item from the highest 12 bit (8 - 19) of ch.
428 ushort index = s_pCategoryLevel1Index[ch >> 8];
429 // Get the level 2 WORD offset from the 4 - 7 bit of ch. This provides the base offset of the level 3 table.
430 // Note that & has the lower precedence than addition, so don't forget the parathesis.
431 index = s_pCategoryLevel1Index[index + ((ch >> 4) & 0x000f)];
432 byte* pBytePtr = (byte*)&(s_pCategoryLevel1Index[index]);
433 // Get the result from the 0 -3 bit of ch.
434 byte valueIndex = pBytePtr[(ch & 0x000f)];
435 byte uc = s_pCategoriesValue[valueIndex * 2 + offset];
437 // Make sure that OtherNotAssigned is the last category in UnicodeCategory.
438 // If that changes, change the following assertion as well.
440 //Contract.Assert(uc >= 0 && uc <= UnicodeCategory.OtherNotAssigned, "Table returns incorrect Unicode category");
444 // internal static BidiCategory GetBidiCategory(char ch) {
445 // return ((BidiCategory)InternalGetCategoryValue(c, BIDI_CATEGORY_OFFSET));
448 internal static BidiCategory GetBidiCategory(String s, int index) {
450 throw new ArgumentNullException("s");
451 if (((uint)index) >= ((uint)s.Length)) {
452 throw new ArgumentOutOfRangeException("index");
454 Contract.EndContractBlock();
455 return ((BidiCategory)InternalGetCategoryValue(InternalConvertToUtf32(s, index), BIDI_CATEGORY_OFFSET));
460 //Action: Returns the Unicode Category property for the character c.
462 // an value in UnicodeCategory enum
464 // value a Unicode String
465 // index Index for the specified string.
471 internal static UnicodeCategory InternalGetUnicodeCategory(String value, int index) {
472 Contract.Assert(value != null, "value can not be null");
473 Contract.Assert(index < value.Length, "index < value.Length");
475 return (InternalGetUnicodeCategory(InternalConvertToUtf32(value, index)));
480 // Get the Unicode category of the character starting at index. If the character is in BMP, charLength will return 1.
481 // If the character is a valid surrogate pair, charLength will return 2.
485 internal static UnicodeCategory InternalGetUnicodeCategory(String str, int index, out int charLength) {
486 Contract.Assert(str != null, "str can not be null");
487 Contract.Assert(str.Length > 0, "str.Length > 0");;
488 Contract.Assert(index >= 0 && index < str.Length, "index >= 0 && index < str.Length");
490 return (InternalGetUnicodeCategory(InternalConvertToUtf32(str, index, out charLength)));
493 internal static bool IsCombiningCategory(UnicodeCategory uc) {
494 Contract.Assert(uc >= 0, "uc >= 0");
496 uc == UnicodeCategory.NonSpacingMark ||
497 uc == UnicodeCategory.SpacingCombiningMark ||
498 uc == UnicodeCategory.EnclosingMark