Optimize some of the string operations

This commit is contained in:
LukeFZ
2023-11-30 05:13:19 +01:00
parent 9f6309fb46
commit ab841ccb2b
4 changed files with 1346 additions and 1302 deletions

View File

@@ -533,11 +533,42 @@ namespace Il2CppInspector.Cpp
GlobalNamer = GlobalsNamespace.MakeNamer<MethodBase>((method) => $"{TypeNamer.GetName(method.DeclaringType)}_{method.Name.ToCIdentifier()}"); 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 // Reserve C/C++ keywords and built-in names
private static CppNamespace CreateNamespace() { private static CppNamespace CreateNamespace() {
var ns = new CppNamespace(); var ns = new CppNamespace();
/* Reserve C/C++ keywords */ /* 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); ns.ReserveName(keyword);
} }
/* Reserve commonly defined C++ symbols for MSVC DLL projects */ /* Reserve commonly defined C++ symbols for MSVC DLL projects */

View File

@@ -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) // Mark a name as reserved without assigning an object to it (e.g. for keywords and built-in names)
public void ReserveName(string name) { public void ReserveName(string name) {
if (renameCount.ContainsKey(name)) { if (!renameCount.TryAdd(name, 0)) {
throw new Exception($"Can't reserve {name}: already taken!"); 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) // 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) { public bool TryReserveName(string name)
if (renameCount.ContainsKey(name)) {
return false; return renameCount.TryAdd(name, 0);
renameCount[name] = 0;
return true;
} }
// Create a Namer object which will give names to objects of type T which are unique within this namespace // Create a Namer object which will give names to objects of type T which are unique within this namespace

View File

@@ -398,7 +398,14 @@ namespace Il2CppInspector.Cpp
fieldString = sbEnum.ToString(); fieldString = sbEnum.ToString();
suffix = ""; 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} */" : "")};"); sb.Append($"\n}}{(format == "o"? $" /* Size: 0x{SizeBytes:x2} */" : "")};");

View File

@@ -81,12 +81,21 @@ namespace Il2CppInspector.Reflection
public static string ToEscapedString(this string str) { public static string ToEscapedString(this string str) {
// Replace standard escape characters // Replace standard escape characters
var s = new StringBuilder(); var s = new StringBuilder();
for (var i = 0; i < str.Length; i++)
// Standard escape characters foreach (var chr in str)
s.Append(escapeChars.ContainsKey(str[i]) ? escapeChars[str[i]] {
// Replace everything else with UTF-16 Unicode if (escapeChars.TryGetValue(chr, out var escaped))
: str[i] < 32 || str[i] > 126 ? @"\u" + $"{(int) str[i]:X4}" s.Append(escaped);
: str[i].ToString()); else if (chr < 32 || chr > 126)
{
s.Append("\\u");
s.Append($"{(int) chr:X4}");
}
else
s.Append(chr);
}
return s.ToString(); return s.ToString();
} }