Restructure into umbrella modules (~55 -> 16 targets)
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>
This commit is contained in:
1 parent
da2e93c405
commit
28b94883fd
156 files changed
+278
-220
No files matched your search
@@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace Void::Encoding::Base64
|
||||
{
|
||||
|
||||
namespace Detail
|
||||
{
|
||||
constexpr std::string_view alphabet =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
constexpr std::string_view alphabet_url =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
||||
|
||||
[[nodiscard]] constexpr std::array<std::int8_t, 256>
|
||||
make_decode_table(std::string_view alpha) noexcept
|
||||
{
|
||||
std::array<std::int8_t, 256> t{};
|
||||
t.fill(-1);
|
||||
for (std::size_t i = 0; i < 64; ++i) {
|
||||
t[static_cast<unsigned char>(alpha[i])] = static_cast<std::int8_t>(i);
|
||||
}
|
||||
t[static_cast<unsigned char>('=')] = -2; // padding sentinel
|
||||
return t;
|
||||
}
|
||||
} // namespace Detail
|
||||
|
||||
/// Encode bytes to standard Base64 (with padding).
|
||||
[[nodiscard]] inline std::string encode(std::span<const std::byte> data)
|
||||
{
|
||||
const auto& alpha = Detail::alphabet;
|
||||
std::string out;
|
||||
out.reserve(((data.size() + 2) / 3) * 4);
|
||||
std::size_t i = 0;
|
||||
for (; i + 2 < data.size(); i += 3) {
|
||||
const auto a = static_cast<std::uint8_t>(data[i]);
|
||||
const auto b = static_cast<std::uint8_t>(data[i + 1]);
|
||||
const auto c = static_cast<std::uint8_t>(data[i + 2]);
|
||||
out += alpha[a >> 2];
|
||||
out += alpha[((a & 3) << 4) | (b >> 4)];
|
||||
out += alpha[((b & 15) << 2) | (c >> 6)];
|
||||
out += alpha[c & 63];
|
||||
}
|
||||
if (data.size() - i == 2) {
|
||||
const auto a = static_cast<std::uint8_t>(data[i]);
|
||||
const auto b = static_cast<std::uint8_t>(data[i + 1]);
|
||||
out += alpha[a >> 2];
|
||||
out += alpha[((a & 3) << 4) | (b >> 4)];
|
||||
out += alpha[(b & 15) << 2];
|
||||
out += '=';
|
||||
} else if (data.size() - i == 1) {
|
||||
const auto a = static_cast<std::uint8_t>(data[i]);
|
||||
out += alpha[a >> 2];
|
||||
out += alpha[(a & 3) << 4];
|
||||
out += '=';
|
||||
out += '=';
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Encode a string_view to standard Base64.
|
||||
[[nodiscard]] inline std::string encode(std::string_view s)
|
||||
{
|
||||
return encode(std::as_bytes(std::span{s.data(), s.size()}));
|
||||
}
|
||||
|
||||
/// Encode to URL-safe Base64 (no padding).
|
||||
[[nodiscard]] inline std::string encode_url(std::string_view s)
|
||||
{
|
||||
const auto& alpha = Detail::alphabet_url;
|
||||
auto data = std::as_bytes(std::span{s.data(), s.size()});
|
||||
std::string out;
|
||||
out.reserve(((data.size() + 2) / 3) * 4);
|
||||
std::size_t i = 0;
|
||||
for (; i + 2 < data.size(); i += 3) {
|
||||
const auto a = static_cast<std::uint8_t>(data[i]);
|
||||
const auto b = static_cast<std::uint8_t>(data[i + 1]);
|
||||
const auto c = static_cast<std::uint8_t>(data[i + 2]);
|
||||
out += alpha[a >> 2];
|
||||
out += alpha[((a & 3) << 4) | (b >> 4)];
|
||||
out += alpha[((b & 15) << 2) | (c >> 6)];
|
||||
out += alpha[c & 63];
|
||||
}
|
||||
if (data.size() - i == 2) {
|
||||
const auto a = static_cast<std::uint8_t>(data[i]);
|
||||
const auto b = static_cast<std::uint8_t>(data[i + 1]);
|
||||
out += alpha[a >> 2];
|
||||
out += alpha[((a & 3) << 4) | (b >> 4)];
|
||||
out += alpha[(b & 15) << 2];
|
||||
} else if (data.size() - i == 1) {
|
||||
const auto a = static_cast<std::uint8_t>(data[i]);
|
||||
out += alpha[a >> 2];
|
||||
out += alpha[(a & 3) << 4];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Decode standard Base64 (with or without padding). Returns nullopt on error.
|
||||
[[nodiscard]] inline std::optional<std::string> decode(std::string_view s)
|
||||
{
|
||||
static constexpr auto table = Detail::make_decode_table(Detail::alphabet);
|
||||
std::string out;
|
||||
out.reserve((s.size() / 4) * 3);
|
||||
int buf = 0, bits = 0;
|
||||
for (unsigned char c : s) {
|
||||
if (c == ' ' || c == '\n' || c == '\r' || c == '\t') continue;
|
||||
const std::int8_t v = table[c];
|
||||
if (v == -2) break; // padding
|
||||
if (v == -1) return std::nullopt;
|
||||
buf = (buf << 6) | v;
|
||||
bits += 6;
|
||||
if (bits >= 8) {
|
||||
bits -= 8;
|
||||
out += static_cast<char>((buf >> bits) & 0xFF);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace Void::Encoding::Base64
|
||||
Reference in new issue
Block a user