12std::mt19937& getRandomGenerator() {
13 static std::random_device random_device{};
14 static std::mt19937 generator{random_device()};
23 return std::ranges::find(s, c) != s.end();
27 int inPos = 0, searchPos = 0;
28 for ( ; inPos < in.length() && searchPos < search.length(); inPos++, searchPos++) {
29 if (search[searchPos] ==
'%') {
30 if (++searchPos == search.length()) {
33 switch (search[searchPos]) {
38 if (!std::isspace(in[inPos]))
return false;
41 if (!(in[inPos] >=
'a' && in[inPos] <=
'z' || in[inPos] >=
'A' && in[inPos] <=
'Z'))
return false;
44 if (!(in[inPos] >=
'A' && in[inPos] <=
'Z'))
return false;
47 if (!(in[inPos] >=
'a' && in[inPos] <=
'z'))
return false;
50 if (!std::isdigit(in[inPos]))
return false;
53 if (in[inPos] !=
'%')
return false;
56 }
else if (in[inPos] != search[searchPos]) {
60 return inPos == in.length() && searchPos == search.length();
64 return std::ranges::equal(s1, s2, [](
char a,
char b) {
return std::tolower(a) == std::tolower(b); });
70 s.erase(s.begin(), std::ranges::find_if(s, [](
char c) { return !std::isspace(c); }));
74 while (!s.empty() && std::isspace(s[0])) {
81 s.erase(std::find_if(s.rbegin(), s.rend(), [](
char c) { return !std::isspace(c); }).base(), s.end());
85 while (!s.empty() && std::isspace(s[s.size() - 1])) {
101 s.erase(std::ranges::unique(s, [](
char lhs,
char rhs) {
return lhs == rhs && std::isspace(lhs); }).begin(), s.end());
111 s.erase(s.begin(), std::ranges::find_if(s, [chars](
char c) {
112 return !contains(chars, c);
117 while (!s.empty() &&
contains(chars, s[0])) {
124 s.erase(std::find_if(s.rbegin(), s.rend(), [chars](
char c) {
125 return !contains(chars, c);
130 while (!s.empty() &&
contains(chars, s[s.size() - 1])) {
141std::string_view
string::trim(std::string_view s, std::string_view chars) {
146 s.erase(std::ranges::unique(s, [chars](
char lhs,
char rhs) {
return lhs == rhs && std::ranges::find(chars, lhs) != chars.end(); }).begin(), s.end());
158 std::vector<std::string> result;
159 std::stringstream ss(std::string{s});
161 while (std::getline(ss, item, delim)) {
162 result.push_back(item);
168 std::ranges::transform(input, input.begin(), [](
unsigned char c){ return std::tolower(c); });
172 std::string out{input};
178 std::ranges::transform(input, input.begin(), [](
unsigned char c){ return std::toupper(c); });
182 std::string out{input};
188 auto& generator = ::getRandomGenerator();
189 std::uniform_int_distribution distribution{0,
static_cast<int>(chars.length() - 1)};
192 for (uint16_t i = 0; i < length; i++) {
193 out += chars[distribution(generator)];
200 static constexpr std::string_view chars =
"0123456789abcdef";
202 auto& generator = ::getRandomGenerator();
203 std::uniform_int_distribution distribution{0,
static_cast<int>(chars.length() - 1)};
206 for (uint16_t i = 0; i < 8; i++) {
207 out += chars[distribution(generator)];
210 for (uint16_t i = 0; i < 3; i++) {
211 for (uint16_t j = 0; j < 4; j++) {
212 out += chars[distribution(generator)];
216 for (uint16_t i = 0; i < 12; i++) {
217 out += chars[distribution(generator)];
224 return std::format(
"{:0>{}}", number, width);
228 std::ranges::replace(path,
'\\',
'/');
229 if (stripSlashPrefix && path.starts_with(
'/')) {
230 path = path.substr(1);
232 if (stripSlashSuffix && path.ends_with(
'/')) {
238 std::ranges::replace(path,
'/',
'\\');
239 if (stripSlashPrefix && path.starts_with(
'\\')) {
240 path = path.substr(1);
242 if (stripSlashSuffix && path.ends_with(
'\\')) {
247std::from_chars_result
string::toBool(std::string_view number,
bool& out,
int base) {
249 const auto result = std::from_chars(number.data(), number.data() + number.size(), tmp, base);
255 static constexpr auto hexChar2Int = [](
char c) -> uint8_t {
256 if (c >=
'0' && c <=
'9') {
259 if (c >=
'a' && c <=
'f') {
262 if (c >=
'A' && c <=
'F') {
268 const bool remainder = hex.size() % 2;
270 std::vector<std::byte> hexData;
271 hexData.reserve(hex.size() / 2 + remainder);
273 hexData.push_back(
static_cast<std::byte
>(hexChar2Int(hex.front())));
275 for (
int i = remainder; i < hex.length(); i += 2) {
276 hexData.push_back(
static_cast<std::byte
>(hexChar2Int(hex[i]) << 4 | hexChar2Int(hex[i + 1])));
283 hexStr.reserve(hex.size() * 2);
284 for (
const auto byte : hex) {
285 hexStr.append(std::format(
"{:02x}",
static_cast<int>(
byte)));
std::string createRandom(uint16_t length=32, std::string_view chars="0123456789_abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ")
std::string padNumber(int64_t number, int width)
std::vector< std::byte > decodeHex(std::string_view hex)
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()
std::string encodeHex(std::span< const std::byte > hex)