diff --git a/src/Nethermind/Nethermind.Core.Test/BytesTests.cs b/src/Nethermind/Nethermind.Core.Test/BytesTests.cs index f3371d834c1c..a8e64a7de955 100644 --- a/src/Nethermind/Nethermind.Core.Test/BytesTests.cs +++ b/src/Nethermind/Nethermind.Core.Test/BytesTests.cs @@ -19,11 +19,11 @@ public class BytesTests { [TestCase("0x", "0x", 0)] [TestCase(null, null, 0)] - [TestCase(null, "0x", 1)] - [TestCase("0x", null, -1)] + [TestCase(null, "0x", -1)] + [TestCase("0x", null, 1)] [TestCase("0x01", "0x01", 0)] - [TestCase("0x01", "0x0102", 1)] - [TestCase("0x0102", "0x01", -1)] + [TestCase("0x01", "0x0102", -1)] + [TestCase("0x0102", "0x01", 1)] public void Compares_bytes_properly(string? hexString1, string? hexString2, int expectedResult) { IComparer comparer = Bytes.Comparer; diff --git a/src/Nethermind/Nethermind.Core/Extensions/Bytes.cs b/src/Nethermind/Nethermind.Core/Extensions/Bytes.cs index 8e395ecb7208..4fe4d044ee55 100644 --- a/src/Nethermind/Nethermind.Core/Extensions/Bytes.cs +++ b/src/Nethermind/Nethermind.Core/Extensions/Bytes.cs @@ -73,64 +73,17 @@ public override int Compare(byte[]? x, byte[]? y) if (x is null) { - return y is null ? 0 : 1; + return y is null ? 0 : -1; } - if (y is null) - { - return -1; - } - - if (x.Length == 0) - { - return y.Length == 0 ? 0 : 1; - } - - for (int i = 0; i < x.Length; i++) - { - if (y.Length <= i) - { - return -1; - } + if (y is null) return 1; - int result = x[i].CompareTo(y[i]); - if (result != 0) - { - return result; - } - } - - return y.Length > x.Length ? 1 : 0; + return x.SequenceCompareTo(y); } public static int Compare(ReadOnlySpan x, ReadOnlySpan y) { - if (Unsafe.AreSame(ref MemoryMarshal.GetReference(x), ref MemoryMarshal.GetReference(y)) && - x.Length == y.Length) - { - return 0; - } - - if (x.Length == 0) - { - return y.Length == 0 ? 0 : 1; - } - - for (int i = 0; i < x.Length; i++) - { - if (y.Length <= i) - { - return -1; - } - - int result = x[i].CompareTo(y[i]); - if (result != 0) - { - return result; - } - } - - return y.Length > x.Length ? 1 : 0; + return x.SequenceCompareTo(y); } }