-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharExtensions.cs
146 lines (121 loc) · 4.71 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// THis file comes from ExCSS almost unmodified
namespace SkiaSharpOpenGLBenchmark
{
#if !NET40
#endif
internal static class CharExtensions
{
public static int FromHex(this char c)
{
return c.IsDigit() ? c - Symbols.Zero : c - (c.IsLowercaseAscii() ? Symbols.CapitalW : Symbols.Seven);
}
public static string ToHex(this char character)
{
return ((int)character).ToString("x");
}
public static byte ToByte(this char character)
{
byte c = (byte)character;
c -= (byte)'0';
if (c > 9)
c -= (byte)'A' - (byte)'9' - 1;
if (c > 15)
c -= (byte)'a' - (byte)'A';
return c;
}
public static bool IsInRange(this char c, int lower, int upper)
{
return c >= lower && c <= upper;
}
public static bool IsNormalQueryCharacter(this char c)
{
return c.IsInRange(Symbols.ExclamationMark, Symbols.Tilde) &&
c != Symbols.DoubleQuote &&
c != Symbols.CurvedQuote && c != Symbols.Num &&
c != Symbols.LessThan && c != Symbols.GreaterThan;
}
public static bool IsNormalPathCharacter(this char c)
{
return c.IsInRange(Symbols.Space, Symbols.Tilde) &&
c != Symbols.DoubleQuote &&
c != Symbols.CurvedQuote && c != Symbols.Num &&
c != Symbols.LessThan && c != Symbols.GreaterThan &&
c != Symbols.Space && c != Symbols.QuestionMark;
}
public static bool IsUppercaseAscii(this char c)
{
return c >= Symbols.CapitalA && c <= Symbols.CapitalZ;
}
public static bool IsLowercaseAscii(this char c)
{
return c >= Symbols.LowerA && c <= Symbols.LowerZ;
}
public static bool IsAlphanumericAscii(this char c)
{
return c.IsDigit() || c.IsUppercaseAscii() || c.IsLowercaseAscii();
}
public static bool IsHex(this char c)
{
return c.IsDigit() || c >= Symbols.CapitalA && c <= Symbols.CapitalF ||
c >= Symbols.LowerA && c <= Symbols.LowerF;
}
public static bool IsNonAscii(this char c)
{
return c != Symbols.EndOfFile && c >= Symbols.ExtendedAsciiStart;
}
public static bool IsNonPrintable(this char c)
{
return c >= Symbols.Null && c <= Symbols.Backspace ||
c >= Symbols.ShiftOut && c <= Symbols.UnitSeparator ||
c >= Symbols.Delete && c < Symbols.NonBreakingSpace;
}
public static bool IsLetter(this char c)
{
return IsUppercaseAscii(c) || IsLowercaseAscii(c);
}
public static bool IsName(this char c)
{
return c.IsNonAscii() || c.IsLetter() || c == Symbols.Underscore || c == Symbols.Minus || c.IsDigit();
}
public static bool IsNameStart(this char c)
{
return c.IsNonAscii() || c.IsUppercaseAscii() || c.IsLowercaseAscii() || c == Symbols.Underscore;
}
public static bool IsLineBreak(this char c)
{
return c == Symbols.LineFeed || c == Symbols.CarriageReturn;
}
public static bool IsSpaceCharacter(this char c)
{
return c == Symbols.Space || c == Symbols.Tab || c == Symbols.LineFeed ||
c == Symbols.CarriageReturn || c == Symbols.FormFeed;
}
public static bool IsDigit(this char c)
{
return c >= Symbols.Zero && c <= Symbols.Nine;
}
// HTML forbids the use of Universal Character Set / Unicode code points
// - 0 to 31, except 9, 10, and 13 C0 control characters
// - 127 DEL character
// - 128 to 159 (0x80 to 0x9F, C1 control characters
// - 55296 to 57343 (0xD800 – xDFFF, UTF-16 surrogate halves)
// - 65534 and 65535 (xFFFE – xFFFF, non-characters, related to xFEFF, the byte order mark)
public static bool IsInvalid(this int c)
{
return c == 0 || c > Symbols.MaximumCodepoint ||
c > Symbols.UTF16SurrogateMin && c < Symbols.UTF16SurrogateMax;
}
public static bool IsOneOf(this char c, char a, char b)
{
return a == c || b == c;
}
public static bool IsOneOf(this char c, char o1, char o2, char o3)
{
return c == o1 || c == o2 || c == o3;
}
public static bool IsOneOf(this char c, char o1, char o2, char o3, char o4)
{
return c == o1 || c == o2 || c == o3 || c == o4;
}
}
}