11std::mt19937& getRandomGenerator() {
12 static std::random_device random_device{};
13 static std::mt19937 generator{random_device()};
22 return std::find(s.begin(), s.end(), c) != s.end();
26 int inPos = 0, searchPos = 0;
27 for ( ; inPos < in.length() && searchPos < search.length(); inPos++, searchPos++) {
28 if (search[searchPos] ==
'%') {
29 if (++searchPos == search.length()) {
32 switch (search[searchPos]) {
37 if (!std::isspace(in[inPos]))
return false;
40 if (!(in[inPos] >=
'a' && in[inPos] <=
'z' || in[inPos] >=
'A' && in[inPos] <=
'Z'))
return false;
43 if (!(in[inPos] >=
'A' && in[inPos] <=
'Z'))
return false;
46 if (!(in[inPos] >=
'a' && in[inPos] <=
'z'))
return false;
49 if (!std::isdigit(in[inPos]))
return false;
52 if (in[inPos] !=
'%')
return false;
55 }
else if (in[inPos] != search[searchPos]) {
59 return inPos == in.length() && searchPos == search.length();
63 return std::ranges::equal(s1, s2, [](
char a,
char b) {
return std::tolower(a) == std::tolower(b); });
69 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](
char c) { return !std::isspace(c); }));
73 while (!s.empty() && std::isspace(s[0])) {
80 s.erase(std::find_if(s.rbegin(), s.rend(), [](
char c) { return !std::isspace(c); }).base(), s.end());
84 while (!s.empty() && std::isspace(s[s.size() - 1])) {
100 s.erase(std::ranges::unique(s, [](
char lhs,
char rhs) {
return lhs == rhs && std::isspace(lhs); }).begin(), s.end());
110 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [chars](
char c) {
111 return !contains(chars, c);
116 while (!s.empty() &&
contains(chars, s[0])) {
123 s.erase(std::find_if(s.rbegin(), s.rend(), [chars](
char c) {
124 return !contains(chars, c);
129 while (!s.empty() &&
contains(chars, s[s.size() - 1])) {
140std::string_view
string::trim(std::string_view s, std::string_view chars) {
145 s.erase(std::ranges::unique(s, [chars](
char lhs,
char rhs) {
return lhs == rhs && std::ranges::find(chars, lhs) != chars.end(); }).begin(), s.end());
157 std::vector<std::string> result;
158 std::stringstream ss(std::string{s});
160 while (std::getline(ss, item, delim)) {
161 result.push_back(item);
167 std::transform(input.begin(), input.end(), input.begin(), [](
unsigned char c){ return std::tolower(c); });
171 std::string out{input};
177 std::transform(input.begin(), input.end(), input.begin(), [](
unsigned char c){ return std::toupper(c); });
181 std::string out{input};
187 auto& generator = ::getRandomGenerator();
188 std::uniform_int_distribution distribution{0,
static_cast<int>(chars.length() - 1)};
191 for (uint16_t i = 0; i < length; i++) {
192 out += chars[distribution(generator)];
199 static constexpr std::string_view chars =
"0123456789abcdef";
201 auto& generator = ::getRandomGenerator();
202 std::uniform_int_distribution distribution{0,
static_cast<int>(chars.length() - 1)};
205 for (uint16_t i = 0; i < 8; i++) {
206 out += chars[distribution(generator)];
209 for (uint16_t i = 0; i < 3; i++) {
210 for (uint16_t j = 0; j < 4; j++) {
211 out += chars[distribution(generator)];
215 for (uint16_t i = 0; i < 12; i++) {
216 out += chars[distribution(generator)];
223 return std::format(
"{:0>{}}", number, width);
227 std::ranges::replace(path,
'\\',
'/');
228 if (stripSlashPrefix && path.starts_with(
'/')) {
229 path = path.substr(1);
231 if (stripSlashSuffix && path.ends_with(
'/')) {
237 std::replace(path.begin(), path.end(),
'/',
'\\');
238 if (stripSlashPrefix && path.starts_with(
'\\')) {
239 path = path.substr(1);
241 if (stripSlashSuffix && path.ends_with(
'\\')) {
246std::from_chars_result
string::toBool(std::string_view number,
bool& out,
int base) {
248 const auto result = std::from_chars(number.data(), number.data() + number.size(), tmp, base);
std::string createRandom(uint16_t length=32, std::string_view chars="0123456789_abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ")
std::string padNumber(int64_t number, int width)
bool contains(std::string_view s, char c)
void normalizeSlashes(std::string &path, bool stripSlashPrefix=false, bool stripSlashSuffix=true)
std::from_chars_result toBool(std::string_view number, bool &out, int base=10)
void ltrim(std::string &s)
std::vector< std::string > split(std::string_view s, char delim)
void denormalizeSlashes(std::string &path, bool stripSlashPrefix=false, bool stripSlashSuffix=true)
bool matches(std::string_view in, std::string_view search)
A very basic regex-like pattern checker for ASCII strings.
void trimInternal(std::string &s)
void rtrim(std::string &s)
void toUpper(std::string &input)
void trim(std::string &s)
bool iequals(std::string_view s1, std::string_view s2)
void toLower(std::string &input)
std::string generateUUIDv4()