From 767322dc376085c5d65d45c3420c7351f2bff147 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 6 Oct 2020 15:46:36 -0700 Subject: [PATCH] ircd::rand: Simplify interface; comments/cleanup. --- include/ircd/rand.h | 24 ++++++++++++++++++------ ircd/rand.cc | 18 +++--------------- matrix/id.cc | 6 ++++-- matrix/user_tokens.cc | 4 ++-- modules/media/upload.cc | 2 +- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/include/ircd/rand.h b/include/ircd/rand.h index bfe625e78..ba06d8ad1 100644 --- a/include/ircd/rand.h +++ b/include/ircd/rand.h @@ -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 dist{min, max}; + std::uniform_int_distribution dist + { + min, max + }; + return dist(mt); } +/// Random 64-bits inline uint64_t ircd::rand::integer() { diff --git a/ircd/rand.cc b/ircd/rand.cc index 4ffd97327..ed5d1e75a 100644 --- a/ircd/rand.cc +++ b/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 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; diff --git a/matrix/id.cc b/matrix/id.cc index 417b5069e..3fdee9b01 100644 --- a/matrix/id.cc +++ b/matrix/id.cc @@ -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; } diff --git a/matrix/user_tokens.cc b/matrix/user_tokens.cc index 260c898c2..3e83cbb07 100644 --- a/matrix/user_tokens.cc +++ b/matrix/user_tokens.cc @@ -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); } diff --git a/modules/media/upload.cc b/modules/media/upload.cc index 195b798ae..040cc5b38 100644 --- a/modules/media/upload.cc +++ b/modules/media/upload.cc @@ -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