From f26b55e8bec3f08450aec2bfd2a00e147e5821cc Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Wed, 27 Jun 2018 14:45:19 -0400 Subject: [PATCH] stackalloc for UTF8_ToManaged --- src/SDL2.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/SDL2.cs b/src/SDL2.cs index cde4e27..faf01bf 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -89,9 +89,10 @@ namespace SDL2 ); #else /* Old C# requires an extra memcpy, bleh! */ - byte[] bytes = new byte[ptr - (byte*) s]; - Marshal.Copy(s, bytes, 0, bytes.Length); - string result = System.Text.Encoding.UTF8.GetString(bytes); + int len = (int) (ptr - (byte*) s); + char* chars = stackalloc char[len]; + System.Text.Encoding.UTF8.GetChars((byte*) s, len, chars, len); + string result = new string(chars); #endif /* Some SDL functions will malloc, we have to free! */