stringtools.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include <iostream>
  2. #include "Exception.h"
  3. #include "stringtools.h"
  4. using namespace std;
  5. string delSpaces(const string& s)
  6. {
  7. string res = "";
  8. for (unsigned int i = 0; i < s.size(); ++i)
  9. {
  10. if (!isspace(s[i]))
  11. res += s[i];
  12. }
  13. return res;
  14. }
  15. void substitute(std::string& s, char c1, char c2)
  16. {
  17. for (unsigned int i = 0; i < s.size(); ++i)
  18. if (s[i] == c1) s[i] = c2;
  19. }
  20. void reduceToOne(std::string& s, char c)
  21. {
  22. unsigned int pr = 0;
  23. unsigned int pw = 0;
  24. while (pr < s.size())
  25. {
  26. char ac = s[pr];
  27. s[pw] = ac;
  28. ++pr;
  29. ++pw;
  30. if (ac == c)
  31. {
  32. while (pr < s.size() && s[pr] == c) // skip further characters c
  33. ++pr;
  34. }
  35. }
  36. s.resize(pw);
  37. }
  38. string trim(const string& s)
  39. {
  40. string res = "";
  41. int last = s.size() - 1;
  42. while (last >= 0 && isspace(s[last]))
  43. --last;
  44. int first = 0;
  45. while (first < (int)s.size() && isspace(s[first]))
  46. ++first;
  47. for (int i = first; i <= last; ++i)
  48. res += s[i];
  49. return res;
  50. }
  51. bool startsWith(const string& s, const string& start)
  52. {
  53. int len = start.length();
  54. return s.substr(0, len) == start;
  55. }
  56. int split(const string& s, Strings& parts, char del, int expectedParts)
  57. {
  58. parts.clear();
  59. string p = "";
  60. if (!s.empty())
  61. {
  62. for (unsigned int i = 0; i < s.size(); ++i)
  63. {
  64. if (s[i] == del)
  65. {
  66. parts.push_back(p);
  67. p.clear();
  68. }
  69. else
  70. p += s[i];
  71. }
  72. parts.push_back(p);
  73. }
  74. if (expectedParts > 0 && expectedParts != (int)parts.size())
  75. throw Exception("split", string("Unexpected number of parts: ") + s);
  76. return parts.size();
  77. }
  78. void skipWS(const string& s, unsigned int& i)
  79. {
  80. static const string ws = " \t\n";
  81. while (i < s.size() && ws.find(s[i]) != string::npos)
  82. ++i;
  83. }
  84. int getInt(const string& s, unsigned int& i)
  85. {
  86. if (!isdigit(s[i]))
  87. throw Exception("getInt", "digit expected");
  88. string is;
  89. while (i < s.size() && isdigit(s[i]))
  90. {
  91. is += s[i];
  92. ++i;
  93. }
  94. int res = stoi(is);
  95. skipWS(s, i);
  96. return res;
  97. }
  98. long int getLongInt(const string& s, unsigned int& i)
  99. {
  100. if (!isdigit(s[i]))
  101. throw Exception("getLongInt", "digit expected");
  102. string is;
  103. while (i < s.size() && isdigit(s[i]))
  104. {
  105. is += s[i];
  106. ++i;
  107. }
  108. long int res = stol(is);
  109. skipWS(s, i);
  110. return res;
  111. }
  112. string getWord(const string& s, unsigned int& i)
  113. {
  114. if (!isalpha(s[i]) && s[i] != '-' && s[i] != '_')
  115. throw Exception("getWord", "letter expected");
  116. string is;
  117. while (i < s.size() && (isalpha(s[i]) || s[i] == '-' || s[i] == '_'))
  118. {
  119. is += s[i];
  120. ++i;
  121. }
  122. skipWS(s, i);
  123. return is;
  124. }
  125. time_t stot(const string& s)
  126. {
  127. unsigned int i = 0;
  128. skipWS(s, i);
  129. int val = getInt(s, i);
  130. if (i >= s.length())
  131. return val;
  132. string unit = getWord(s, i);
  133. if (unit.back() != 's')
  134. unit += 's';
  135. if (unit == "secs")
  136. return val;
  137. val *= 60;
  138. if (unit == "mins")
  139. return val;
  140. val *= 60;
  141. if (unit == "hours")
  142. return val;
  143. val *= 24;
  144. if (unit == "days")
  145. return val;
  146. if (unit == "weeks")
  147. return val * 7;
  148. val *= 30;
  149. if (unit == "months")
  150. return val;
  151. val *= 12;
  152. if (unit != "years")
  153. throw Exception("Parse time", "unknown time unit " + unit);
  154. return val;
  155. }
  156. long int getNumber(const string& l)
  157. {
  158. // read *all* digits from string l ignoring all other characters
  159. string d;
  160. for (unsigned int i = 0; i < l.size(); ++i)
  161. if (isdigit(l[i]))
  162. d += l[i];
  163. // return long int value of digits
  164. return stol(d);
  165. }
  166. void replacePlaceHolder(string& s,
  167. const string& placeholder,
  168. const string& content)
  169. {
  170. int psize = placeholder.size();
  171. size_t pos = s.find(placeholder);
  172. while (pos != string::npos)
  173. {
  174. s = s.substr(0, pos) + content + s.substr(pos + psize);
  175. pos = s.find(placeholder);
  176. }
  177. }