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
|
||||
@@ -0,0 +1,160 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Diff
|
||||
{
|
||||
|
||||
// ── Edit operations ───────────────────────────────────────────────────────────
|
||||
|
||||
enum class Op { Equal, Insert, Delete };
|
||||
|
||||
template <typename T>
|
||||
struct Edit {
|
||||
Op op;
|
||||
T value;
|
||||
};
|
||||
|
||||
// ── Myers diff ────────────────────────────────────────────────────────────────
|
||||
// Returns the shortest edit script (SES) to transform `a` into `b`.
|
||||
// Uses Myers' O(ND) algorithm.
|
||||
|
||||
template <typename T, typename Eq = std::equal_to<T>>
|
||||
[[nodiscard]] std::vector<Edit<T>>
|
||||
diff(std::span<const T> a, std::span<const T> b, Eq eq = {})
|
||||
{
|
||||
const int n = static_cast<int>(a.size());
|
||||
const int m = static_cast<int>(b.size());
|
||||
const int max_d = n + m;
|
||||
|
||||
if (max_d == 0) return {};
|
||||
|
||||
// v[k + max_d] = furthest-reaching x in diagonal k
|
||||
std::vector<int> v(2 * max_d + 1, -1);
|
||||
// trace[d] = snapshot of v at step d (for backtracking)
|
||||
std::vector<std::vector<int>> trace;
|
||||
|
||||
v[max_d + 1] = 0;
|
||||
bool done = false;
|
||||
int final_d = 0;
|
||||
|
||||
for (int d = 0; d <= max_d && !done; ++d) {
|
||||
trace.push_back(v);
|
||||
for (int k = -d; k <= d; k += 2) {
|
||||
int x;
|
||||
if (k == -d || (k != d && v[max_d + k - 1] < v[max_d + k + 1]))
|
||||
x = v[max_d + k + 1];
|
||||
else
|
||||
x = v[max_d + k - 1] + 1;
|
||||
int y = x - k;
|
||||
while (x < n && y < m && eq(a[x], b[y])) { ++x; ++y; }
|
||||
v[max_d + k] = x;
|
||||
if (x == n && y == m) { final_d = d; done = true; break; }
|
||||
}
|
||||
}
|
||||
|
||||
// Backtrack to reconstruct the edit script
|
||||
std::vector<Edit<T>> script;
|
||||
int x = n, y = m;
|
||||
for (int d = final_d; d > 0; --d) {
|
||||
const auto& pv = trace[d];
|
||||
const int k = x - y;
|
||||
int prev_k;
|
||||
if (k == -d || (k != d && pv[max_d + k - 1] < pv[max_d + k + 1]))
|
||||
prev_k = k + 1;
|
||||
else
|
||||
prev_k = k - 1;
|
||||
const int prev_x = pv[max_d + prev_k];
|
||||
const int prev_y = prev_x - prev_k;
|
||||
// snake (equal)
|
||||
while (x > prev_x && y > prev_y) {
|
||||
--x; --y;
|
||||
script.push_back({Op::Equal, a[x]});
|
||||
}
|
||||
if (d > 0) {
|
||||
if (x == prev_x) { --y; script.push_back({Op::Insert, b[y]}); }
|
||||
else { --x; script.push_back({Op::Delete, a[x]}); }
|
||||
}
|
||||
x = prev_x; y = prev_y;
|
||||
}
|
||||
// Remaining snake at start
|
||||
while (x > 0 && y > 0) { --x; --y; script.push_back({Op::Equal, a[x]}); }
|
||||
|
||||
std::reverse(script.begin(), script.end());
|
||||
return script;
|
||||
}
|
||||
|
||||
// Convenience overload for vectors
|
||||
template <typename T, typename Eq = std::equal_to<T>>
|
||||
[[nodiscard]] std::vector<Edit<T>>
|
||||
diff(const std::vector<T>& a, const std::vector<T>& b, Eq eq = {})
|
||||
{
|
||||
return diff<T, Eq>(std::span<const T>{a}, std::span<const T>{b}, eq);
|
||||
}
|
||||
|
||||
// ── string diff ───────────────────────────────────────────────────────────────
|
||||
// Line-level diff on strings split by newline.
|
||||
// LineDiff is Edit<string> — count_op and apply work on it directly.
|
||||
|
||||
using LineDiff = Edit<std::string>;
|
||||
|
||||
[[nodiscard]] inline std::vector<LineDiff>
|
||||
line_diff(std::string_view a, std::string_view b)
|
||||
{
|
||||
auto split = [](std::string_view s) {
|
||||
std::vector<std::string> lines;
|
||||
std::size_t pos = 0;
|
||||
while (pos <= s.size()) {
|
||||
const auto nl = s.find('\n', pos);
|
||||
lines.emplace_back(s.substr(pos, nl == std::string_view::npos ? nl : nl - pos));
|
||||
if (nl == std::string_view::npos) break;
|
||||
pos = nl + 1;
|
||||
}
|
||||
return lines;
|
||||
};
|
||||
|
||||
return diff(split(a), split(b));
|
||||
}
|
||||
|
||||
// ── helpers ───────────────────────────────────────────────────────────────────
|
||||
|
||||
// Count edits by type
|
||||
template <typename T>
|
||||
[[nodiscard]] std::size_t count_op(const std::vector<Edit<T>>& edits, Op op)
|
||||
{
|
||||
return static_cast<std::size_t>(std::count_if(edits.begin(), edits.end(),
|
||||
[op](const Edit<T>& e) { return e.op == op; }));
|
||||
}
|
||||
|
||||
// Reconstruct the "b" sequence from an edit script applied to "a"
|
||||
template <typename T>
|
||||
[[nodiscard]] std::vector<T> apply(const std::vector<Edit<T>>& edits)
|
||||
{
|
||||
std::vector<T> out;
|
||||
for (const auto& e : edits)
|
||||
if (e.op != Op::Delete) out.push_back(e.value);
|
||||
return out;
|
||||
}
|
||||
|
||||
// Format a unified-diff-style patch for string line diffs
|
||||
[[nodiscard]] inline std::string format_patch(const std::vector<LineDiff>& edits)
|
||||
{
|
||||
std::string out;
|
||||
for (const auto& e : edits) {
|
||||
switch (e.op) {
|
||||
case Op::Equal: out += ' '; break;
|
||||
case Op::Insert: out += '+'; break;
|
||||
case Op::Delete: out += '-'; break;
|
||||
}
|
||||
out += e.value + '\n';
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace Void::Diff
|
||||
@@ -0,0 +1,167 @@
|
||||
#pragma once
|
||||
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Format
|
||||
{
|
||||
|
||||
// ── Named-placeholder template rendering ──────────────────────────────────────
|
||||
// render("Hello, {name}! You are {age} years old.", {{"name","Alice"},{"age","30"}})
|
||||
// → "Hello, Alice! You are 30 years old."
|
||||
//
|
||||
// Unrecognised keys leave the placeholder untouched.
|
||||
|
||||
[[nodiscard]] inline std::string
|
||||
render(std::string_view tmpl,
|
||||
const std::unordered_map<std::string, std::string>& vars)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(tmpl.size());
|
||||
std::size_t i = 0;
|
||||
while (i < tmpl.size()) {
|
||||
if (tmpl[i] == '{') {
|
||||
if (i + 1 < tmpl.size() && tmpl[i + 1] == '{') {
|
||||
out += '{'; i += 2; continue; // escaped {{
|
||||
}
|
||||
const auto close = tmpl.find('}', i + 1);
|
||||
if (close == std::string_view::npos) {
|
||||
out += tmpl[i++]; continue; // unclosed brace — copy verbatim
|
||||
}
|
||||
const std::string key{tmpl.substr(i + 1, close - i - 1)};
|
||||
const auto it = vars.find(key);
|
||||
if (it != vars.end()) {
|
||||
out += it->second;
|
||||
} else {
|
||||
out += '{'; out += key; out += '}';
|
||||
}
|
||||
i = close + 1;
|
||||
} else if (tmpl[i] == '}' && i + 1 < tmpl.size() && tmpl[i+1] == '}') {
|
||||
out += '}'; i += 2; // escaped }}
|
||||
} else {
|
||||
out += tmpl[i++];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── Positional template rendering ────────────────────────────────────────────
|
||||
// render_pos("Hello, {0}! You are {1} years old.", {"Alice", "30"})
|
||||
// → "Hello, Alice! You are 30 years old."
|
||||
|
||||
[[nodiscard]] inline std::string
|
||||
render_pos(std::string_view tmpl, const std::vector<std::string>& args)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(tmpl.size());
|
||||
std::size_t i = 0;
|
||||
while (i < tmpl.size()) {
|
||||
if (tmpl[i] == '{') {
|
||||
if (i + 1 < tmpl.size() && tmpl[i + 1] == '{') {
|
||||
out += '{'; i += 2; continue;
|
||||
}
|
||||
const auto close = tmpl.find('}', i + 1);
|
||||
if (close == std::string_view::npos) { out += tmpl[i++]; continue; }
|
||||
const std::string_view key = tmpl.substr(i + 1, close - i - 1);
|
||||
std::size_t idx = 0;
|
||||
bool valid = !key.empty();
|
||||
for (char c : key) {
|
||||
if (c < '0' || c > '9') { valid = false; break; }
|
||||
idx = idx * 10 + static_cast<std::size_t>(c - '0');
|
||||
}
|
||||
if (valid && idx < args.size()) {
|
||||
out += args[idx];
|
||||
} else {
|
||||
out += '{'; out += key; out += '}';
|
||||
}
|
||||
i = close + 1;
|
||||
} else {
|
||||
out += tmpl[i++];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── Indent ────────────────────────────────────────────────────────────────────
|
||||
// Add `indent` spaces to the beginning of every line.
|
||||
|
||||
[[nodiscard]] inline std::string indent(std::string_view text,
|
||||
std::size_t spaces = 4)
|
||||
{
|
||||
const std::string prefix(spaces, ' ');
|
||||
std::string out;
|
||||
out.reserve(text.size() + spaces * 8);
|
||||
bool start_of_line = true;
|
||||
for (char c : text) {
|
||||
if (start_of_line && c != '\n') { out += prefix; }
|
||||
out += c;
|
||||
start_of_line = (c == '\n');
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── Word-wrap ─────────────────────────────────────────────────────────────────
|
||||
// Wrap text at `width` columns, breaking at whitespace.
|
||||
|
||||
[[nodiscard]] inline std::string word_wrap(std::string_view text,
|
||||
std::size_t width = 80)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(text.size() + text.size() / width + 1);
|
||||
std::size_t col = 0;
|
||||
std::size_t i = 0;
|
||||
while (i < text.size()) {
|
||||
// Find next word boundary
|
||||
const auto space = text.find_first_of(" \t\n", i);
|
||||
const auto word_end = (space == std::string_view::npos) ? text.size() : space;
|
||||
const std::size_t word_len = word_end - i;
|
||||
if (col > 0 && col + 1 + word_len > width) {
|
||||
out += '\n'; col = 0;
|
||||
} else if (col > 0) {
|
||||
out += ' '; ++col;
|
||||
}
|
||||
out.append(text.data() + i, word_len);
|
||||
col += word_len;
|
||||
i = word_end;
|
||||
// Skip whitespace (normalise runs to single space/newline)
|
||||
while (i < text.size() && (text[i] == ' ' || text[i] == '\t')) ++i;
|
||||
if (i < text.size() && text[i] == '\n') { out += '\n'; col = 0; ++i; }
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── Table formatting ─────────────────────────────────────────────────────────
|
||||
// Format a 2-D table of strings as aligned columns separated by `sep`.
|
||||
|
||||
[[nodiscard]] inline std::string
|
||||
format_table(const std::vector<std::vector<std::string>>& rows,
|
||||
std::string_view sep = " ")
|
||||
{
|
||||
if (rows.empty()) return {};
|
||||
const std::size_t ncols = rows[0].size();
|
||||
std::vector<std::size_t> widths(ncols, 0);
|
||||
for (const auto& row : rows) {
|
||||
for (std::size_t c = 0; c < row.size() && c < ncols; ++c) {
|
||||
widths[c] = std::max(widths[c], row[c].size());
|
||||
}
|
||||
}
|
||||
std::string out;
|
||||
for (const auto& row : rows) {
|
||||
for (std::size_t c = 0; c < ncols; ++c) {
|
||||
const auto& cell = c < row.size() ? row[c] : std::string{};
|
||||
out += cell;
|
||||
if (c + 1 < ncols) {
|
||||
out.append(widths[c] - cell.size(), ' ');
|
||||
out += sep;
|
||||
}
|
||||
}
|
||||
out += '\n';
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace Void::Format
|
||||
@@ -0,0 +1,71 @@
|
||||
#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
|
||||
@@ -0,0 +1,132 @@
|
||||
#pragma once
|
||||
|
||||
#include <compare>
|
||||
#include <cstdlib>
|
||||
#include <format>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace Void::Semver
|
||||
{
|
||||
|
||||
// ── Version ───────────────────────────────────────────────────────────────────
|
||||
|
||||
struct Version
|
||||
{
|
||||
unsigned major{0};
|
||||
unsigned minor{0};
|
||||
unsigned patch{0};
|
||||
std::string pre_release; // e.g. "alpha.1"
|
||||
std::string build; // e.g. "20240101"
|
||||
|
||||
[[nodiscard]] std::string to_string() const
|
||||
{
|
||||
std::string s = std::format("{}.{}.{}", major, minor, patch);
|
||||
if (!pre_release.empty()) { s += '-'; s += pre_release; }
|
||||
if (!build.empty()) { s += '+'; s += build; }
|
||||
return s;
|
||||
}
|
||||
|
||||
// Precedence comparison per semver spec (build metadata ignored).
|
||||
[[nodiscard]] std::strong_ordering
|
||||
operator<=>(const Version& o) const noexcept
|
||||
{
|
||||
if (auto c = major <=> o.major; c != 0) return c;
|
||||
if (auto c = minor <=> o.minor; c != 0) return c;
|
||||
if (auto c = patch <=> o.patch; c != 0) return c;
|
||||
// No pre-release > has pre-release
|
||||
const bool l_pre = !pre_release.empty();
|
||||
const bool r_pre = !o.pre_release.empty();
|
||||
if (!l_pre && r_pre) return std::strong_ordering::greater;
|
||||
if (l_pre && !r_pre) return std::strong_ordering::less;
|
||||
// Both have pre-release — lexicographic (simplified)
|
||||
if (auto c = pre_release <=> o.pre_release; c != 0) {
|
||||
return c < 0 ? std::strong_ordering::less
|
||||
: std::strong_ordering::greater;
|
||||
}
|
||||
return std::strong_ordering::equal;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool operator==(const Version& o) const noexcept
|
||||
{
|
||||
return major == o.major && minor == o.minor && patch == o.patch &&
|
||||
pre_release == o.pre_release;
|
||||
}
|
||||
|
||||
// Is this version compatible with `required`? (same major, >= minor.patch)
|
||||
[[nodiscard]] bool compatible_with(const Version& required) const noexcept
|
||||
{
|
||||
if (major != required.major) return false;
|
||||
return (*this <=> required) >= 0;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool is_stable() const noexcept
|
||||
{
|
||||
return major >= 1 && pre_release.empty();
|
||||
}
|
||||
|
||||
[[nodiscard]] Version bumped_major() const noexcept
|
||||
{
|
||||
return {major + 1, 0, 0};
|
||||
}
|
||||
|
||||
[[nodiscard]] Version bumped_minor() const noexcept
|
||||
{
|
||||
return {major, minor + 1, 0};
|
||||
}
|
||||
|
||||
[[nodiscard]] Version bumped_patch() const noexcept
|
||||
{
|
||||
return {major, minor, patch + 1};
|
||||
}
|
||||
};
|
||||
|
||||
// ── Parsing ───────────────────────────────────────────────────────────────────
|
||||
|
||||
[[nodiscard]] inline std::optional<Version> parse(std::string_view s)
|
||||
{
|
||||
Version v;
|
||||
|
||||
auto parse_uint = [](std::string_view sv, unsigned& out) -> bool {
|
||||
if (sv.empty()) return false;
|
||||
out = 0;
|
||||
for (char c : sv) {
|
||||
if (c < '0' || c > '9') return false;
|
||||
out = out * 10 + static_cast<unsigned>(c - '0');
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// Optional leading 'v'
|
||||
if (!s.empty() && (s[0] == 'v' || s[0] == 'V')) s.remove_prefix(1);
|
||||
|
||||
// Split build metadata first
|
||||
const auto plus = s.find('+');
|
||||
if (plus != std::string_view::npos) {
|
||||
v.build = std::string{s.substr(plus + 1)};
|
||||
s = s.substr(0, plus);
|
||||
}
|
||||
|
||||
// Split pre-release
|
||||
const auto dash = s.find('-');
|
||||
if (dash != std::string_view::npos) {
|
||||
v.pre_release = std::string{s.substr(dash + 1)};
|
||||
s = s.substr(0, dash);
|
||||
}
|
||||
|
||||
// Major.minor.patch
|
||||
const auto d1 = s.find('.');
|
||||
if (d1 == std::string_view::npos) return std::nullopt;
|
||||
if (!parse_uint(s.substr(0, d1), v.major)) return std::nullopt;
|
||||
s = s.substr(d1 + 1);
|
||||
|
||||
const auto d2 = s.find('.');
|
||||
if (d2 == std::string_view::npos) return std::nullopt;
|
||||
if (!parse_uint(s.substr(0, d2), v.minor)) return std::nullopt;
|
||||
if (!parse_uint(s.substr(d2 + 1), v.patch)) return std::nullopt;
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
} // namespace Void::Semver
|
||||
@@ -0,0 +1,211 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <charconv>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <system_error>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Str
|
||||
{
|
||||
|
||||
// ── trim ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
[[nodiscard]] inline std::string_view trim_left(std::string_view s) noexcept
|
||||
{
|
||||
const auto it = std::find_if(s.begin(), s.end(),
|
||||
[](unsigned char c) { return !std::isspace(c); });
|
||||
s.remove_prefix(static_cast<std::size_t>(it - s.begin()));
|
||||
return s;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::string_view trim_right(std::string_view s) noexcept
|
||||
{
|
||||
const auto it = std::find_if(s.rbegin(), s.rend(),
|
||||
[](unsigned char c) { return !std::isspace(c); });
|
||||
s.remove_suffix(static_cast<std::size_t>(it - s.rbegin()));
|
||||
return s;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::string_view trim(std::string_view s) noexcept
|
||||
{
|
||||
return trim_right(trim_left(s));
|
||||
}
|
||||
|
||||
// ── case ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
[[nodiscard]] inline std::string to_lower(std::string_view s)
|
||||
{
|
||||
std::string out{s};
|
||||
std::transform(out.begin(), out.end(), out.begin(),
|
||||
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
||||
return out;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::string to_upper(std::string_view s)
|
||||
{
|
||||
std::string out{s};
|
||||
std::transform(out.begin(), out.end(), out.begin(),
|
||||
[](unsigned char c) { return static_cast<char>(std::toupper(c)); });
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── predicate ─────────────────────────────────────────────────────────────────
|
||||
|
||||
[[nodiscard]] inline bool starts_with(std::string_view s, std::string_view prefix) noexcept
|
||||
{
|
||||
return s.size() >= prefix.size() && s.substr(0, prefix.size()) == prefix;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool ends_with(std::string_view s, std::string_view suffix) noexcept
|
||||
{
|
||||
return s.size() >= suffix.size() && s.substr(s.size() - suffix.size()) == suffix;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool contains(std::string_view s, std::string_view needle) noexcept
|
||||
{
|
||||
return s.find(needle) != std::string_view::npos;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool is_alpha(std::string_view s) noexcept
|
||||
{
|
||||
return !s.empty() && std::all_of(s.begin(), s.end(),
|
||||
[](unsigned char c) { return std::isalpha(c) != 0; });
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool is_digit(std::string_view s) noexcept
|
||||
{
|
||||
return !s.empty() && std::all_of(s.begin(), s.end(),
|
||||
[](unsigned char c) { return std::isdigit(c) != 0; });
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool is_alnum(std::string_view s) noexcept
|
||||
{
|
||||
return !s.empty() && std::all_of(s.begin(), s.end(),
|
||||
[](unsigned char c) { return std::isalnum(c) != 0; });
|
||||
}
|
||||
|
||||
// ── split / join ──────────────────────────────────────────────────────────────
|
||||
|
||||
[[nodiscard]] inline std::vector<std::string_view>
|
||||
split(std::string_view s, char delim)
|
||||
{
|
||||
std::vector<std::string_view> parts;
|
||||
std::size_t pos = 0;
|
||||
while (true) {
|
||||
const auto found = s.find(delim, pos);
|
||||
parts.push_back(s.substr(pos, found - pos));
|
||||
if (found == std::string_view::npos) break;
|
||||
pos = found + 1;
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::vector<std::string_view>
|
||||
split(std::string_view s, std::string_view delim)
|
||||
{
|
||||
std::vector<std::string_view> parts;
|
||||
std::size_t pos = 0;
|
||||
while (true) {
|
||||
const auto found = s.find(delim, pos);
|
||||
parts.push_back(s.substr(pos, found - pos));
|
||||
if (found == std::string_view::npos) break;
|
||||
pos = found + delim.size();
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::vector<std::string_view>
|
||||
split_once(std::string_view s, std::string_view delim)
|
||||
{
|
||||
const auto pos = s.find(delim);
|
||||
if (pos == std::string_view::npos) return {s};
|
||||
return {s.substr(0, pos), s.substr(pos + delim.size())};
|
||||
}
|
||||
|
||||
template <typename Range>
|
||||
[[nodiscard]] inline std::string join(const Range& parts, std::string_view sep)
|
||||
{
|
||||
std::string out;
|
||||
bool first = true;
|
||||
for (const auto& p : parts) {
|
||||
if (!first) out += sep;
|
||||
out += p;
|
||||
first = false;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── replace ───────────────────────────────────────────────────────────────────
|
||||
|
||||
[[nodiscard]] inline std::string replace_all(
|
||||
std::string_view s, std::string_view from, std::string_view to)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(s.size());
|
||||
std::size_t pos = 0;
|
||||
while (true) {
|
||||
const auto found = s.find(from, pos);
|
||||
out += s.substr(pos, found - pos);
|
||||
if (found == std::string_view::npos) break;
|
||||
out += to;
|
||||
pos = found + from.size();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── repeat ────────────────────────────────────────────────────────────────────
|
||||
|
||||
[[nodiscard]] inline std::string repeat(std::string_view s, std::size_t n)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(s.size() * n);
|
||||
for (std::size_t i = 0; i < n; ++i) out += s;
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── count_occurrences ─────────────────────────────────────────────────────────
|
||||
|
||||
[[nodiscard]] inline std::size_t count(std::string_view s, std::string_view needle) noexcept
|
||||
{
|
||||
if (needle.empty()) return 0;
|
||||
std::size_t n = 0;
|
||||
std::size_t pos = 0;
|
||||
while ((pos = s.find(needle, pos)) != std::string_view::npos) {
|
||||
++n;
|
||||
pos += needle.size();
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
// ── pad ───────────────────────────────────────────────────────────────────────
|
||||
|
||||
[[nodiscard]] inline std::string pad_left(std::string_view s, std::size_t width, char fill = ' ')
|
||||
{
|
||||
if (s.size() >= width) return std::string{s};
|
||||
return std::string(width - s.size(), fill) + std::string{s};
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::string pad_right(std::string_view s, std::size_t width, char fill = ' ')
|
||||
{
|
||||
if (s.size() >= width) return std::string{s};
|
||||
return std::string{s} + std::string(width - s.size(), fill);
|
||||
}
|
||||
|
||||
// ── numeric conversion ────────────────────────────────────────────────────────
|
||||
|
||||
[[nodiscard]] inline bool to_int(std::string_view s, int& out) noexcept
|
||||
{
|
||||
const auto [ptr, ec] = std::from_chars(s.data(), s.data() + s.size(), out);
|
||||
return ec == std::errc{} && ptr == s.data() + s.size();
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool to_double(std::string_view s, double& out) noexcept
|
||||
{
|
||||
const auto [ptr, ec] = std::from_chars(s.data(), s.data() + s.size(), out);
|
||||
return ec == std::errc{} && ptr == s.data() + s.size();
|
||||
}
|
||||
|
||||
} // namespace Void::Str
|
||||
@@ -0,0 +1,189 @@
|
||||
#pragma once
|
||||
|
||||
#include <cctype>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Tokenizer
|
||||
{
|
||||
|
||||
// ── Token ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
struct Token {
|
||||
std::string kind; // rule name / category
|
||||
std::string value; // raw matched text
|
||||
std::size_t line; // 1-based
|
||||
std::size_t col; // 1-based, column of first char
|
||||
};
|
||||
|
||||
// ── Rule ──────────────────────────────────────────────────────────────────────
|
||||
// A rule matches a prefix of the remaining input.
|
||||
// Returns the number of characters consumed, or 0 on no match.
|
||||
|
||||
using MatchFn = std::function<std::size_t(std::string_view)>;
|
||||
|
||||
struct Rule {
|
||||
std::string kind;
|
||||
MatchFn match;
|
||||
bool skip{false}; // if true, consume but do not emit a token
|
||||
};
|
||||
|
||||
// ── Built-in match helpers ────────────────────────────────────────────────────
|
||||
|
||||
// Matches a fixed literal string.
|
||||
[[nodiscard]] inline MatchFn literal(std::string_view s)
|
||||
{
|
||||
return [s = std::string{s}](std::string_view in) -> std::size_t {
|
||||
if (in.starts_with(s)) return s.size();
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
|
||||
// Matches one or more characters satisfying a predicate.
|
||||
[[nodiscard]] inline MatchFn chars(std::function<bool(char)> pred, std::size_t min = 1)
|
||||
{
|
||||
return [pred, min](std::string_view in) -> std::size_t {
|
||||
std::size_t n = 0;
|
||||
while (n < in.size() && pred(in[n])) ++n;
|
||||
return n >= min ? n : 0;
|
||||
};
|
||||
}
|
||||
|
||||
// Common character classes.
|
||||
[[nodiscard]] inline MatchFn alpha()
|
||||
{
|
||||
return chars([](char c) { return std::isalpha(static_cast<unsigned char>(c)) != 0; });
|
||||
}
|
||||
[[nodiscard]] inline MatchFn digit()
|
||||
{
|
||||
return chars([](char c) { return std::isdigit(static_cast<unsigned char>(c)) != 0; });
|
||||
}
|
||||
[[nodiscard]] inline MatchFn alnum_or_underscore()
|
||||
{
|
||||
return chars([](char c) {
|
||||
return std::isalnum(static_cast<unsigned char>(c)) != 0 || c == '_';
|
||||
});
|
||||
}
|
||||
[[nodiscard]] inline MatchFn whitespace()
|
||||
{
|
||||
return chars([](char c) { return std::isspace(static_cast<unsigned char>(c)) != 0; });
|
||||
}
|
||||
|
||||
// Matches a C-style identifier: [a-zA-Z_][a-zA-Z0-9_]*
|
||||
[[nodiscard]] inline MatchFn identifier()
|
||||
{
|
||||
return [](std::string_view in) -> std::size_t {
|
||||
if (in.empty()) return 0;
|
||||
const char first = in[0];
|
||||
if (!std::isalpha(static_cast<unsigned char>(first)) && first != '_') return 0;
|
||||
std::size_t n = 1;
|
||||
while (n < in.size() &&
|
||||
(std::isalnum(static_cast<unsigned char>(in[n])) || in[n] == '_'))
|
||||
++n;
|
||||
return n;
|
||||
};
|
||||
}
|
||||
|
||||
// Matches a decimal integer literal.
|
||||
[[nodiscard]] inline MatchFn integer_literal()
|
||||
{
|
||||
return [](std::string_view in) -> std::size_t {
|
||||
if (in.empty()) return 0;
|
||||
std::size_t n = 0;
|
||||
if (in[0] == '-' || in[0] == '+') ++n;
|
||||
const std::size_t start = n;
|
||||
while (n < in.size() && std::isdigit(static_cast<unsigned char>(in[n]))) ++n;
|
||||
return n > start ? n : 0;
|
||||
};
|
||||
}
|
||||
|
||||
// Matches a double-quoted string (with simple backslash escape).
|
||||
[[nodiscard]] inline MatchFn quoted_string()
|
||||
{
|
||||
return [](std::string_view in) -> std::size_t {
|
||||
if (in.empty() || in[0] != '"') return 0;
|
||||
std::size_t n = 1;
|
||||
while (n < in.size()) {
|
||||
if (in[n] == '\\') { n += 2; continue; }
|
||||
if (in[n] == '"') { ++n; return n; }
|
||||
++n;
|
||||
}
|
||||
return 0; // unterminated
|
||||
};
|
||||
}
|
||||
|
||||
// ── Tokenizer ─────────────────────────────────────────────────────────────────
|
||||
// Greedy first-match: the first rule that matches at the current position wins.
|
||||
|
||||
class Tokenizer
|
||||
{
|
||||
public:
|
||||
void add_rule(Rule rule) { rules_.push_back(std::move(rule)); }
|
||||
|
||||
void skip(std::string kind, MatchFn fn)
|
||||
{
|
||||
rules_.push_back({std::move(kind), std::move(fn), true});
|
||||
}
|
||||
|
||||
struct Error {
|
||||
std::size_t line;
|
||||
std::size_t col;
|
||||
char unexpected;
|
||||
std::string message;
|
||||
};
|
||||
|
||||
// Tokenize input, returning all tokens (skipped rules emit nothing).
|
||||
// On unrecognized input returns an error.
|
||||
[[nodiscard]] std::variant<std::vector<Token>, Error>
|
||||
tokenize(std::string_view input) const
|
||||
{
|
||||
std::vector<Token> tokens;
|
||||
std::size_t pos = 0;
|
||||
std::size_t line = 1, col = 1;
|
||||
|
||||
while (pos < input.size()) {
|
||||
bool matched = false;
|
||||
for (const auto& rule : rules_) {
|
||||
const std::size_t n = rule.match(input.substr(pos));
|
||||
if (n == 0) continue;
|
||||
if (!rule.skip) {
|
||||
tokens.push_back({rule.kind,
|
||||
std::string{input.substr(pos, n)},
|
||||
line, col});
|
||||
}
|
||||
// Update position bookkeeping
|
||||
for (std::size_t i = 0; i < n; ++i) {
|
||||
if (input[pos + i] == '\n') { ++line; col = 1; }
|
||||
else { ++col; }
|
||||
}
|
||||
pos += n;
|
||||
matched = true;
|
||||
break;
|
||||
}
|
||||
if (!matched) {
|
||||
return Error{line, col, input[pos],
|
||||
std::string{"unexpected character: '"} + input[pos] + "'"};
|
||||
}
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
// Convenience: throws on error.
|
||||
[[nodiscard]] std::vector<Token> tokenize_or_throw(std::string_view input) const
|
||||
{
|
||||
auto result = tokenize(input);
|
||||
if (auto* err = std::get_if<Error>(&result))
|
||||
throw std::runtime_error{err->message};
|
||||
return std::get<std::vector<Token>>(std::move(result));
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Rule> rules_;
|
||||
};
|
||||
|
||||
} // namespace Void::Tokenizer
|
||||
Reference in new issue
Block a user