stringtools.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. double getDouble(const string& s, unsigned int& i)
  114. {
  115. if (!isdigit(s[i]) && s[i] != '-')
  116. throw Exception("getDouble", "digit expected in ");
  117. string is;
  118. while (i < s.size() && (isdigit(s[i]) || s[i] == '-' || s[i] == '.' || s[i] == 'e' || s[i] == 'E'))
  119. {
  120. is += s[i];
  121. ++i;
  122. }
  123. double res = stod(is);
  124. skipWS(s, i);
  125. return res;
  126. }
  127. string getWord(const string& s, unsigned int& i)
  128. {
  129. if (!isalpha(s[i]) && s[i] != '-' && s[i] != '_')
  130. throw Exception("getWord", "letter expected");
  131. string is;
  132. while (i < s.size() && (isalnum(s[i]) || s[i] == '-' || s[i] == '_'))
  133. {
  134. is += s[i];
  135. ++i;
  136. }
  137. skipWS(s, i);
  138. return is;
  139. }
  140. time_t stot(const string& str)
  141. {
  142. // converts a string describing time periods to
  143. // time in seconds
  144. // numbers without units are in seconds
  145. // all given values are accumulated
  146. // use of sign is optional
  147. // 1 hour
  148. // 3 hours 5 minutes
  149. // 1 day -5 minutes
  150. // 25 -3
  151. Lexer s(str);
  152. time_t value = 0;
  153. while (!s.empty())
  154. {
  155. bool minus = false;
  156. if (s.type == Lexer::singlecharacter)
  157. {
  158. if (s.token == "-")
  159. minus = true;
  160. else if (s.token != "+")
  161. throw Exception("string to time", "expected sign");
  162. s.nextToken();
  163. }
  164. time_t thisValue = s.getInt();
  165. string unit = "s";
  166. if (s.type == Lexer::identifier)
  167. unit = s.getWord();
  168. //eliminate plural s to simplify comparisn
  169. if (unit.size() > 1 && unit[unit.size() - 1] == 's')
  170. unit.resize(unit.size() - 1);
  171. // check all units and common abbreviations
  172. if (unit == "s" || unit == "sec" || unit == "second")
  173. thisValue = thisValue;
  174. else if (unit == "min" || unit == "minute")
  175. thisValue = thisValue * 60;
  176. else if (unit == "h" || unit == "hour")
  177. thisValue = thisValue * 60 * 60;
  178. else if (unit == "d" || unit == "day")
  179. thisValue = thisValue * 60 * 60 * 24;
  180. else if (unit == "week")
  181. thisValue = thisValue * 60 * 60 * 24 * 7;
  182. else if (unit == "month")
  183. thisValue = thisValue * 60 * 60 * 24 * 30;
  184. else if (unit == "a" || unit == "year")
  185. thisValue = thisValue * 60 * 60 * 24 * 365;
  186. else
  187. throw Exception("string to time", "unknown unit " + unit);
  188. if (!minus)
  189. value += thisValue;
  190. else
  191. value -= thisValue;
  192. }
  193. return value;
  194. }
  195. string timeString(time_t t)
  196. {
  197. string res;
  198. if (t < 0)
  199. {
  200. res = "- ";
  201. t = -t;
  202. }
  203. int sec = t % 60;
  204. t /= 60;
  205. int min = t % 60;
  206. t /= 60;
  207. int hour = t % 24;
  208. t /= 24;
  209. int days = t;
  210. int nParts = 0;
  211. if (days > 0)
  212. {
  213. res += to_string(days) + " days ";
  214. nParts++;
  215. }
  216. if (hour > 0)
  217. {
  218. res += to_string(hour) + " hours ";
  219. nParts++;
  220. }
  221. if (min > 0 && nParts < 2)
  222. {
  223. res += to_string(min) + " minutes ";
  224. nParts++;
  225. }
  226. if ((sec > 0 && nParts < 2) || nParts == 0)
  227. res += to_string(sec) + " seconds ";
  228. return res;
  229. }
  230. double getNumber(const string& l)
  231. {
  232. // read *all* digits from string l ignoring all other characters
  233. // "read 3,000,421 Bytes" => 3000421
  234. string d;
  235. for (unsigned int i = 0; i < l.size(); ++i)
  236. if (isdigit(l[i]))
  237. d += l[i];
  238. // return long int value of digits
  239. long int res = 0;
  240. try
  241. {
  242. res = stof(d);
  243. }
  244. catch (...)
  245. {
  246. // we ignore overflow here because value is only informative
  247. cout << "stol failed on " << l << endl;
  248. res = -1;
  249. }
  250. return res;
  251. }
  252. void replacePlaceHolder(string& s,
  253. const string& placeholder,
  254. const string& content)
  255. {
  256. int psize = placeholder.size();
  257. size_t pos = s.find(placeholder);
  258. while (pos != string::npos)
  259. {
  260. s = s.substr(0, pos) + content + s.substr(pos + psize);
  261. pos = s.find(placeholder);
  262. }
  263. }