C#/C++: Handle Unicode characters in identifiers

This commit is contained in:
Katy Coe
2020-09-14 12:27:12 +02:00
parent ff7110144a
commit 27b9b290da

View File

@@ -91,6 +91,14 @@ namespace Il2CppInspector.Reflection
public static string ToCIdentifier(this string str, bool allowScopeQualifiers = false) {
// replace * with Ptr
str = str.Replace("*", "Ptr");
// escape non-ASCII characters
var s = new StringBuilder();
for (var i = 0; i < str.Length; i++)
if (str[i] < 32 || str[i] > 126)
s.Append($"u{(int) str[i]:X4}");
else
s.Append(str[i]);
str = s.ToString();
// replace illegal characters
str = Regex.Replace(str, allowScopeQualifiers? @"[^a-zA-Z0-9_\.:]" : "[^a-zA-Z0-9_]", "_");
// ensure identifier starts with a letter or _ (and is non-empty)