0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-25 00:02:34 +01:00

ircd::rand: Simplify interface; comments/cleanup.

This commit is contained in:
Jason Volk 2020-10-06 15:46:36 -07:00
parent 66525fd6f3
commit 767322dc37
5 changed files with 28 additions and 26 deletions

View file

@ -24,36 +24,48 @@ namespace ircd::rand::dict
/// Tools for randomization
namespace ircd::rand
{
// Interface state.
extern std::random_device device;
extern std::mt19937_64 mt;
// Random integer
uint64_t integer();
uint64_t integer(const uint64_t &min, const uint64_t &max); // inclusive
// Random character from dictionary
char character(const std::string &dict = dict::alnum);
// Random string from dictionary
string_view string(const std::string &dict, const mutable_buffer &out);
std::string string(const std::string &dict, const size_t &len);
// Random string from dictionary, fills buffer
string_view string(const mutable_buffer &out, const std::string &dict);
}
// Random character from dictionary
/// Random character from dictionary
inline char
ircd::rand::character(const std::string &dict)
{
assert(!dict.empty());
return dict.at(integer(0, dict.size() - 1));
const auto pos
{
integer(0, dict.size() - 1)
};
return dict.at(pos);
}
/// Random integer in range (inclusive)
inline uint64_t
ircd::rand::integer(const uint64_t &min,
const uint64_t &max)
{
std::uniform_int_distribution<uint64_t> dist{min, max};
std::uniform_int_distribution<uint64_t> dist
{
min, max
};
return dist(mt);
}
/// Random 64-bits
inline uint64_t
ircd::rand::integer()
{

View file

@ -44,20 +44,9 @@ decltype(ircd::rand::dict::numeric) ircd::rand::dict::numeric
"0123456789"
};
std::string
ircd::rand::string(const std::string &dict,
const size_t &len)
{
return ircd::util::string(len, [&dict]
(const mutable_buffer &buf)
{
return string(dict, buf);
});
}
ircd::string_view
ircd::rand::string(const std::string &dict,
const mutable_buffer &out)
ircd::rand::string(const mutable_buffer &out,
const std::string &dict)
{
std::uniform_int_distribution<size_t> dist
{
@ -65,9 +54,8 @@ ircd::rand::string(const std::string &dict,
};
std::generate(data(out), data(out) + size(out), [&dict, &dist]
() -> char
{
return dict.at(dist(mt));
return char(dict.at(dist(mt)));
});
return out;

View file

@ -527,14 +527,16 @@ ircd::m::id::id(const enum sigil &sigil,
case sigil::ROOM:
{
static const auto &dict{rand::dict::alnum};
name = rand::string(dict, mutable_buffer{namebuf, 18});
const mutable_buffer dst{namebuf, 18};
name = rand::string(dst, dict);
break;
}
case sigil::DEVICE:
{
static const auto &dict{rand::dict::alnum};
name = rand::string(dict, mutable_buffer{namebuf, 10});
const mutable_buffer dst{namebuf, 10};
name = rand::string(dst, dict);
break;
}

View file

@ -249,8 +249,8 @@ ircd::m::user::tokens::generate(const mutable_buffer &buf)
const mutable_buffer out
{
data(buf), std::min(token_max, size(buf))
buf, token_max
};
return rand::string(token_dict, out);
return rand::string(out, token_dict);
}

View file

@ -52,7 +52,7 @@ post__upload(client &client,
char randbuf[32];
const auto randstr
{
rand::string(rand::dict::alpha, randbuf)
rand::string(randbuf, rand::dict::alpha)
};
const m::media::mxc mxc