...
This commit is contained in:
@@ -11,6 +11,7 @@ set(LIB_HEADERS
|
|||||||
include/snipplib/utils/snplib_keyvalue_expr.h
|
include/snipplib/utils/snplib_keyvalue_expr.h
|
||||||
include/snipplib/utils/snplib_utils.h
|
include/snipplib/utils/snplib_utils.h
|
||||||
include/snipplib/containers/snplib_hmap.h
|
include/snipplib/containers/snplib_hmap.h
|
||||||
|
include/snipplib/containers/snplib_hvec.h
|
||||||
include/snipplib/serialization/snplib_serialization.h
|
include/snipplib/serialization/snplib_serialization.h
|
||||||
include/snipplib/network/snplib_endpoint.h
|
include/snipplib/network/snplib_endpoint.h
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -84,6 +84,13 @@ int main()
|
|||||||
std::println("valueSeq = '{}'", kvl.valueSeq<std::list<std::string>>());
|
std::println("valueSeq = '{}'", kvl.valueSeq<std::list<std::string>>());
|
||||||
|
|
||||||
|
|
||||||
|
snplib_randoom_string_generator_t<> rstr_gen;
|
||||||
|
|
||||||
|
std::println("\n\n");
|
||||||
|
std::println("RAND STR: {}", rstr_gen.generate());
|
||||||
|
std::println("RAND STR: {}", rstr_gen.generate());
|
||||||
|
std::println("UUID v4: {}", rstr_gen.UUIDv4String());
|
||||||
|
std::println("UUID v4: {}", rstr_gen.UUIDv4String());
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <functional>
|
||||||
|
#include <random>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "../concepts/snplib_concepts.h"
|
#include "../concepts/snplib_concepts.h"
|
||||||
#include "../serialization/snplib_serialization.h"
|
|
||||||
|
|
||||||
namespace snplib
|
namespace snplib
|
||||||
{
|
{
|
||||||
@@ -333,5 +334,89 @@ static std::optional<T> snplib_num_from_range(R&& r)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* RANDOM STRING GENERATOR */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <std::uniform_random_bit_generator RANDOM_GEN_T = std::mt19937_64>
|
||||||
|
class snplib_randoom_string_generator_t
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::random_device _randomDev{};
|
||||||
|
|
||||||
|
RANDOM_GEN_T _randomGen;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static constexpr std::string_view DEFAULT_RANDOM_CHAR_RANGE =
|
||||||
|
"0123456789"
|
||||||
|
"abcdefghijklmnopqrstuvwxyz"
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
"$_#~@%-*!";
|
||||||
|
|
||||||
|
static constexpr std::string_view UUID_STRING_RANGE = "0123456789abcdef";
|
||||||
|
|
||||||
|
|
||||||
|
static constexpr size_t DEFAULT_RANDOM_STR_LEN = 32;
|
||||||
|
|
||||||
|
snplib_randoom_string_generator_t() : _randomGen(_randomDev()) {}
|
||||||
|
|
||||||
|
template <typename SEED_T>
|
||||||
|
snplib_randoom_string_generator_t(SEED_T seed) : _randomGen(seed)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <snplib_output_char_range_c OR,
|
||||||
|
snplib_input_char_range_c IR = decltype(snplib_randoom_string_generator_t::DEFAULT_RANDOM_CHAR_RANGE)>
|
||||||
|
OR generate(size_t len = snplib_randoom_string_generator_t::DEFAULT_RANDOM_STR_LEN,
|
||||||
|
IR char_range = snplib_randoom_string_generator_t::DEFAULT_RANDOM_CHAR_RANGE)
|
||||||
|
{
|
||||||
|
OR res;
|
||||||
|
|
||||||
|
size_t max_idx = std::ranges::size(char_range);
|
||||||
|
if (max_idx < 2) {
|
||||||
|
char_range = snplib_randoom_string_generator_t::DEFAULT_RANDOM_CHAR_RANGE;
|
||||||
|
max_idx = snplib_randoom_string_generator_t::DEFAULT_RANDOM_CHAR_RANGE.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::uniform_int_distribution<size_t> rdist(0, --max_idx);
|
||||||
|
|
||||||
|
if (!len) {
|
||||||
|
len = snplib_randoom_string_generator_t::DEFAULT_RANDOM_STR_LEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < len; ++i) {
|
||||||
|
std::back_inserter(res) = char_range[rdist(_randomGen)];
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <snplib_input_char_range_c IR = decltype(snplib_randoom_string_generator_t::DEFAULT_RANDOM_CHAR_RANGE)>
|
||||||
|
std::string generate(size_t len = snplib_randoom_string_generator_t::DEFAULT_RANDOM_STR_LEN,
|
||||||
|
IR char_range = snplib_randoom_string_generator_t::DEFAULT_RANDOM_CHAR_RANGE)
|
||||||
|
{
|
||||||
|
return generate<std::string>(len, char_range);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <snplib_output_char_range_c OR>
|
||||||
|
OR UUIDv4String()
|
||||||
|
{
|
||||||
|
OR res = generate(36, snplib_randoom_string_generator_t::UUID_STRING_RANGE);
|
||||||
|
auto iter = res.begin();
|
||||||
|
size_t i = 0;
|
||||||
|
for (size_t idx : std::array<size_t, 4>{8, 13, 18, 23}) {
|
||||||
|
std::advance(iter, idx - i);
|
||||||
|
*iter = '-';
|
||||||
|
i = idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string UUIDv4String()
|
||||||
|
{
|
||||||
|
return UUIDv4String<std::string>();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace snplib
|
} // namespace snplib
|
||||||
Reference in New Issue
Block a user