4#ifndef DASMIG_CORE_SEED_SIGNATURE_HPP
5#define DASMIG_CORE_SEED_SIGNATURE_HPP
18#include <system_error>
58 const auto dash = text.find(
'-');
59 const auto seed = parse_hex(text.substr(0, dash));
60 const auto data = dash == std::string_view::npos
61 ? std::optional<std::uint64_t>{0}
62 : parse_hex(text.substr(dash + 1));
65 throw std::invalid_argument(
"seed_signature: expected 16 hex digits, "
66 "optionally followed by '-' and 16 more");
68 return {.seed = *
seed, .data = *
data};
82 static std::string hex(std::uint64_t v)
84 constexpr std::string_view digits =
"0123456789abcdef";
85 std::string out(16,
'0');
86 for (std::size_t i = 16; i-- > 0;)
88 out[i] = digits[v & 0xFU];
94 static std::optional<std::uint64_t> parse_hex(std::string_view text)
96 if (text.size() != 16)
return std::nullopt;
97 for (
const char c : text)
99 const bool digit = c >=
'0' && c <=
'9';
100 const bool lower_hex = c >=
'a' && c <=
'f';
101 if (!digit && !lower_hex)
return std::nullopt;
104 const auto res = std::from_chars(std::to_address(text.begin()), std::to_address(text.end()), v, 16);
105 if (res.ec != std::errc{})
return std::nullopt;
Builds one JSON document.
json_writer & begin_object()
Open an object.
json_writer & field(std::string_view name, const V &v)
Write a key and its value.
json_writer & end_object()
Close the innermost object.
A minimal, dependency-free JSON writer for to_json().
What a result needs to be generated again: its call seed and the data it was generated from.
friend constexpr bool operator==(const seed_signature &, const seed_signature &) noexcept=default
Signatures are equal when seed and data are equal.
void to_json(json_writer &w) const
Write as a JSON object {"seed":"…","data":"…"}.
std::uint64_t seed
The call seed.
std::uint64_t data
Hash of the data used; 0 when not recorded.
static seed_signature parse(std::string_view text)
Parse the text form. A lone seed (16 hex digits) is accepted with data 0.
std::string to_string() const
The text form.