diff --git a/SDL3-CS/Utf8String.cs b/SDL3-CS/Utf8String.cs index 832b44f..8754bfb 100644 --- a/SDL3-CS/Utf8String.cs +++ b/SDL3-CS/Utf8String.cs @@ -24,10 +24,16 @@ namespace SDL if (str == null) return new Utf8String(null); - if (str.EndsWith('\0')) + if (str.Length == 0) + return new Utf8String("\0"u8); + + if (str[^1] == '\0') return new Utf8String(Encoding.UTF8.GetBytes(str)); - return new Utf8String(Encoding.UTF8.GetBytes(str + '\0')); + int len = Encoding.UTF8.GetByteCount(str); + byte[] bytes = new byte[len + 1]; + Encoding.UTF8.GetBytes(str, bytes); + return new Utf8String(bytes); } public static implicit operator Utf8String(ReadOnlySpan raw) @@ -36,16 +42,14 @@ namespace SDL return new Utf8String(null); if (raw.Length == 0) - return new Utf8String(new ReadOnlySpan([0])); + return new Utf8String("\0"u8); - if (raw[^1] != 0) - { - byte[] copy = new byte[raw.Length + 1]; - raw.CopyTo(copy); - raw = copy; - } + if (raw[^1] == 0) + return new Utf8String(raw); - return new Utf8String(raw); + byte[] copy = new byte[raw.Length + 1]; + raw.CopyTo(copy); + return new Utf8String(copy); } internal ref readonly byte GetPinnableReference() => ref Raw.GetPinnableReference();