Added utility to generate random strings
This commit is contained in:
15
esputils.h
15
esputils.h
@ -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
|
||||||
|
Reference in New Issue
Block a user