Move duplicated sanitizeIdentifier to Extensions

Also ensure that the identifier always starts with an underscore or
letter, which fixes issues with obfuscated identifiers.
This commit is contained in:
Robert Xiao
2020-06-23 00:12:00 -07:00
committed by Katy
parent 95ee085374
commit de6d9e2230
3 changed files with 18 additions and 15 deletions

View File

@@ -8,6 +8,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Il2CppInspector.Reflection
{
@@ -83,6 +84,15 @@ namespace Il2CppInspector.Reflection
return s.ToString();
}
public static string ToCIdentifier(this string str) {
// replace illegal characters
str = Regex.Replace(str, "[^a-zA-Z0-9_]", "_");
// ensure identifier starts with a letter or _ (and is non-empty)
if (!Regex.IsMatch(str, "^[a-zA-Z_]"))
str = "_" + str;
return str;
}
// Output a value in C#-friendly syntax
public static string ToCSharpValue(this object value, TypeInfo type, Scope usingScope = null) {
if (value is bool)