Files
Library/include/Void/Core/Uuid.hpp

204 lines
7.0 KiB
C++

#pragma once
#include <Void/Core/Hash.hpp>
#include <array>
#include <concepts>
#include <cstddef>
#include <cstdint>
#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]] constexpr bool operator==(const Uuid&) const noexcept = default;
[[nodiscard]] constexpr auto operator<=>(const Uuid&) const noexcept = default;
[[nodiscard]] constexpr 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;
[[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) ───────────────────────────────────────────────────────────────
/// 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;
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] = static_cast<std::uint8_t>((u.bytes[6] & 0x0Fu) | 0x40u);
// variant 10xx (RFC 4122)
u.bytes[8] = static_cast<std::uint8_t>((u.bytes[8] & 0x3Fu) | 0x80u);
return u;
}
/// 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};
}
}
/// 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;
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_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);
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) & 0x0Fu) | 0x50u); // version 5
u.bytes[7] = static_cast<std::uint8_t>(h64);
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);
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 (constexpr, zero allocation) ──────────────────────────────────────
namespace Detail
{
[[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) ─────────────────────────────────────────
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