diff --git a/BInaryKit-Test/EndianTest.cs b/BInaryKit-Test/EndianTest.cs index cc65906..36aaa4b 100644 --- a/BInaryKit-Test/EndianTest.cs +++ b/BInaryKit-Test/EndianTest.cs @@ -1,4 +1,5 @@ -using Iks.BinaryToolkit; +using System.Buffers.Binary; +using Iks.BinaryToolkit; using Xunit.Abstractions; namespace BinaryKit_Test; @@ -34,5 +35,30 @@ public unsafe void Read_Struct() output.WriteLine("actual: " + actual); Assert.Equal(expected, actual); } + + #region Multiple-Test + + [Fact] + public void ReverseManyTest() + { + Span old = [10,20,30,40,50,60,70,80]; + Span expected = stackalloc int[old.Length]; + for (var index = 0; index < old.Length; index++) + { + var item = old[index]; + expected[index] = BinaryPrimitives.ReverseEndianness(item); + } + + EndianToolkit.ReverseMany(old); + for (int i = 0; i < old.Length; i++) + { + if (old[i] != expected[i]) + { + Assert.Fail(); + } + } + } + + #endregion } \ No newline at end of file diff --git a/BinaryToolKit/EndianToolkit.cs b/BinaryToolKit/EndianToolkit.cs index d6a832d..9dd5a39 100644 --- a/BinaryToolKit/EndianToolkit.cs +++ b/BinaryToolKit/EndianToolkit.cs @@ -10,6 +10,8 @@ namespace Iks.BinaryToolkit; /// public static class EndianToolkit { + #region Single + /// /// reverses the endianness of an unmanaged type(value). /// @@ -91,6 +93,7 @@ allows ref struct /// converts the endianness of an unmanaged type(value) from one to another. /// it cannot to make sure struct members are all in the same endian.it just reverses byte-endianness of the whole struct. /// + /// target value /// source endian,can use local /// target endian,can use local public static void Convert(scoped ref T value, Endianness from, Endianness to) where T : unmanaged @@ -109,7 +112,9 @@ allows ref struct Reverse(ref value); } - + #endregion + + #region Multiple /// @@ -138,7 +143,8 @@ allows ref struct { throw new ArgumentException(ErrorMessage.Not_Supported_Type_With_Pointer, nameof(T)); } - + //do not reverse + if(sizeof(T) ==1)return; while (lenght-- > 0) { ReverseNoCheck(target); @@ -146,6 +152,21 @@ allows ref struct } } + /// + /// converts the endianness of multiple unmanaged type(value) from one to another. + /// + /// target position to reverse endianness + /// source endian,can use local + /// target endian,can use local + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe void ConvertMany(Span target, Endianness from, Endianness to) where T : unmanaged + { + fixed (T* ptr = &MemoryMarshal.GetReference(target)) + { + ConvertMany(ptr, target.Length, from, to); + } + } + /// /// converts the endianness of multiple unmanaged type(value) from one to another. /// @@ -153,6 +174,7 @@ allows ref struct /// the number of ptr field /// source endian,can use local /// target endian,can use local + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void ConvertMany(T* target, int lenght, Endianness from, Endianness to) where T : unmanaged #if NET9_0_OR_GREATER , @@ -160,38 +182,16 @@ public static unsafe void ConvertMany(T* target, int lenght, Endianness from, allows ref struct #endif { - if (RuntimeHelpers.IsReferenceOrContainsReferences()) - { - throw new ArgumentException(ErrorMessage.Not_Supported_Type_With_Pointer, nameof(T)); - } - //process local if (from is Endianness.Local) from = BitConverter.IsLittleEndian ? Endianness.Little : Endianness.Big; if (to is Endianness.Local) to = BitConverter.IsLittleEndian ? Endianness.Little : Endianness.Big; // same,do nothing if (from == to) return; // differ, reverse - while (lenght-- > 0) - { - ReverseNoCheck(target); - target++; - } + ReverseMany(target, lenght); } - /// - /// converts the endianness of multiple unmanaged type(value) from one to another. - /// - /// target position to reverse endianness - /// source endian,can use local - /// target endian,can use local - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void ConvertMany(Span target, Endianness from, Endianness to) where T : unmanaged - { - fixed (T* ptr = &MemoryMarshal.GetReference(target)) - { - ConvertMany(ptr, target.Length, from, to); - } - } + #endregion }