StringUtils.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "SharedArrayPtr.h"
  25. #include "StringUtils.h"
  26. #include <cstdio>
  27. #include <cstdlib>
  28. #include <ctype.h>
  29. #include "DebugNew.h"
  30. static const int TEMP_BUFFER_LENGTH = 128;
  31. char tempBuffer[TEMP_BUFFER_LENGTH];
  32. std::vector<std::string> split(const std::string& source, char separator)
  33. {
  34. std::vector<std::string> ret;
  35. unsigned pos = 0;
  36. while (pos < source.length())
  37. {
  38. unsigned start = pos;
  39. while (start < source.length())
  40. {
  41. if (source[start] == separator)
  42. break;
  43. start++;
  44. }
  45. if (start == source.length())
  46. {
  47. ret.push_back(source.substr(pos));
  48. break;
  49. }
  50. unsigned end = start;
  51. while (end < source.length())
  52. {
  53. if (source[end] != separator)
  54. break;
  55. end++;
  56. }
  57. ret.push_back(source.substr(pos, start - pos));
  58. pos = end;
  59. }
  60. return ret;
  61. }
  62. std::string replace(const std::string& source, char replaceThis, char replaceWith)
  63. {
  64. std::string ret;
  65. ret.resize(source.length());
  66. for (unsigned i = 0; i < ret.length(); ++i)
  67. {
  68. if (source[i] == replaceThis)
  69. ret[i] = replaceWith;
  70. else
  71. ret[i] = source[i];
  72. }
  73. return ret;
  74. }
  75. std::string replace(const std::string& source, const std::string& replaceThis, const std::string& replaceWith)
  76. {
  77. std::string ret = source;
  78. replaceInPlace(ret, replaceThis, replaceWith);
  79. return ret;
  80. }
  81. std::string toLower(const std::string& source)
  82. {
  83. std::string ret;
  84. ret.resize(source.length());
  85. for (unsigned i = 0; i < ret.length(); ++i)
  86. ret[i] = tolower(source[i]);
  87. return ret;
  88. }
  89. std::string toUpper(const std::string& source)
  90. {
  91. std::string ret;
  92. ret.resize(source.length());
  93. for (unsigned i = 0; i < ret.length(); ++i)
  94. ret[i] = toupper(source[i]);
  95. return ret;
  96. }
  97. void replaceInPlace(std::string& str, char replaceThis, char replaceWith)
  98. {
  99. for (unsigned i = 0; i < str.length(); ++i)
  100. {
  101. if (str[i] == replaceThis)
  102. str[i] = replaceWith;
  103. }
  104. }
  105. void replaceInPlace(std::string& str, const std::string& replaceThis, const std::string& replaceWith)
  106. {
  107. // Check for possibility of infinite loop and terminate in that case
  108. if (replaceWith.find(replaceThis) != std::string::npos)
  109. return;
  110. for (;;)
  111. {
  112. size_t index = str.find(replaceThis);
  113. if (index == std::string::npos)
  114. break;
  115. str.replace(index, replaceThis.length(), replaceWith);
  116. }
  117. }
  118. void toLowerInPlace(std::string& str)
  119. {
  120. for (unsigned i = 0; i < str.length(); ++i)
  121. str[i] = tolower(str[i]);
  122. }
  123. void toUpperInPlace(std::string& str)
  124. {
  125. for (unsigned i = 0; i < str.length(); ++i)
  126. str[i] = toupper(str[i]);
  127. }
  128. bool toBool(const std::string& source)
  129. {
  130. std::string temp = toLower(source);
  131. if (temp.find("true") != std::string::npos)
  132. return true;
  133. else
  134. return false;
  135. }
  136. int toInt(const std::string& source)
  137. {
  138. if (!source.length())
  139. return 0;
  140. return atoi(source.c_str());
  141. }
  142. unsigned toUInt(const std::string& source)
  143. {
  144. if (!source.length())
  145. return 0;
  146. return (unsigned)atoi(source.c_str());
  147. }
  148. float toFloat(const std::string& source)
  149. {
  150. if (!source.length())
  151. return 0.0f;
  152. return (float)atof(source.c_str());
  153. }
  154. Color toColor(const std::string& source)
  155. {
  156. std::vector<std::string> colors = split(source, ' ');
  157. if (colors.size() < 3)
  158. return Color();
  159. Color ret(toFloat(colors[0]), toFloat(colors[1]), toFloat(colors[2]));
  160. if (colors.size() > 3)
  161. ret.mA = toFloat(colors[3]);
  162. return ret;
  163. }
  164. IntRect toIntRect(const std::string& source)
  165. {
  166. std::vector<std::string> coords = split(source, ' ');
  167. if (coords.size() < 4)
  168. return IntRect::sZero;
  169. else
  170. return IntRect(toInt(coords[0]), toInt(coords[1]), toInt(coords[2]), toInt(coords[3]));
  171. }
  172. IntVector2 toIntVector2(const std::string& source)
  173. {
  174. std::vector<std::string> coords = split(source, ' ');
  175. if (coords.size() < 2)
  176. return IntVector2::sZero;
  177. else
  178. return IntVector2(toInt(coords[0]), toInt(coords[1]));
  179. }
  180. Rect toRect(const std::string& source)
  181. {
  182. std::vector<std::string> coords = split(source, ' ');
  183. if (coords.size() < 4)
  184. return Rect::sZero;
  185. else
  186. return Rect(toFloat(coords[0]), toFloat(coords[1]), toFloat(coords[2]), toFloat(coords[3]));
  187. }
  188. Quaternion toQuaternion(const std::string& source)
  189. {
  190. std::vector<std::string> coords = split(source, ' ');
  191. if (coords.size() < 3)
  192. return Quaternion::sIdentity;
  193. else if (coords.size() < 4)
  194. // 3 coords specified: conversion from Euler angles
  195. return Quaternion(toFloat(coords[0]), toFloat(coords[1]), toFloat(coords[2]));
  196. else
  197. // 4 coords specified: full quaternion
  198. return Quaternion(toFloat(coords[0]), toFloat(coords[1]), toFloat(coords[2]), toFloat(coords[3])).getNormalized();
  199. }
  200. Vector2 toVector2(const std::string& source)
  201. {
  202. std::vector<std::string> coords = split(source, ' ');
  203. if (coords.size() < 2)
  204. return Vector2::sZero;
  205. else
  206. return Vector2(toFloat(coords[0]), toFloat(coords[1]));
  207. }
  208. Vector3 toVector3(const std::string& source)
  209. {
  210. std::vector<std::string> coords = split(source, ' ');
  211. if (coords.size() < 3)
  212. return Vector3::sZero;
  213. else
  214. return Vector3(toFloat(coords[0]), toFloat(coords[1]), toFloat(coords[2]));
  215. }
  216. Vector4 toVector4(const std::string& source, bool allowMissingCoords)
  217. {
  218. std::vector<std::string> coords = split(source, ' ');
  219. if (!allowMissingCoords)
  220. {
  221. if (coords.size() < 4)
  222. return Vector4::sZero;
  223. return Vector4(toFloat(coords[0]), toFloat(coords[1]), toFloat(coords[2]), toFloat(coords[3]));
  224. }
  225. else
  226. {
  227. unsigned num = coords.size();
  228. Vector4 ret(Vector4::sZero);
  229. if (num > 0)
  230. ret.mX = toFloat(coords[0]);
  231. if (num > 1)
  232. ret.mY = toFloat(coords[1]);
  233. if (num > 2)
  234. ret.mZ = toFloat(coords[2]);
  235. if (num > 3)
  236. ret.mW = toFloat(coords[3]);
  237. return ret;
  238. }
  239. }
  240. std::string toString(bool value)
  241. {
  242. if (value)
  243. return "true";
  244. else
  245. return "false";
  246. }
  247. std::string toString(float value)
  248. {
  249. sprintf(tempBuffer, "%g", value);
  250. return std::string(tempBuffer);
  251. }
  252. std::string toString(int value)
  253. {
  254. sprintf(tempBuffer, "%d", value);
  255. return std::string(tempBuffer);
  256. }
  257. std::string toString(unsigned value)
  258. {
  259. sprintf(tempBuffer, "%u", value);
  260. return std::string(tempBuffer);
  261. }
  262. std::string toString(const Color& value)
  263. {
  264. sprintf(tempBuffer, "%g %g %g %g", value.mR, value.mG, value.mB, value.mA);
  265. return std::string(tempBuffer);
  266. }
  267. std::string toString(const IntRect& value)
  268. {
  269. sprintf(tempBuffer, "%d %d %d &d", value.mLeft, value.mTop, value.mRight, value.mBottom);
  270. return std::string(tempBuffer);
  271. }
  272. std::string toString(const IntVector2& value)
  273. {
  274. sprintf(tempBuffer, "%d %d", value.mX, value.mY);
  275. return std::string(tempBuffer);
  276. }
  277. std::string toString(const Rect& value)
  278. {
  279. sprintf(tempBuffer, "%g %g %g %g", value.mMin.mX, value.mMin.mY, value.mMax.mX, value.mMax.mY);
  280. return std::string(tempBuffer);
  281. }
  282. std::string toString(const Quaternion& value)
  283. {
  284. sprintf(tempBuffer, "%g %g %g %g", value.mW, value.mX, value.mY, value.mZ);
  285. return std::string(tempBuffer);
  286. }
  287. std::string toString(const StringHash& value)
  288. {
  289. const std::string& str = hashToString(value);
  290. // Return reverse-mapped string if possible
  291. if (str.empty())
  292. {
  293. sprintf(tempBuffer, "%08X", value.mData);
  294. return std::string(tempBuffer);
  295. }
  296. else
  297. return str;
  298. }
  299. std::string toString(const ShortStringHash& value)
  300. {
  301. const std::string& str = shortHashToString(value);
  302. // Return reverse-mapped string if possible
  303. if (str.empty())
  304. {
  305. sprintf(tempBuffer, "%04X", value.mData);
  306. return std::string(tempBuffer);
  307. }
  308. else
  309. return str;
  310. }
  311. std::string toString(const Vector2& value)
  312. {
  313. sprintf(tempBuffer, "%g %g", value.mX, value.mY);
  314. return std::string(tempBuffer);
  315. }
  316. std::string toString(const Vector3& value)
  317. {
  318. sprintf(tempBuffer, "%g %g %g", value.mX, value.mY, value.mZ);
  319. return std::string(tempBuffer);
  320. }
  321. std::string toString(const Vector4& value)
  322. {
  323. sprintf(tempBuffer, "%g %g %g %g", value.mX, value.mY, value.mZ, value.mW);
  324. return std::string(tempBuffer);
  325. }
  326. std::string toStringHex(int value)
  327. {
  328. sprintf(tempBuffer, "%08x", value);
  329. return std::string(tempBuffer);
  330. }
  331. unsigned getIndexFromStringList(const std::string& value, const std::string* strings, unsigned count, unsigned defaultIndex,
  332. bool caseSensitive)
  333. {
  334. for (unsigned i = 0; i < count; ++i)
  335. {
  336. if (caseSensitive)
  337. {
  338. if (value == strings[i])
  339. return i;
  340. }
  341. else
  342. {
  343. if (toLower(value) == toLower(strings[i]))
  344. return i;
  345. }
  346. }
  347. return defaultIndex;
  348. }