forked from MochiLibraries/Biohazrd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCSharpCodeWriter.Sanitization.cs
309 lines (278 loc) · 12.1 KB
/
CSharpCodeWriter.Sanitization.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
using System;
using System.Globalization;
using System.Text;
namespace Biohazrd.CSharp
{
partial class CSharpCodeWriter
{
/// <remarks>Identifiers containing Unicode escape sequences (IE: <c>\u0057\u0048\u0059</c>) are not supported and will be sanitized away.</remarks>
public static string SanitizeIdentifier(string identifier)
{
switch (identifier)
{
case "abstract":
case "as":
case "base":
case "bool":
case "break":
case "byte":
case "case":
case "catch":
case "char":
case "checked":
case "class":
case "const":
case "continue":
case "decimal":
case "default":
case "delegate":
case "do":
case "double":
case "else":
case "enum":
case "event":
case "explicit":
case "extern":
case "false":
case "finally":
case "fixed":
case "float":
case "for":
case "foreach":
case "goto":
case "if":
case "implicit":
case "in":
case "int":
case "interface":
case "internal":
case "is":
case "lock":
case "long":
case "namespace":
case "new":
case "null":
case "object":
case "operator":
case "out":
case "override":
case "params":
case "private":
case "protected":
case "public":
case "readonly":
case "ref":
case "return":
case "sbyte":
case "sealed":
case "short":
case "sizeof":
case "stackalloc":
case "static":
case "string":
case "struct":
case "switch":
case "this":
case "throw":
case "true":
case "try":
case "typeof":
case "uint":
case "ulong":
case "unchecked":
case "unsafe":
case "ushort":
case "using":
case "virtual":
case "void":
case "volatile":
case "while":
return "@" + identifier;
default:
return SanitizeNonKeywordIdentifier(identifier);
}
}
/// <remarks>Identifiers containing Unicode escape sequences (IE: <c>\u0057\u0048\u0059</c>) are not supported.</remarks>
public static bool IsLegalIdentifier(string identifier)
{
// Relevant spec: https://github.com/dotnet/csharplang/blob/ca09fc178fb0e8285e80d2244786e99e04eed882/spec/lexical-structure.md#identifiers
if (identifier.Length == 0)
{ return false; }
if (!IsValidIdentifierStartCharacter(identifier[0]))
{ return false; }
for (int i = 1; i < identifier.Length; i++)
{
if (!IsValidIdentifierCharacter(identifier[i]))
{ return false; }
}
// If we got this far, all characters in the identifier are valid.
return true;
}
/// <remarks>Identifiers containing Unicode escape sequences (IE: <c>\u0057\u0048\u0059</c>) are not supported.</remarks>
private static string SanitizeNonKeywordIdentifier(string identifier)
{
// Relevant spec: https://github.com/dotnet/csharplang/blob/ca09fc178fb0e8285e80d2244786e99e04eed882/spec/lexical-structure.md#identifiers
if (String.IsNullOrEmpty(identifier))
{ throw new ArgumentException("The specified identifier is null or empty.", nameof(identifier)); }
StringBuilder? ret = null;
static string Escaped(char c)
=> $"__UNICODE_{((short)c):X4}__";
if (!IsValidIdentifierStartCharacter(identifier[0]))
{
// (Capacity is +32 as a guess that most identifiers won't need more than 2 character replacements.)
ret = new StringBuilder(identifier.Length + 32);
ret.Append(Escaped(identifier[0]));
}
for (int i = 1; i < identifier.Length; i++)
{
char character = identifier[i];
if (!IsValidIdentifierCharacter(character))
{
// If this is the first replacement, initialize the StringBuilder
// (Capacity is +32 as a guess that most identifiers won't need more than 2 character replacements.)
if (ret is null)
{ ret = new StringBuilder(identifier, 0, i, identifier.Length + 32); }
ret.Append(Escaped(character));
}
else if (ret is not null)
{ ret.Append(character); }
}
return ret is not null ? ret.ToString() : identifier;
}
/// <summary>Checks that the specified character is a valid <c>identifier_start_character</c>.</summary>
private static bool IsValidIdentifierStartCharacter(char c)
{
if (c == '_')
{ return true; }
switch (Char.GetUnicodeCategory(c))
{
// letter_character
case UnicodeCategory.UppercaseLetter: // Lu
case UnicodeCategory.LowercaseLetter: // Ll
case UnicodeCategory.TitlecaseLetter: // Lt
case UnicodeCategory.ModifierLetter: // Lm
case UnicodeCategory.OtherLetter: // Lo
case UnicodeCategory.LetterNumber: // Nl
return true;
default:
return false;
}
}
/// <summary>Checks whether the specified character is a valid <c>identifier_part_character</c>.</summary>
private static bool IsValidIdentifierCharacter(char c)
{
switch (Char.GetUnicodeCategory(c))
{
// letter_character
case UnicodeCategory.UppercaseLetter: // Lu
case UnicodeCategory.LowercaseLetter: // Ll
case UnicodeCategory.TitlecaseLetter: // Lt
case UnicodeCategory.ModifierLetter: // Lm
case UnicodeCategory.OtherLetter: // Lo
case UnicodeCategory.LetterNumber: // Nl
// decimal_digit_character
case UnicodeCategory.DecimalDigitNumber: // Nd
// connecting_character
case UnicodeCategory.ConnectorPunctuation: // Pc
// combining_character
case UnicodeCategory.NonSpacingMark: // Mn
case UnicodeCategory.SpacingCombiningMark: // Mc
// formatting_character
case UnicodeCategory.Format: // Cf
return true;
default:
return false;
}
}
public static string SanitizeNamespace(string fullNamespaceName)
{
//PERF: Ideally we should process this as a span and only allocate new strings once it's determined that sanitization is needed
// This would require reworking quite a bit of our identifier sanitization infrastructure
if (!fullNamespaceName.Contains('.'))
{ return SanitizeIdentifier(fullNamespaceName); }
string[] parts = fullNamespaceName.Split('.');
StringBuilder ret = new(fullNamespaceName.Length);
bool first = true;
foreach (string part in parts)
{
if (first)
{ first = false; }
else
{ ret.Append('.'); }
ret.Append(SanitizeIdentifier(part));
}
return ret.ToString();
}
public static string SanitizeStringLiteral(string value)
{
// Relevant spec: https://github.com/dotnet/csharplang/blob/0e365431d7ac2a6250089be9e77728ba2742d450/spec/lexical-structure.md#string-literals
StringBuilder? ret = null;
for (int i = 0; i < value.Length; i++)
{
char character = value[i];
// Basic replacements
string? replacement = character switch
{
// Forbidden by single_regular_string_literal_character
'\\' => @"\\", // Backslash
'"' => "\\\"", // Double quote
// new_line_character
'\n' => @"\n", // Line feed
'\r' => @"\r", // Carriage return
'\x0085' => @"\x0085", // Next line character (NEL)
'\x2028' => @"\x2028", // Unicode line separator
'\x2029' => @"\x2029", // Unicode paragraph separator
// Not forbidden, but these are unprintable simple_escape_sequence values not covered above
'\0' => @"\0", // Null
'\a' => @"\a", // Alert
'\b' => @"\b", // Backspace
'\f' => @"\f", // Form feed
'\t' => @"\t", // Tab
'\v' => @"\v", // Vertical tab
_ => null
};
// More complex replacements
// Technically the compiler should gladly parse a file with any of these in a string literal, but they have the potential to make the string hard to read in an editor.
// (Although on the flip side, some of these are printable when used in the correct context. So this might mangle certain non-English text.)
if (replacement is null)
{
switch (CharUnicodeInfo.GetUnicodeCategory(character))
{
case UnicodeCategory.Control:
case UnicodeCategory.Format:
case UnicodeCategory.LineSeparator:
case UnicodeCategory.ModifierLetter:
case UnicodeCategory.ModifierSymbol:
case UnicodeCategory.NonSpacingMark:
case UnicodeCategory.OtherNotAssigned:
case UnicodeCategory.ParagraphSeparator:
case UnicodeCategory.PrivateUse:
case UnicodeCategory.SpacingCombiningMark:
case UnicodeCategory.Surrogate:
{
ushort codePoint = (ushort)character;
replacement = $"\\x{codePoint:X4}";
}
break;
}
}
// If we have no replacement nor string builder, just keep checking
if (replacement is null && ret is null)
{ continue; }
// If this is the first replacement, initialize the StringBuilder
// (Capacity is +10 as a guess that most strings won't need more than 10 basic character replacements.)
if (ret is null)
{ ret = new StringBuilder(value, 0, i, value.Length + 10); }
// Append the character or the replacement
if (replacement is null)
{ ret.Append(character); }
else
{ ret.Append(replacement); }
}
return ret?.ToString() ?? value;
}
public static string SanitizeMultiLineComment(string comment)
// Break */ with a zero-width space so it reads correctly but doesn't end the comment
=> comment.Replace("*/", "*\x200B/");
}
}