StringUtils.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. //
  2. // Copyright (c) 2008-2015 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 "../Core/StringUtils.h"
  23. #include <cstdio>
  24. #include "../DebugNew.h"
  25. namespace Urho3D
  26. {
  27. unsigned CountElements(const char* buffer, char separator)
  28. {
  29. if (!buffer)
  30. return 0;
  31. const char* endPos = buffer + String::CStringLength(buffer);
  32. const char* pos = buffer;
  33. unsigned ret = 0;
  34. while (pos < endPos)
  35. {
  36. if (*pos != separator)
  37. break;
  38. ++pos;
  39. }
  40. while (pos < endPos)
  41. {
  42. const char* start = pos;
  43. while (start < endPos)
  44. {
  45. if (*start == separator)
  46. break;
  47. ++start;
  48. }
  49. if (start == endPos)
  50. {
  51. ++ret;
  52. break;
  53. }
  54. const char* end = start;
  55. while (end < endPos)
  56. {
  57. if (*end != separator)
  58. break;
  59. ++end;
  60. }
  61. ++ret;
  62. pos = end;
  63. }
  64. return ret;
  65. }
  66. bool ToBool(const String& source)
  67. {
  68. return ToBool(source.CString());
  69. }
  70. bool ToBool(const char* source)
  71. {
  72. unsigned length = String::CStringLength(source);
  73. for (unsigned i = 0; i < length; ++i)
  74. {
  75. char c = tolower(source[i]);
  76. if (c == 't' || c == 'y' || c == '1')
  77. return true;
  78. else if (c != ' ' && c != '\t')
  79. break;
  80. }
  81. return false;
  82. }
  83. int ToInt(const String& source)
  84. {
  85. return ToInt(source.CString());
  86. }
  87. int ToInt(const char* source)
  88. {
  89. if (!source)
  90. return 0;
  91. // Explicitly ask for base 10 to prevent source starts with '0' or '0x' from being converted to base 8 or base 16, respectively
  92. return strtol(source, 0, 10);
  93. }
  94. unsigned ToUInt(const String& source)
  95. {
  96. return ToUInt(source.CString());
  97. }
  98. unsigned ToUInt(const char* source)
  99. {
  100. if (!source)
  101. return 0;
  102. return strtoul(source, 0, 10);
  103. }
  104. float ToFloat(const String& source)
  105. {
  106. return ToFloat(source.CString());
  107. }
  108. float ToFloat(const char* source)
  109. {
  110. if (!source)
  111. return 0;
  112. return (float)strtod(source, 0);
  113. }
  114. double ToDouble(const String& source)
  115. {
  116. return ToDouble(source.CString());
  117. }
  118. double ToDouble(const char* source)
  119. {
  120. if (!source)
  121. return 0;
  122. return strtod(source, 0);
  123. }
  124. Color ToColor(const String& source)
  125. {
  126. return ToColor(source.CString());
  127. }
  128. Color ToColor(const char* source)
  129. {
  130. Color ret;
  131. unsigned elements = CountElements(source, ' ');
  132. if (elements < 3)
  133. return ret;
  134. char* ptr = (char*)source;
  135. ret.r_ = (float)strtod(ptr, &ptr);
  136. ret.g_ = (float)strtod(ptr, &ptr);
  137. ret.b_ = (float)strtod(ptr, &ptr);
  138. if (elements > 3)
  139. ret.a_ = (float)strtod(ptr, &ptr);
  140. return ret;
  141. }
  142. IntRect ToIntRect(const String& source)
  143. {
  144. return ToIntRect(source.CString());
  145. }
  146. IntRect ToIntRect(const char* source)
  147. {
  148. IntRect ret(IntRect::ZERO);
  149. unsigned elements = CountElements(source, ' ');
  150. if (elements < 4)
  151. return ret;
  152. char* ptr = (char*)source;
  153. ret.left_ = (int)strtol(ptr, &ptr, 10);
  154. ret.top_ = (int)strtol(ptr, &ptr, 10);
  155. ret.right_ = (int)strtol(ptr, &ptr, 10);
  156. ret.bottom_ = (int)strtol(ptr, &ptr, 10);
  157. return ret;
  158. }
  159. IntVector2 ToIntVector2(const String& source)
  160. {
  161. return ToIntVector2(source.CString());
  162. }
  163. IntVector2 ToIntVector2(const char* source)
  164. {
  165. IntVector2 ret(IntVector2::ZERO);
  166. unsigned elements = CountElements(source, ' ');
  167. if (elements < 2)
  168. return ret;
  169. char* ptr = (char*)source;
  170. ret.x_ = (int)strtol(ptr, &ptr, 10);
  171. ret.y_ = (int)strtol(ptr, &ptr, 10);
  172. return ret;
  173. }
  174. Rect ToRect(const String& source)
  175. {
  176. return ToRect(source.CString());
  177. }
  178. Rect ToRect(const char* source)
  179. {
  180. Rect ret(Rect::ZERO);
  181. unsigned elements = CountElements(source, ' ');
  182. if (elements < 4)
  183. return ret;
  184. char* ptr = (char*)source;
  185. ret.min_.x_ = (float)strtod(ptr, &ptr);
  186. ret.min_.y_ = (float)strtod(ptr, &ptr);
  187. ret.max_.x_ = (float)strtod(ptr, &ptr);
  188. ret.max_.y_ = (float)strtod(ptr, &ptr);
  189. return ret;
  190. }
  191. Quaternion ToQuaternion(const String& source)
  192. {
  193. return ToQuaternion(source.CString());
  194. }
  195. Quaternion ToQuaternion(const char* source)
  196. {
  197. unsigned elements = CountElements(source, ' ');
  198. char* ptr = (char*)source;
  199. if (elements < 3)
  200. return Quaternion::IDENTITY;
  201. else if (elements < 4)
  202. {
  203. // 3 coords specified: conversion from Euler angles
  204. float x, y, z;
  205. x = (float)strtod(ptr, &ptr);
  206. y = (float)strtod(ptr, &ptr);
  207. z = (float)strtod(ptr, &ptr);
  208. return Quaternion(x, y, z);
  209. }
  210. else
  211. {
  212. // 4 coords specified: full quaternion
  213. Quaternion ret;
  214. ret.w_ = (float)strtod(ptr, &ptr);
  215. ret.x_ = (float)strtod(ptr, &ptr);
  216. ret.y_ = (float)strtod(ptr, &ptr);
  217. ret.z_ = (float)strtod(ptr, &ptr);
  218. return ret;
  219. }
  220. }
  221. Vector2 ToVector2(const String& source)
  222. {
  223. return ToVector2(source.CString());
  224. }
  225. Vector2 ToVector2(const char* source)
  226. {
  227. Vector2 ret(Vector2::ZERO);
  228. unsigned elements = CountElements(source, ' ');
  229. if (elements < 2)
  230. return ret;
  231. char* ptr = (char*)source;
  232. ret.x_ = (float)strtod(ptr, &ptr);
  233. ret.y_ = (float)strtod(ptr, &ptr);
  234. return ret;
  235. }
  236. Vector3 ToVector3(const String& source)
  237. {
  238. return ToVector3(source.CString());
  239. }
  240. Vector3 ToVector3(const char* source)
  241. {
  242. Vector3 ret(Vector3::ZERO);
  243. unsigned elements = CountElements(source, ' ');
  244. if (elements < 3)
  245. return ret;
  246. char* ptr = (char*)source;
  247. ret.x_ = (float)strtod(ptr, &ptr);
  248. ret.y_ = (float)strtod(ptr, &ptr);
  249. ret.z_ = (float)strtod(ptr, &ptr);
  250. return ret;
  251. }
  252. Vector4 ToVector4(const String& source, bool allowMissingCoords)
  253. {
  254. return ToVector4(source.CString(), allowMissingCoords);
  255. }
  256. Vector4 ToVector4(const char* source, bool allowMissingCoords)
  257. {
  258. Vector4 ret(Vector4::ZERO);
  259. unsigned elements = CountElements(source, ' ');
  260. char* ptr = (char*)source;
  261. if (!allowMissingCoords)
  262. {
  263. if (elements < 4)
  264. return ret;
  265. ret.x_ = (float)strtod(ptr, &ptr);
  266. ret.y_ = (float)strtod(ptr, &ptr);
  267. ret.z_ = (float)strtod(ptr, &ptr);
  268. ret.w_ = (float)strtod(ptr, &ptr);
  269. return ret;
  270. }
  271. else
  272. {
  273. if (elements > 0)
  274. ret.x_ = (float)strtod(ptr, &ptr);
  275. if (elements > 1)
  276. ret.y_ = (float)strtod(ptr, &ptr);
  277. if (elements > 2)
  278. ret.z_ = (float)strtod(ptr, &ptr);
  279. if (elements > 3)
  280. ret.w_ = (float)strtod(ptr, &ptr);
  281. return ret;
  282. }
  283. }
  284. Variant ToVectorVariant(const String& source)
  285. {
  286. return ToVectorVariant(source.CString());
  287. }
  288. Variant ToVectorVariant(const char* source)
  289. {
  290. Variant ret;
  291. unsigned elements = CountElements(source, ' ');
  292. switch (elements)
  293. {
  294. case 1:
  295. ret.FromString(VAR_FLOAT, source);
  296. break;
  297. case 2:
  298. ret.FromString(VAR_VECTOR2, source);
  299. break;
  300. case 3:
  301. ret.FromString(VAR_VECTOR3, source);
  302. break;
  303. case 4:
  304. ret.FromString(VAR_VECTOR4, source);
  305. break;
  306. case 9:
  307. ret.FromString(VAR_MATRIX3, source);
  308. break;
  309. case 12:
  310. ret.FromString(VAR_MATRIX3X4, source);
  311. break;
  312. case 16:
  313. ret.FromString(VAR_MATRIX4, source);
  314. break;
  315. }
  316. return ret;
  317. }
  318. Matrix3 ToMatrix3(const String& source)
  319. {
  320. return ToMatrix3(source.CString());
  321. }
  322. Matrix3 ToMatrix3(const char* source)
  323. {
  324. Matrix3 ret(Matrix3::ZERO);
  325. unsigned elements = CountElements(source, ' ');
  326. if (elements < 9)
  327. return ret;
  328. char* ptr = (char*)source;
  329. ret.m00_ = (float)strtod(ptr, &ptr);
  330. ret.m01_ = (float)strtod(ptr, &ptr);
  331. ret.m02_ = (float)strtod(ptr, &ptr);
  332. ret.m10_ = (float)strtod(ptr, &ptr);
  333. ret.m11_ = (float)strtod(ptr, &ptr);
  334. ret.m12_ = (float)strtod(ptr, &ptr);
  335. ret.m20_ = (float)strtod(ptr, &ptr);
  336. ret.m21_ = (float)strtod(ptr, &ptr);
  337. ret.m22_ = (float)strtod(ptr, &ptr);
  338. return ret;
  339. }
  340. Matrix3x4 ToMatrix3x4(const String& source)
  341. {
  342. return ToMatrix3x4(source.CString());
  343. }
  344. Matrix3x4 ToMatrix3x4(const char* source)
  345. {
  346. Matrix3x4 ret(Matrix3x4::ZERO);
  347. unsigned elements = CountElements(source, ' ');
  348. if (elements < 12)
  349. return ret;
  350. char* ptr = (char*)source;
  351. ret.m00_ = (float)strtod(ptr, &ptr);
  352. ret.m01_ = (float)strtod(ptr, &ptr);
  353. ret.m02_ = (float)strtod(ptr, &ptr);
  354. ret.m03_ = (float)strtod(ptr, &ptr);
  355. ret.m10_ = (float)strtod(ptr, &ptr);
  356. ret.m11_ = (float)strtod(ptr, &ptr);
  357. ret.m12_ = (float)strtod(ptr, &ptr);
  358. ret.m13_ = (float)strtod(ptr, &ptr);
  359. ret.m20_ = (float)strtod(ptr, &ptr);
  360. ret.m21_ = (float)strtod(ptr, &ptr);
  361. ret.m22_ = (float)strtod(ptr, &ptr);
  362. ret.m23_ = (float)strtod(ptr, &ptr);
  363. return ret;
  364. }
  365. Matrix4 ToMatrix4(const String& source)
  366. {
  367. return ToMatrix4(source.CString());
  368. }
  369. Matrix4 ToMatrix4(const char* source)
  370. {
  371. Matrix4 ret(Matrix4::ZERO);
  372. unsigned elements = CountElements(source, ' ');
  373. if (elements < 16)
  374. return ret;
  375. char* ptr = (char*)source;
  376. ret.m00_ = (float)strtod(ptr, &ptr);
  377. ret.m01_ = (float)strtod(ptr, &ptr);
  378. ret.m02_ = (float)strtod(ptr, &ptr);
  379. ret.m03_ = (float)strtod(ptr, &ptr);
  380. ret.m10_ = (float)strtod(ptr, &ptr);
  381. ret.m11_ = (float)strtod(ptr, &ptr);
  382. ret.m12_ = (float)strtod(ptr, &ptr);
  383. ret.m13_ = (float)strtod(ptr, &ptr);
  384. ret.m20_ = (float)strtod(ptr, &ptr);
  385. ret.m21_ = (float)strtod(ptr, &ptr);
  386. ret.m22_ = (float)strtod(ptr, &ptr);
  387. ret.m23_ = (float)strtod(ptr, &ptr);
  388. ret.m30_ = (float)strtod(ptr, &ptr);
  389. ret.m31_ = (float)strtod(ptr, &ptr);
  390. ret.m32_ = (float)strtod(ptr, &ptr);
  391. ret.m33_ = (float)strtod(ptr, &ptr);
  392. return ret;
  393. }
  394. String ToString(void* value)
  395. {
  396. return ToStringHex((unsigned)(size_t)value);
  397. }
  398. String ToStringHex(unsigned value)
  399. {
  400. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  401. sprintf(tempBuffer, "%08x", value);
  402. return String(tempBuffer);
  403. }
  404. void BufferToString(String& dest, const void* data, unsigned size)
  405. {
  406. // Precalculate needed string size
  407. const unsigned char* bytes = (const unsigned char*)data;
  408. unsigned length = 0;
  409. for (unsigned i = 0; i < size; ++i)
  410. {
  411. // Room for separator
  412. if (i)
  413. ++length;
  414. // Room for the value
  415. if (bytes[i] < 10)
  416. ++length;
  417. else if (bytes[i] < 100)
  418. length += 2;
  419. else
  420. length += 3;
  421. }
  422. dest.Resize(length);
  423. unsigned index = 0;
  424. // Convert values
  425. for (unsigned i = 0; i < size; ++i)
  426. {
  427. if (i)
  428. dest[index++] = ' ';
  429. if (bytes[i] < 10)
  430. {
  431. dest[index++] = '0' + bytes[i];
  432. }
  433. else if (bytes[i] < 100)
  434. {
  435. dest[index++] = '0' + bytes[i] / 10;
  436. dest[index++] = '0' + bytes[i] % 10;
  437. }
  438. else
  439. {
  440. dest[index++] = '0' + bytes[i] / 100;
  441. dest[index++] = '0' + bytes[i] % 100 / 10;
  442. dest[index++] = '0' + bytes[i] % 10;
  443. }
  444. }
  445. }
  446. void StringToBuffer(PODVector<unsigned char>& dest, const String& source)
  447. {
  448. StringToBuffer(dest, source.CString());
  449. }
  450. void StringToBuffer(PODVector<unsigned char>& dest, const char* source)
  451. {
  452. if (!source)
  453. {
  454. dest.Clear();
  455. return;
  456. }
  457. unsigned size = CountElements(source, ' ');
  458. dest.Resize(size);
  459. bool inSpace = true;
  460. unsigned index = 0;
  461. unsigned value = 0;
  462. // Parse values
  463. const char* ptr = source;
  464. while (*ptr)
  465. {
  466. if (inSpace && *ptr != ' ')
  467. {
  468. inSpace = false;
  469. value = *ptr - '0';
  470. }
  471. else if (!inSpace && *ptr != ' ')
  472. {
  473. value *= 10;
  474. value += *ptr - '0';
  475. }
  476. else if (!inSpace && *ptr == ' ')
  477. {
  478. dest[index++] = value;
  479. inSpace = true;
  480. }
  481. ++ptr;
  482. }
  483. // Write the final value
  484. if (!inSpace && index < size)
  485. dest[index] = value;
  486. }
  487. unsigned GetStringListIndex(const String& value, const String* strings, unsigned defaultIndex, bool caseSensitive)
  488. {
  489. return GetStringListIndex(value.CString(), strings, defaultIndex, caseSensitive);
  490. }
  491. unsigned GetStringListIndex(const char* value, const String* strings, unsigned defaultIndex, bool caseSensitive)
  492. {
  493. unsigned i = 0;
  494. while (!strings[i].Empty())
  495. {
  496. if (!strings[i].Compare(value, caseSensitive))
  497. return i;
  498. ++i;
  499. }
  500. return defaultIndex;
  501. }
  502. unsigned GetStringListIndex(const char* value, const char** strings, unsigned defaultIndex, bool caseSensitive)
  503. {
  504. unsigned i = 0;
  505. while (strings[i])
  506. {
  507. if (!String::Compare(value, strings[i], caseSensitive))
  508. return i;
  509. ++i;
  510. }
  511. return defaultIndex;
  512. }
  513. String ToString(const char* formatString, ...)
  514. {
  515. String ret;
  516. va_list args;
  517. va_start(args, formatString);
  518. ret.AppendWithFormatArgs(formatString, args);
  519. va_end(args);
  520. return ret;
  521. }
  522. bool IsAlpha(unsigned ch)
  523. {
  524. return ch < 256 ? isalpha(ch) != 0 : false;
  525. }
  526. bool IsDigit(unsigned ch)
  527. {
  528. return ch < 256 ? isdigit(ch) != 0 : false;
  529. }
  530. unsigned ToUpper(unsigned ch)
  531. {
  532. return toupper(ch);
  533. }
  534. unsigned ToLower(unsigned ch)
  535. {
  536. return tolower(ch);
  537. }
  538. }