Add `PtrToStringUTF8()` for `byte *`

This commit is contained in:
Susko3 2024-04-06 14:08:42 +02:00
parent 702dc06a03
commit 711e004ecd
1 changed files with 29 additions and 0 deletions

29
SDL3-CS/SDL3.cs Normal file
View File

@ -0,0 +1,29 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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
{
/// <summary>
/// Allocates a managed <c>string</c> and copies all characters up to the first null character from an unmanaged UTF-8 string into it.
/// </summary>
/// <param name="ptr">The address of the first character of the unmanaged string.</param>
/// <param name="free">Whether to free the pointer after copying the string.</param>
/// <returns>
/// A managed string that holds a copy of the unmanaged string if the value of the <paramref name="ptr"/> parameter is not <c>null</c>; otherwise, this method returns <c>null</c>.
/// </returns>
public static string? PtrToStringUTF8(byte* ptr, bool free = false)
{
string? s = Marshal.PtrToStringUTF8((IntPtr)ptr);
if (free)
SDL_free(ptr);
return s;
}
}
}