Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
72 lines
2.0 KiB
C++
72 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
namespace Void::Encoding::Hex
|
|
{
|
|
|
|
/// Encode bytes to lowercase hex string.
|
|
[[nodiscard]] inline std::string encode(std::span<const std::byte> data)
|
|
{
|
|
constexpr std::string_view digits = "0123456789abcdef";
|
|
std::string out;
|
|
out.reserve(data.size() * 2);
|
|
for (auto b : data) {
|
|
const auto v = static_cast<std::uint8_t>(b);
|
|
out += digits[v >> 4];
|
|
out += digits[v & 0xF];
|
|
}
|
|
return out;
|
|
}
|
|
|
|
[[nodiscard]] inline std::string encode(std::string_view s)
|
|
{
|
|
return encode(std::as_bytes(std::span{s.data(), s.size()}));
|
|
}
|
|
|
|
/// Decode hex string to bytes. Returns nullopt on invalid input.
|
|
[[nodiscard]] inline std::optional<std::string> decode(std::string_view s)
|
|
{
|
|
if (s.size() % 2 != 0) return std::nullopt;
|
|
auto hex_val = [](char c) -> std::optional<std::uint8_t> {
|
|
if (c >= '0' && c <= '9') return static_cast<std::uint8_t>(c - '0');
|
|
if (c >= 'a' && c <= 'f') return static_cast<std::uint8_t>(c - 'a' + 10);
|
|
if (c >= 'A' && c <= 'F') return static_cast<std::uint8_t>(c - 'A' + 10);
|
|
return std::nullopt;
|
|
};
|
|
std::string out;
|
|
out.reserve(s.size() / 2);
|
|
for (std::size_t i = 0; i < s.size(); i += 2) {
|
|
auto hi = hex_val(s[i]);
|
|
auto lo = hex_val(s[i + 1]);
|
|
if (!hi || !lo) return std::nullopt;
|
|
out += static_cast<char>((*hi << 4) | *lo);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/// Uppercase hex encoding.
|
|
[[nodiscard]] inline std::string encode_upper(std::span<const std::byte> data)
|
|
{
|
|
constexpr std::string_view digits = "0123456789ABCDEF";
|
|
std::string out;
|
|
out.reserve(data.size() * 2);
|
|
for (auto b : data) {
|
|
const auto v = static_cast<std::uint8_t>(b);
|
|
out += digits[v >> 4];
|
|
out += digits[v & 0xF];
|
|
}
|
|
return out;
|
|
}
|
|
|
|
[[nodiscard]] inline std::string encode_upper(std::string_view s)
|
|
{
|
|
return encode_upper(std::as_bytes(std::span{s.data(), s.size()}));
|
|
}
|
|
|
|
} // namespace Void::Encoding::Hex
|