From cc42d568bde1a59c8c3531faa4a2f4469f4c0bcd Mon Sep 17 00:00:00 2001 From: iks-rain Date: Sat, 6 Dec 2025 16:42:09 +0800 Subject: [PATCH 1/4] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0single=20region?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BinaryToolKit/EndianToolkit.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/BinaryToolKit/EndianToolkit.cs b/BinaryToolKit/EndianToolkit.cs index d6a832d..1bfb890 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 /// From 977f3e07a6cc337da5a3e916b35378a5d02697d1 Mon Sep 17 00:00:00 2001 From: iks-rain Date: Sat, 6 Dec 2025 16:56:55 +0800 Subject: [PATCH 2/4] =?UTF-8?q?perf:=E9=95=BF=E5=BA=A6=E4=B8=BA1=E6=97=B6?= =?UTF-8?q?=E6=97=A0=E9=9C=80=E5=8F=8D=E8=BD=AC,?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BinaryToolKit/EndianToolkit.cs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/BinaryToolKit/EndianToolkit.cs b/BinaryToolKit/EndianToolkit.cs index 1bfb890..f482cc7 100644 --- a/BinaryToolKit/EndianToolkit.cs +++ b/BinaryToolKit/EndianToolkit.cs @@ -143,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); @@ -158,6 +159,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 , @@ -165,22 +167,13 @@ 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); } /// From acd1356de6df38a07fe04882775d361db76e93bf Mon Sep 17 00:00:00 2001 From: iks-rain Date: Sat, 6 Dec 2025 17:03:09 +0800 Subject: [PATCH 3/4] =?UTF-8?q?test:=E6=B7=BB=E5=8A=A0ReverseMany=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BInaryKit-Test/EndianTest.cs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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 From 29f0cc05e351a84ae206289d28522bbe89cdd60e Mon Sep 17 00:00:00 2001 From: iks-rain Date: Sat, 6 Dec 2025 17:04:28 +0800 Subject: [PATCH 4/4] =?UTF-8?q?style:=E8=B0=83=E6=8D=A2=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BinaryToolKit/EndianToolkit.cs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/BinaryToolKit/EndianToolkit.cs b/BinaryToolKit/EndianToolkit.cs index f482cc7..9dd5a39 100644 --- a/BinaryToolKit/EndianToolkit.cs +++ b/BinaryToolKit/EndianToolkit.cs @@ -152,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. /// @@ -176,20 +191,7 @@ allows ref struct 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 }