Optimize some of the string operations
This commit is contained in:
@@ -533,11 +533,42 @@ namespace Il2CppInspector.Cpp
|
||||
GlobalNamer = GlobalsNamespace.MakeNamer<MethodBase>((method) => $"{TypeNamer.GetName(method.DeclaringType)}_{method.Name.ToCIdentifier()}");
|
||||
}
|
||||
|
||||
private static readonly string[] ReservedCppKeywords =
|
||||
[
|
||||
"_Alignas", "_Alignof", "_Atomic", "_Bool",
|
||||
"_Complex", "_Generic",
|
||||
"_Imaginary", "_Noreturn", "_Static_assert", "_Thread_local",
|
||||
"alignas", "alignof", "and", "and_eq",
|
||||
"asm", "auto", "bitand", "bitor",
|
||||
"bool", "break", "case", "catch",
|
||||
"char", "char16_t", "char32_t", "char8_t",
|
||||
"class", "co_await", "co_return", "co_yield",
|
||||
"compl", "concept", "const", "const_cast",
|
||||
"consteval", "constexpr", "constinit", "continue",
|
||||
"decltype", "default", "delete", "do",
|
||||
"double", "dynamic_cast", "else", "enum",
|
||||
"explicit", "export", "extern", "false",
|
||||
"final", "float", "for", "friend",
|
||||
"goto", "if", "inline", "int",
|
||||
"long", "mutable", "namespace", "new",
|
||||
"noexcept", "not", "not_eq", "nullptr",
|
||||
"operator", "or", "or_eq", "private",
|
||||
"protected", "public", "reflexpr", "register",
|
||||
"reinterpret_cast", "requires", "restrict", "return",
|
||||
"short", "signed", "sizeof", "static",
|
||||
"static_assert", "static_cast", "struct", "switch",
|
||||
"synchronized", "template", "this", "thread_local",
|
||||
"throw", "true", "try", "typedef",
|
||||
"typeid", "typename", "union", "unsigned",
|
||||
"using", "virtual", "void", "volatile",
|
||||
"wchar_t", "while", "xor", "xor_eq"
|
||||
];
|
||||
|
||||
// Reserve C/C++ keywords and built-in names
|
||||
private static CppNamespace CreateNamespace() {
|
||||
var ns = new CppNamespace();
|
||||
/* Reserve C/C++ keywords */
|
||||
foreach (var keyword in new [] { "_Alignas", "_Alignof", "_Atomic", "_Bool", "_Complex", "_Generic", "_Imaginary", "_Noreturn", "_Static_assert", "_Thread_local", "alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char", "char16_t", "char32_t", "char8_t", "class", "co_await", "co_return", "co_yield", "compl", "concept", "const", "const_cast", "consteval", "constexpr", "constinit", "continue", "decltype", "default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", "final", "float", "for", "friend", "goto", "if", "inline", "int", "long", "mutable", "namespace", "new", "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected", "public", "reflexpr", "register", "reinterpret_cast", "requires", "restrict", "return", "short", "signed", "sizeof", "static", "static_assert", "static_cast", "struct", "switch", "synchronized", "template", "this", "thread_local", "throw", "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq" }) {
|
||||
foreach (var keyword in ReservedCppKeywords) {
|
||||
ns.ReserveName(keyword);
|
||||
}
|
||||
/* Reserve commonly defined C++ symbols for MSVC DLL projects */
|
||||
|
||||
@@ -24,18 +24,15 @@ namespace Il2CppInspector.Cpp
|
||||
|
||||
// Mark a name as reserved without assigning an object to it (e.g. for keywords and built-in names)
|
||||
public void ReserveName(string name) {
|
||||
if (renameCount.ContainsKey(name)) {
|
||||
if (!renameCount.TryAdd(name, 0)) {
|
||||
throw new Exception($"Can't reserve {name}: already taken!");
|
||||
}
|
||||
renameCount[name] = 0;
|
||||
}
|
||||
|
||||
// Try to mark a name as reserved without assigning an object to it (e.g. for keywords and built-in names)
|
||||
public bool TryReserveName(string name) {
|
||||
if (renameCount.ContainsKey(name))
|
||||
return false;
|
||||
renameCount[name] = 0;
|
||||
return true;
|
||||
public bool TryReserveName(string name)
|
||||
{
|
||||
return renameCount.TryAdd(name, 0);
|
||||
}
|
||||
|
||||
// Create a Namer object which will give names to objects of type T which are unique within this namespace
|
||||
|
||||
@@ -398,7 +398,14 @@ namespace Il2CppInspector.Cpp
|
||||
fieldString = sbEnum.ToString();
|
||||
suffix = "";
|
||||
}
|
||||
sb.Append("\n " + string.Join("\n ", fieldString.Split('\n')) + suffix);
|
||||
|
||||
sb.Append("\n ");
|
||||
foreach (var fieldStr in fieldString.Split('\n'))
|
||||
{
|
||||
sb.Append(fieldStr);
|
||||
sb.Append("\n ");
|
||||
}
|
||||
sb.Append(suffix);
|
||||
}
|
||||
|
||||
sb.Append($"\n}}{(format == "o"? $" /* Size: 0x{SizeBytes:x2} */" : "")};");
|
||||
|
||||
@@ -81,12 +81,21 @@ namespace Il2CppInspector.Reflection
|
||||
public static string ToEscapedString(this string str) {
|
||||
// Replace standard escape characters
|
||||
var s = new StringBuilder();
|
||||
for (var i = 0; i < str.Length; i++)
|
||||
// Standard escape characters
|
||||
s.Append(escapeChars.ContainsKey(str[i]) ? escapeChars[str[i]]
|
||||
// Replace everything else with UTF-16 Unicode
|
||||
: str[i] < 32 || str[i] > 126 ? @"\u" + $"{(int) str[i]:X4}"
|
||||
: str[i].ToString());
|
||||
|
||||
foreach (var chr in str)
|
||||
{
|
||||
if (escapeChars.TryGetValue(chr, out var escaped))
|
||||
s.Append(escaped);
|
||||
else if (chr < 32 || chr > 126)
|
||||
{
|
||||
s.Append("\\u");
|
||||
s.Append($"{(int) chr:X4}");
|
||||
}
|
||||
else
|
||||
s.Append(chr);
|
||||
|
||||
}
|
||||
|
||||
return s.ToString();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user