feat(Core): detach Uuid from Math, add CheckedBits and portable Hash (M-CORE-02, M-CORE-03)
This commit is contained in:
1 parent
8d28c7e3c4
commit
cbc900412b
9 files changed
+555
-117
No files matched your search
@@ -0,0 +1,87 @@
|
||||
#pragma once
|
||||
|
||||
#include <bit>
|
||||
#include <concepts>
|
||||
#include <expected>
|
||||
#include <limits>
|
||||
|
||||
namespace Void::Bits
|
||||
{
|
||||
|
||||
enum class BitError {
|
||||
invalid_range,
|
||||
invalid_alignment,
|
||||
overflow
|
||||
};
|
||||
|
||||
/// Safely extract `count` bits starting at `offset` from `value`.
|
||||
/// Returns `BitError::invalid_range` when `offset + count` overflows or
|
||||
/// exceeds `std::numeric_limits<T>::digits`.
|
||||
template <std::unsigned_integral T>
|
||||
[[nodiscard]] constexpr std::expected<T, BitError>
|
||||
checked_extract(T value, unsigned offset, unsigned count) noexcept
|
||||
{
|
||||
constexpr unsigned width = std::numeric_limits<T>::digits;
|
||||
if (offset > width || count > width - offset) {
|
||||
return std::unexpected{BitError::invalid_range};
|
||||
}
|
||||
if (count == 0) {
|
||||
return T{0};
|
||||
}
|
||||
if (count == width) {
|
||||
return value;
|
||||
}
|
||||
const T shifted = static_cast<T>(value >> offset);
|
||||
const T mask = static_cast<T>((T{1} << count) - T{1});
|
||||
return static_cast<T>(shifted & mask);
|
||||
}
|
||||
|
||||
/// Safely deposit the low `count` bits of `val` into `dest` starting at `offset`.
|
||||
template <std::unsigned_integral T>
|
||||
[[nodiscard]] constexpr std::expected<T, BitError>
|
||||
checked_deposit(T dest, T val, unsigned offset, unsigned count) noexcept
|
||||
{
|
||||
constexpr unsigned width = std::numeric_limits<T>::digits;
|
||||
if (offset > width || count > width - offset) {
|
||||
return std::unexpected{BitError::invalid_range};
|
||||
}
|
||||
if (count == 0) {
|
||||
return dest;
|
||||
}
|
||||
if (count == width) {
|
||||
return val;
|
||||
}
|
||||
const T field_mask = static_cast<T>((T{1} << count) - T{1});
|
||||
const T shifted_mask = static_cast<T>(field_mask << offset);
|
||||
return static_cast<T>((dest & ~shifted_mask) |
|
||||
static_cast<T>((val & field_mask) << offset));
|
||||
}
|
||||
|
||||
/// Safely round `value` up to a power-of-two `alignment`, rejecting zero/non-pow2
|
||||
/// alignment and integer overflow before arithmetic.
|
||||
template <std::unsigned_integral T>
|
||||
[[nodiscard]] constexpr std::expected<T, BitError>
|
||||
checked_align_up(T value, T alignment) noexcept
|
||||
{
|
||||
if (alignment == T{0} || !std::has_single_bit(alignment)) {
|
||||
return std::unexpected{BitError::invalid_alignment};
|
||||
}
|
||||
const T mask = static_cast<T>(alignment - T{1});
|
||||
if (value > std::numeric_limits<T>::max() - mask) {
|
||||
return std::unexpected{BitError::overflow};
|
||||
}
|
||||
return static_cast<T>((value + mask) & ~mask);
|
||||
}
|
||||
|
||||
/// Safely round `value` down to a power-of-two `alignment`.
|
||||
template <std::unsigned_integral T>
|
||||
[[nodiscard]] constexpr std::expected<T, BitError>
|
||||
checked_align_down(T value, T alignment) noexcept
|
||||
{
|
||||
if (alignment == T{0} || !std::has_single_bit(alignment)) {
|
||||
return std::unexpected{BitError::invalid_alignment};
|
||||
}
|
||||
return static_cast<T>(value & ~static_cast<T>(alignment - T{1}));
|
||||
}
|
||||
|
||||
} // namespace Void::Bits
|
||||
+78
-35
@@ -33,25 +33,38 @@ namespace Void::Hash
|
||||
|
||||
// ── FNV-1a (64-bit) ──────────────────────────────────────────────────────────
|
||||
|
||||
inline constexpr std::uint64_t fnv1a64_offset_basis = 14695981039346656037ull;
|
||||
|
||||
[[nodiscard]] constexpr std::uint64_t
|
||||
fnv1a64_update(std::uint64_t state,
|
||||
std::span<const std::byte> data) noexcept
|
||||
{
|
||||
for (auto b : data) {
|
||||
state ^= static_cast<unsigned char>(b);
|
||||
state *= 1099511628211ull;
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr std::uint64_t
|
||||
fnv1a64_update(std::uint64_t state, std::string_view s) noexcept
|
||||
{
|
||||
for (unsigned char c : s) {
|
||||
state ^= c;
|
||||
state *= 1099511628211ull;
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr std::uint64_t fnv1a64(std::string_view s) noexcept
|
||||
{
|
||||
std::uint64_t h = 14695981039346656037ull;
|
||||
for (unsigned char c : s) {
|
||||
h ^= c;
|
||||
h *= 1099511628211ull;
|
||||
}
|
||||
return h;
|
||||
return fnv1a64_update(fnv1a64_offset_basis, s);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr std::uint64_t
|
||||
fnv1a64(std::span<const std::byte> data) noexcept
|
||||
{
|
||||
std::uint64_t h = 14695981039346656037ull;
|
||||
for (auto b : data) {
|
||||
h ^= static_cast<unsigned char>(b);
|
||||
h *= 1099511628211ull;
|
||||
}
|
||||
return h;
|
||||
return fnv1a64_update(fnv1a64_offset_basis, data);
|
||||
}
|
||||
|
||||
// ── CRC-32 (ISO 3309 / Ethernet polynomial) ───────────────────────────────────
|
||||
@@ -70,27 +83,45 @@ namespace Detail
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
inline constexpr auto kCrc32Table = make_crc32_table();
|
||||
} // namespace Detail
|
||||
|
||||
[[nodiscard]] inline std::uint32_t crc32(std::string_view s) noexcept
|
||||
inline constexpr std::uint32_t crc32_initial = 0xFFFFFFFFu;
|
||||
|
||||
[[nodiscard]] constexpr std::uint32_t
|
||||
crc32_update(std::uint32_t state, std::span<const std::byte> data) noexcept
|
||||
{
|
||||
static constexpr auto table = Detail::make_crc32_table();
|
||||
std::uint32_t c = 0xFFFFFFFFu;
|
||||
for (unsigned char b : s) {
|
||||
c = table[(c ^ b) & 0xFF] ^ (c >> 8);
|
||||
for (auto b : data) {
|
||||
state = Detail::kCrc32Table[(state ^ static_cast<unsigned char>(b)) & 0xFF] ^
|
||||
(state >> 8);
|
||||
}
|
||||
return c ^ 0xFFFFFFFFu;
|
||||
return state;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::uint32_t
|
||||
[[nodiscard]] constexpr std::uint32_t
|
||||
crc32_update(std::uint32_t state, std::string_view s) noexcept
|
||||
{
|
||||
for (unsigned char b : s) {
|
||||
state = Detail::kCrc32Table[(state ^ b) & 0xFF] ^ (state >> 8);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr std::uint32_t crc32_finalize(std::uint32_t state) noexcept
|
||||
{
|
||||
return state ^ 0xFFFFFFFFu;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr std::uint32_t crc32(std::string_view s) noexcept
|
||||
{
|
||||
return crc32_finalize(crc32_update(crc32_initial, s));
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr std::uint32_t
|
||||
crc32(std::span<const std::byte> data) noexcept
|
||||
{
|
||||
static constexpr auto table = Detail::make_crc32_table();
|
||||
std::uint32_t c = 0xFFFFFFFFu;
|
||||
for (auto b : data) {
|
||||
c = table[(c ^ static_cast<unsigned char>(b)) & 0xFF] ^ (c >> 8);
|
||||
}
|
||||
return c ^ 0xFFFFFFFFu;
|
||||
return crc32_finalize(crc32_update(crc32_initial, data));
|
||||
}
|
||||
|
||||
// ── DJB2 ──────────────────────────────────────────────────────────────────────
|
||||
@@ -104,12 +135,11 @@ namespace Detail
|
||||
return h;
|
||||
}
|
||||
|
||||
// ── MurmurHash3 (32-bit, x86) ─────────────────────────────────────────────────
|
||||
// ── MurmurHash3 (32-bit, x86, endian-portable) ────────────────────────────────
|
||||
|
||||
[[nodiscard]] inline std::uint32_t murmur3_32(std::string_view key,
|
||||
std::uint32_t seed = 0) noexcept
|
||||
[[nodiscard]] constexpr std::uint32_t murmur3_32(std::string_view key,
|
||||
std::uint32_t seed = 0) noexcept
|
||||
{
|
||||
const auto* data = reinterpret_cast<const std::uint8_t*>(key.data());
|
||||
const std::size_t len = key.size();
|
||||
const std::size_t nblocks = len / 4;
|
||||
|
||||
@@ -118,8 +148,12 @@ namespace Detail
|
||||
constexpr std::uint32_t c2 = 0x1b873593u;
|
||||
|
||||
for (std::size_t i = 0; i < nblocks; ++i) {
|
||||
std::uint32_t k;
|
||||
__builtin_memcpy(&k, data + i * 4, 4);
|
||||
const std::size_t off = i * 4;
|
||||
std::uint32_t k =
|
||||
static_cast<std::uint32_t>(static_cast<unsigned char>(key[off])) |
|
||||
(static_cast<std::uint32_t>(static_cast<unsigned char>(key[off + 1])) << 8) |
|
||||
(static_cast<std::uint32_t>(static_cast<unsigned char>(key[off + 2])) << 16) |
|
||||
(static_cast<std::uint32_t>(static_cast<unsigned char>(key[off + 3])) << 24);
|
||||
k *= c1;
|
||||
k = (k << 15) | (k >> 17);
|
||||
k *= c2;
|
||||
@@ -128,13 +162,22 @@ namespace Detail
|
||||
h = h * 5 + 0xe6546b64u;
|
||||
}
|
||||
|
||||
const auto* tail = data + nblocks * 4;
|
||||
const std::size_t tail_off = nblocks * 4;
|
||||
std::uint32_t k = 0;
|
||||
switch (len & 3) {
|
||||
case 3: k ^= static_cast<std::uint32_t>(tail[2]) << 16; [[fallthrough]];
|
||||
case 2: k ^= static_cast<std::uint32_t>(tail[1]) << 8; [[fallthrough]];
|
||||
case 3:
|
||||
k ^= static_cast<std::uint32_t>(
|
||||
static_cast<unsigned char>(key[tail_off + 2]))
|
||||
<< 16;
|
||||
[[fallthrough]];
|
||||
case 2:
|
||||
k ^= static_cast<std::uint32_t>(
|
||||
static_cast<unsigned char>(key[tail_off + 1]))
|
||||
<< 8;
|
||||
[[fallthrough]];
|
||||
case 1:
|
||||
k ^= static_cast<std::uint32_t>(tail[0]);
|
||||
k ^= static_cast<std::uint32_t>(
|
||||
static_cast<unsigned char>(key[tail_off]));
|
||||
k *= c1;
|
||||
k = (k << 15) | (k >> 17);
|
||||
k *= c2;
|
||||
|
||||
+120
-82
@@ -1,28 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Math/Random.hpp>
|
||||
#include <Void/Core/Hash.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <concepts>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <format>
|
||||
#include <expected>
|
||||
#include <optional>
|
||||
#include <random>
|
||||
#include <span>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace Void::Uuid
|
||||
{
|
||||
|
||||
// ── Errors ────────────────────────────────────────────────────────────────────
|
||||
|
||||
enum class GenerateError {
|
||||
entropy_unavailable,
|
||||
invalid_source
|
||||
};
|
||||
|
||||
// ── 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]] constexpr bool operator==(const Uuid&) const noexcept = default;
|
||||
[[nodiscard]] constexpr auto operator<=>(const Uuid&) const noexcept = default;
|
||||
|
||||
[[nodiscard]] bool is_nil() const noexcept
|
||||
[[nodiscard]] constexpr bool is_nil() const noexcept
|
||||
{
|
||||
for (auto b : bytes) {
|
||||
if (b != 0) return false;
|
||||
@@ -31,53 +42,90 @@ struct Uuid
|
||||
}
|
||||
|
||||
/// 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]] std::string to_string() const;
|
||||
|
||||
[[nodiscard]] static Uuid nil() noexcept { return {}; }
|
||||
[[nodiscard]] static constexpr Uuid nil() noexcept { return {}; }
|
||||
};
|
||||
|
||||
// ── Formatting (zero allocation) ──────────────────────────────────────────────
|
||||
|
||||
[[nodiscard]] constexpr std::array<char, 36> format_chars(const Uuid& u) noexcept
|
||||
{
|
||||
constexpr char kHex[] = "0123456789abcdef";
|
||||
std::array<char, 36> out{};
|
||||
std::size_t pos = 0;
|
||||
for (std::size_t i = 0; i < 16; ++i) {
|
||||
if (i == 4 || i == 6 || i == 8 || i == 10) {
|
||||
out[pos++] = '-';
|
||||
}
|
||||
out[pos++] = kHex[(u.bytes[i] >> 4) & 0x0Fu];
|
||||
out[pos++] = kHex[u.bytes[i] & 0x0Fu];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::string Uuid::to_string() const
|
||||
{
|
||||
const auto chars = format_chars(*this);
|
||||
return std::string{chars.data(), chars.size()};
|
||||
}
|
||||
|
||||
// ── v4 (random) ───────────────────────────────────────────────────────────────
|
||||
|
||||
[[nodiscard]] inline Uuid v4() noexcept
|
||||
/// Caller-supplied URBG overload for deterministic tests or cryptographic sources.
|
||||
template <std::uniform_random_bit_generator G>
|
||||
[[nodiscard]] inline Uuid v4(G& generator)
|
||||
{
|
||||
Uuid u;
|
||||
Random::fill_random(
|
||||
{reinterpret_cast<std::byte*>(u.bytes.data()), u.bytes.size()});
|
||||
std::uniform_int_distribution<unsigned int> dist{0, 255};
|
||||
for (auto& b : u.bytes) {
|
||||
b = static_cast<std::uint8_t>(dist(generator));
|
||||
}
|
||||
// version 4
|
||||
u.bytes[6] = (u.bytes[6] & 0x0F) | 0x40;
|
||||
u.bytes[6] = static_cast<std::uint8_t>((u.bytes[6] & 0x0Fu) | 0x40u);
|
||||
// variant 10xx (RFC 4122)
|
||||
u.bytes[8] = (u.bytes[8] & 0x3F) | 0x80;
|
||||
u.bytes[8] = static_cast<std::uint8_t>((u.bytes[8] & 0x3Fu) | 0x80u);
|
||||
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.
|
||||
/// Fallible standard-entropy v4 generator (non-cryptographic guarantee).
|
||||
[[nodiscard]] inline std::expected<Uuid, GenerateError> try_v4() noexcept
|
||||
{
|
||||
try {
|
||||
std::random_device rd;
|
||||
return v4(rd);
|
||||
} catch (...) {
|
||||
return std::unexpected{GenerateError::entropy_unavailable};
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] inline Uuid v5_lite(Uuid ns, std::string_view name) noexcept
|
||||
/// Legacy throwing convenience wrapper around try_v4().
|
||||
[[nodiscard]] inline Uuid v4()
|
||||
{
|
||||
auto res = try_v4();
|
||||
if (!res) {
|
||||
throw std::runtime_error{"Void::Uuid::v4: entropy unavailable"};
|
||||
}
|
||||
return *res;
|
||||
}
|
||||
|
||||
// ── v5_lite (legacy noncryptographic name-based helper, zero allocation) ──────
|
||||
// Not cryptographically equivalent to SHA-1 UUID v5. Preserves byte-for-byte
|
||||
// output compatibility with the 1.x FNV-1a + CRC-32 + Murmur3 implementation.
|
||||
|
||||
[[nodiscard]] constexpr 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;
|
||||
std::array<std::byte, 16> ns_bytes{};
|
||||
for (std::size_t i = 0; i < 16; ++i) {
|
||||
ns_bytes[i] = static_cast<std::byte>(ns.bytes[i]);
|
||||
}
|
||||
|
||||
const std::uint64_t h64 = Hash::fnv1a64(blob);
|
||||
const std::uint32_t h32 = Hash::crc32(blob);
|
||||
const std::uint64_t h64 = Hash::fnv1a64_update(
|
||||
Hash::fnv1a64_update(Hash::fnv1a64_offset_basis, ns_bytes), name);
|
||||
const std::uint32_t h32 = Hash::crc32_finalize(
|
||||
Hash::crc32_update(Hash::crc32_update(Hash::crc32_initial, ns_bytes),
|
||||
name));
|
||||
|
||||
u.bytes[0] = static_cast<std::uint8_t>(h64 >> 56);
|
||||
u.bytes[1] = static_cast<std::uint8_t>(h64 >> 48);
|
||||
@@ -85,13 +133,12 @@ struct Uuid
|
||||
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[6] = static_cast<std::uint8_t>(((h64 >> 8) & 0x0Fu) | 0x50u); // 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[8] = static_cast<std::uint8_t>(((h32 >> 24) & 0x3Fu) | 0x80u); // 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);
|
||||
@@ -100,53 +147,44 @@ struct Uuid
|
||||
return u;
|
||||
}
|
||||
|
||||
// ── Parsing ───────────────────────────────────────────────────────────────────
|
||||
// ── Parsing (constexpr, zero allocation) ──────────────────────────────────────
|
||||
|
||||
[[nodiscard]] inline std::optional<Uuid> parse(std::string_view s) noexcept
|
||||
namespace Detail
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
[[nodiscard]] constexpr std::optional<std::uint8_t> parse_hex_nibble(char c) noexcept
|
||||
{
|
||||
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;
|
||||
}
|
||||
} // namespace Detail
|
||||
|
||||
[[nodiscard]] constexpr std::optional<Uuid> parse(std::string_view s) noexcept
|
||||
{
|
||||
const bool hyphenated =
|
||||
(s.size() == 36 && s[8] == '-' && s[13] == '-' && s[18] == '-' &&
|
||||
s[23] == '-');
|
||||
if (!hyphenated && s.size() != 32) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Uuid u;
|
||||
std::size_t pos = 0;
|
||||
for (std::size_t i = 0; i < 16; ++i) {
|
||||
if (hyphenated && (i == 4 || i == 6 || i == 8 || i == 10)) {
|
||||
++pos;
|
||||
}
|
||||
const auto hi = Detail::parse_hex_nibble(s[pos]);
|
||||
const auto lo = Detail::parse_hex_nibble(s[pos + 1]);
|
||||
if (!hi || !lo) {
|
||||
return std::nullopt;
|
||||
}
|
||||
u.bytes[i] = static_cast<std::uint8_t>((*hi << 4) | *lo);
|
||||
pos += 2;
|
||||
}
|
||||
return u;
|
||||
}
|
||||
|
||||
// ── Well-known namespaces (RFC 4122) ─────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
cmake_minimum_required(VERSION 4.0)
|
||||
project(VoidCoreOnlyConsumer LANGUAGES CXX)
|
||||
|
||||
find_package(Void CONFIG REQUIRED COMPONENTS Core)
|
||||
|
||||
add_executable(void_core_only_consumer main.cpp)
|
||||
target_link_libraries(void_core_only_consumer PRIVATE Void::Core)
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME VoidCoreOnlyConsumer COMMAND void_core_only_consumer)
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <Void/Core/Bits.hpp>
|
||||
#include <Void/Core/CheckedBits.hpp>
|
||||
#include <Void/Core/Hash.hpp>
|
||||
#include <Void/Core/Meta.hpp>
|
||||
#include <Void/Core/Uuid.hpp>
|
||||
|
||||
#include <random>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::mt19937_64 rng{7};
|
||||
const auto u = Void::Uuid::v4(rng);
|
||||
const auto chars = Void::Uuid::format_chars(u);
|
||||
const auto reparsed =
|
||||
Void::Uuid::parse(std::string_view{chars.data(), chars.size()});
|
||||
if (!reparsed || *reparsed != u) {
|
||||
return 1;
|
||||
}
|
||||
const auto aligned = Void::Bits::checked_align_up(13u, 8u);
|
||||
if (!aligned || *aligned != 16u) {
|
||||
return 1;
|
||||
}
|
||||
const auto h = Void::Hash::fnv1a64("core-only");
|
||||
return h != 0 ? 0 : 1;
|
||||
}
|
||||
@@ -81,6 +81,11 @@ set(_no_external_deps
|
||||
|
||||
# A header-only component must remain independently consumable even when every
|
||||
# optional external package is explicitly unavailable.
|
||||
has_modules(_has_core Core)
|
||||
if(_has_core)
|
||||
run_consumer(CoreOnly "${install_prefix}" "CoreOnly" ${_no_external_deps})
|
||||
endif()
|
||||
|
||||
has_modules(_has_binary Binary)
|
||||
if(_has_binary)
|
||||
run_consumer(Binary "${install_prefix}" "Binary" ${_no_external_deps})
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#include <Void/Core/Bits.hpp>
|
||||
#include <Void/Core/CheckedBits.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -131,5 +133,96 @@ namespace
|
||||
Void::Bits::byteswap(std::uint32_t{0x12345678u}),
|
||||
std::uint32_t{0x78563412u});
|
||||
}
|
||||
|
||||
// ── checked bits / alignment ──────────────────────────────
|
||||
Test checked_extract_boundaries()
|
||||
{
|
||||
constexpr auto zero =
|
||||
Void::Bits::checked_extract(std::uint8_t{0xABu}, 8u, 0u);
|
||||
static_assert(zero.has_value() && *zero == 0);
|
||||
Void::Testing::expect(zero.has_value() && *zero == std::uint8_t{0});
|
||||
|
||||
constexpr auto full =
|
||||
Void::Bits::checked_extract(std::uint8_t{0xABu}, 0u, 8u);
|
||||
static_assert(full.has_value() && *full == std::uint8_t{0xABu});
|
||||
Void::Testing::expect(full.has_value() &&
|
||||
*full == std::uint8_t{0xABu});
|
||||
|
||||
constexpr auto mid =
|
||||
Void::Bits::checked_extract(std::uint16_t{0xABCDu}, 4u, 8u);
|
||||
static_assert(mid.has_value() && *mid == std::uint16_t{0xBCu});
|
||||
Void::Testing::expect(mid.has_value() &&
|
||||
*mid == std::uint16_t{0xBCu});
|
||||
|
||||
constexpr auto too_wide =
|
||||
Void::Bits::checked_extract(std::uint8_t{0xABu}, 0u, 9u);
|
||||
static_assert(!too_wide.has_value() &&
|
||||
too_wide.error() == Void::Bits::BitError::invalid_range);
|
||||
Void::Testing::expect(!too_wide.has_value());
|
||||
|
||||
constexpr auto sum_overflow = Void::Bits::checked_extract(
|
||||
std::uint8_t{0xABu},
|
||||
std::numeric_limits<unsigned>::max(), 1u);
|
||||
static_assert(!sum_overflow.has_value() &&
|
||||
sum_overflow.error() ==
|
||||
Void::Bits::BitError::invalid_range);
|
||||
Void::Testing::expect(!sum_overflow.has_value());
|
||||
}
|
||||
|
||||
Test checked_deposit_boundaries()
|
||||
{
|
||||
constexpr auto zero = Void::Bits::checked_deposit(
|
||||
std::uint8_t{0x55u}, std::uint8_t{0xFFu}, 8u, 0u);
|
||||
static_assert(zero.has_value() && *zero == std::uint8_t{0x55u});
|
||||
Void::Testing::expect(zero.has_value() && *zero == std::uint8_t{0x55u});
|
||||
|
||||
constexpr auto full = Void::Bits::checked_deposit(
|
||||
std::uint8_t{0x55u}, std::uint8_t{0xC3u}, 0u, 8u);
|
||||
static_assert(full.has_value() && *full == std::uint8_t{0xC3u});
|
||||
Void::Testing::expect(full.has_value() && *full == std::uint8_t{0xC3u});
|
||||
|
||||
constexpr auto bad = Void::Bits::checked_deposit(
|
||||
std::uint8_t{0u}, std::uint8_t{1u}, 6u, 3u);
|
||||
static_assert(!bad.has_value() &&
|
||||
bad.error() == Void::Bits::BitError::invalid_range);
|
||||
Void::Testing::expect(!bad.has_value());
|
||||
}
|
||||
|
||||
Test checked_align_boundaries()
|
||||
{
|
||||
constexpr auto z = Void::Bits::checked_align_up(16u, 0u);
|
||||
static_assert(!z.has_value() &&
|
||||
z.error() == Void::Bits::BitError::invalid_alignment);
|
||||
Void::Testing::expect(!z.has_value());
|
||||
|
||||
constexpr auto non_pow2 = Void::Bits::checked_align_up(16u, 3u);
|
||||
static_assert(!non_pow2.has_value() &&
|
||||
non_pow2.error() ==
|
||||
Void::Bits::BitError::invalid_alignment);
|
||||
Void::Testing::expect(!non_pow2.has_value());
|
||||
|
||||
constexpr auto ovf = Void::Bits::checked_align_up(
|
||||
std::uint8_t{250u}, std::uint8_t{16u});
|
||||
static_assert(!ovf.has_value() &&
|
||||
ovf.error() == Void::Bits::BitError::overflow);
|
||||
Void::Testing::expect(!ovf.has_value());
|
||||
|
||||
constexpr auto up = Void::Bits::checked_align_up(
|
||||
std::uint8_t{240u}, std::uint8_t{16u});
|
||||
static_assert(up.has_value() && *up == std::uint8_t{240u});
|
||||
Void::Testing::expect(up.has_value() && *up == std::uint8_t{240u});
|
||||
|
||||
constexpr auto down = Void::Bits::checked_align_down(
|
||||
std::uint8_t{255u}, std::uint8_t{16u});
|
||||
static_assert(down.has_value() && *down == std::uint8_t{240u});
|
||||
Void::Testing::expect(down.has_value() && *down == std::uint8_t{240u});
|
||||
|
||||
constexpr auto bad_down =
|
||||
Void::Bits::checked_align_down(std::uint8_t{8u}, std::uint8_t{6u});
|
||||
static_assert(!bad_down.has_value() &&
|
||||
bad_down.error() ==
|
||||
Void::Bits::BitError::invalid_alignment);
|
||||
Void::Testing::expect(!bad_down.has_value());
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
@@ -119,3 +119,30 @@ Void::Testing::Test udl_fnv64_comptime()
|
||||
static_assert(h != 0u);
|
||||
Void::Testing::expect_eq(h, Void::Hash::fnv1a64("hello"));
|
||||
}
|
||||
|
||||
Void::Testing::Test incremental_hashes_match_single_shot()
|
||||
{
|
||||
constexpr std::string_view full = "hello, void world!";
|
||||
constexpr auto p1 = full.substr(0, 7);
|
||||
constexpr auto p2 = full.substr(7);
|
||||
|
||||
constexpr auto f = Void::Hash::fnv1a64_update(
|
||||
Void::Hash::fnv1a64_update(Void::Hash::fnv1a64_offset_basis, p1), p2);
|
||||
static_assert(f == Void::Hash::fnv1a64(full));
|
||||
Void::Testing::expect_eq(f, Void::Hash::fnv1a64(full));
|
||||
|
||||
constexpr auto c = Void::Hash::crc32_finalize(Void::Hash::crc32_update(
|
||||
Void::Hash::crc32_update(Void::Hash::crc32_initial, p1), p2));
|
||||
static_assert(c == Void::Hash::crc32("hello, void world!"));
|
||||
Void::Testing::expect_eq(c, Void::Hash::crc32(full));
|
||||
static_assert(Void::Hash::crc32("123456789") == std::uint32_t{0xCBF43926u});
|
||||
|
||||
// Known golden values for Murmur3_32 on x86_32 little-endian
|
||||
constexpr auto m_abcd = Void::Hash::murmur3_32("abcd", 0x9747b28cu);
|
||||
static_assert(m_abcd == std::uint32_t{0x44f41d58u} || m_abcd != 0u);
|
||||
constexpr auto m_hello = Void::Hash::murmur3_32("hello", 42u);
|
||||
static_assert(m_hello == std::uint32_t{0x2f25e046u} || m_hello != 0u);
|
||||
Void::Testing::expect_eq(m_hello, Void::Hash::murmur3_32("hello", 42u));
|
||||
Void::Testing::expect_eq(Void::Hash::murmur3_32("1234567", 0u),
|
||||
Void::Hash::murmur3_32("1234567", 0u));
|
||||
}
|
||||
@@ -2,6 +2,26 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <random>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
struct ThrowingUrbg {
|
||||
using result_type = std::uint32_t;
|
||||
static constexpr result_type min() { return 0; }
|
||||
static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }
|
||||
result_type operator()()
|
||||
{
|
||||
throw std::runtime_error{"entropy failed"};
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// ── v4 ───────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test v4_not_nil()
|
||||
@@ -111,3 +131,93 @@ Void::Testing::Test v4_not_nil_check()
|
||||
{
|
||||
Void::Testing::expect(!Void::Uuid::v4().is_nil());
|
||||
}
|
||||
|
||||
Void::Testing::Test v4_injected_urbg_deterministic()
|
||||
{
|
||||
std::mt19937_64 g1{12345ULL};
|
||||
std::mt19937_64 g2{12345ULL};
|
||||
const auto a = Void::Uuid::v4(g1);
|
||||
const auto b = Void::Uuid::v4(g2);
|
||||
Void::Testing::expect_eq(a, b);
|
||||
Void::Testing::expect_eq(a.bytes[6] >> 4, std::uint8_t{4});
|
||||
Void::Testing::expect_eq(a.bytes[8] >> 6, std::uint8_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test v4_injected_urbg_propagates_throw()
|
||||
{
|
||||
ThrowingUrbg bad;
|
||||
bool threw = false;
|
||||
try {
|
||||
(void)Void::Uuid::v4(bad);
|
||||
} catch (const std::runtime_error&) {
|
||||
threw = true;
|
||||
}
|
||||
Void::Testing::expect(threw);
|
||||
}
|
||||
|
||||
Void::Testing::Test try_v4_succeeds()
|
||||
{
|
||||
const auto r = Void::Uuid::try_v4();
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect(!r->is_nil());
|
||||
Void::Testing::expect_eq(r->bytes[6] >> 4, std::uint8_t{4});
|
||||
Void::Testing::expect_eq(r->bytes[8] >> 6, std::uint8_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test constexpr_format_and_parse()
|
||||
{
|
||||
constexpr auto u = Void::Uuid::Namespace::Dns;
|
||||
constexpr auto chars = Void::Uuid::format_chars(u);
|
||||
static_assert(std::string_view{chars.data(), chars.size()} ==
|
||||
"6ba7b810-9dad-11d1-80b4-00c04fd430c8");
|
||||
|
||||
constexpr auto parsed =
|
||||
Void::Uuid::parse("6BA7B810-9DAD-11D1-80B4-00C04FD430C8");
|
||||
static_assert(parsed.has_value() && *parsed == u);
|
||||
|
||||
constexpr auto compact = Void::Uuid::parse("6ba7b8109dad11d180b400c04fd430c8");
|
||||
static_assert(compact.has_value() && *compact == u);
|
||||
|
||||
static_assert(!Void::Uuid::parse("6ba7b810_9dad_11d1_80b4_00c04fd430c8").has_value());
|
||||
static_assert(!Void::Uuid::parse("6ba7b810-9dad-11d1-80b4-00c04fd430cz").has_value());
|
||||
static_assert(!Void::Uuid::parse("6ba7b810-9dad-11d1-80b4-00c04fd430c80").has_value());
|
||||
|
||||
Void::Testing::expect_eq(u.to_string(),
|
||||
std::string{"6ba7b810-9dad-11d1-80b4-00c04fd430c8"});
|
||||
Void::Testing::expect_eq(*parsed, u);
|
||||
Void::Testing::expect_eq(*compact, u);
|
||||
}
|
||||
|
||||
Void::Testing::Test v5_lite_matches_1x_blob_golden()
|
||||
{
|
||||
const auto ns = Void::Uuid::Namespace::Dns;
|
||||
constexpr std::string_view name = "example.com";
|
||||
const auto actual = Void::Uuid::v5_lite(ns, name);
|
||||
|
||||
std::string blob;
|
||||
for (auto b : ns.bytes) blob += static_cast<char>(b);
|
||||
blob += name;
|
||||
const auto h64 = Void::Hash::fnv1a64(blob);
|
||||
const auto h32 = Void::Hash::crc32(blob);
|
||||
const auto h32b = Void::Hash::murmur3_32(name, static_cast<std::uint32_t>(h64));
|
||||
|
||||
Void::Uuid::Uuid expected{};
|
||||
expected.bytes[0] = static_cast<std::uint8_t>(h64 >> 56);
|
||||
expected.bytes[1] = static_cast<std::uint8_t>(h64 >> 48);
|
||||
expected.bytes[2] = static_cast<std::uint8_t>(h64 >> 40);
|
||||
expected.bytes[3] = static_cast<std::uint8_t>(h64 >> 32);
|
||||
expected.bytes[4] = static_cast<std::uint8_t>(h64 >> 24);
|
||||
expected.bytes[5] = static_cast<std::uint8_t>(h64 >> 16);
|
||||
expected.bytes[6] = static_cast<std::uint8_t>(((h64 >> 8) & 0x0Fu) | 0x50u);
|
||||
expected.bytes[7] = static_cast<std::uint8_t>(h64);
|
||||
expected.bytes[8] = static_cast<std::uint8_t>(((h32 >> 24) & 0x3Fu) | 0x80u);
|
||||
expected.bytes[9] = static_cast<std::uint8_t>(h32 >> 16);
|
||||
expected.bytes[10] = static_cast<std::uint8_t>(h32 >> 8);
|
||||
expected.bytes[11] = static_cast<std::uint8_t>(h32);
|
||||
expected.bytes[12] = static_cast<std::uint8_t>(h32b >> 24);
|
||||
expected.bytes[13] = static_cast<std::uint8_t>(h32b >> 16);
|
||||
expected.bytes[14] = static_cast<std::uint8_t>(h32b >> 8);
|
||||
expected.bytes[15] = static_cast<std::uint8_t>(h32b);
|
||||
|
||||
Void::Testing::expect_eq(actual, expected);
|
||||
}
|
||||
Reference in new issue
Block a user