mirror of https://github.com/ppy/SDL3-CS.git
Merge pull request #207 from SaxxonPike/Utf8String-Allocations
Allocate string memory only once in Utf8String when input lacks null terminator
This commit is contained in:
commit
c61b11ee5d
|
|
@ -24,10 +24,16 @@ namespace SDL
|
||||||
if (str == null)
|
if (str == null)
|
||||||
return new Utf8String(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));
|
||||||
|
|
||||||
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<byte> raw)
|
public static implicit operator Utf8String(ReadOnlySpan<byte> raw)
|
||||||
|
|
@ -36,16 +42,14 @@ namespace SDL
|
||||||
return new Utf8String(null);
|
return new Utf8String(null);
|
||||||
|
|
||||||
if (raw.Length == 0)
|
if (raw.Length == 0)
|
||||||
return new Utf8String(new ReadOnlySpan<byte>([0]));
|
return new Utf8String("\0"u8);
|
||||||
|
|
||||||
if (raw[^1] != 0)
|
if (raw[^1] == 0)
|
||||||
{
|
return new Utf8String(raw);
|
||||||
byte[] copy = new byte[raw.Length + 1];
|
|
||||||
raw.CopyTo(copy);
|
|
||||||
raw = copy;
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
internal ref readonly byte GetPinnableReference() => ref Raw.GetPinnableReference();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue