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 9b79281
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 46 deletions.
57 changes: 26 additions & 31 deletions sources/swarm/library/sources/json/placeholder_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
#include <array>
#include <cstdint>
#include <cstring>
#include <span>

namespace swarm {

class StringAppend {
public:
explicit StringAppend(std::string& string) : string_{string}, pos_{0} {}
explicit StringAppend(size_t length, std::string& string)
: string_{string}, pos_{0} {
string_.resize(length, '\0');
}

inline constexpr void Append(const char value) noexcept {
string_[pos_++] = value;
Expand All @@ -29,6 +31,16 @@ class StringAppend {
}
}

using HexArray = const std::array<char, 16>;
static constexpr HexArray kHex = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

inline void AppendAsHex(std::byte value) noexcept {
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 All @@ -37,18 +49,16 @@ class StringAppend {
};

inline void WriteLineNumber(size_t line_number,
StringAppend& hex_vew) noexcept {
hex_vew.AppendDigit(line_number >> 12);
hex_vew.AppendDigit(line_number >> 8);
hex_vew.AppendDigit(line_number >> 4);
hex_vew.AppendDigit(line_number);
hex_vew.Append(':');
hex_vew.Append(' ');
StringAppend& hex_view) noexcept {
hex_view.AppendDigit(line_number >> 12);
hex_view.AppendDigit(line_number >> 8);
hex_view.AppendDigit(line_number >> 4);
hex_view.AppendDigit(line_number);
hex_view.Append(':');
hex_view.Append(' ');
}

template <typename T>
[[nodiscard]] inline constexpr std::span<const char> SpanFrom(
T& text) noexcept {
[[nodiscard]] inline std::span<const char> SpanFrom(const char* text) noexcept {
return std::span(text, strlen(text));
}

Expand All @@ -64,25 +74,17 @@ 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');
const auto bytes_per_row = size_t{16};
const auto rows = length / bytes_per_row;
StringAppend str{hex_view};
StringAppend str{length * 5 + 20, hex_view};

for (size_t i = 0; i < rows; ++i) {
const auto total_bytes = i * bytes_per_row;
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 +103,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
6 changes: 1 addition & 5 deletions sources/swarm/library/sources/json/placeholder_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@

#include <span>
#include <string>
#include <string_view>

namespace swarm {

template <typename T>
[[nodiscard]] auto PlaceholderSumm(T a, T b) -> T {
return a + b;
}

void HexAsciiViewFrom(std::span<const std::byte> data,
std::string& hex_view) noexcept;

Expand Down
20 changes: 20 additions & 0 deletions sources/swarm/library/sources/json/placeholder_function.h.autosave
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: © 2024 Melg Eight <[email protected]>
//
// SPDX-License-Identifier: MIT

#ifndef PLACEHOLDER_FUNCTION_H
#define PLACEHOLDER_FUNCTION_H

#include <span>
#include <string>

namespace swarm {

void HexAsciiViewFrom(std::span<const std::byte> data,
std::string& hex_view) noexcept;

auto HexAsciiViewFrom(std::span<const std::byte> data) noexcept -> std::string;

} // namespace swarm

#endif // PLACEHOLDER_FUNCTION_H
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 9b79281

Please sign in to comment.