mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-26 04:58:24 +01:00
first
This commit is contained in:
parent
bd62d51ec9
commit
b578433e37
3 changed files with 784842 additions and 0 deletions
27
Brutal.Core/Extensions/BitOperationsEx.cs
Normal file
27
Brutal.Core/Extensions/BitOperationsEx.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
namespace System.Numerics;
|
||||
|
||||
public static class BitOperationsEx
|
||||
{
|
||||
extension(BitOperations)
|
||||
{
|
||||
/// <summary>
|
||||
/// If <paramref name="mask"/> has only a single bit set, output its <paramref name="index"/>, otherwise return false.
|
||||
/// </summary>
|
||||
/// <param name="mask">A bit-mask value that potentially only contains a single bit.</param>
|
||||
/// <param name="index">The index of the single bit, if true.</param>
|
||||
/// <returns>Whether <paramref name="mask"/> contains a single bit.</returns>
|
||||
public static bool TryGetBitIndex<T>(T mask, out byte index)
|
||||
where T : unmanaged, IBinaryInteger<T>
|
||||
{
|
||||
if (!T.IsPow2(mask))
|
||||
{
|
||||
index = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
var tzc = T.TrailingZeroCount(mask);
|
||||
index = byte.CreateTruncating(tzc);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Brutal.Core/Extensions/SpanExtensions.cs
Normal file
35
Brutal.Core/Extensions/SpanExtensions.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
using CommunityToolkit.HighPerformance;
|
||||
|
||||
namespace Brutal.Extensions;
|
||||
|
||||
public static class SpanExtensions
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool Contains<T>(this Span<T> span, T value)
|
||||
where T : unmanaged, Enum
|
||||
{
|
||||
return ((ReadOnlySpan<T>)span).Contains(value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe bool Contains<T>(this ReadOnlySpan<T> span, T value)
|
||||
where T : unmanaged, Enum
|
||||
{
|
||||
return sizeof(T) switch
|
||||
{
|
||||
sizeof(byte) => span.Cast<T, byte>().Contains(Unsafe.BitCast<T, byte>(value)),
|
||||
sizeof(short) => span.Cast<T, short>().Contains(Unsafe.BitCast<T, short>(value)),
|
||||
sizeof(int) => span.Cast<T, int>().Contains(Unsafe.BitCast<T, int>(value)),
|
||||
sizeof(long) => span.Cast<T, long>().Contains(Unsafe.BitCast<T, long>(value)),
|
||||
_ => throw new NotSupportedException($"Enum of size {sizeof(T)} is not supported."),
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ReadOnlySpan{T}.TryCopyTo"/>
|
||||
public static bool TryCopyTo<T>(this ReadOnlySpan<T> value, Span<T> destination, out int elementsWritten)
|
||||
{
|
||||
var result = value.TryCopyTo(destination);
|
||||
elementsWritten = result ? value.Length : 0;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
29
Brutal.Core/Extensions/Utf8Ex.cs
Normal file
29
Brutal.Core/Extensions/Utf8Ex.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
namespace System.Text.Unicode;
|
||||
|
||||
public static class Utf8Ex
|
||||
{
|
||||
extension(Utf8)
|
||||
{
|
||||
/// <inheritdoc cref="Utf8.TryWrite(Span{byte}, ref Utf8.TryWriteInterpolatedStringHandler, out int)" />
|
||||
public static bool TryFormat<T>(
|
||||
T value,
|
||||
Span<byte> destination,
|
||||
out int bytesWritten,
|
||||
string? format = null,
|
||||
IFormatProvider? provider = null
|
||||
)
|
||||
{
|
||||
// Hijack TryWriteInterpolatedStringHandler, which handles all cases,
|
||||
// including some enum internals we don't have access to.
|
||||
var handler = new Utf8.TryWriteInterpolatedStringHandler(0, 1, destination, provider, out _);
|
||||
if (format == null)
|
||||
handler.AppendFormatted(value);
|
||||
else
|
||||
handler.AppendFormatted(value, format);
|
||||
|
||||
// handler.AppendFormatted() already returns a success bool;
|
||||
// we only call TryWrite() to access bytesWritten, which is internal
|
||||
return Utf8.TryWrite([], ref handler, out bytesWritten);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue