Skip to content

Commit

Permalink
Small refactor for Bytes (#7729)
Browse files Browse the repository at this point in the history
  • Loading branch information
ak88 authored Nov 6, 2024
1 parent 7e827cd commit ab2937c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Linq;
using Nethermind.Core.Collections;
using Nethermind.Core.ExecutionRequest;
Expand All @@ -18,7 +19,7 @@ public byte[][]? RequestDataParts
set
{
_requestDataParts = value;
RequestData = value is null ? null : Bytes.Concat(value);
RequestData = value is null ? null : Bytes.Concat(value.AsSpan());
}
}
}
Expand Down
38 changes: 12 additions & 26 deletions src/Nethermind/Nethermind.Core/Extensions/Bytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,19 +281,25 @@ public static byte[] Concat(byte[] part1, byte[] part2)
}

public static byte[] Concat(params byte[][] parts)
{
return Concat(parts.AsSpan());
}

public static byte[] Concat(ReadOnlySpan<byte[]> bytes)
{
int totalLength = 0;
for (int i = 0; i < parts.Length; i++)
foreach (byte[] byteArray in bytes)
{
totalLength += parts[i].Length;
totalLength += byteArray.Length;
}

byte[] result = new byte[totalLength];
int position = 0;
for (int i = 0; i < parts.Length; i++)
int offset = 0;

foreach (byte[] byteArray in bytes)
{
Buffer.BlockCopy(parts[i], 0, result, position, parts[i].Length);
position += parts[i].Length;
Array.Copy(byteArray, 0, result, offset, byteArray.Length);
offset += byteArray.Length;
}

return result;
Expand Down Expand Up @@ -334,26 +340,6 @@ public static byte[] Concat(byte[] bytes, byte suffix)
return result;
}

public static byte[] Concat(ReadOnlySpan<byte[]> bytes)
{
int totalLength = 0;
foreach (byte[] byteArray in bytes)
{
totalLength += byteArray.Length;
}

byte[] result = new byte[totalLength];
int offset = 0;

foreach (byte[] byteArray in bytes)
{
Array.Copy(byteArray, 0, result, offset, byteArray.Length);
offset += byteArray.Length;
}

return result;
}

public static byte[] Reverse(byte[] bytes)
{
byte[] result = new byte[bytes.Length];
Expand Down

0 comments on commit ab2937c

Please sign in to comment.