Fixed randomString() generating invalid \0 chars
This commit is contained in:
@ -47,34 +47,25 @@ std::string stringReplaceAll(char search, std::string_view replace, std::string_
|
|||||||
//std::string stringReplaceAll(std::string_view search, char replace, std::string_view subject);
|
//std::string stringReplaceAll(std::string_view search, char replace, std::string_view subject);
|
||||||
std::string stringReplaceAll(std::string_view search, std::string_view replace, std::string_view subject);
|
std::string stringReplaceAll(std::string_view search, std::string_view replace, std::string_view subject);
|
||||||
|
|
||||||
template<typename Trandom>
|
constexpr const std::string_view allDigitsAndCharacters =
|
||||||
std::string randomString(std::size_t length, Trandom &rng)
|
|
||||||
{
|
|
||||||
constexpr const char chars[] =
|
|
||||||
"0123456789"
|
"0123456789"
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
"abcdefghijklmnopqrstuvwxyz";
|
"abcdefghijklmnopqrstuvwxyz";
|
||||||
|
|
||||||
std::uniform_int_distribution dist{{}, std::size(chars) - 1};
|
template<typename Trandom>
|
||||||
|
std::string randomString(std::size_t length, std::string_view allowedChars, Trandom &rng)
|
||||||
|
{
|
||||||
|
std::uniform_int_distribution dist{{}, allowedChars.size() - 1};
|
||||||
|
|
||||||
std::string result(length, '\0');
|
std::string result(length, {});
|
||||||
std::generate_n(std::begin(result), length, [&]() { return chars[dist(rng)]; });
|
std::generate_n(std::begin(result), length, [&](){ return allowedChars[dist(rng)]; });
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Trandom>
|
template<typename Trandom>
|
||||||
std::string randomString(std::size_t length, Trandom &&rng)
|
std::string randomString(std::size_t length, std::string_view allowedChars, Trandom &&rng)
|
||||||
{
|
{
|
||||||
constexpr const char chars[] =
|
return randomString(length, allowedChars, rng);
|
||||||
"0123456789"
|
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
||||||
"abcdefghijklmnopqrstuvwxyz";
|
|
||||||
|
|
||||||
std::uniform_int_distribution dist{{}, std::size(chars) - 1};
|
|
||||||
|
|
||||||
std::string result(length, '\0');
|
|
||||||
std::generate_n(std::begin(result), length, [&]() { return chars[dist(rng)]; });
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace cpputils
|
} // namespace cpputils
|
||||||
|
Reference in New Issue
Block a user