Split FrameworkUtil into separate extension classes

Adds bigendian float/double logic
This commit is contained in:
Kurt 2022-07-06 17:30:14 -07:00
parent 240fe8d60f
commit d13488f517
5 changed files with 125 additions and 58 deletions

View file

@ -1,58 +0,0 @@
#if !NET6
#pragma warning disable
// ReSharper disable once UnusedType.Global
global using static PKHeX.Core.Buffers.Binary.Extra.BinaryPrimitives;
using System;
using System.Runtime.InteropServices;
namespace System
{
public static class FutureFeatures
{
public static bool StartsWith(this string str, char value) => str.Length != 0 && str[0] == value;
}
}
namespace PKHeX.Core.Buffers.Binary.Extra
{
internal static class BinaryPrimitives
{
public static float ReadSingleLittleEndian(ReadOnlySpan<byte> data) => MemoryMarshal.Read<float>(data);
public static void WriteSingleLittleEndian(Span<byte> data, float value) => MemoryMarshal.Write(data, ref value);
public static double ReadDoubleLittleEndian(ReadOnlySpan<byte> data) => MemoryMarshal.Read<double>(data);
public static void WriteDoubleLittleEndian(Span<byte> data, double value) => MemoryMarshal.Write(data, ref value);
}
}
namespace System.Runtime.CompilerServices
{
using Diagnostics;
using Diagnostics.CodeAnalysis;
/// <summary>
/// Reserved to be used by the compiler for tracking metadata.
/// This class should not be used by developers in source code.
/// </summary>
[ExcludeFromCodeCoverage, DebuggerNonUserCode]
internal static class IsExternalInit
{
}
}
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Parameter)]
internal sealed class NotNullWhenAttribute : Attribute
{
/// <summary>Initializes the attribute with the specified return value condition.</summary>
/// <param name="returnValue">
/// The return value condition. If the method returns this value, the associated parameter will not be null.
/// </param>
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
/// <summary>Gets the return value condition.</summary>
public bool ReturnValue { get; }
}
}
#pragma warning restore
#endif

View file

@ -0,0 +1,86 @@
#if !NET6
global using static PKHeX.Core.Buffers.Binary.Extra.BinaryPrimitives;
using System;
using System.Runtime.InteropServices;
namespace PKHeX.Core.Buffers.Binary.Extra;
internal static class BinaryPrimitives
{
public static float ReadSingleLittleEndian(ReadOnlySpan<byte> data)
{
var value = MemoryMarshal.Read<float>(data);
if (BitConverter.IsLittleEndian)
return value;
return ReverseEndianness(value);
}
public static double ReadDoubleLittleEndian(ReadOnlySpan<byte> data)
{
var value = MemoryMarshal.Read<double>(data);
if (BitConverter.IsLittleEndian)
return value;
return ReverseEndianness(value);
}
public static float ReadSingleBigEndian(Span<byte> data)
{
var value = MemoryMarshal.Read<float>(data);
if (!BitConverter.IsLittleEndian)
return value;
return ReverseEndianness(value);
}
public static double ReadDoubleBigEndian(Span<byte> data)
{
var value = MemoryMarshal.Read<double>(data);
if (!BitConverter.IsLittleEndian)
return value;
return ReverseEndianness(value);
}
public static void WriteSingleLittleEndian(Span<byte> data, float value)
{
if (!BitConverter.IsLittleEndian)
value = ReverseEndianness(value);
MemoryMarshal.Write(data, ref value);
}
public static void WriteDoubleLittleEndian(Span<byte> data, double value)
{
if (!BitConverter.IsLittleEndian)
value = ReverseEndianness(value);
MemoryMarshal.Write(data, ref value);
}
public static void WriteSingleBigEndian(Span<byte> data, float value)
{
if (BitConverter.IsLittleEndian)
value = ReverseEndianness(value);
MemoryMarshal.Write(data, ref value);
}
public static void WriteDoubleBigEndian(Span<byte> data, double value)
{
if (BitConverter.IsLittleEndian)
value = ReverseEndianness(value);
MemoryMarshal.Write(data, ref value);
}
public static float ReverseEndianness(float input)
{
Span<byte> span = stackalloc byte[4];
MemoryMarshal.Write(span, ref input);
span.Reverse();
return MemoryMarshal.Read<float>(span);
}
public static double ReverseEndianness(double input)
{
Span<byte> span = stackalloc byte[8];
MemoryMarshal.Write(span, ref input);
span.Reverse();
return MemoryMarshal.Read<double>(span);
}
}
#endif

View file

@ -0,0 +1,16 @@
#if !NET6
namespace System.Diagnostics.CodeAnalysis;
[AttributeUsage(AttributeTargets.Parameter)]
internal sealed class NotNullWhenAttribute : Attribute
{
/// <summary>Initializes the attribute with the specified return value condition.</summary>
/// <param name="returnValue">
/// The return value condition. If the method returns this value, the associated parameter will not be null.
/// </param>
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
/// <summary>Gets the return value condition.</summary>
public bool ReturnValue { get; }
}
#endif

View file

@ -0,0 +1,15 @@
#if !NET6
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace System.Runtime.CompilerServices;
/// <summary>
/// Reserved to be used by the compiler for tracking metadata.
/// This class should not be used by developers in source code.
/// </summary>
[ExcludeFromCodeCoverage, DebuggerNonUserCode]
internal static class IsExternalInit
{
}
#endif

View file

@ -0,0 +1,8 @@
#if !NET6
namespace System;
public static class FutureFeatures
{
public static bool StartsWith(this string str, char value) => str.Length != 0 && str[0] == value;
}
#endif