strutil.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <algorithm>
  2. #include <stdarg.h>
  3. #include "strutil.h"
  4. //////////////////////////////////////////////////////////////////////////////////
  5. // strucase
  6. std::string & strucase(std::string &s)
  7. {
  8. std::transform(s.begin(), s.end(), s.begin(), ::toupper);
  9. return s;
  10. }
  11. std::string strucase(std::string s)
  12. {
  13. std::transform(s.begin(), s.end(), s.begin(), ::toupper);
  14. return s;
  15. }
  16. std::string strucase(const char *ps)
  17. {
  18. std::string s(ps);
  19. std::transform(s.begin(), s.end(), s.begin(), ::toupper);
  20. return s;
  21. }
  22. //////////////////////////////////////////////////////////////////////////////////
  23. // strlcase
  24. std::string & strlcase(std::string &s)
  25. {
  26. std::transform(s.begin(), s.end(), s.begin(), ::tolower);
  27. return s;
  28. }
  29. std::string strlcase(std::string s)
  30. {
  31. std::transform(s.begin(), s.end(), s.begin(), ::tolower);
  32. return s;
  33. }
  34. std::string strlcase(const char *ps)
  35. {
  36. std::string s(ps);
  37. std::transform(s.begin(), s.end(), s.begin(), ::tolower);
  38. return s;
  39. }
  40. //////////////////////////////////////////////////////////////////////////////////
  41. // trim from start (in place)
  42. std::string & ltrim(std::string &s)
  43. {
  44. s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
  45. return s;
  46. }
  47. std::string & ltrim(std::string &s, const char *sep)
  48. {
  49. std::size_t f = s.find_first_not_of(sep);
  50. if(f != std::string::npos)
  51. s.erase(0, f);
  52. return s;
  53. }
  54. //////////////////////////////////////////////////////////////////////////////////
  55. // trim from end (in place)
  56. std::string & rtrim(std::string &s)
  57. {
  58. s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
  59. return s;
  60. }
  61. std::string & rtrim(std::string &s, const char *sep)
  62. {
  63. std::size_t f = s.find_last_not_of(sep);
  64. if(f != std::string::npos)
  65. s.erase(f + 1);
  66. return s;
  67. }
  68. //////////////////////////////////////////////////////////////////////////////////
  69. // trim from both ends (in place)
  70. std::string & trim(std::string &s)
  71. {
  72. return ltrim(rtrim(s));
  73. }
  74. std::string & trim(std::string &s, const char *sep)
  75. {
  76. return ltrim(rtrim(s, sep), sep);
  77. }
  78. //////////////////////////////////////////////////////////////////////////////////
  79. // trim from both ends (copy)
  80. std::string trim(const std::string &s)
  81. {
  82. std::string t(s);
  83. return ltrim(rtrim(t));
  84. }
  85. std::string trim(const std::string &s, const char *sep)
  86. {
  87. std::string t(s);
  88. return ltrim(rtrim(t, sep), sep);
  89. }
  90. //////////////////////////////////////////////////////////////////////////////////
  91. // split string
  92. int strsplit(const std::string &str, const char *sep, std::vector<std::string> &vec)
  93. {
  94. int nElems = 0;
  95. std::regex reg(sep);
  96. const std::sregex_token_iterator end;
  97. for(std::sregex_token_iterator iter(str.begin(), str.end(), reg); iter != end; ++iter)
  98. {
  99. vec.push_back(*iter);
  100. nElems++;
  101. }
  102. return nElems;
  103. }
  104. //////////////////////////////////////////////////////////////////////////////////
  105. // formatString
  106. std::string formatString(const char *fmt, ...)
  107. {
  108. int n;
  109. std::string s;
  110. char *p = NULL;
  111. va_list ap;
  112. va_start(ap, fmt);
  113. n = ::vasprintf(&p, fmt, ap);
  114. va_end(ap);
  115. if(n >= 0)
  116. {
  117. s = p;
  118. free(p);
  119. }
  120. return s;
  121. }
  122. //////////////////////////////////////////////////////////////////////////////////
  123. // strFormatByteSize
  124. std::string strFormatByteSize(uint64_t s, unsigned int dec)
  125. {
  126. char szBuf[64];
  127. if(dec > 16)
  128. dec = 16;
  129. if(s < 1024)
  130. sprintf(szBuf, "%ju Byte", s);
  131. else
  132. {
  133. static const char *a[] = {"KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
  134. uint64_t e = (uint64_t)1 << 60;
  135. int i;
  136. for(i = 5; (i > 0) && (e > s); --i, e >>= 10)
  137. ;
  138. sprintf(szBuf, "%.*f %s", dec, (double)s / (double)e, a[i]);
  139. }
  140. return szBuf;
  141. }