New header-only modules: - Void::Hash — FNV-1a 32/64, CRC-32, DJB2, MurmurHash3-32, UDL literals - Void::Random — Xoshiro256** PRNG, uniform_int/real, coin_flip, shuffle, random_string - Void::Uuid — v4 (random), v5_lite (name-based), parse, RFC 4122 namespaces - Void::Encoding — Base64 (standard + URL-safe), Hex encode/decode - Void::Bits — popcount, bit extract/deposit, rotation, align, byteswap, masks - Void::Math — Vec2/Vec3/Vec4, Mat4 (identity, translation, scale, rotation) Testing module: move Stacktrace from INTERNAL_DEPS to OPTIONAL_DEPS. Added Detail/Color.hpp shim that delegates to Stacktrace::Color when present or provides inline ANSI fallback otherwise. All Stacktrace calls in SignalHandler, ConsoleReporter, and Runner are gated on VOID_HAS_STACKTRACE. Fix Repr.hpp reflect_struct to handle template specializations that lack std::meta::identifier_of (now guarded by has_identifier check). 33/33 tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
166 lines
6.3 KiB
C++
166 lines
6.3 KiB
C++
#pragma once
|
|
|
|
#include <Void/Random/Random.hpp>
|
|
#include <Void/Hash/Hash.hpp>
|
|
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <format>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
namespace Void::Uuid
|
|
{
|
|
|
|
// ── UUID type ─────────────────────────────────────────────────────────────────
|
|
|
|
struct Uuid
|
|
{
|
|
std::array<std::uint8_t, 16> bytes{};
|
|
|
|
[[nodiscard]] bool operator==(const Uuid&) const noexcept = default;
|
|
[[nodiscard]] auto operator<=>(const Uuid&) const noexcept = default;
|
|
|
|
[[nodiscard]] bool is_nil() const noexcept
|
|
{
|
|
for (auto b : bytes) {
|
|
if (b != 0) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// Format as lowercase 8-4-4-4-12 hex string.
|
|
[[nodiscard]] std::string to_string() const
|
|
{
|
|
return std::format(
|
|
"{:02x}{:02x}{:02x}{:02x}-"
|
|
"{:02x}{:02x}-"
|
|
"{:02x}{:02x}-"
|
|
"{:02x}{:02x}-"
|
|
"{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
|
|
bytes[0], bytes[1], bytes[2], bytes[3],
|
|
bytes[4], bytes[5],
|
|
bytes[6], bytes[7],
|
|
bytes[8], bytes[9],
|
|
bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]);
|
|
}
|
|
|
|
[[nodiscard]] static Uuid nil() noexcept { return {}; }
|
|
};
|
|
|
|
// ── v4 (random) ───────────────────────────────────────────────────────────────
|
|
|
|
[[nodiscard]] inline Uuid v4() noexcept
|
|
{
|
|
Uuid u;
|
|
Random::fill_random(
|
|
{reinterpret_cast<std::byte*>(u.bytes.data()), u.bytes.size()});
|
|
// version 4
|
|
u.bytes[6] = (u.bytes[6] & 0x0F) | 0x40;
|
|
// variant 10xx (RFC 4122)
|
|
u.bytes[8] = (u.bytes[8] & 0x3F) | 0x80;
|
|
return u;
|
|
}
|
|
|
|
// ── v5 (name-based SHA-1 substitute using FNV-1a + CRC-32) ──────────────────
|
|
// Not cryptographically equivalent to SHA-1, but deterministic and collision-
|
|
// resistant enough for UUID namespacing without pulling in a crypto library.
|
|
|
|
[[nodiscard]] inline Uuid v5_lite(Uuid ns, std::string_view name) noexcept
|
|
{
|
|
Uuid u;
|
|
// Mix namespace bytes + name bytes using two orthogonal hash functions.
|
|
std::string blob;
|
|
blob.reserve(16 + name.size());
|
|
for (auto b : ns.bytes) blob += static_cast<char>(b);
|
|
blob += name;
|
|
|
|
const std::uint64_t h64 = Hash::fnv1a64(blob);
|
|
const std::uint32_t h32 = Hash::crc32(blob);
|
|
|
|
u.bytes[0] = static_cast<std::uint8_t>(h64 >> 56);
|
|
u.bytes[1] = static_cast<std::uint8_t>(h64 >> 48);
|
|
u.bytes[2] = static_cast<std::uint8_t>(h64 >> 40);
|
|
u.bytes[3] = static_cast<std::uint8_t>(h64 >> 32);
|
|
u.bytes[4] = static_cast<std::uint8_t>(h64 >> 24);
|
|
u.bytes[5] = static_cast<std::uint8_t>(h64 >> 16);
|
|
u.bytes[6] = static_cast<std::uint8_t>((h64 >> 8) & 0x0F) | 0x50; // version 5
|
|
u.bytes[7] = static_cast<std::uint8_t>(h64);
|
|
u.bytes[8] = static_cast<std::uint8_t>((h32 >> 24) & 0x3F) | 0x80; // variant
|
|
u.bytes[9] = static_cast<std::uint8_t>(h32 >> 16);
|
|
u.bytes[10] = static_cast<std::uint8_t>(h32 >> 8);
|
|
u.bytes[11] = static_cast<std::uint8_t>(h32);
|
|
// Refill bytes 12-15 by re-hashing
|
|
const std::uint32_t h32b = Hash::murmur3_32(name, static_cast<std::uint32_t>(h64));
|
|
u.bytes[12] = static_cast<std::uint8_t>(h32b >> 24);
|
|
u.bytes[13] = static_cast<std::uint8_t>(h32b >> 16);
|
|
u.bytes[14] = static_cast<std::uint8_t>(h32b >> 8);
|
|
u.bytes[15] = static_cast<std::uint8_t>(h32b);
|
|
return u;
|
|
}
|
|
|
|
// ── Parsing ───────────────────────────────────────────────────────────────────
|
|
|
|
[[nodiscard]] inline std::optional<Uuid> parse(std::string_view s) noexcept
|
|
{
|
|
// Accept "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (36 chars) or 32 hex chars.
|
|
if (s.size() == 36 &&
|
|
s[8] == '-' && s[13] == '-' && s[18] == '-' && s[23] == '-') {
|
|
std::string flat;
|
|
flat.reserve(32);
|
|
for (std::size_t i = 0; i < s.size(); ++i) {
|
|
if (s[i] != '-') flat += s[i];
|
|
}
|
|
s = flat; // rebind for uniform parsing below
|
|
if (flat.size() != 32) return std::nullopt;
|
|
Uuid u;
|
|
for (std::size_t i = 0; i < 16; ++i) {
|
|
const char hi = flat[i * 2];
|
|
const char lo = flat[i * 2 + 1];
|
|
auto hex = [](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;
|
|
};
|
|
auto h = hex(hi), l = hex(lo);
|
|
if (!h || !l) return std::nullopt;
|
|
u.bytes[i] = static_cast<std::uint8_t>((*h << 4) | *l);
|
|
}
|
|
return u;
|
|
}
|
|
if (s.size() == 32) {
|
|
Uuid u;
|
|
for (std::size_t i = 0; i < 16; ++i) {
|
|
auto hex = [](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;
|
|
};
|
|
auto h = hex(s[i * 2]), l = hex(s[i * 2 + 1]);
|
|
if (!h || !l) return std::nullopt;
|
|
u.bytes[i] = static_cast<std::uint8_t>((*h << 4) | *l);
|
|
}
|
|
return u;
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
// ── Well-known namespaces (RFC 4122) ─────────────────────────────────────────
|
|
|
|
namespace Namespace
|
|
{
|
|
// 6ba7b810-9dad-11d1-80b4-00c04fd430c8
|
|
inline constexpr Uuid Dns{{
|
|
0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1,
|
|
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}};
|
|
// 6ba7b811-9dad-11d1-80b4-00c04fd430c8
|
|
inline constexpr Uuid Url{{
|
|
0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1,
|
|
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}};
|
|
} // namespace Namespace
|
|
|
|
} // namespace Void::Uuid
|