FIX: StringUtilities to use byte instead of sbyte.

This commit is contained in:
Amer Koleci
2024-07-22 14:35:56 +02:00
parent 7f320de4fa
commit c0fbd7e57a
3 changed files with 12 additions and 14 deletions

View File

@@ -11,7 +11,7 @@
<!-- Version --> <!-- Version -->
<PropertyGroup> <PropertyGroup>
<VersionPrefix>2.2.3</VersionPrefix> <VersionPrefix>2.2.4</VersionPrefix>
<VersionSuffix Condition="'$(VersionSuffix)' == ''"></VersionSuffix> <VersionSuffix Condition="'$(VersionSuffix)' == ''"></VersionSuffix>
</PropertyGroup> </PropertyGroup>

View File

@@ -10,7 +10,7 @@ public static unsafe class ID3D11DeviceChildExtensions
public static string? GetDebugName<TD3D11DeviceChild>(ref this TD3D11DeviceChild self) public static string? GetDebugName<TD3D11DeviceChild>(ref this TD3D11DeviceChild self)
where TD3D11DeviceChild : unmanaged, ID3D11DeviceChild.Interface where TD3D11DeviceChild : unmanaged, ID3D11DeviceChild.Interface
{ {
sbyte* pname = stackalloc sbyte[1024]; byte* pname = stackalloc byte[1024];
uint size = 1024 - 1; uint size = 1024 - 1;
if (self.GetPrivateData(ID3D11DeviceChild.D3DDebugObjectNameGuid, &size, pname).Failure) if (self.GetPrivateData(ID3D11DeviceChild.D3DDebugObjectNameGuid, &size, pname).Failure)
{ {
@@ -30,7 +30,7 @@ public static unsafe class ID3D11DeviceChildExtensions
} }
else else
{ {
fixed (sbyte* valuePtr = value.GetUtf8Span()) fixed (byte* valuePtr = value.GetUtf8Span())
{ {
_ = self.SetPrivateData(ID3D11DeviceChild.D3DDebugObjectNameGuid, (uint)value!.Length, valuePtr); _ = self.SetPrivateData(ID3D11DeviceChild.D3DDebugObjectNameGuid, (uint)value!.Length, valuePtr);
} }

View File

@@ -1,9 +1,7 @@
// Copyright (c) Amer Koleci and contributors. // Copyright (c) Amer Koleci and contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information. // Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
using System.Text; using System.Text;
using static Win32.UnsafeUtilities;
namespace Win32; namespace Win32;
@@ -12,7 +10,7 @@ namespace Win32;
/// </summary> /// </summary>
public static unsafe class StringUtilities public static unsafe class StringUtilities
{ {
public static string? GetString(sbyte* pointer, int maxLength = -1) public static string? GetString(byte* pointer, int maxLength = -1)
{ {
return GetUtf8Span(pointer, maxLength).GetString(); return GetUtf8Span(pointer, maxLength).GetString();
} }
@@ -26,7 +24,7 @@ public static unsafe class StringUtilities
/// <param name="source">The string for which to get the null-terminated UTF8 character sequence.</param> /// <param name="source">The string for which to get the null-terminated UTF8 character sequence.</param>
/// <returns>A null-terminated UTF8 character sequence created from <paramref name="source" />.</returns> /// <returns>A null-terminated UTF8 character sequence created from <paramref name="source" />.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<sbyte> GetUtf8Span(this string? source) public static ReadOnlySpan<byte> GetUtf8Span(this string? source)
{ {
ReadOnlySpan<byte> result; ReadOnlySpan<byte> result;
@@ -42,7 +40,7 @@ public static unsafe class StringUtilities
result = null; result = null;
} }
return result.As<byte, sbyte>(); return result;
} }
/// <summary>Gets a span for a null-terminated UTF8 character sequence.</summary> /// <summary>Gets a span for a null-terminated UTF8 character sequence.</summary>
@@ -50,7 +48,7 @@ public static unsafe class StringUtilities
/// <param name="maxLength">The maxmimum length of <paramref name="source" /> or <c>-1</c> if the maximum length is unknown.</param> /// <param name="maxLength">The maxmimum length of <paramref name="source" /> or <c>-1</c> if the maximum length is unknown.</param>
/// <returns>A span that starts at <paramref name="source" /> and extends to <paramref name="maxLength" /> or the first null character, whichever comes first.</returns> /// <returns>A span that starts at <paramref name="source" /> and extends to <paramref name="maxLength" /> or the first null character, whichever comes first.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<sbyte> GetUtf8Span(sbyte* source, int maxLength = -1) public static ReadOnlySpan<byte> GetUtf8Span(byte* source, int maxLength = -1)
=> (source != null) ? GetUtf8Span(in source[0], maxLength) : null; => (source != null) ? GetUtf8Span(in source[0], maxLength) : null;
/// <summary>Gets a span for a null-terminated UTF8 character sequence.</summary> /// <summary>Gets a span for a null-terminated UTF8 character sequence.</summary>
@@ -58,9 +56,9 @@ public static unsafe class StringUtilities
/// <param name="maxLength">The maxmimum length of <paramref name="source" /> or <c>-1</c> if the maximum length is unknown.</param> /// <param name="maxLength">The maxmimum length of <paramref name="source" /> or <c>-1</c> if the maximum length is unknown.</param>
/// <returns>A span that starts at <paramref name="source" /> and extends to <paramref name="maxLength" /> or the first null character, whichever comes first.</returns> /// <returns>A span that starts at <paramref name="source" /> and extends to <paramref name="maxLength" /> or the first null character, whichever comes first.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<sbyte> GetUtf8Span(in sbyte source, int maxLength = -1) public static ReadOnlySpan<byte> GetUtf8Span(in byte source, int maxLength = -1)
{ {
ReadOnlySpan<sbyte> result; ReadOnlySpan<byte> result;
if (!Unsafe.IsNullRef(in source)) if (!Unsafe.IsNullRef(in source))
{ {
@@ -70,7 +68,7 @@ public static unsafe class StringUtilities
} }
result = MemoryMarshal.CreateReadOnlySpan(in source, maxLength); result = MemoryMarshal.CreateReadOnlySpan(in source, maxLength);
var length = result.IndexOf((sbyte)'\0'); var length = result.IndexOf((byte)'\0');
if (length != -1) if (length != -1)
{ {
@@ -147,11 +145,11 @@ public static unsafe class StringUtilities
/// <param name="span">The span for which to create the string.</param> /// <param name="span">The span for which to create the string.</param>
/// <returns>A string created from <paramref name="span" />.</returns> /// <returns>A string created from <paramref name="span" />.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? GetString(this ReadOnlySpan<sbyte> span) public static string? GetString(this ReadOnlySpan<byte> span)
{ {
if (span.GetPointer() == null) if (span.GetPointer() == null)
return null; return null;
return Encoding.UTF8.GetString(span.As<sbyte, byte>()); return Encoding.UTF8.GetString(span);
} }
} }