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:
parent
66525fd6f3
commit
767322dc37
5 changed files with 28 additions and 26 deletions
|
@ -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()
|
||||
{
|
||||
|
|
18
ircd/rand.cc
18
ircd/rand.cc
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue