0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-02 03:32:52 +01:00

ircd:Ⓜ️ Move sigil prefixing out of the random functors in generate_t ctor.

This commit is contained in:
Jason Volk 2018-02-15 13:53:40 -08:00
parent bc186e832d
commit d366523b64

View file

@ -294,9 +294,9 @@ catch(const qi::expectation_failure<const char *> &e)
struct ircd::m::id::printer struct ircd::m::id::printer
:output<const char *> :output<const char *>
{ {
static string_view random_alpha(const id::sigil &, const mutable_buffer &buf, const size_t &len); static string_view random_alpha(const mutable_buffer &buf, const size_t &len);
static string_view random_timebased(const id::sigil &, const mutable_buffer &); static string_view random_timebased(const mutable_buffer &);
static string_view random_prefixed(const id::sigil &, const string_view &prefix, const mutable_buffer &); static string_view random_prefixed(const string_view &prefix, const mutable_buffer &);
template<class generator, template<class generator,
class attribute> class attribute>
@ -351,23 +351,21 @@ struct ircd::m::id::printer
const ircd::m::id::printer; const ircd::m::id::printer;
ircd::string_view ircd::string_view
ircd::m::id::printer::random_prefixed(const id::sigil &sigil, ircd::m::id::printer::random_prefixed(const string_view &prefix,
const string_view &prefix,
const mutable_buffer &buf) const mutable_buffer &buf)
{ {
using buffer::data; using buffer::data;
const auto len const auto len
{ {
fmt::sprintf(buf, "%c%s%u", char(sigil), prefix, rand::integer()) fmt::sprintf(buf, "%s%u", prefix, rand::integer())
}; };
return { data(buf), size_t(len) }; return { data(buf), size_t(len) };
} }
ircd::string_view ircd::string_view
ircd::m::id::printer::random_timebased(const id::sigil &sigil, ircd::m::id::printer::random_timebased(const mutable_buffer &buf)
const mutable_buffer &buf)
{ {
using buffer::data; using buffer::data;
using buffer::size; using buffer::size;
@ -375,15 +373,14 @@ ircd::m::id::printer::random_timebased(const id::sigil &sigil,
const auto utime(microtime()); const auto utime(microtime());
const auto len const auto len
{ {
snprintf(data(buf), size(buf), "%c%zd%06d", char(sigil), utime.first, utime.second) snprintf(data(buf), size(buf), "%zd%06d", utime.first, utime.second)
}; };
return { data(buf), size_t(len) }; return { data(buf), size_t(len) };
} }
ircd::string_view ircd::string_view
ircd::m::id::printer::random_alpha(const id::sigil &sigil, ircd::m::id::printer::random_alpha(const mutable_buffer &buf,
const mutable_buffer &buf,
const size_t &len) const size_t &len)
{ {
using buffer::data; using buffer::data;
@ -463,25 +460,25 @@ ircd::m::id::id(const enum sigil &sigil,
char name[64]; switch(sigil) char name[64]; switch(sigil)
{ {
case sigil::USER: case sigil::USER:
printer::random_prefixed(sigil::USER, "guest", name); printer::random_prefixed("guest", name);
break; break;
case sigil::ROOM_ALIAS: case sigil::ROOM_ALIAS:
printer::random_prefixed(sigil::ROOM_ALIAS, "", name); printer::random_prefixed("", name);
break; break;
case sigil::DEVICE: case sigil::DEVICE:
printer::random_alpha(sigil::DEVICE, name, 16); printer::random_alpha(name, 16);
break; break;
default: default:
printer::random_timebased(sigil, name); printer::random_timebased(name);
break; break;
}; };
const auto len const auto len
{ {
fmt::sprintf(buf, "%s:%s", name, host) fmt::sprintf(buf, "%c%s:%s", char(sigil), name, host)
}; };
return string_view { buffer::data(buf), size_t(len) }; return string_view { buffer::data(buf), size_t(len) };