Skip to content

Commit

Permalink
refactor: hex ascii view
Browse files Browse the repository at this point in the history
Refactor hex ascii view.
  • Loading branch information
melg8 committed Dec 10, 2024
1 parent 2f0fb2b commit 27ad4dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
29 changes: 13 additions & 16 deletions sources/swarm/library/sources/json/placeholder_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ class StringAppend {
}
}

inline constexpr void AppendAsHex(const std::byte value) noexcept {
using HexArray = const std::array<char, 16>;
constexpr HexArray kHex = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
Append(kHex[static_cast<size_t>(value >> 4)]);
Append(kHex[static_cast<size_t>(value & std::byte{0x0f})]);
Append(' ');
}

inline void FinalizeSize() noexcept { string_.resize(pos_, '\0'); }

private:
Expand Down Expand Up @@ -64,9 +73,7 @@ inline void WriteSizeOfData(size_t size, StringAppend& hex_view) noexcept {

void HexAsciiViewFrom(std::span<const std::byte> data,
std::string& hex_view) noexcept {
using HexArray = const std::array<char, 16>;
constexpr HexArray kHex = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

const auto length = data.size();
hex_view.reserve(length * 5 + 20);
hex_view.resize(length * 5 + 20, '\0');
Expand All @@ -79,10 +86,7 @@ void HexAsciiViewFrom(std::span<const std::byte> data,
WriteLineNumber(total_bytes, str);

for (size_t j = 0; j < bytes_per_row; ++j) {
const auto byte = data[total_bytes + j];
str.Append(kHex[static_cast<size_t>(byte >> 4)]);
str.Append(kHex[static_cast<size_t>(byte & std::byte{0x0f})]);
str.Append(' ');
str.AppendAsHex(data[total_bytes + j]);
}
str.Append(' ');
for (size_t j = 0; j < bytes_per_row; ++j) {
Expand All @@ -101,21 +105,14 @@ void HexAsciiViewFrom(std::span<const std::byte> data,

// Fill hex for last row values.
for (size_t j = 0; j < rest_of_bytes; ++j) {
const auto byte = data[rows * bytes_per_row + j];
str.Append(kHex[static_cast<size_t>(byte >> 4)]);
str.Append(kHex[static_cast<size_t>(byte & std::byte{0x0f})]);
str.Append(' ');
str.AppendAsHex(data[rows * bytes_per_row + j]);
}

// Fill hex with spaces at last row if it is not full.
for (size_t j = 0; j < bytes_per_row - rest_of_bytes; ++j) {
for (const char c : SpanFrom(" ")) {
str.Append(c);
}
str.Append(SpanFrom(" "));
}

str.Append(' ');

// Fill ascii last row values.
for (size_t j = 0; j < rest_of_bytes; ++j) {
const auto byte = data[rows * bytes_per_row + j];
Expand Down
17 changes: 7 additions & 10 deletions sources/swarm/tests/unit_tests/sources/placeholder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@ namespace swarm::test {
}

[[nodiscard]] inline std::string ExpectedOuput() noexcept {
std::string e{};
e.reserve(231);
e += "0000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f "
"................\n";
e += "0010: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f "
"................\n";
e += "0020: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e "
" !\"#$%&'()*+,-. \n";
e += "Size: 47 bytes\n";
return e;
// clang-format off
return
"0000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................\n"
"0010: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f ................\n"
"0020: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e !\"#$%&'()*+,-. \n"
"Size: 47 bytes\n";
// clang-format on
}

SCENARIO("placeholder") {
Expand Down

0 comments on commit 27ad4dc

Please sign in to comment.