Skip to content

Commit

Permalink
Handle nulls
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Mar 2, 2023
1 parent a6d49f1 commit 065dabd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Core.Test/KeccakTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public void Zero()

[TestCase("0x0000000000000000000000000000000000000000000000000000000000000000", null, -1)]
[TestCase("0x0000000000000000000000000000000000000000000000000000000000000000", "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", -1)]
[TestCase("0x0000000000000000000000000000000000000000000000000000000000000000", "0x0000000000000000000000000000000000000000000000000000000000000000", 0)]
[TestCase("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", 0)]
[TestCase("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", "0x0000000000000000000000000000000000000000000000000000000000000000", 1)]
public void Compare(string a, string b, int result)
{
Expand Down
28 changes: 28 additions & 0 deletions src/Nethermind/Nethermind.Core/Extensions/Bytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,39 @@ public class BytesComparer : Comparer<byte[]>
{
public override int Compare(byte[]? x, byte[]? y)
{
if (ReferenceEquals(x, y)) return 0;

if (x is null)
{
return y is null ? 0 : 1;
}

if (y is null)
{
return -1;
}

return x.AsSpan().SequenceCompareTo(y);
}

public int Compare(Span<byte> x, Span<byte> 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;
}

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

return x.SequenceCompareTo(y);
}
}
Expand Down

0 comments on commit 065dabd

Please sign in to comment.