Remove string allocation in ID3DIncludeForD2DHelpers.Open

このコミットが含まれているのは:
Sergio Pedri committed 2023-09-12 16:29:12 +02:00
1 親 f28fdd0d54
コミット a23b438b5d
2ファイル変更
+29 -2

検索条件に一致するファイルはありません

+21
ファイルの表示
@@ -18,4 +18,25 @@ internal static class MemoryMarshal
{
return ref global::System.Runtime.InteropServices.MemoryMarshal.GetReference(array.AsSpan());
}
/// <summary>
/// Creates a new read-only span for a <see langword="null"/>-terminated UTF-8 string.
/// </summary>
/// <param name="value">The pointer to the <see langword="null"/>-terminated string of bytes.</param>
/// <returns>A read-only span representing the specified <see langword="null"/>-terminated string, or an empty span if the pointer is <see langword="null"/>.</returns>
/// <remarks>The returned span does not include the <see langword="null"/> terminator, nor does it validate the well-formedness of the UTF-8 data.</remarks>
/// <exception cref="ArgumentException">The string is longer than <see cref="int.MaxValue"/>.</exception>
public static unsafe ReadOnlySpan<byte> CreateReadOnlySpanFromNullTerminated(byte* value)
{
for (int i = 0; i < int.MaxValue; i++)
{
// Stop when the null-terminator has been found
if (value[i] == 0)
{
return new(value, i);
}
}
throw new ArgumentException("The string must be null-terminated.");
}
}