mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
Add misc helper functions and fix null assertion text
This commit is contained in:
parent
ec6eff156c
commit
f444a999ba
3 changed files with 64 additions and 4 deletions
|
@ -4,6 +4,33 @@ using System.Runtime.InteropServices;
|
|||
|
||||
namespace LibHac.Common
|
||||
{
|
||||
public static class SpanExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the element at the specified zero-based index or gets 0 if the index is out-of-bounds.
|
||||
/// </summary>
|
||||
/// <param name="span">The <see cref="ReadOnlySpan{T}"/> containing the element to get.</param>
|
||||
/// <param name="i">The zero-based index of the element.</param>
|
||||
/// <returns>The element at the specified index or 0 if out-of-bounds.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static byte At(in this ReadOnlySpan<byte> span, int i)
|
||||
{
|
||||
return (uint)i >= (uint)span.Length ? (byte)0 : span[i];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the element at the specified zero-based index or gets 0 if the index is out-of-bounds.
|
||||
/// </summary>
|
||||
/// <param name="span">The <see cref="Span{T}"/> containing the element to get.</param>
|
||||
/// <param name="i">The zero-based index of the element.</param>
|
||||
/// <returns>The element at the specified index or 0 if out-of-bounds.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static byte At(in this Span<byte> span, int i)
|
||||
{
|
||||
return (uint)i >= (uint)span.Length ? (byte)0 : span[i];
|
||||
}
|
||||
}
|
||||
|
||||
public static class SpanHelpers
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
|
|
@ -10,14 +10,14 @@ namespace LibHac.Diag.Impl
|
|||
internal static void InvokeAssertionNotNull(AssertionType assertionType, string valueText, string functionName,
|
||||
string fileName, int lineNumber)
|
||||
{
|
||||
Assert.OnAssertionFailure(assertionType, valueText, functionName, fileName, lineNumber,
|
||||
Assert.OnAssertionFailure(assertionType, "NotNull", functionName, fileName, lineNumber,
|
||||
$"{valueText} must not be nullptr.");
|
||||
}
|
||||
|
||||
internal static void InvokeAssertionNull(AssertionType assertionType, string valueText, string functionName,
|
||||
string fileName, int lineNumber)
|
||||
{
|
||||
Assert.OnAssertionFailure(assertionType, valueText, functionName, fileName, lineNumber,
|
||||
Assert.OnAssertionFailure(assertionType, "Null", functionName, fileName, lineNumber,
|
||||
$"{valueText} must be nullptr.");
|
||||
}
|
||||
|
||||
|
@ -137,12 +137,12 @@ namespace LibHac.Diag.Impl
|
|||
|
||||
public static bool NotNull<T>(Span<T> span)
|
||||
{
|
||||
return !Unsafe.IsNullRef(ref MemoryMarshal.GetReference(span));
|
||||
return !Unsafe.IsNullRef(ref MemoryMarshal.GetReference(span)) || span.Length == 0;
|
||||
}
|
||||
|
||||
public static bool NotNull<T>(ReadOnlySpan<T> span)
|
||||
{
|
||||
return !Unsafe.IsNullRef(ref MemoryMarshal.GetReference(span));
|
||||
return !Unsafe.IsNullRef(ref MemoryMarshal.GetReference(span)) || span.Length == 0;
|
||||
}
|
||||
|
||||
public static bool WithinRange(int value, int lowerInclusive, int upperExclusive)
|
||||
|
|
|
@ -22,6 +22,22 @@ namespace LibHac.Util
|
|||
return i;
|
||||
}
|
||||
|
||||
public static int Copy(Span<byte> dest, ReadOnlySpan<byte> source, int maxLen)
|
||||
{
|
||||
int maxLenLocal = Math.Min(Math.Min(dest.Length, source.Length), maxLen);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < maxLenLocal && source[i] != 0; i++)
|
||||
dest[i] = source[i];
|
||||
|
||||
if (i < dest.Length)
|
||||
{
|
||||
dest[i] = 0;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
public static int GetLength(ReadOnlySpan<byte> s)
|
||||
{
|
||||
int i = 0;
|
||||
|
@ -160,6 +176,23 @@ namespace LibHac.Util
|
|||
return iDest;
|
||||
}
|
||||
|
||||
public static int Find(ReadOnlySpan<byte> haystack, ReadOnlySpan<byte> needle)
|
||||
{
|
||||
if (needle.Length == 0)
|
||||
return 0;
|
||||
|
||||
for (int i = 0; i <= haystack.Length - needle.Length; i++)
|
||||
{
|
||||
int j;
|
||||
for (j = 0; j < needle.Length && haystack[i + j] == needle[j]; j++) { }
|
||||
|
||||
if (j == needle.Length)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static ReadOnlySpan<byte> StringToUtf8(string value)
|
||||
{
|
||||
return Encoding.UTF8.GetBytes(value).AsSpan();
|
||||
|
|
Loading…
Reference in a new issue