Fix byte order when printing util::uuid & add uuid formatter

This commit is contained in:
Nikita Lisitsa 2026-08-14 16:35:42 +03:00
parent 99063348e3
commit ce5531dbbb
2 changed files with 29 additions and 5 deletions

View file

@ -6,6 +6,8 @@
#include <cstdint>
#include <string_view>
#include <iostream>
#include <bit>
#include <format>
namespace psemek::util
{
@ -44,6 +46,8 @@ namespace psemek::util
return uuid;
}
// Make a version 3 UUID (name-based, using MD5)
// NB: the namespace UUID is omitted (empty)
// TODO: use SHA-1 or something like that?
constexpr uuid make_uuid(std::string_view str)
{
@ -76,4 +80,21 @@ namespace std
}
};
template <typename Char>
struct formatter<::psemek::util::uuid, Char>
{
constexpr auto parse(std::format_parse_context & ctx)
{
return ctx.begin();
}
template <typename FormatContext>
auto format(::psemek::util::uuid const & uuid, FormatContext & ctx) const
{
auto x0 = std::byteswap(uuid[0]);
auto x1 = std::byteswap(uuid[1]);
return std::format_to(ctx.out(), "{:08x}-{:04x}-{:04x}-{:04x}-{:012x}", x0 & 0xffffffffull, (x0 >> 4) & 0xfffful, (x0 >> 6) & 0xfffful, x1 & 0xfffful, (x1 >> 2) & 0xffffffffffffull);
}
};
}

View file

@ -7,16 +7,19 @@ namespace psemek::util
std::ostream & operator << (std::ostream & os, uuid const & uuid)
{
auto x0 = std::byteswap(uuid[0]);
auto x1 = std::byteswap(uuid[1]);
os << std::hex << std::setfill('0') << std::left;
os << std::setw(8) << (uuid[0] & 0x00000000ffffffffull);
os << std::setw(8) << (x0 & 0xffffffffull);
os << std::setw(1) << '-';
os << std::setw(4) << ((uuid[0] & 0x0000ffff00000000ull) >> 32);
os << std::setw(4) << ((x0 >> 4) & 0xfffful);
os << std::setw(1) << '-';
os << std::setw(4) << ((uuid[0] & 0xffff000000000000ull) >> 48);
os << std::setw(4) << ((x0 >> 6) & 0xfffful);
os << std::setw(1) << '-';
os << std::setw(4) << (uuid[1] & 0x000000000000ffffull);
os << std::setw(4) << (x1 & 0xfffful);
os << std::setw(1) << '-';
os << std::setw(12) << ((uuid[1] & 0xffffffffffff0000ull) >> 16);
os << std::setw(12) << ((x1 >> 2) & 0xffffffffffffull);
return os;
}