Inline `ensureTrailingNull()`

This commit is contained in:
Susko3 2024-04-17 13:37:11 +02:00
parent 8f5e52d944
commit c40e8b150c
1 changed files with 5 additions and 6 deletions

View File

@ -2,7 +2,6 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
namespace SDL namespace SDL
@ -10,7 +9,7 @@ namespace SDL
/// <summary> /// <summary>
/// Null pointer or a null-byte terminated UTF8 string suitable for use in native methods. /// Null pointer or a null-byte terminated UTF8 string suitable for use in native methods.
/// </summary> /// </summary>
/// <remarks>Should only be instantiated through implicit conversions.</remarks> /// <remarks>Should only be instantiated through implicit conversions or with <c>null</c>.</remarks>
public readonly ref struct Utf8String public readonly ref struct Utf8String
{ {
internal readonly ReadOnlySpan<byte> Raw; internal readonly ReadOnlySpan<byte> Raw;
@ -25,11 +24,11 @@ namespace SDL
if (str == null) if (str == null)
return new Utf8String(null); return new Utf8String(null);
return new Utf8String(Encoding.UTF8.GetBytes(ensureTrailingNull(str))); if (str.EndsWith('\0'))
} return new Utf8String(Encoding.UTF8.GetBytes(str));
[MethodImpl(MethodImplOptions.AggressiveInlining)] return new Utf8String(Encoding.UTF8.GetBytes(str + '\0'));
private static string ensureTrailingNull(string str) => str.EndsWith('\0') ? str : str + '\0'; }
public static implicit operator Utf8String(ReadOnlySpan<byte> raw) public static implicit operator Utf8String(ReadOnlySpan<byte> raw)
{ {