2023-12-10 22:02:43 -06:00
|
|
|
#ifndef UTIL_H
|
|
|
|
|
#define UTIL_H
|
|
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <iterator>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2024-10-17 16:49:05 -05:00
|
|
|
std::string replace(std::string subject, const std::string& search, const std::string& replace) {
|
|
|
|
|
size_t pos = 0;
|
|
|
|
|
while ((pos = subject.find(search, pos)) != std::string::npos) {
|
|
|
|
|
subject.replace(pos, search.length(), replace);
|
|
|
|
|
pos += replace.length();
|
|
|
|
|
}
|
|
|
|
|
return subject;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-10 22:02:43 -06:00
|
|
|
template <typename Out>
|
|
|
|
|
void split(const std::string &s, char delim, Out result, int limit) {
|
|
|
|
|
int it = 0;
|
|
|
|
|
std::istringstream iss(s);
|
|
|
|
|
std::string item;
|
|
|
|
|
while (std::getline(iss, item, delim) && it <= limit) {
|
|
|
|
|
*result++ = item;
|
|
|
|
|
if (limit > 0) it++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> split(const std::string &s, char delim, int limit) {
|
|
|
|
|
std::vector<std::string> elems;
|
|
|
|
|
split(s, delim, std::back_inserter(elems), limit);
|
|
|
|
|
return elems;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static std::string toLower(std::string str) {
|
|
|
|
|
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){ return std::tolower(c); });
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static std::string toUpper(std::string str) {
|
|
|
|
|
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){ return std::toupper(c); });
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char* trim(char *str) {
|
|
|
|
|
char *end;
|
|
|
|
|
while(isspace(*str))
|
|
|
|
|
str++;
|
|
|
|
|
if(*str == 0)
|
|
|
|
|
return str;
|
|
|
|
|
end = str + strnlen(str, 128) - 1;
|
|
|
|
|
while(end > str && isspace(*end))
|
|
|
|
|
end--;
|
|
|
|
|
*(end+1) = '\0';
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|