Added utility to generate random strings

This commit is contained in:
2021-01-31 21:36:01 +01:00
parent f6fb251d98
commit 635ad7ffb2

View File

@ -5,8 +5,10 @@
#include <cstdint> #include <cstdint>
#include <functional> #include <functional>
#include <vector> #include <vector>
#include <cstring>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <random>
/* /*
Avoid "unused parameter" warnings Avoid "unused parameter" warnings
@ -227,5 +229,18 @@ bool is_in(First &&first, T && ... t)
return ((first == t) || ...); return ((first == t) || ...);
} }
std::string randomString(std::size_t length)
{
static constexpr auto chars =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
thread_local auto rng = std::default_random_engine();
auto dist = std::uniform_int_distribution{{}, std::strlen(chars) - 1};
auto result = std::string(length, '\0');
std::generate_n(std::begin(result), length, [&]() { return chars[dist(rng)]; });
return result;
}
} // namespace } // namespace
} // namespace espcpputils } // namespace espcpputils