strutil.cpp 4.3 KB

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