Watch
3
0
Fork
You've already forked raylib-cs
0

Apply master changes and fix utf8 buffer naming

This commit is contained in:
Chris 2023-08-13 10:28:05 +01:00
commit a0d662a352
2 changed files with 102 additions and 65 deletions

View file

@ -0,0 +1,36 @@
using System;
using System.Runtime.InteropServices;
namespace Raylib_cs
{
/// <summary>
/// Converts text to a Ansi buffer for passing to native code
/// </summary>
public readonly ref struct AnsiBuffer
{
private readonly IntPtr data;
public AnsiBuffer(string text)
{
data = Marshal.StringToHGlobalAnsi(text);
}
public unsafe sbyte* AsPointer()
{
return (sbyte*)data.ToPointer();
}
public void Dispose()
{
Marshal.FreeHGlobal(data);
}
}
public static class AnsiStringUtils
{
public static AnsiBuffer ToAnsiBuffer(this string text)
{
return new AnsiBuffer(text);
}
}
}

View file

@ -0,0 +1,63 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Raylib_cs
{
/// <summary>
/// Converts text to a UTF8 buffer for passing to native code
/// </summary>
public readonly ref struct Utf8Buffer
{
private readonly IntPtr _data;
public Utf8Buffer(string text)
{
_data = Marshal.StringToCoTaskMemUTF8(text);
}
public unsafe sbyte* AsPointer()
{
return (sbyte*)_data.ToPointer();
}
public void Dispose()
{
Marshal.ZeroFreeCoTaskMemUTF8(_data);
}
}
public static class Utf8StringUtils
{
public static Utf8Buffer ToUtf8Buffer(this string text)
{
return new Utf8Buffer(text);
}
public static byte[] ToUtf8String(this string text)
{
if (text == null)
{
return null;
}
var length = Encoding.UTF8.GetByteCount(text);
var byteArray = new byte[length + 1];
var wrote = Encoding.UTF8.GetBytes(text, 0, text.Length, byteArray, 0);
byteArray[wrote] = 0;
return byteArray;
}
public static unsafe string GetUTF8String(sbyte* bytes)
{
return Marshal.PtrToStringUTF8((IntPtr)bytes);
}
public static byte[] GetUTF8Bytes(this string text)
{
return Encoding.UTF8.GetBytes(text);
}
}
}