2021-03-11 01:01:43 +00:00
|
|
|
#include "buffer.hpp"
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
|
2021-03-12 13:50:21 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2022-07-16 00:41:14 +00:00
|
|
|
std::string
|
|
|
|
buffer_printer::ToString() const
|
2021-03-11 01:01:43 +00:00
|
|
|
{
|
2022-07-16 00:41:14 +00:00
|
|
|
auto& b = buf;
|
|
|
|
std::string out;
|
|
|
|
auto ins = std::back_inserter(out);
|
|
|
|
fmt::format_to(ins, "Buffer[{}/{:#x} bytes]:", b.size(), b.size());
|
|
|
|
|
2021-03-11 01:01:43 +00:00
|
|
|
for (size_t i = 0; i < b.size(); i += 32)
|
|
|
|
{
|
2022-07-16 00:41:14 +00:00
|
|
|
fmt::format_to(ins, "\n{:04x} ", i);
|
2021-03-11 01:01:43 +00:00
|
|
|
|
|
|
|
size_t stop = std::min(b.size(), i + 32);
|
|
|
|
for (size_t j = 0; j < 32; j++)
|
|
|
|
{
|
|
|
|
auto k = i + j;
|
|
|
|
if (j % 4 == 0)
|
2022-07-16 00:41:14 +00:00
|
|
|
out.push_back(' ');
|
2022-09-10 00:51:34 +00:00
|
|
|
if (k >= stop)
|
2022-07-16 00:41:14 +00:00
|
|
|
out.append(" ");
|
2021-03-11 01:01:43 +00:00
|
|
|
else
|
2022-07-16 00:41:14 +00:00
|
|
|
fmt::format_to(ins, "{:02x}", std::to_integer<uint_fast16_t>(b[k]));
|
2021-03-11 01:01:43 +00:00
|
|
|
}
|
2022-07-16 00:41:14 +00:00
|
|
|
out.append(u8" ┃");
|
2021-03-11 01:01:43 +00:00
|
|
|
for (size_t j = i; j < stop; j++)
|
|
|
|
{
|
|
|
|
auto c = std::to_integer<char>(b[j]);
|
|
|
|
if (c == 0x00)
|
2022-07-16 00:41:14 +00:00
|
|
|
out.append(u8"∅");
|
2021-03-11 01:01:43 +00:00
|
|
|
else if (c < 0x20 || c > 0x7e)
|
2022-07-16 00:41:14 +00:00
|
|
|
out.append(u8"·");
|
2021-03-11 01:01:43 +00:00
|
|
|
else
|
2022-07-16 00:41:14 +00:00
|
|
|
out.push_back(c);
|
2021-03-11 01:01:43 +00:00
|
|
|
}
|
2022-07-16 00:41:14 +00:00
|
|
|
out.append(u8"┃");
|
2021-03-11 01:01:43 +00:00
|
|
|
}
|
2022-07-16 00:41:14 +00:00
|
|
|
return out;
|
2021-03-11 01:01:43 +00:00
|
|
|
}
|
2021-03-12 13:50:21 +00:00
|
|
|
} // namespace llarp
|