123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- #include <algorithm>
- #include <stdarg.h>
- #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<int, int>(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<int, int>(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<std::string> &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;
- }
|