Added randomutils and fixed some str utils
This commit is contained in:
@ -13,6 +13,7 @@ set(headers
|
||||
src/delayedconstruction.h
|
||||
src/makearray.h
|
||||
src/numberparsing.h
|
||||
src/randomutils.h
|
||||
src/refwhenneeded.h
|
||||
src/strutils.h
|
||||
)
|
||||
|
50
src/randomutils.h
Normal file
50
src/randomutils.h
Normal file
@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
// system includes
|
||||
#include <random>
|
||||
|
||||
namespace cpputils {
|
||||
|
||||
template<typename T, typename Trandom>
|
||||
T randomNumber(T min, T max, Trandom &&rng)
|
||||
{
|
||||
std::uniform_int_distribution<T> dist{min, max};
|
||||
return dist(rng);
|
||||
}
|
||||
|
||||
//template<typename Trandom>
|
||||
//float randomNumber<float, Trandom>(float min, float max, Trandom &rng)
|
||||
//{
|
||||
// std::uniform_real_distribution<float> dist{min, max};
|
||||
// return dist(rng);
|
||||
//}
|
||||
|
||||
//template<typename Trandom>
|
||||
//double randomNumber<double, Trandom>(double min, double max, Trandom &rng)
|
||||
//{
|
||||
// std::uniform_real_distribution<double> dist{min, max};
|
||||
// return dist(rng);
|
||||
//}
|
||||
|
||||
template<typename T, typename Trandom>
|
||||
T randomNumber(T max, Trandom &&rng)
|
||||
{
|
||||
std::uniform_int_distribution<T> dist{{}, max};
|
||||
return dist(rng);
|
||||
}
|
||||
|
||||
//template<typename Trandom>
|
||||
//float randomNumber<float, Trandom>(float max, Trandom &rng)
|
||||
//{
|
||||
// std::uniform_real_distribution<float> dist{{}, max};
|
||||
// return dist(rng);
|
||||
//}
|
||||
|
||||
//template<typename Trandom>
|
||||
//double randomNumber<double, Trandom>(double max, Trandom &rng)
|
||||
//{
|
||||
// std::uniform_real_distribution<double> dist{{}, max};
|
||||
// return dist(rng);
|
||||
//}
|
||||
|
||||
} // namespace cpputils
|
@ -1,14 +1,11 @@
|
||||
#include "strutils.h"
|
||||
|
||||
// system includes
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
namespace cpputils {
|
||||
bool stringEqualsIgnoreCase(std::string_view a, std::string_view b)
|
||||
{
|
||||
return a == b; // HACK for now...
|
||||
|
||||
if (a.size() != b.size())
|
||||
return false;
|
||||
|
||||
@ -25,14 +22,10 @@ bool stringStartsWith(std::string_view fullString, std::string_view begin)
|
||||
|
||||
bool stringEndsWith(std::string_view fullString, std::string_view ending)
|
||||
{
|
||||
if (fullString.length() >= ending.length())
|
||||
{
|
||||
return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fullString.length() < ending.length())
|
||||
return false;
|
||||
}
|
||||
|
||||
return fullString.compare(fullString.length() - ending.length(), ending.length(), ending) == 0;
|
||||
}
|
||||
|
||||
//void stringReplaceAll(char search, char replace, std::string &subject)
|
||||
|
@ -4,6 +4,8 @@
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
#include <random>
|
||||
#include <algorithm>
|
||||
|
||||
namespace cpputils {
|
||||
inline std::string toString(bool val) { return val ? "true" : "false"; }
|
||||
@ -44,4 +46,20 @@ void stringReplaceAll(std::string_view search, std::string_view replace, std::st
|
||||
std::string stringReplaceAll(char search, std::string_view 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);
|
||||
|
||||
template<typename Trandom>
|
||||
std::string randomString(std::size_t length, Trandom &rng)
|
||||
{
|
||||
constexpr const char chars[] =
|
||||
"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
|
||||
|
Reference in New Issue
Block a user