From 711e004ecd40dabeb087ebab46a3e491f44c266c Mon Sep 17 00:00:00 2001 From: Susko3 Date: Sat, 6 Apr 2024 14:08:42 +0200 Subject: [PATCH] Add `PtrToStringUTF8()` for `byte *` --- SDL3-CS/SDL3.cs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 SDL3-CS/SDL3.cs diff --git a/SDL3-CS/SDL3.cs b/SDL3-CS/SDL3.cs new file mode 100644 index 0000000..5b46e55 --- /dev/null +++ b/SDL3-CS/SDL3.cs @@ -0,0 +1,29 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Runtime.InteropServices; + +namespace SDL +{ + public static unsafe partial class SDL3 + { + /// + /// Allocates a managed string and copies all characters up to the first null character from an unmanaged UTF-8 string into it. + /// + /// The address of the first character of the unmanaged string. + /// Whether to free the pointer after copying the string. + /// + /// A managed string that holds a copy of the unmanaged string if the value of the parameter is not null; otherwise, this method returns null. + /// + public static string? PtrToStringUTF8(byte* ptr, bool free = false) + { + string? s = Marshal.PtrToStringUTF8((IntPtr)ptr); + + if (free) + SDL_free(ptr); + + return s; + } + } +}