stringtools.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include <iostream>
  2. #include "Exception.h"
  3. #include "stringtools.h"
  4. #include "Lexer.h"
  5. using namespace std;
  6. string delSpaces(const string& s)
  7. {
  8. string res = "";
  9. for (unsigned int i = 0; i < s.size(); ++i)
  10. {
  11. if (!isspace(s[i]))
  12. res += s[i];
  13. }
  14. return res;
  15. }
  16. void substitute(std::string& s, char c1, char c2)
  17. {
  18. for (unsigned int i = 0; i < s.size(); ++i)
  19. if (s[i] == c1) s[i] = c2;
  20. }
  21. void reduceToOne(std::string& s, char c)
  22. {
  23. unsigned int pr = 0;
  24. unsigned int pw = 0;
  25. while (pr < s.size())
  26. {
  27. char ac = s[pr];
  28. s[pw] = ac;
  29. ++pr;
  30. ++pw;
  31. if (ac == c)
  32. {
  33. while (pr < s.size() && s[pr] == c) // skip further characters c
  34. ++pr;
  35. }
  36. }
  37. s.resize(pw);
  38. }
  39. string trim(const string& s)
  40. {
  41. string res = "";
  42. int last = s.size() - 1;
  43. while (last >= 0 && isspace(s[last]))
  44. --last;
  45. int first = 0;
  46. while (first < (int)s.size() && isspace(s[first]))
  47. ++first;
  48. for (int i = first; i <= last; ++i)
  49. res += s[i];
  50. return res;
  51. }
  52. bool startsWith(const string& s, const string& start)
  53. {
  54. int len = start.length();
  55. return s.substr(0, len) == start;
  56. }
  57. int split(const string& s, Strings& parts, char del, int expectedParts)
  58. {
  59. parts.clear();
  60. string p = "";
  61. if (!s.empty())
  62. {
  63. for (unsigned int i = 0; i < s.size(); ++i)
  64. {
  65. if (s[i] == del)
  66. {
  67. parts.push_back(p);
  68. p.clear();
  69. }
  70. else
  71. p += s[i];
  72. }
  73. parts.push_back(p);
  74. }
  75. if (expectedParts > 0 && expectedParts != (int)parts.size())
  76. throw Exception("split", string("Unexpected number of parts: ") + s);
  77. return parts.size();
  78. }
  79. void skipWS(const string& s, unsigned int& i)
  80. {
  81. static const string ws = " \t\n";
  82. while (i < s.size() && ws.find(s[i]) != string::npos)
  83. ++i;
  84. }
  85. int getInt(const string& s, unsigned int& i)
  86. {
  87. if (!isdigit(s[i]))
  88. throw Exception("getInt", "digit expected");
  89. string is;
  90. while (i < s.size() && isdigit(s[i]))
  91. {
  92. is += s[i];
  93. ++i;
  94. }
  95. int res = stoi(is);
  96. skipWS(s, i);
  97. return res;
  98. }
  99. long int getLongInt(const string& s, unsigned int& i)
  100. {
  101. if (!isdigit(s[i]))
  102. throw Exception("getLongInt", "digit expected");
  103. string is;
  104. while (i < s.size() && isdigit(s[i]))
  105. {
  106. is += s[i];
  107. ++i;
  108. }
  109. long int res = stol(is);
  110. skipWS(s, i);
  111. return res;
  112. }
  113. string getWord(const string& s, unsigned int& i)
  114. {
  115. if (!isalpha(s[i]) && s[i] != '-' && s[i] != '_')
  116. throw Exception("getWord", "letter expected");
  117. string is;
  118. while (i < s.size() && (isalpha(s[i]) || s[i] == '-' || s[i] == '_'))
  119. {
  120. is += s[i];
  121. ++i;
  122. }
  123. skipWS(s, i);
  124. return is;
  125. }
  126. time_t stot(const string& str)
  127. {
  128. // converts a string describing time periods to
  129. // time in seconds
  130. // numbers without units are in seconds
  131. // all given values are accumulated
  132. // use of sign is optional
  133. // 1 hour
  134. // 3 hours 5 minutes
  135. // 1 day -5 minutes
  136. // 25 -3
  137. Lexer s(str);
  138. time_t value = 0;
  139. while (!s.empty())
  140. {
  141. bool minus = false;
  142. if (s.type == Lexer::singlecharacter)
  143. {
  144. if (s.token == "-")
  145. minus = true;
  146. else if (s.token != "+")
  147. throw Exception("string to time", "expected sign");
  148. s.nextToken();
  149. }
  150. time_t thisValue = s.getInt();
  151. string unit = "s";
  152. if (s.type == Lexer::identifier)
  153. unit = s.getWord();
  154. //eliminate plural s to simplify comparisn
  155. if (unit.size() > 1 && unit[unit.size() - 1] == 's')
  156. unit.resize(unit.size() - 1);
  157. // check all units and common abbreviations
  158. if (unit == "s" || unit == "sec" || unit == "second")
  159. thisValue = thisValue;
  160. else if (unit == "min" || unit == "minute")
  161. thisValue = thisValue * 60;
  162. else if (unit == "h" || unit == "hour")
  163. thisValue = thisValue * 60 * 60;
  164. else if (unit == "d" || unit == "day")
  165. thisValue = thisValue * 60 * 60 * 24;
  166. else if (unit == "week")
  167. thisValue = thisValue * 60 * 60 * 24 * 7;
  168. else if (unit == "month")
  169. thisValue = thisValue * 60 * 60 * 24 * 30;
  170. else if (unit == "a" || unit == "year")
  171. thisValue = thisValue * 60 * 60 * 24 * 365;
  172. else
  173. throw Exception("string to time", "unknown unit " + unit);
  174. if (!minus)
  175. value += thisValue;
  176. else
  177. value -= thisValue;
  178. }
  179. return value;
  180. }
  181. string timeString(time_t t)
  182. {
  183. string res;
  184. if (t < 0)
  185. {
  186. res = "- ";
  187. t = -t;
  188. }
  189. int sec = t % 60;
  190. t /= 60;
  191. int min = t % 60;
  192. t /= 60;
  193. int hour = t % 24;
  194. t /= 24;
  195. int days = t;
  196. if (days > 0)
  197. res += to_string(days) + " days ";
  198. if (hour > 0)
  199. res += to_string(hour) + " hours ";
  200. if (min > 0)
  201. res += to_string(min) + " minutes ";
  202. if (sec > 0 || res.empty())
  203. res += to_string(sec) + " seconds ";
  204. return res;
  205. }
  206. long int getNumber(const string& l)
  207. {
  208. // read *all* digits from string l ignoring all other characters
  209. // "read 3,000,421 Bytes" => 3000421
  210. string d;
  211. for (unsigned int i = 0; i < l.size(); ++i)
  212. if (isdigit(l[i]))
  213. d += l[i];
  214. // return long int value of digits
  215. long int res = 0;
  216. try
  217. {
  218. res = stol(d);
  219. }
  220. catch (...)
  221. {
  222. // we ignore overflow here because value is only informative
  223. cout << "stol failed on " << l << endl;
  224. res = -1;
  225. }
  226. return res;
  227. }
  228. void replacePlaceHolder(string& s,
  229. const string& placeholder,
  230. const string& content)
  231. {
  232. int psize = placeholder.size();
  233. size_t pos = s.find(placeholder);
  234. while (pos != string::npos)
  235. {
  236. s = s.substr(0, pos) + content + s.substr(pos + psize);
  237. pos = s.find(placeholder);
  238. }
  239. }