Numerics extensions to new namespace

This commit is contained in:
Matt Nadareski
2026-03-21 20:57:03 -04:00
parent 5e907da93a
commit add545ec55
41 changed files with 18672 additions and 18213 deletions

View File

@@ -2,7 +2,7 @@ using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using SabreTools.Numerics;
using SabreTools.Numerics.Extensions;
namespace SabreTools.IO.Extensions
{
@@ -11,688 +11,6 @@ namespace SabreTools.IO.Extensions
/// </summary>
public static class ByteArrayWriterExtensions
{
/// <summary>
/// Write a UInt8 and increment the pointer to an array
/// </summary>
public static bool Write(this byte[] content, ref int offset, byte value)
=> WriteFromBuffer(content, ref offset, [value]);
/// <summary>
/// Write a UInt8 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt8 value)
{
bool actual = content.Write(ref offset, value.LittleEndian);
actual &= content.Write(ref offset, value.BigEndian);
return actual;
}
/// <summary>
/// Write a UInt8[] and increment the pointer to an array
/// </summary>
public static bool Write(this byte[] content, ref int offset, byte[] value)
=> WriteFromBuffer(content, ref offset, value);
/// <summary>
/// Write a UInt8[] and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, byte[] value)
{
Array.Reverse(value);
return WriteFromBuffer(content, ref offset, value);
}
/// <summary>
/// Write an Int8 and increment the pointer to an array
/// </summary>
public static bool Write(this byte[] content, ref int offset, sbyte value)
=> WriteFromBuffer(content, ref offset, [(byte)value]);
/// <summary>
/// Write a Int8 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt8 value)
{
bool actual = content.Write(ref offset, value.LittleEndian);
actual &= content.Write(ref offset, value.BigEndian);
return actual;
}
/// <summary>
/// Write a Char and increment the pointer to an array
/// </summary>
public static bool Write(this byte[] content, ref int offset, char value)
{
byte[] buffer = BitConverter.GetBytes(value);
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a Char with an Encoding and increment the pointer to an array
/// </summary>
public static bool Write(this byte[] content, ref int offset, char value, Encoding encoding)
{
byte[] buffer = encoding.GetBytes($"{value}");
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write an Int16 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, short value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write an Int16 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, short value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write an Int16 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, short value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a Int16 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt16 value)
{
bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian);
actual &= content.WriteBigEndian(ref offset, value.BigEndian);
return actual;
}
/// <summary>
/// Write a UInt16 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, ushort value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write a UInt16 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, ushort value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt16 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, ushort value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt16 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt16 value)
{
bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian);
actual &= content.WriteBigEndian(ref offset, value.BigEndian);
return actual;
}
#if NET5_0_OR_GREATER
/// <summary>
/// Write a Half and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, Half value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write a Half and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, Half value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a Half and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, Half value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
#endif
/// <summary>
/// Write an Int24 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, Int24 value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write an Int24 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, Int24 value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write an Int24 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, Int24 value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt24 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, UInt24 value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write a UInt24 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, UInt24 value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt24 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, UInt24 value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write an Int32 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, int value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write an Int32 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, int value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write an Int32 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, int value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a Int32 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt32 value)
{
bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian);
actual &= content.WriteBigEndian(ref offset, value.BigEndian);
return actual;
}
/// <summary>
/// Write a UInt32 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, uint value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write a UInt32 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, uint value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt32 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, uint value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt32 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt32 value)
{
bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian);
actual &= content.WriteBigEndian(ref offset, value.BigEndian);
return actual;
}
/// <summary>
/// Write a Single and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, float value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write a Single and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, float value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a Single and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, float value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write an Int48 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, Int48 value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write an Int48 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, Int48 value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write an Int48 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, Int48 value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt48 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, UInt48 value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write a UInt48 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, UInt48 value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt48 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, UInt48 value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write an Int64 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, long value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write an Int64 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, long value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write an Int64 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, long value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a Int64 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt64 value)
{
bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian);
actual &= content.WriteBigEndian(ref offset, value.BigEndian);
return actual;
}
/// <summary>
/// Write a UInt64 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, ulong value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write a UInt64 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, ulong value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt64 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, ulong value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt64 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt64 value)
{
bool actual = content.WriteLittleEndian(ref offset, value.LittleEndian);
actual &= content.WriteBigEndian(ref offset, value.BigEndian);
return actual;
}
/// <summary>
/// Write a Double and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, double value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write a Double and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, double value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a Double and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, double value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a Guid and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, Guid value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write a Guid and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, Guid value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a Guid and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, Guid value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
#if NET7_0_OR_GREATER
/// <summary>
/// Write an Int128 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, Int128 value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write an Int128 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, Int128 value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write an Int128 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, Int128 value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt128 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, UInt128 value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write a UInt128 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, UInt128 value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt128 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, UInt128 value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
#endif
/// <summary>
/// Write a Decimal and increment the pointer to an array
/// </summary>
/// <remarks>Writes in machine native format</remarks>
public static bool Write(this byte[] content, ref int offset, decimal value)
{
if (BitConverter.IsLittleEndian)
return content.WriteLittleEndian(ref offset, value);
else
return content.WriteBigEndian(ref offset, value);
}
/// <summary>
/// Write a Decimal and increment the pointer to an array
/// </summary>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this byte[] content, ref int offset, decimal value)
{
byte[] buffer = value.GetBytesBigEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a Decimal and increment the pointer to an array
/// </summary>
/// <remarks>Writes in little-endian format</remarks>
public static bool WriteLittleEndian(this byte[] content, ref int offset, decimal value)
{
byte[] buffer = value.GetBytesLittleEndian();
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a null-terminated string to the array
/// </summary>