StringUtils.cpp 11 KB

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