StringUtils.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. size_t nextIndex = 0;
  108. while (nextIndex < str.length())
  109. {
  110. size_t index = str.find(replaceThis, nextIndex);
  111. if (index == std::string::npos)
  112. break;
  113. str.replace(index, replaceThis.length(), replaceWith);
  114. nextIndex = index + replaceWith.length();
  115. }
  116. }
  117. void toLowerInPlace(std::string& str)
  118. {
  119. for (unsigned i = 0; i < str.length(); ++i)
  120. str[i] = tolower(str[i]);
  121. }
  122. void toUpperInPlace(std::string& str)
  123. {
  124. for (unsigned i = 0; i < str.length(); ++i)
  125. str[i] = toupper(str[i]);
  126. }
  127. bool toBool(const std::string& source)
  128. {
  129. std::string temp = toLower(source);
  130. if (temp.find("true") != std::string::npos)
  131. return true;
  132. else
  133. return false;
  134. }
  135. int toInt(const std::string& source)
  136. {
  137. if (!source.length())
  138. return 0;
  139. return atoi(source.c_str());
  140. }
  141. unsigned toUInt(const std::string& source)
  142. {
  143. if (!source.length())
  144. return 0;
  145. return (unsigned)atoi(source.c_str());
  146. }
  147. float toFloat(const std::string& source)
  148. {
  149. if (!source.length())
  150. return 0.0f;
  151. return (float)atof(source.c_str());
  152. }
  153. Color toColor(const std::string& source)
  154. {
  155. std::vector<std::string> colors = split(source, ' ');
  156. if (colors.size() < 3)
  157. return Color();
  158. Color ret(toFloat(colors[0]), toFloat(colors[1]), toFloat(colors[2]));
  159. if (colors.size() > 3)
  160. ret.mA = toFloat(colors[3]);
  161. return ret;
  162. }
  163. IntRect toIntRect(const std::string& source)
  164. {
  165. std::vector<std::string> coords = split(source, ' ');
  166. if (coords.size() < 4)
  167. return IntRect::sZero;
  168. else
  169. return IntRect(toInt(coords[0]), toInt(coords[1]), toInt(coords[2]), toInt(coords[3]));
  170. }
  171. IntVector2 toIntVector2(const std::string& source)
  172. {
  173. std::vector<std::string> coords = split(source, ' ');
  174. if (coords.size() < 2)
  175. return IntVector2::sZero;
  176. else
  177. return IntVector2(toInt(coords[0]), toInt(coords[1]));
  178. }
  179. Rect toRect(const std::string& source)
  180. {
  181. std::vector<std::string> coords = split(source, ' ');
  182. if (coords.size() < 4)
  183. return Rect::sZero;
  184. else
  185. return Rect(toFloat(coords[0]), toFloat(coords[1]), toFloat(coords[2]), toFloat(coords[3]));
  186. }
  187. Quaternion toQuaternion(const std::string& source)
  188. {
  189. std::vector<std::string> coords = split(source, ' ');
  190. if (coords.size() < 3)
  191. return Quaternion::sIdentity;
  192. else if (coords.size() < 4)
  193. // 3 coords specified: conversion from Euler angles
  194. return Quaternion(toFloat(coords[0]), toFloat(coords[1]), toFloat(coords[2]));
  195. else
  196. // 4 coords specified: full quaternion
  197. return Quaternion(toFloat(coords[0]), toFloat(coords[1]), toFloat(coords[2]), toFloat(coords[3])).getNormalized();
  198. }
  199. Vector2 toVector2(const std::string& source)
  200. {
  201. std::vector<std::string> coords = split(source, ' ');
  202. if (coords.size() < 2)
  203. return Vector2::sZero;
  204. else
  205. return Vector2(toFloat(coords[0]), toFloat(coords[1]));
  206. }
  207. Vector3 toVector3(const std::string& source)
  208. {
  209. std::vector<std::string> coords = split(source, ' ');
  210. if (coords.size() < 3)
  211. return Vector3::sZero;
  212. else
  213. return Vector3(toFloat(coords[0]), toFloat(coords[1]), toFloat(coords[2]));
  214. }
  215. Vector4 toVector4(const std::string& source, bool allowMissingCoords)
  216. {
  217. std::vector<std::string> coords = split(source, ' ');
  218. if (!allowMissingCoords)
  219. {
  220. if (coords.size() < 4)
  221. return Vector4::sZero;
  222. return Vector4(toFloat(coords[0]), toFloat(coords[1]), toFloat(coords[2]), toFloat(coords[3]));
  223. }
  224. else
  225. {
  226. unsigned num = coords.size();
  227. Vector4 ret(Vector4::sZero);
  228. if (num > 0)
  229. ret.mX = toFloat(coords[0]);
  230. if (num > 1)
  231. ret.mY = toFloat(coords[1]);
  232. if (num > 2)
  233. ret.mZ = toFloat(coords[2]);
  234. if (num > 3)
  235. ret.mW = toFloat(coords[3]);
  236. return ret;
  237. }
  238. }
  239. std::string toString(bool value)
  240. {
  241. if (value)
  242. return "true";
  243. else
  244. return "false";
  245. }
  246. std::string toString(float value)
  247. {
  248. sprintf(tempBuffer, "%g", value);
  249. return std::string(tempBuffer);
  250. }
  251. std::string toString(int value)
  252. {
  253. sprintf(tempBuffer, "%d", value);
  254. return std::string(tempBuffer);
  255. }
  256. std::string toString(unsigned value)
  257. {
  258. sprintf(tempBuffer, "%u", value);
  259. return std::string(tempBuffer);
  260. }
  261. std::string toString(const Color& value)
  262. {
  263. sprintf(tempBuffer, "%g %g %g %g", value.mR, value.mG, value.mB, value.mA);
  264. return std::string(tempBuffer);
  265. }
  266. std::string toString(const IntRect& value)
  267. {
  268. sprintf(tempBuffer, "%d %d %d &d", value.mLeft, value.mTop, value.mRight, value.mBottom);
  269. return std::string(tempBuffer);
  270. }
  271. std::string toString(const IntVector2& value)
  272. {
  273. sprintf(tempBuffer, "%d %d", value.mX, value.mY);
  274. return std::string(tempBuffer);
  275. }
  276. std::string toString(const Rect& value)
  277. {
  278. sprintf(tempBuffer, "%g %g %g %g", value.mMin.mX, value.mMin.mY, value.mMax.mX, value.mMax.mY);
  279. return std::string(tempBuffer);
  280. }
  281. std::string toString(const Quaternion& value)
  282. {
  283. sprintf(tempBuffer, "%g %g %g %g", value.mW, value.mX, value.mY, value.mZ);
  284. return std::string(tempBuffer);
  285. }
  286. std::string toString(const StringHash& value)
  287. {
  288. const std::string& str = hashToString(value);
  289. // Return reverse-mapped string if possible
  290. if (str.empty())
  291. {
  292. sprintf(tempBuffer, "%08X", value.mData);
  293. return std::string(tempBuffer);
  294. }
  295. else
  296. return str;
  297. }
  298. std::string toString(const ShortStringHash& value)
  299. {
  300. const std::string& str = shortHashToString(value);
  301. // Return reverse-mapped string if possible
  302. if (str.empty())
  303. {
  304. sprintf(tempBuffer, "%04X", value.mData);
  305. return std::string(tempBuffer);
  306. }
  307. else
  308. return str;
  309. }
  310. std::string toString(const Vector2& value)
  311. {
  312. sprintf(tempBuffer, "%g %g", value.mX, value.mY);
  313. return std::string(tempBuffer);
  314. }
  315. std::string toString(const Vector3& value)
  316. {
  317. sprintf(tempBuffer, "%g %g %g", value.mX, value.mY, value.mZ);
  318. return std::string(tempBuffer);
  319. }
  320. std::string toString(const Vector4& value)
  321. {
  322. sprintf(tempBuffer, "%g %g %g %g", value.mX, value.mY, value.mZ, value.mW);
  323. return std::string(tempBuffer);
  324. }
  325. std::string toStringHex(int value)
  326. {
  327. sprintf(tempBuffer, "%08x", value);
  328. return std::string(tempBuffer);
  329. }
  330. unsigned getIndexFromStringList(const std::string& value, const std::string* strings, unsigned count, unsigned defaultIndex,
  331. bool caseSensitive)
  332. {
  333. for (unsigned i = 0; i < count; ++i)
  334. {
  335. if (caseSensitive)
  336. {
  337. if (value == strings[i])
  338. return i;
  339. }
  340. else
  341. {
  342. if (toLower(value) == toLower(strings[i]))
  343. return i;
  344. }
  345. }
  346. return defaultIndex;
  347. }