2022-02-16 21:20:03 +00:00
|
|
|
|
using System.Buffers;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
|
|
|
|
namespace Shared;
|
2021-11-29 04:04:34 +00:00
|
|
|
|
|
|
|
|
|
public static class Extensions {
|
|
|
|
|
public static string Hex(this Span<byte> span) {
|
|
|
|
|
return span.ToArray().Hex();
|
|
|
|
|
}
|
2022-02-10 01:44:50 +00:00
|
|
|
|
|
|
|
|
|
public static string Hex(this IEnumerable<byte> array) {
|
|
|
|
|
return string.Join(' ', array.ToArray().Select(x => x.ToString("X2")));
|
|
|
|
|
}
|
2022-02-08 16:15:31 +00:00
|
|
|
|
|
|
|
|
|
public static unsafe byte* Ptr(this Span<byte> span) {
|
2022-02-10 01:44:50 +00:00
|
|
|
|
fixed (byte* data = span) {
|
|
|
|
|
return data;
|
|
|
|
|
}
|
2022-02-08 16:15:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string TrimNullTerm(this string text) {
|
|
|
|
|
return text.Split('\0').FirstOrDefault() ?? "";
|
|
|
|
|
}
|
2022-02-16 21:20:03 +00:00
|
|
|
|
|
|
|
|
|
public static IMemoryOwner<byte> RentZero(this MemoryPool<byte> pool, int minSize) {
|
|
|
|
|
IMemoryOwner<byte> owner = pool.Rent(minSize);
|
|
|
|
|
CryptographicOperations.ZeroMemory(owner.Memory.Span);
|
|
|
|
|
return owner;
|
|
|
|
|
}
|
2021-11-29 04:04:34 +00:00
|
|
|
|
}
|