-
Notifications
You must be signed in to change notification settings - Fork 370
/
UnicodeCharacterUtilities.cs
181 lines (152 loc) · 5.71 KB
/
UnicodeCharacterUtilities.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Globalization;
namespace Microsoft.TemplateEngine.Orchestrator.RunnableProjects.Utilities
{
/// <summary>
/// Defines a set of helper methods to classify Unicode characters.
/// </summary>
internal static partial class UnicodeCharacterUtilities
{
public static bool IsIdentifierStartCharacter(char ch)
{
// identifier-start-character:
// letter-character
// _ (the underscore character U+005F)
if (ch < 'a') // '\u0061'
{
if (ch < 'A') // '\u0041'
{
return false;
}
return ch is <= 'Z' // '\u005A'
or '_'; // '\u005F'
}
if (ch <= 'z') // '\u007A'
{
return true;
}
if (ch <= '\u007F') // max ASCII
{
return false;
}
return IsLetterChar(CharUnicodeInfo.GetUnicodeCategory(ch));
}
/// <summary>
/// Returns true if the Unicode character can be a part of an identifier.
/// </summary>
/// <param name="ch">The Unicode character.</param>
public static bool IsIdentifierPartCharacter(char ch)
{
// identifier-part-character:
// letter-character
// decimal-digit-character
// connecting-character
// combining-character
// formatting-character
if (ch < 'a') // '\u0061'
{
if (ch < 'A') // '\u0041'
{
return ch is >= '0' // '\u0030'
and <= '9'; // '\u0039'
}
return ch is <= 'Z' // '\u005A'
or '_'; // '\u005F'
}
if (ch <= 'z') // '\u007A'
{
return true;
}
if (ch <= '\u007F') // max ASCII
{
return false;
}
UnicodeCategory cat = CharUnicodeInfo.GetUnicodeCategory(ch);
return IsLetterChar(cat)
|| IsDecimalDigitChar(cat)
|| IsConnectingChar(cat)
|| IsCombiningChar(cat)
|| IsFormattingChar(cat);
}
/// <summary>
/// Check that the name is a valid Unicode identifier.
/// </summary>
public static bool IsValidIdentifier(string? name)
{
if (string.IsNullOrEmpty(name))
{
return false;
}
if (!IsIdentifierStartCharacter(name![0]))
{
return false;
}
int nameLength = name.Length;
for (int i = 1; i < nameLength; i++) //NB: start at 1
{
if (!IsIdentifierPartCharacter(name[i]))
{
return false;
}
}
return true;
}
/// <summary>
/// Returns true if the Unicode character is a formatting character (Unicode class Cf).
/// </summary>
/// <param name="ch">The Unicode character.</param>
internal static bool IsFormattingChar(char ch)
{
// There are no FormattingChars in ASCII range
return ch > 127 && IsFormattingChar(CharUnicodeInfo.GetUnicodeCategory(ch));
}
private static bool IsLetterChar(UnicodeCategory cat)
{
// letter-character:
// A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl
// A Unicode-escape-sequence representing a character of classes Lu, Ll, Lt, Lm, Lo, or Nl
return cat switch
{
UnicodeCategory.UppercaseLetter or UnicodeCategory.LowercaseLetter or UnicodeCategory.TitlecaseLetter or UnicodeCategory.ModifierLetter or UnicodeCategory.OtherLetter or UnicodeCategory.LetterNumber => true,
_ => false,
};
}
private static bool IsCombiningChar(UnicodeCategory cat)
{
// combining-character:
// A Unicode character of classes Mn or Mc
// A Unicode-escape-sequence representing a character of classes Mn or Mc
return cat switch
{
UnicodeCategory.NonSpacingMark or UnicodeCategory.SpacingCombiningMark => true,
_ => false,
};
}
private static bool IsDecimalDigitChar(UnicodeCategory cat)
{
// decimal-digit-character:
// A Unicode character of the class Nd
// A unicode-escape-sequence representing a character of the class Nd
return cat == UnicodeCategory.DecimalDigitNumber;
}
private static bool IsConnectingChar(UnicodeCategory cat)
{
// connecting-character:
// A Unicode character of the class Pc
// A unicode-escape-sequence representing a character of the class Pc
return cat == UnicodeCategory.ConnectorPunctuation;
}
/// <summary>
/// Returns true if the Unicode character is a formatting character (Unicode class Cf).
/// </summary>
/// <param name="cat">The Unicode character.</param>
private static bool IsFormattingChar(UnicodeCategory cat)
{
// formatting-character:
// A Unicode character of the class Cf
// A unicode-escape-sequence representing a character of the class Cf
return cat == UnicodeCategory.Format;
}
}
}