#include #include #include #include "strutil.h" ////////////////////////////////////////////////////////////////////////////////// // strucase std::string & strucase(std::string &s) { std::transform(s.begin(), s.end(), s.begin(), ::toupper); return s; } std::string strucase(std::string s) { std::transform(s.begin(), s.end(), s.begin(), ::toupper); return s; } std::string strucase(const char *ps) { std::string s(ps); std::transform(s.begin(), s.end(), s.begin(), ::toupper); return s; } ////////////////////////////////////////////////////////////////////////////////// // strlcase std::string & strlcase(std::string &s) { std::transform(s.begin(), s.end(), s.begin(), ::tolower); return s; } std::string strlcase(std::string s) { std::transform(s.begin(), s.end(), s.begin(), ::tolower); return s; } std::string strlcase(const char *ps) { std::string s(ps); std::transform(s.begin(), s.end(), s.begin(), ::tolower); return s; } ////////////////////////////////////////////////////////////////////////////////// // trim from start (in place) std::string & ltrim(std::string &s) { s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(std::isspace)))); return s; } std::string & ltrim(std::string &s, const char *sep) { std::size_t f = s.find_first_not_of(sep); if(f != std::string::npos) s.erase(0, f); return s; } ////////////////////////////////////////////////////////////////////////////////// // trim from end (in place) std::string & rtrim(std::string &s) { s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(std::isspace))).base(), s.end()); return s; } std::string & rtrim(std::string &s, const char *sep) { std::size_t f = s.find_last_not_of(sep); if(f != std::string::npos) s.erase(f + 1); return s; } ////////////////////////////////////////////////////////////////////////////////// // trim from both ends (in place) std::string & trim(std::string &s) { return ltrim(rtrim(s)); } std::string & trim(std::string &s, const char *sep) { return ltrim(rtrim(s, sep), sep); } ////////////////////////////////////////////////////////////////////////////////// // trim from both ends (copy) std::string trim(const std::string &s) { std::string t(s); return ltrim(rtrim(t)); } std::string trim(const std::string &s, const char *sep) { std::string t(s); return ltrim(rtrim(t, sep), sep); } ////////////////////////////////////////////////////////////////////////////////// // split string int strsplit(const std::string &str, const char *sep, std::vector &vec) { int nElems = 0; std::regex reg(sep); const std::sregex_token_iterator end; for(std::sregex_token_iterator iter(str.begin(), str.end(), reg); iter != end; ++iter) { vec.push_back(*iter); nElems++; } return nElems; } ////////////////////////////////////////////////////////////////////////////////// // formatString std::string formatString(const char *fmt, ...) { int n; std::string s; char *p = NULL; va_list ap; va_start(ap, fmt); n = ::vasprintf(&p, fmt, ap); va_end(ap); if(n >= 0) { s = p; free(p); } return s; } ////////////////////////////////////////////////////////////////////////////////// // strFormatByteSize std::string strFormatByteSize(uint64_t s, unsigned int dec) { char szBuf[64]; if(dec > 16) dec = 16; if(s < 1024) sprintf(szBuf, "%ju Byte", s); else { static const char *a[] = {"KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}; uint64_t e = (uint64_t)1 << 60; int i; for(i = 5; (i > 0) && (e > s); --i, e >>= 10) ; sprintf(szBuf, "%.*f %s", dec, (double)s / (double)e, a[i]); } return szBuf; } ////////////////////////////////////////////////////////////////////////////////// // readFile bool readFile(const char *pszFilePath, std::string &str) { std::ifstream stm(pszFilePath); bool bGood = stm.good(); if(bGood) { stm.seekg(0, std::ios::end); str.reserve((size_t)stm.tellg()); stm.seekg(0, std::ios::beg); str.assign((std::istreambuf_iterator(stm)), std::istreambuf_iterator()); } return bGood; } ////////////////////////////////////////////////////////////////////////////////// // writeFile bool writeFile(const char *pszFilePath, const std::string &str) { std::ofstream stm(pszFilePath); stm << str; stm.close(); return true; }