Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Nethermind/Nethermind.Core.Test/BytesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte[]> comparer = Bytes.Comparer;
Expand Down
37 changes: 6 additions & 31 deletions src/Nethermind/Nethermind.Core/Extensions/Bytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,24 @@ 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;
return 1;
}

if (x.Length == 0)
{
return y.Length == 0 ? 0 : 1;
return y.Length == 0 ? 0 : -1;
}

for (int i = 0; i < x.Length; i++)
{
if (y.Length <= i)
{
return -1;
return 1;
}

int result = x[i].CompareTo(y[i]);
Comment thread
LukaszRozmej marked this conversation as resolved.
Outdated
Expand All @@ -100,37 +100,12 @@ public override int Compare(byte[]? x, byte[]? y)
}
}

return y.Length > x.Length ? 1 : 0;
return y.Length > x.Length ? -1 : 0;
}

public static int Compare(ReadOnlySpan<byte> x, ReadOnlySpan<byte> y)
Comment thread
LukaszRozmej marked this conversation as resolved.
{
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);
}
}

Expand Down