utilities: fix refl::calcFieldCount for enum

add unordered_vector utility
modernize hexdump
This commit is contained in:
DH 2024-10-31 22:57:08 +03:00
parent 7d0f277ad5
commit b012964099
3 changed files with 47 additions and 17 deletions

View File

@ -139,11 +139,11 @@ constexpr auto calcFieldCount() {
} else {
constexpr auto c = getNameOf<EnumT(N)>()[0];
if constexpr (requires { EnumT::Count; }) {
return EnumT::Count;
return static_cast<std::size_t>(EnumT::Count);
} else if constexpr (requires { EnumT::_count; }) {
return EnumT::_count;
return static_cast<std::size_t>(EnumT::_count);
} else if constexpr (requires { EnumT::count; }) {
return EnumT::count;
return static_cast<std::size_t>(EnumT::count);
} else if constexpr (!requires { getNameOf<EnumT(N)>()[0]; }) {
return N;
} else if constexpr (c >= '0' && c <= '9') {

View File

@ -0,0 +1,29 @@
#pragma once
#include <cstddef>
#include <utility>
namespace rx {
template <typename VectorT>
void unordered_vector_erase(VectorT &vector, std::size_t pos) {
if (pos + 1 == vector.size()) {
vector.pop_back();
return;
}
std::swap(vector[pos], vector.back());
vector.pop_back();
}
template <typename VectorT, typename Value>
void unordered_vector_insert(VectorT &vector, std::size_t pos, Value &&value) {
bool isLast = pos + 1 == vector.size();
vector.push_back(std::move(value));
if (isLast) {
return;
}
std::swap(vector[pos], vector.back());
}
} // namespace rx

View File

@ -1,6 +1,7 @@
#include "hexdump.hpp"
#include <cstdio>
#include <cstring>
#include <print>
#include <string>
void rx::hexdump(std::span<std::byte> bytes) {
@ -16,13 +17,13 @@ void rx::hexdump(std::span<std::byte> bytes) {
}
flockfile(stderr);
std::fprintf(stderr, "%s ", std::string(sizeWidth, ' ').c_str());
std::print(stderr, "{} ", std::string(sizeWidth, ' '));
for (unsigned i = 0; i < 16; ++i) {
std::fprintf(stderr, " %02x", i);
std::print(stderr, " {:02x}", i);
}
std::fprintf(stderr, "\n%s ", std::string(sizeWidth, ' ').c_str());
std::print(stderr, "\n{} ", std::string(sizeWidth, ' '));
for (unsigned i = 0; i < 16; ++i) {
std::fprintf(stderr, " --");
std::print(stderr, " --");
}
std::byte zeros[16]{};
@ -36,7 +37,7 @@ void rx::hexdump(std::span<std::byte> bytes) {
if (std::memcmp(bytes.data() + i, zeros, 16) == 0) {
if (!dotsPrinted) {
dotsPrinted = true;
std::printf("\n...");
std::print(stderr, "\n...");
}
i += 15;
continue;
@ -44,22 +45,22 @@ void rx::hexdump(std::span<std::byte> bytes) {
}
if (!dotsPrinted) {
std::fprintf(stderr, " | ");
std::print(stderr, " | ");
for (std::size_t j = i - 16; j < i; ++j) {
auto c = unsigned(bytes[j]);
std::fprintf(stderr, "%c",
std::print(stderr, "{:c}",
(std::isprint(c) && c != '\n') ? c : '.');
}
}
}
std::fprintf(stderr, "\n");
std::fprintf(stderr, "%0*zx ", sizeWidth, i);
std::println(stderr, "");
std::print(stderr, "{:0{}x} ", i, sizeWidth);
wasAllZeros = true;
dotsPrinted = false;
}
std::fprintf(stderr, " %02x", unsigned(bytes[i]));
std::print(stderr, " {:02x}", unsigned(bytes[i]));
if (bytes[i] != std::byte{0}) {
wasAllZeros = false;
@ -68,18 +69,18 @@ void rx::hexdump(std::span<std::byte> bytes) {
if (!bytes.empty()) {
for (std::size_t i = 0; i < (16 - (bytes.size() % 16)) % 16; ++i) {
std::fprintf(stderr, " ");
std::print(stderr, " ");
}
std::fprintf(stderr, " | ");
std::print(stderr, " | ");
for (std::size_t j = bytes.size() -
std::min(bytes.size(),
(bytes.size() % 16 ? bytes.size() % 16 : 16));
j < bytes.size(); ++j) {
auto c = unsigned(bytes[j]);
std::fprintf(stderr, "%c", (std::isprint(c) && c != '\n') ? c : '.');
std::print(stderr, "{:c}", (std::isprint(c) && c != '\n') ? c : '.');
}
}
std::fprintf(stderr, "\n");
std::println(stderr, "");
funlockfile(stderr);
}