string_util.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Copyright 2013 Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "string_util.h"
  17. #include <algorithm>
  18. #ifdef _WINDOWS
  19. #include <windows.h>
  20. #define snprintf _snprintf
  21. #define strncasecmp _strnicmp
  22. #define strcasecmp _stricmp
  23. #else
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <iostream>
  27. #endif
  28. #ifndef va_copy
  29. #define va_copy(d,s) ((d) = (s))
  30. #endif
  31. // original source:
  32. // https://github.com/facebook/folly/blob/master/folly/String.cpp
  33. //
  34. const std::string vStringFormat(const char* format, va_list args)
  35. {
  36. std::string output;
  37. va_list tmpargs;
  38. va_copy(tmpargs,args);
  39. int characters_used = vsnprintf(nullptr, 0, format, tmpargs);
  40. va_end(tmpargs);
  41. // Looks like we have a valid format string.
  42. if (characters_used > 0) {
  43. output.resize(characters_used + 1);
  44. va_copy(tmpargs,args);
  45. characters_used = vsnprintf(&output[0], output.capacity(), format, tmpargs);
  46. va_end(tmpargs);
  47. output.resize(characters_used);
  48. // We shouldn't have a format error by this point, but I can't imagine what error we
  49. // could have by this point. Still, return empty string;
  50. if (characters_used < 0)
  51. output.clear();
  52. }
  53. return output;
  54. }
  55. const std::string str_tolower(std::string s)
  56. {
  57. std::transform(
  58. s.begin(), s.end(), s.begin(),
  59. [](unsigned char c) { return ::tolower(c); }
  60. );
  61. return s;
  62. }
  63. std::vector<std::string> split(std::string str_to_split, char delimiter)
  64. {
  65. std::stringstream ss(str_to_split);
  66. std::string item;
  67. std::vector<std::string> exploded_values;
  68. while (std::getline(ss, item, delimiter)) {
  69. exploded_values.push_back(item);
  70. }
  71. return exploded_values;
  72. }
  73. const std::string str_toupper(std::string s)
  74. {
  75. std::transform(
  76. s.begin(), s.end(), s.begin(),
  77. [](unsigned char c) { return ::toupper(c); }
  78. );
  79. return s;
  80. }
  81. const std::string ucfirst(std::string s)
  82. {
  83. std::string output = s;
  84. if (!s.empty())
  85. output[0] = static_cast<char>(::toupper(s[0]));
  86. return output;
  87. }
  88. const std::string StringFormat(const char *format, ...)
  89. {
  90. va_list args;
  91. va_start(args, format);
  92. std::string output = vStringFormat(format, args);
  93. va_end(args);
  94. return output;
  95. }
  96. std::vector<std::string> SplitString(const std::string &str, char delim) {
  97. std::vector<std::string> ret;
  98. std::stringstream ss(str);
  99. std::string item;
  100. while(std::getline(ss, item, delim)) {
  101. ret.push_back(item);
  102. }
  103. return ret;
  104. }
  105. std::string implode(std::string glue, std::vector<std::string> src)
  106. {
  107. if (src.empty()) {
  108. return {};
  109. }
  110. std::ostringstream output;
  111. std::vector<std::string>::iterator src_iter;
  112. for (src_iter = src.begin(); src_iter != src.end(); src_iter++) {
  113. output << *src_iter << glue;
  114. }
  115. std::string final_output = output.str();
  116. final_output.resize (output.str().size () - glue.size());
  117. return final_output;
  118. }
  119. std::string EscapeString(const std::string &s) {
  120. std::string ret;
  121. size_t sz = s.length();
  122. for(size_t i = 0; i < sz; ++i) {
  123. char c = s[i];
  124. switch(c) {
  125. case '\x00':
  126. ret += "\\x00";
  127. break;
  128. case '\n':
  129. ret += "\\n";
  130. break;
  131. case '\r':
  132. ret += "\\r";
  133. break;
  134. case '\\':
  135. ret += "\\\\";
  136. break;
  137. case '\'':
  138. ret += "\\'";
  139. break;
  140. case '\"':
  141. ret += "\\\"";
  142. break;
  143. case '\x1a':
  144. ret += "\\x1a";
  145. break;
  146. default:
  147. ret.push_back(c);
  148. break;
  149. }
  150. }
  151. return ret;
  152. }
  153. std::string EscapeString(const char *src, size_t sz) {
  154. std::string ret;
  155. for(size_t i = 0; i < sz; ++i) {
  156. char c = src[i];
  157. switch(c) {
  158. case '\x00':
  159. ret += "\\x00";
  160. break;
  161. case '\n':
  162. ret += "\\n";
  163. break;
  164. case '\r':
  165. ret += "\\r";
  166. break;
  167. case '\\':
  168. ret += "\\\\";
  169. break;
  170. case '\'':
  171. ret += "\\'";
  172. break;
  173. case '\"':
  174. ret += "\\\"";
  175. break;
  176. case '\x1a':
  177. ret += "\\x1a";
  178. break;
  179. default:
  180. ret.push_back(c);
  181. break;
  182. }
  183. }
  184. return ret;
  185. }
  186. bool StringIsNumber(const std::string &s) {
  187. try {
  188. auto r = stod(s);
  189. return true;
  190. }
  191. catch (std::exception &) {
  192. return false;
  193. }
  194. }
  195. void ToLowerString(std::string &s) {
  196. std::transform(s.begin(), s.end(), s.begin(), ::tolower);
  197. }
  198. void ToUpperString(std::string &s) {
  199. std::transform(s.begin(), s.end(), s.begin(), ::toupper);
  200. }
  201. std::string JoinString(const std::vector<std::string>& ar, const std::string &delim) {
  202. std::string ret;
  203. for (size_t i = 0; i < ar.size(); ++i) {
  204. if (i != 0) {
  205. ret += delim;
  206. }
  207. ret += ar[i];
  208. }
  209. return ret;
  210. }
  211. void find_replace(std::string &string_subject, const std::string &search_string, const std::string &replace_string)
  212. {
  213. if (string_subject.find(search_string) == std::string::npos) {
  214. return;
  215. }
  216. size_t start_pos = 0;
  217. while((start_pos = string_subject.find(search_string, start_pos)) != std::string::npos) {
  218. string_subject.replace(start_pos, search_string.length(), replace_string);
  219. start_pos += replace_string.length();
  220. }
  221. }
  222. void ParseAccountString(const std::string &s, std::string &account, std::string &loginserver)
  223. {
  224. auto split = SplitString(s, ':');
  225. if (split.size() == 2) {
  226. loginserver = split[0];
  227. account = split[1];
  228. }
  229. else if(split.size() == 1) {
  230. account = split[0];
  231. }
  232. }
  233. //Const char based
  234. // normal strncpy doesnt put a null term on copied strings, this one does
  235. // ref: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecrt/htm/_wcecrt_strncpy_wcsncpy.asp
  236. char* strn0cpy(char* dest, const char* source, uint32 size) {
  237. if (!dest)
  238. return 0;
  239. if (size == 0 || source == 0) {
  240. dest[0] = 0;
  241. return dest;
  242. }
  243. strncpy(dest, source, size);
  244. dest[size - 1] = 0;
  245. return dest;
  246. }
  247. // String N w/null Copy Truncated?
  248. // return value =true if entire string(source) fit, false if it was truncated
  249. bool strn0cpyt(char* dest, const char* source, uint32 size) {
  250. if (!dest)
  251. return 0;
  252. if (size == 0 || source == 0) {
  253. dest[0] = 0;
  254. return false;
  255. }
  256. strncpy(dest, source, size);
  257. dest[size - 1] = 0;
  258. return (bool)(source[strlen(dest)] == 0);
  259. }
  260. const char *MakeLowerString(const char *source) {
  261. static char str[128];
  262. if (!source)
  263. return nullptr;
  264. MakeLowerString(source, str);
  265. return str;
  266. }
  267. void MakeLowerString(const char *source, char *target) {
  268. if (!source || !target) {
  269. *target = 0;
  270. return;
  271. }
  272. while (*source)
  273. {
  274. *target = tolower(*source);
  275. target++; source++;
  276. }
  277. *target = 0;
  278. }
  279. int MakeAnyLenString(char** ret, const char* format, ...) {
  280. int buf_len = 128;
  281. int chars = -1;
  282. va_list argptr, tmpargptr;
  283. va_start(argptr, format);
  284. while (chars == -1 || chars >= buf_len) {
  285. safe_delete_array(*ret);
  286. if (chars == -1)
  287. buf_len *= 2;
  288. else
  289. buf_len = chars + 1;
  290. *ret = new char[buf_len];
  291. va_copy(tmpargptr, argptr);
  292. chars = vsnprintf(*ret, buf_len, format, tmpargptr);
  293. }
  294. va_end(argptr);
  295. return chars;
  296. }
  297. uint32 AppendAnyLenString(char** ret, uint32* bufsize, uint32* strlen, const char* format, ...) {
  298. if (*bufsize == 0)
  299. *bufsize = 256;
  300. if (*ret == 0)
  301. *strlen = 0;
  302. int chars = -1;
  303. char* oldret = 0;
  304. va_list argptr, tmpargptr;
  305. va_start(argptr, format);
  306. while (chars == -1 || chars >= (int32)(*bufsize - *strlen)) {
  307. if (chars == -1)
  308. *bufsize += 256;
  309. else
  310. *bufsize += chars + 25;
  311. oldret = *ret;
  312. *ret = new char[*bufsize];
  313. if (oldret) {
  314. if (*strlen)
  315. memcpy(*ret, oldret, *strlen);
  316. safe_delete_array(oldret);
  317. }
  318. va_copy(tmpargptr, argptr);
  319. chars = vsnprintf(&(*ret)[*strlen], (*bufsize - *strlen), format, tmpargptr);
  320. }
  321. va_end(argptr);
  322. *strlen += chars;
  323. return *strlen;
  324. }
  325. uint32 hextoi(const char* num) {
  326. if (num == nullptr)
  327. return 0;
  328. int len = strlen(num);
  329. if (len < 3)
  330. return 0;
  331. if (num[0] != '0' || (num[1] != 'x' && num[1] != 'X'))
  332. return 0;
  333. uint32 ret = 0;
  334. int mul = 1;
  335. for (int i = len - 1; i >= 2; i--) {
  336. if (num[i] >= 'A' && num[i] <= 'F')
  337. ret += ((num[i] - 'A') + 10) * mul;
  338. else if (num[i] >= 'a' && num[i] <= 'f')
  339. ret += ((num[i] - 'a') + 10) * mul;
  340. else if (num[i] >= '0' && num[i] <= '9')
  341. ret += (num[i] - '0') * mul;
  342. else
  343. return 0;
  344. mul *= 16;
  345. }
  346. return ret;
  347. }
  348. uint64 hextoi64(const char* num) {
  349. if (num == nullptr)
  350. return 0;
  351. int len = strlen(num);
  352. if (len < 3)
  353. return 0;
  354. if (num[0] != '0' || (num[1] != 'x' && num[1] != 'X'))
  355. return 0;
  356. uint64 ret = 0;
  357. int mul = 1;
  358. for (int i = len - 1; i >= 2; i--) {
  359. if (num[i] >= 'A' && num[i] <= 'F')
  360. ret += ((num[i] - 'A') + 10) * mul;
  361. else if (num[i] >= 'a' && num[i] <= 'f')
  362. ret += ((num[i] - 'a') + 10) * mul;
  363. else if (num[i] >= '0' && num[i] <= '9')
  364. ret += (num[i] - '0') * mul;
  365. else
  366. return 0;
  367. mul *= 16;
  368. }
  369. return ret;
  370. }
  371. bool atobool(const char* iBool) {
  372. if (iBool == nullptr)
  373. return false;
  374. if (!strcasecmp(iBool, "true"))
  375. return true;
  376. if (!strcasecmp(iBool, "false"))
  377. return false;
  378. if (!strcasecmp(iBool, "yes"))
  379. return true;
  380. if (!strcasecmp(iBool, "no"))
  381. return false;
  382. if (!strcasecmp(iBool, "on"))
  383. return true;
  384. if (!strcasecmp(iBool, "off"))
  385. return false;
  386. if (!strcasecmp(iBool, "enable"))
  387. return true;
  388. if (!strcasecmp(iBool, "disable"))
  389. return false;
  390. if (!strcasecmp(iBool, "enabled"))
  391. return true;
  392. if (!strcasecmp(iBool, "disabled"))
  393. return false;
  394. if (!strcasecmp(iBool, "y"))
  395. return true;
  396. if (!strcasecmp(iBool, "n"))
  397. return false;
  398. if (atoi(iBool))
  399. return true;
  400. return false;
  401. }
  402. // removes the crap and turns the underscores into spaces.
  403. char *CleanMobName(const char *in, char *out)
  404. {
  405. unsigned i, j;
  406. for (i = j = 0; i < strlen(in); i++)
  407. {
  408. // convert _ to space.. any other conversions like this? I *think* this
  409. // is the only non alpha char that's not stripped but converted.
  410. if (in[i] == '_')
  411. {
  412. out[j++] = ' ';
  413. }
  414. else
  415. {
  416. if (isalpha(in[i]) || (in[i] == '`')) // numbers, #, or any other crap just gets skipped
  417. out[j++] = in[i];
  418. }
  419. }
  420. out[j] = 0; // terimnate the string before returning it
  421. return out;
  422. }
  423. void RemoveApostrophes(std::string &s)
  424. {
  425. for (unsigned int i = 0; i < s.length(); ++i)
  426. if (s[i] == '\'')
  427. s[i] = '_';
  428. }
  429. char *RemoveApostrophes(const char *s)
  430. {
  431. auto NewString = new char[strlen(s) + 1];
  432. strcpy(NewString, s);
  433. for (unsigned int i = 0; i < strlen(NewString); ++i)
  434. if (NewString[i] == '\'')
  435. NewString[i] = '_';
  436. return NewString;
  437. }
  438. const char *ConvertArray(int input, char *returnchar)
  439. {
  440. sprintf(returnchar, "%i", input);
  441. return returnchar;
  442. }
  443. const char *ConvertArrayF(float input, char *returnchar)
  444. {
  445. sprintf(returnchar, "%0.2f", input);
  446. return returnchar;
  447. }
  448. bool isAlphaNumeric(const char *text)
  449. {
  450. for (unsigned int charIndex = 0; charIndex<strlen(text); charIndex++) {
  451. if ((text[charIndex] < 'a' || text[charIndex] > 'z') &&
  452. (text[charIndex] < 'A' || text[charIndex] > 'Z') &&
  453. (text[charIndex] < '0' || text[charIndex] > '9'))
  454. return false;
  455. }
  456. return true;
  457. }