C#: Sanitize type names (#70)

This commit is contained in:
Katy Coe
2020-09-14 05:02:47 +02:00
parent 8d015c8271
commit fcbfc65204
2 changed files with 7 additions and 8 deletions

View File

@@ -88,11 +88,11 @@ namespace Il2CppInspector.Reflection
return s.ToString();
}
public static string ToCIdentifier(this string str) {
public static string ToCIdentifier(this string str, bool allowScopeQualifiers = false) {
// replace * with Ptr
str = str.Replace("*", "Ptr");
// replace illegal characters
str = Regex.Replace(str, "[^a-zA-Z0-9_]", "_");
str = Regex.Replace(str, allowScopeQualifiers? @"[^a-zA-Z0-9_\.:]" : "[^a-zA-Z0-9_]", "_");
// ensure identifier starts with a letter or _ (and is non-empty)
if (!Regex.IsMatch(str, "^[a-zA-Z_]"))
str = "_" + str;