-
Notifications
You must be signed in to change notification settings - Fork 5
/
CharExtensions.cs
130 lines (109 loc) · 3.24 KB
/
CharExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
using System.Globalization;
namespace System
{
public static class CharExtensions
{
public static char ToUpperInvariant (this char c)
{
return Char.ToLowerInvariant (c);
}
public static char ToLowerInvariant (this char c)
{
return Char.ToLower (c, CultureInfo.InvariantCulture);
}
/// <summary>
/// Determines if a character is a digit
/// </summary>
/// <param name="c">character to test</param>
/// <returns><b>true</b> if the character is a digit, otherwise <b>false</b></returns>
public static bool IsDigit (this char c)
{
return char.IsDigit (c);
}
}
public static class CharEx
{
public static char ToUpperInvariant (char c)
{
return Char.ToUpper (c, CultureInfo.InvariantCulture);
}
public static string ConvertFromUtf32 (int utf32)
{
if (utf32 < 0 || utf32 > 0x10FFFF)
throw new ArgumentOutOfRangeException ("utf32", "The argument must be from 0 to 0x10FFFF.");
if (0xD800 <= utf32 && utf32 <= 0xDFFF)
throw new ArgumentOutOfRangeException ("utf32", "The argument must not be in surrogate pair range.");
if (utf32 < 0x10000)
return new string ((char)utf32, 1);
utf32 -= 0x10000;
return new string (
new char[] {(char) ((utf32 >> 10) + 0xD800),
(char) (utf32 % 0x0400 + 0xDC00)});
}
public static int ConvertToUtf32 (char highSurrogate, char lowSurrogate)
{
if (highSurrogate < 0xD800 || 0xDBFF < highSurrogate)
throw new ArgumentOutOfRangeException ("highSurrogate");
if (lowSurrogate < 0xDC00 || 0xDFFF < lowSurrogate)
throw new ArgumentOutOfRangeException ("lowSurrogate");
return 0x10000 + ((highSurrogate - 0xD800) << 10) + (lowSurrogate - 0xDC00);
}
public static int ConvertToUtf32 (string s, int index)
{
CheckParameter (s, index);
if (!Char.IsSurrogate (s[index]))
return s[index];
if (!CharEx.IsHighSurrogate (s[index])
|| index == s.Length - 1
|| !CharEx.IsLowSurrogate (s[index + 1]))
throw new ArgumentException (String.Format ("The string contains invalid surrogate pair character at {0}", index));
return ConvertToUtf32 (s[index], s[index + 1]);
}
public static bool IsHighSurrogate (char c)
{
return c >= '\uD800' && c <= '\uDBFF';
}
public static bool IsHighSurrogate (string s, int index)
{
CheckParameter (s, index);
return IsHighSurrogate (s[index]);
}
public static bool IsLowSurrogate (char c)
{
return c >= '\uDC00' && c <= '\uDFFF';
}
public static bool IsLowSurrogate (string s, int index)
{
CheckParameter (s, index);
return IsLowSurrogate (s[index]);
}
private static void CheckParameter (string s, int index)
{
if (index < 0 || index >= s.Length)
throw new ArgumentOutOfRangeException ("index");
}
public static bool TryParse (string s, out char result)
{
if (s == null || s.Length != 1)
{
result = (char)0;
return false;
}
result = s[0];
return true;
}
public static char Parse (string s)
{
if (s == null)
throw new ArgumentNullException ("s");
if (s.Length != 1)
throw new FormatException ("s contains more than one character.");
return s[0];
}
}
}