StringUtils.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 "StringUtils.h"
  25. #include <cstdio>
  26. #include <cstring>
  27. #include <ctype.h>
  28. #include "DebugNew.h"
  29. static const int TEMP_BUFFER_LENGTH = 128;
  30. std::vector<std::string> Split(const std::string& source, char separator)
  31. {
  32. std::vector<std::string> ret;
  33. unsigned pos = 0;
  34. while (pos < source.length())
  35. {
  36. unsigned start = pos;
  37. while (start < source.length())
  38. {
  39. if (source[start] == separator)
  40. break;
  41. start++;
  42. }
  43. if (start == source.length())
  44. {
  45. ret.push_back(source.substr(pos));
  46. break;
  47. }
  48. unsigned end = start;
  49. while (end < source.length())
  50. {
  51. if (source[end] != separator)
  52. break;
  53. end++;
  54. }
  55. ret.push_back(source.substr(pos, start - pos));
  56. pos = end;
  57. }
  58. return ret;
  59. }
  60. std::string Replace(const std::string& source, char replaceThis, char replaceWith)
  61. {
  62. std::string ret;
  63. ret.resize(source.length());
  64. for (unsigned i = 0; i < ret.length(); ++i)
  65. {
  66. if (source[i] == replaceThis)
  67. ret[i] = replaceWith;
  68. else
  69. ret[i] = source[i];
  70. }
  71. return ret;
  72. }
  73. std::string Replace(const std::string& source, const std::string& replaceThis, const std::string& replaceWith)
  74. {
  75. std::string ret = source;
  76. ReplaceInPlace(ret, replaceThis, replaceWith);
  77. return ret;
  78. }
  79. std::string ToLower(const std::string& source)
  80. {
  81. std::string ret;
  82. ret.resize(source.length());
  83. for (unsigned i = 0; i < ret.length(); ++i)
  84. ret[i] = tolower(source[i]);
  85. return ret;
  86. }
  87. std::string ToUpper(const std::string& source)
  88. {
  89. std::string ret;
  90. ret.resize(source.length());
  91. for (unsigned i = 0; i < ret.length(); ++i)
  92. ret[i] = toupper(source[i]);
  93. return ret;
  94. }
  95. void ReplaceInPlace(std::string& str, char replaceThis, char replaceWith)
  96. {
  97. for (unsigned i = 0; i < str.length(); ++i)
  98. {
  99. if (str[i] == replaceThis)
  100. str[i] = replaceWith;
  101. }
  102. }
  103. void ReplaceInPlace(std::string& str, const std::string& replaceThis, const std::string& replaceWith)
  104. {
  105. size_t nextIndex = 0;
  106. while (nextIndex < str.length())
  107. {
  108. size_t index = str.find(replaceThis, nextIndex);
  109. if (index == std::string::npos)
  110. break;
  111. str.replace(index, replaceThis.length(), replaceWith);
  112. nextIndex = index + replaceWith.length();
  113. }
  114. }
  115. void ToLowerInPlace(std::string& str)
  116. {
  117. for (unsigned i = 0; i < str.length(); ++i)
  118. str[i] = tolower(str[i]);
  119. }
  120. void ToUpperInPlace(std::string& str)
  121. {
  122. for (unsigned i = 0; i < str.length(); ++i)
  123. str[i] = toupper(str[i]);
  124. }
  125. bool ToBool(const std::string& source)
  126. {
  127. std::string temp = ToLower(source);
  128. if (temp.find("true") != std::string::npos)
  129. return true;
  130. else
  131. return false;
  132. }
  133. int ToInt(const std::string& source)
  134. {
  135. if (!source.length())
  136. return 0;
  137. return atoi(source.c_str());
  138. }
  139. unsigned ToUInt(const std::string& source)
  140. {
  141. if (!source.length())
  142. return 0;
  143. return (unsigned)atoi(source.c_str());
  144. }
  145. float ToFloat(const std::string& source)
  146. {
  147. if (!source.length())
  148. return 0.0f;
  149. return (float)atof(source.c_str());
  150. }
  151. Color ToColor(const std::string& source)
  152. {
  153. std::vector<std::string> colors = Split(source, ' ');
  154. if (colors.size() < 3)
  155. return Color();
  156. Color ret(ToFloat(colors[0]), ToFloat(colors[1]), ToFloat(colors[2]));
  157. if (colors.size() > 3)
  158. ret.a_ = ToFloat(colors[3]);
  159. return ret;
  160. }
  161. IntRect ToIntRect(const std::string& source)
  162. {
  163. std::vector<std::string> coords = Split(source, ' ');
  164. if (coords.size() < 4)
  165. return IntRect::ZERO;
  166. else
  167. return IntRect(ToInt(coords[0]), ToInt(coords[1]), ToInt(coords[2]), ToInt(coords[3]));
  168. }
  169. IntVector2 ToIntVector2(const std::string& source)
  170. {
  171. std::vector<std::string> coords = Split(source, ' ');
  172. if (coords.size() < 2)
  173. return IntVector2::ZERO;
  174. else
  175. return IntVector2(ToInt(coords[0]), ToInt(coords[1]));
  176. }
  177. Rect ToRect(const std::string& source)
  178. {
  179. std::vector<std::string> coords = Split(source, ' ');
  180. if (coords.size() < 4)
  181. return Rect::ZERO;
  182. else
  183. return Rect(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]), ToFloat(coords[3]));
  184. }
  185. Quaternion ToQuaternion(const std::string& source)
  186. {
  187. std::vector<std::string> coords = Split(source, ' ');
  188. if (coords.size() < 3)
  189. return Quaternion::IDENTITY;
  190. else if (coords.size() < 4)
  191. // 3 coords specified: conversion from Euler angles
  192. return Quaternion(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]));
  193. else
  194. // 4 coords specified: full quaternion
  195. return Quaternion(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]), ToFloat(coords[3])).GetNormalized();
  196. }
  197. Vector2 ToVector2(const std::string& source)
  198. {
  199. std::vector<std::string> coords = Split(source, ' ');
  200. if (coords.size() < 2)
  201. return Vector2::ZERO;
  202. else
  203. return Vector2(ToFloat(coords[0]), ToFloat(coords[1]));
  204. }
  205. Vector3 ToVector3(const std::string& source)
  206. {
  207. std::vector<std::string> coords = Split(source, ' ');
  208. if (coords.size() < 3)
  209. return Vector3::ZERO;
  210. else
  211. return Vector3(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]));
  212. }
  213. Vector4 ToVector4(const std::string& source, bool allowMissingCoords)
  214. {
  215. std::vector<std::string> coords = Split(source, ' ');
  216. if (!allowMissingCoords)
  217. {
  218. if (coords.size() < 4)
  219. return Vector4::ZERO;
  220. return Vector4(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]), ToFloat(coords[3]));
  221. }
  222. else
  223. {
  224. unsigned num = coords.size();
  225. Vector4 ret(Vector4::ZERO);
  226. if (num > 0)
  227. ret.x_ = ToFloat(coords[0]);
  228. if (num > 1)
  229. ret.y_ = ToFloat(coords[1]);
  230. if (num > 2)
  231. ret.z_ = ToFloat(coords[2]);
  232. if (num > 3)
  233. ret.w_ = ToFloat(coords[3]);
  234. return ret;
  235. }
  236. }
  237. std::string ToString(bool value)
  238. {
  239. if (value)
  240. return "true";
  241. else
  242. return "false";
  243. }
  244. std::string ToString(float value)
  245. {
  246. char tempBuffer[TEMP_BUFFER_LENGTH];
  247. sprintf(tempBuffer, "%g", value);
  248. return std::string(tempBuffer);
  249. }
  250. std::string ToString(int value)
  251. {
  252. char tempBuffer[TEMP_BUFFER_LENGTH];
  253. sprintf(tempBuffer, "%d", value);
  254. return std::string(tempBuffer);
  255. }
  256. std::string ToString(unsigned value)
  257. {
  258. char tempBuffer[TEMP_BUFFER_LENGTH];
  259. sprintf(tempBuffer, "%u", value);
  260. return std::string(tempBuffer);
  261. }
  262. std::string ToString(const Color& value)
  263. {
  264. char tempBuffer[TEMP_BUFFER_LENGTH];
  265. sprintf(tempBuffer, "%g %g %g %g", value.r_, value.g_, value.b_, value.a_);
  266. return std::string(tempBuffer);
  267. }
  268. std::string ToString(const IntRect& value)
  269. {
  270. char tempBuffer[TEMP_BUFFER_LENGTH];
  271. sprintf(tempBuffer, "%d %d %d &d", value.left_, value.top_, value.right_, value.bottom_);
  272. return std::string(tempBuffer);
  273. }
  274. std::string ToString(const IntVector2& value)
  275. {
  276. char tempBuffer[TEMP_BUFFER_LENGTH];
  277. sprintf(tempBuffer, "%d %d", value.x_, value.y_);
  278. return std::string(tempBuffer);
  279. }
  280. std::string ToString(const Rect& value)
  281. {
  282. char tempBuffer[TEMP_BUFFER_LENGTH];
  283. sprintf(tempBuffer, "%g %g %g %g", value.min_.x_, value.min_.y_, value.max_.x_, value.max_.y_);
  284. return std::string(tempBuffer);
  285. }
  286. std::string ToString(const Quaternion& value)
  287. {
  288. char tempBuffer[TEMP_BUFFER_LENGTH];
  289. sprintf(tempBuffer, "%g %g %g %g", value.w_, value.x_, value.y_, value.z_);
  290. return std::string(tempBuffer);
  291. }
  292. std::string ToString(const StringHash& value)
  293. {
  294. char tempBuffer[TEMP_BUFFER_LENGTH];
  295. sprintf(tempBuffer, "%08X", value.GetValue());
  296. return std::string(tempBuffer);
  297. }
  298. std::string ToString(const ShortStringHash& value)
  299. {
  300. char tempBuffer[TEMP_BUFFER_LENGTH];
  301. sprintf(tempBuffer, "%04X", value.GetValue());
  302. return std::string(tempBuffer);
  303. }
  304. std::string ToString(const Vector2& value)
  305. {
  306. char tempBuffer[TEMP_BUFFER_LENGTH];
  307. sprintf(tempBuffer, "%g %g", value.x_, value.y_);
  308. return std::string(tempBuffer);
  309. }
  310. std::string ToString(const Vector3& value)
  311. {
  312. char tempBuffer[TEMP_BUFFER_LENGTH];
  313. sprintf(tempBuffer, "%g %g %g", value.x_, value.y_, value.z_);
  314. return std::string(tempBuffer);
  315. }
  316. std::string ToString(const Vector4& value)
  317. {
  318. char tempBuffer[TEMP_BUFFER_LENGTH];
  319. sprintf(tempBuffer, "%g %g %g %g", value.x_, value.y_, value.z_, value.w_);
  320. return std::string(tempBuffer);
  321. }
  322. std::string ToString(void* value)
  323. {
  324. return ToStringHex((int)value);
  325. }
  326. std::string ToStringHex(unsigned value)
  327. {
  328. char tempBuffer[TEMP_BUFFER_LENGTH];
  329. sprintf(tempBuffer, "%08x", value);
  330. return std::string(tempBuffer);
  331. }
  332. unsigned GetStringListIndex(const std::string& value, const std::string* strings, unsigned count, unsigned defaultIndex,
  333. bool caseSensitive)
  334. {
  335. for (unsigned i = 0; i < count; ++i)
  336. {
  337. if (caseSensitive)
  338. {
  339. if (value == strings[i])
  340. return i;
  341. }
  342. else
  343. {
  344. if (ToLower(value) == ToLower(strings[i]))
  345. return i;
  346. }
  347. }
  348. return defaultIndex;
  349. }
  350. unsigned GetStringListIndex(const char* value, const char** strings, unsigned count, unsigned defaultIndex,
  351. bool caseSensitive)
  352. {
  353. for (unsigned i = 0; i < count; ++i)
  354. {
  355. if (caseSensitive)
  356. {
  357. if (!strcmp(value, strings[i]))
  358. return i;
  359. }
  360. else
  361. {
  362. if (!_stricmp(value, strings[i]))
  363. return i;
  364. }
  365. }
  366. return defaultIndex;
  367. }