StringUtils.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "StringUtils.h"
  24. #include <cstdio>
  25. #include "DebugNew.h"
  26. namespace Urho3D
  27. {
  28. unsigned CountElements(const char* buffer, char separator)
  29. {
  30. if (!buffer)
  31. return 0;
  32. const char* endPos = buffer + String::CStringLength(buffer);
  33. const char* pos = buffer;
  34. unsigned ret = 0;
  35. while (pos < endPos)
  36. {
  37. if (*pos != separator)
  38. break;
  39. ++pos;
  40. }
  41. while (pos < endPos)
  42. {
  43. const char* start = pos;
  44. while (start < endPos)
  45. {
  46. if (*start == separator)
  47. break;
  48. ++start;
  49. }
  50. if (start == endPos)
  51. {
  52. ++ret;
  53. break;
  54. }
  55. const char* end = start;
  56. while (end < endPos)
  57. {
  58. if (*end != separator)
  59. break;
  60. ++end;
  61. }
  62. ++ret;
  63. pos = end;
  64. }
  65. return ret;
  66. }
  67. bool ToBool(const String& source)
  68. {
  69. return ToBool(source.CString());
  70. }
  71. bool ToBool(const char* source)
  72. {
  73. unsigned length = String::CStringLength(source);
  74. for (unsigned i = 0; i < length; ++i)
  75. {
  76. char c = tolower(source[i]);
  77. if (c == 't' || c == 'y' || c == '1')
  78. return true;
  79. else if (c != ' ' && c != '\t')
  80. break;
  81. }
  82. return false;
  83. }
  84. int ToInt(const String& source)
  85. {
  86. return ToInt(source.CString());
  87. }
  88. int ToInt(const char* source)
  89. {
  90. if (!source)
  91. return 0;
  92. // Explicitly ask for base 10 to prevent source starts with '0' or '0x' from being converted to base 8 or base 16, respectively
  93. return strtol(source, 0, 10);
  94. }
  95. unsigned ToUInt(const String& source)
  96. {
  97. return ToUInt(source.CString());
  98. }
  99. unsigned ToUInt(const char* source)
  100. {
  101. if (!source)
  102. return 0;
  103. return strtoul(source, 0, 10);
  104. }
  105. float ToFloat(const String& source)
  106. {
  107. return ToFloat(source.CString());
  108. }
  109. float ToFloat(const char* source)
  110. {
  111. if (!source)
  112. return 0;
  113. return (float)strtod(source, 0);
  114. }
  115. Color ToColor(const String& source)
  116. {
  117. return ToColor(source.CString());
  118. }
  119. Color ToColor(const char* source)
  120. {
  121. Color ret;
  122. unsigned elements = CountElements(source, ' ');
  123. if (elements < 3)
  124. return ret;
  125. char* ptr = (char*)source;
  126. ret.r_ = (float)strtod(ptr, &ptr);
  127. ret.g_ = (float)strtod(ptr, &ptr);
  128. ret.b_ = (float)strtod(ptr, &ptr);
  129. if (elements > 3)
  130. ret.a_ = (float)strtod(ptr, &ptr);
  131. return ret;
  132. }
  133. IntRect ToIntRect(const String& source)
  134. {
  135. return ToIntRect(source.CString());
  136. }
  137. IntRect ToIntRect(const char* source)
  138. {
  139. IntRect ret(IntRect::ZERO);
  140. unsigned elements = CountElements(source, ' ');
  141. if (elements < 4)
  142. return ret;
  143. char* ptr = (char*)source;
  144. ret.left_ = strtol(ptr, &ptr, 10);
  145. ret.top_ = strtol(ptr, &ptr, 10);
  146. ret.right_ = strtol(ptr, &ptr, 10);
  147. ret.bottom_ = strtol(ptr, &ptr, 10);
  148. return ret;
  149. }
  150. IntVector2 ToIntVector2(const String& source)
  151. {
  152. return ToIntVector2(source.CString());
  153. }
  154. IntVector2 ToIntVector2(const char* source)
  155. {
  156. IntVector2 ret(IntVector2::ZERO);
  157. unsigned elements = CountElements(source, ' ');
  158. if (elements < 2)
  159. return ret;
  160. char* ptr = (char*)source;
  161. ret.x_ = strtol(ptr, &ptr, 10);
  162. ret.y_ = strtol(ptr, &ptr, 10);
  163. return ret;
  164. }
  165. Rect ToRect(const String& source)
  166. {
  167. return ToRect(source.CString());
  168. }
  169. Rect ToRect(const char* source)
  170. {
  171. Rect ret(Rect::ZERO);
  172. unsigned elements = CountElements(source, ' ');
  173. if (elements < 4)
  174. return ret;
  175. char* ptr = (char*)source;
  176. ret.min_.x_ = (float)strtod(ptr, &ptr);
  177. ret.min_.y_ = (float)strtod(ptr, &ptr);
  178. ret.max_.x_ = (float)strtod(ptr, &ptr);
  179. ret.max_.y_ = (float)strtod(ptr, &ptr);
  180. return ret;
  181. }
  182. Quaternion ToQuaternion(const String& source)
  183. {
  184. return ToQuaternion(source.CString());
  185. }
  186. Quaternion ToQuaternion(const char* source)
  187. {
  188. unsigned elements = CountElements(source, ' ');
  189. char* ptr = (char*)source;
  190. if (elements < 3)
  191. return Quaternion::IDENTITY;
  192. else if (elements < 4)
  193. {
  194. // 3 coords specified: conversion from Euler angles
  195. float x, y, z;
  196. x = (float)strtod(ptr, &ptr);
  197. y = (float)strtod(ptr, &ptr);
  198. z = (float)strtod(ptr, &ptr);
  199. return Quaternion(x, y, z);
  200. }
  201. else
  202. {
  203. // 4 coords specified: full quaternion
  204. Quaternion ret;
  205. ret.w_ = (float)strtod(ptr, &ptr);
  206. ret.x_ = (float)strtod(ptr, &ptr);
  207. ret.y_ = (float)strtod(ptr, &ptr);
  208. ret.z_ = (float)strtod(ptr, &ptr);
  209. return ret;
  210. }
  211. }
  212. Vector2 ToVector2(const String& source)
  213. {
  214. return ToVector2(source.CString());
  215. }
  216. Vector2 ToVector2(const char* source)
  217. {
  218. Vector2 ret(Vector2::ZERO);
  219. unsigned elements = CountElements(source, ' ');
  220. if (elements < 2)
  221. return ret;
  222. char* ptr = (char*)source;
  223. ret.x_ = (float)strtod(ptr, &ptr);
  224. ret.y_ = (float)strtod(ptr, &ptr);
  225. return ret;
  226. }
  227. Vector3 ToVector3(const String& source)
  228. {
  229. return ToVector3(source.CString());
  230. }
  231. Vector3 ToVector3(const char* source)
  232. {
  233. Vector3 ret(Vector3::ZERO);
  234. unsigned elements = CountElements(source, ' ');
  235. if (elements < 3)
  236. return ret;
  237. char* ptr = (char*)source;
  238. ret.x_ = (float)strtod(ptr, &ptr);
  239. ret.y_ = (float)strtod(ptr, &ptr);
  240. ret.z_ = (float)strtod(ptr, &ptr);
  241. return ret;
  242. }
  243. Vector4 ToVector4(const String& source, bool allowMissingCoords)
  244. {
  245. return ToVector4(source.CString(), allowMissingCoords);
  246. }
  247. Vector4 ToVector4(const char* source, bool allowMissingCoords)
  248. {
  249. Vector4 ret(Vector4::ZERO);
  250. unsigned elements = CountElements(source, ' ');
  251. char* ptr = (char*)source;
  252. if (!allowMissingCoords)
  253. {
  254. if (elements < 4)
  255. return ret;
  256. ret.x_ = (float)strtod(ptr, &ptr);
  257. ret.y_ = (float)strtod(ptr, &ptr);
  258. ret.z_ = (float)strtod(ptr, &ptr);
  259. ret.w_ = (float)strtod(ptr, &ptr);
  260. return ret;
  261. }
  262. else
  263. {
  264. if (elements > 0)
  265. ret.x_ = (float)strtod(ptr, &ptr);
  266. if (elements > 1)
  267. ret.y_ = (float)strtod(ptr, &ptr);
  268. if (elements > 2)
  269. ret.z_ = (float)strtod(ptr, &ptr);
  270. if (elements > 3)
  271. ret.w_ = (float)strtod(ptr, &ptr);
  272. return ret;
  273. }
  274. }
  275. Variant ToVectorVariant(const String& source)
  276. {
  277. return ToVectorVariant(source.CString());
  278. }
  279. Variant ToVectorVariant(const char* source)
  280. {
  281. Variant ret;
  282. unsigned elements = CountElements(source, ' ');
  283. switch (elements)
  284. {
  285. case 1:
  286. ret.FromString(VAR_FLOAT, source);
  287. break;
  288. case 2:
  289. ret.FromString(VAR_VECTOR2, source);
  290. break;
  291. case 3:
  292. ret.FromString(VAR_VECTOR3, source);
  293. break;
  294. case 4:
  295. ret.FromString(VAR_VECTOR4, source);
  296. break;
  297. }
  298. return ret;
  299. }
  300. String ToString(void* value)
  301. {
  302. return ToStringHex((unsigned)(size_t)value);
  303. }
  304. String ToStringHex(unsigned value)
  305. {
  306. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  307. sprintf(tempBuffer, "%08x", value);
  308. return String(tempBuffer);
  309. }
  310. void BufferToString(String& dest, const void* data, unsigned size)
  311. {
  312. // Precalculate needed string size
  313. const unsigned char* bytes = (const unsigned char*)data;
  314. unsigned length = 0;
  315. for (unsigned i = 0; i < size; ++i)
  316. {
  317. // Room for separator
  318. if (i)
  319. ++length;
  320. // Room for the value
  321. if (bytes[i] < 10)
  322. ++length;
  323. else if (bytes[i] < 100)
  324. length += 2;
  325. else
  326. length += 3;
  327. }
  328. dest.Resize(length);
  329. unsigned index = 0;
  330. // Convert values
  331. for (unsigned i = 0; i < size; ++i)
  332. {
  333. if (i)
  334. dest[index++] = ' ';
  335. if (bytes[i] < 10)
  336. {
  337. dest[index++] = '0' + bytes[i];
  338. }
  339. else if (bytes[i] < 100)
  340. {
  341. dest[index++] = '0' + bytes[i] / 10;
  342. dest[index++] = '0' + bytes[i] % 10;
  343. }
  344. else
  345. {
  346. dest[index++] = '0' + bytes[i] / 100;
  347. dest[index++] = '0' + bytes[i] % 100 / 10;
  348. dest[index++] = '0' + bytes[i] % 10;
  349. }
  350. }
  351. }
  352. void StringToBuffer(PODVector<unsigned char>& dest, const String& source)
  353. {
  354. StringToBuffer(dest, source.CString());
  355. }
  356. void StringToBuffer(PODVector<unsigned char>& dest, const char* source)
  357. {
  358. if (!source)
  359. {
  360. dest.Clear();
  361. return;
  362. }
  363. unsigned size = CountElements(source, ' ');
  364. dest.Resize(size);
  365. bool inSpace = true;
  366. unsigned index = 0;
  367. unsigned value = 0;
  368. // Parse values
  369. const char* ptr = source;
  370. while (*ptr)
  371. {
  372. if (inSpace && *ptr != ' ')
  373. {
  374. inSpace = false;
  375. value = *ptr - '0';
  376. }
  377. else if (!inSpace && *ptr != ' ')
  378. {
  379. value *= 10;
  380. value += *ptr - '0';
  381. }
  382. else if (!inSpace && *ptr == ' ')
  383. {
  384. dest[index++] = value;
  385. inSpace = true;
  386. }
  387. ++ptr;
  388. }
  389. // Write the final value
  390. if (!inSpace && index < size)
  391. dest[index] = value;
  392. }
  393. unsigned GetStringListIndex(const String& value, const String* strings, unsigned defaultIndex, bool caseSensitive)
  394. {
  395. return GetStringListIndex(value.CString(), strings, defaultIndex, caseSensitive);
  396. }
  397. unsigned GetStringListIndex(const char* value, const String* strings, unsigned defaultIndex, bool caseSensitive)
  398. {
  399. unsigned i = 0;
  400. while (!strings[i].Empty())
  401. {
  402. if (!strings[i].Compare(value, caseSensitive))
  403. return i;
  404. ++i;
  405. }
  406. return defaultIndex;
  407. }
  408. unsigned GetStringListIndex(const char* value, const char** strings, unsigned defaultIndex, bool caseSensitive)
  409. {
  410. unsigned i = 0;
  411. while (strings[i])
  412. {
  413. if (!String::Compare(value, strings[i], caseSensitive))
  414. return i;
  415. ++i;
  416. }
  417. return defaultIndex;
  418. }
  419. String ToString(const char* formatString, ...)
  420. {
  421. String ret;
  422. va_list args;
  423. va_start(args, formatString);
  424. ret.AppendWithFormatArgs(formatString, args);
  425. va_end(args);
  426. return ret;
  427. }
  428. bool IsAlpha(unsigned ch)
  429. {
  430. return ch < 256 ? isalpha(ch) != 0 : false;
  431. }
  432. bool IsDigit(unsigned ch)
  433. {
  434. return ch < 256 ? isdigit(ch) != 0 : false;
  435. }
  436. unsigned ToUpper(unsigned ch)
  437. {
  438. return toupper(ch);
  439. }
  440. unsigned ToLower(unsigned ch)
  441. {
  442. return tolower(ch);
  443. }
  444. }