2
0
mirror of https://github.com/raylib-cs/raylib-cs synced 2025-09-09 03:01:41 -04:00

Move Utf8String

This commit is contained in:
2021-12-05 01:46:46 +11:00
parent 04b5fe2130
commit cfef858a0f

View File

@@ -66,8 +66,8 @@ public readonly ref struct Utf8String
public static Utf8String FromSpan(ReadOnlySpan<byte> span) public static Utf8String FromSpan(ReadOnlySpan<byte> span)
{ {
if ( if (
(span.Length > 0) span.Length > 0
&& (span[^1] != 0) && span[^1] != 0
) )
{ {
throw new ArgumentException("zero terminator required"); throw new ArgumentException("zero terminator required");
@@ -82,11 +82,9 @@ public readonly ref struct Utf8String
{ {
return new Utf8String(ReadOnlySpan<byte>.Empty); return new Utf8String(ReadOnlySpan<byte>.Empty);
} }
else
{
return new Utf8String(s.ToUtf8String()); return new Utf8String(s.ToUtf8String());
} }
}
static unsafe long GetLength(byte* p) static unsafe long GetLength(byte* p)
{ {
@@ -112,14 +110,7 @@ public readonly ref struct Utf8String
/// </summary> /// </summary>
public static unsafe Utf8String FromPtr(byte* p) public static unsafe Utf8String FromPtr(byte* p)
{ {
if (p == null) return p == null ? new Utf8String(ReadOnlySpan<byte>.Empty) : new Utf8String(FindZeroTerminator(p));
{
return new Utf8String(ReadOnlySpan<byte>.Empty);
}
else
{
return new Utf8String(FindZeroTerminator(p));
}
} }
// TODO maybe remove this and just use FromSpan? // TODO maybe remove this and just use FromSpan?
@@ -144,30 +135,28 @@ public readonly ref struct Utf8String
{ {
return new Utf8String(ReadOnlySpan<byte>.Empty); return new Utf8String(ReadOnlySpan<byte>.Empty);
} }
else
{
// the given len does NOT include the zero terminator // the given len does NOT include the zero terminator
var sp = new ReadOnlySpan<byte>(p, len + 1); var sp = new ReadOnlySpan<byte>(p, len + 1);
return FromSpan(sp); return FromSpan(sp);
} }
}
public static unsafe Utf8String FromIntPtr(IntPtr p) public static unsafe Utf8String FromIntPtr(IntPtr p)
{ {
if (p == IntPtr.Zero) return p == IntPtr.Zero ? new Utf8String(ReadOnlySpan<byte>.Empty) : new Utf8String(FindZeroTerminator((byte*)p.ToPointer()));
{
return new Utf8String(ReadOnlySpan<byte>.Empty);
}
else
{
return new Utf8String(FindZeroTerminator((byte*)(p.ToPointer())));
}
} }
/// <summary>
/// UTF16 String representation
/// Returns "NUL" on Null native type
/// </summary>
/// <returns></returns>
public override string ToString() public override string ToString()
{ {
if (sp.Length == 0) if (sp.Length == 0)
{ {
// object.ToString() should never thrown
// throw new NullReferenceException();
return "NUL"; return "NUL";
} }