#ifndef UTILS_H #define UTILS_H #include #include #include #include #include #include #include #include #include namespace utils { std::vector split(std::string str, char delim); std::vector split(std::string str, const std::string &delim); std::vector split(const std::string &str, std::regex ®ex); std::string urldecode(std::string_view str); std::string strreplace(std::string str, const std::string &search, const std::string &replace); std::string html_xss(std::string_view str); std::string getenv(const std::string &key); template U getKeyOrEmpty(const std::map &map, const T &key) { auto k = map.find(key); if(k != map.end()) { return k->second; } return U(); } template U getKeyOrEmpty(const std::multimap &map, const T &key) { auto k = map.find(key); if(k != map.end()) { return k->second; } return U(); } template std::vector getAll(const std::multimap &map, const T &key) { std::vector result; auto range = map.equal_range(key); for(auto it = range.first; it != range.second; it++) { result.push_back(it->second); } return result; } std::string regex_callback_replacer(std::regex regex, const std::string &input, std::function callback); std::string readCompleteFile(std::string_view filepath); inline std::string nz(const char *s) { if(s == nullptr) { return std::string{}; } return std::string{s}; } // TODO: optional inline unsigned int toUInt(const std::string &str) { if(str == "") { return 0; } auto result = std::stoul(str); if(result > std::numeric_limits::max()) { throw std::out_of_range(str + " is too large for unsigned int "); } return result; } std::string formatLocalDate(time_t t, std::string format); std::string toISODate(time_t t); std::string toISODateTime(time_t t); template inline std::string toString(const T &v) { return std::string(v.begin(), v.end()); } std::string trim(std::string_view view); } // namespace utils #endif