diff --git a/src/ImGui.NET/ImGui.Manual.cs b/src/ImGui.NET/ImGui.Manual.cs index edadfc9..72400d9 100644 --- a/src/ImGui.NET/ImGui.Manual.cs +++ b/src/ImGui.NET/ImGui.Manual.cs @@ -1,5 +1,6 @@ using System; using System.Numerics; +using System.Runtime.CompilerServices; using System.Text; namespace ImGuiNET @@ -88,14 +89,17 @@ namespace ImGuiNET Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount); } - int bytesNeeded = Encoding.UTF8.GetByteCount(input); - int stackBufSize = Math.Max((int)maxLength, bytesNeeded); + int originalByteCount = Encoding.UTF8.GetByteCount(input); + int stackBufSize = Math.Max((int)maxLength, originalByteCount); byte* bufBytes = stackalloc byte[stackBufSize]; fixed (char* u16Ptr = input) { Encoding.UTF8.GetBytes(u16Ptr, input.Length, bufBytes, stackBufSize); } + byte* originalBufBytes = stackalloc byte[originalByteCount]; + Unsafe.CopyBlock(originalBufBytes, bufBytes, (uint)originalByteCount); + byte result = ImGuiNative.igInputText( labelBytes, bufBytes, @@ -103,7 +107,7 @@ namespace ImGuiNET flags, callback, user_data.ToPointer()); - if (!Util.AreStringsEqual(input, bufBytes)) + if (!Util.AreStringsEqual(originalBufBytes, originalByteCount, bufBytes)) { input = Util.StringFromPtr(bufBytes); } @@ -148,14 +152,17 @@ namespace ImGuiNET Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount); } - int bytesNeeded = Encoding.UTF8.GetByteCount(input); - int stackBufSize = Math.Max((int)maxLength, bytesNeeded); + int originalByteCount = Encoding.UTF8.GetByteCount(input); + int stackBufSize = Math.Max((int)maxLength, originalByteCount); byte* bufBytes = stackalloc byte[stackBufSize]; fixed (char* u16Ptr = input) { Encoding.UTF8.GetBytes(u16Ptr, input.Length, bufBytes, stackBufSize); } + byte* originalBufBytes = stackalloc byte[originalByteCount]; + Unsafe.CopyBlock(originalBufBytes, bufBytes, (uint)originalByteCount); + byte result = ImGuiNative.igInputTextMultiline( labelBytes, bufBytes, @@ -164,7 +171,7 @@ namespace ImGuiNET flags, callback, user_data.ToPointer()); - if (!Util.AreStringsEqual(input, bufBytes)) + if (!Util.AreStringsEqual(originalBufBytes, originalByteCount, bufBytes)) { input = Util.StringFromPtr(bufBytes); } diff --git a/src/ImGui.NET/Util.cs b/src/ImGui.NET/Util.cs index 81a590a..e15fc03 100644 --- a/src/ImGui.NET/Util.cs +++ b/src/ImGui.NET/Util.cs @@ -15,23 +15,14 @@ namespace ImGuiNET return Encoding.UTF8.GetString(ptr, characters); } - internal static unsafe bool AreStringsEqual(string a, byte* b) + internal static unsafe bool AreStringsEqual(byte* a, int aLength, byte* b) { - if (a.Length == 0) { return b[0] == 0; } - - int aCount = Encoding.UTF8.GetByteCount(a); - byte* aBytes = stackalloc byte[aCount]; - fixed (char* labelPtr = a) - { - Encoding.UTF8.GetBytes(labelPtr, a.Length, aBytes, aCount); - } - - for (int i = 0; i < aCount; i++) + for (int i = 0; i < aLength; i++) { - if (aBytes[i] != b[i]) { return false; } + if (a[i] != b[i]) { return false; } } - if (b[aCount] != 0) { return false; } + if (b[aLength] != 0) { return false; } return true; }