From 27b9b290da7c5b25a1d56a3475c6715cd70c6fc2 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Mon, 14 Sep 2020 12:27:12 +0200 Subject: [PATCH] C#/C++: Handle Unicode characters in identifiers --- Il2CppInspector.Common/Reflection/Extensions.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Il2CppInspector.Common/Reflection/Extensions.cs b/Il2CppInspector.Common/Reflection/Extensions.cs index fbd309b..ad9d6c4 100644 --- a/Il2CppInspector.Common/Reflection/Extensions.cs +++ b/Il2CppInspector.Common/Reflection/Extensions.cs @@ -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)