StringUtils.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. //
  2. // Copyright (c) 2008-2016 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 "../Core/StringUtils.h"
  24. #include <cstdio>
  25. #include "../DebugNew.h"
  26. // The built-in strtod() function uses the current locale for parsing strings. On certain
  27. // locales (German, Swedish, ...) a comma is used as decimal separator. For portable XMLs,
  28. // we need to parse floating point values the same way on all platforms no matter what
  29. // locale is set. The strtod_c_locale() is a workaround using the "C" locale explicitly,
  30. // which uses a dot as decimal separator.
  31. #include <locale.h>
  32. #ifdef __APPLE__
  33. #include <xlocale.h>
  34. #endif
  35. #include <stdlib.h>
  36. #ifdef _WIN32
  37. static
  38. _locale_t get_c_locale()
  39. {
  40. static _locale_t loc = _create_locale(LC_ALL, "C");
  41. return loc;
  42. }
  43. static
  44. double strtod_c_locale(const char* nptr, char** endptr)
  45. {
  46. return _strtod_l(nptr, endptr, get_c_locale());
  47. }
  48. #else
  49. static
  50. locale_t get_c_locale()
  51. {
  52. static locale_t loc = newlocale(LC_ALL_MASK, "C", NULL);
  53. return loc;
  54. }
  55. static
  56. double strtod_c_locale(const char* nptr, char** endptr)
  57. {
  58. return strtod_l(nptr, endptr, get_c_locale());
  59. }
  60. #endif // _WIN32
  61. namespace Atomic
  62. {
  63. unsigned CountElements(const char* buffer, char separator)
  64. {
  65. if (!buffer)
  66. return 0;
  67. const char* endPos = buffer + String::CStringLength(buffer);
  68. const char* pos = buffer;
  69. unsigned ret = 0;
  70. while (pos < endPos)
  71. {
  72. if (*pos != separator)
  73. break;
  74. ++pos;
  75. }
  76. while (pos < endPos)
  77. {
  78. const char* start = pos;
  79. while (start < endPos)
  80. {
  81. if (*start == separator)
  82. break;
  83. ++start;
  84. }
  85. if (start == endPos)
  86. {
  87. ++ret;
  88. break;
  89. }
  90. const char* end = start;
  91. while (end < endPos)
  92. {
  93. if (*end != separator)
  94. break;
  95. ++end;
  96. }
  97. ++ret;
  98. pos = end;
  99. }
  100. return ret;
  101. }
  102. bool ToBool(const String& source)
  103. {
  104. return ToBool(source.CString());
  105. }
  106. bool ToBool(const char* source)
  107. {
  108. unsigned length = String::CStringLength(source);
  109. for (unsigned i = 0; i < length; ++i)
  110. {
  111. char c = (char)tolower(source[i]);
  112. if (c == 't' || c == 'y' || c == '1')
  113. return true;
  114. else if (c != ' ' && c != '\t')
  115. break;
  116. }
  117. return false;
  118. }
  119. int ToInt(const String& source, int base)
  120. {
  121. return ToInt(source.CString(), base);
  122. }
  123. int ToInt(const char* source, int base)
  124. {
  125. if (!source)
  126. return 0;
  127. // Shield against runtime library assert by converting illegal base values to 0 (autodetect)
  128. if (base < 2 || base > 36)
  129. base = 0;
  130. return (int)strtol(source, 0, base);
  131. }
  132. unsigned ToUInt(const String& source, int base)
  133. {
  134. return ToUInt(source.CString(), base);
  135. }
  136. unsigned ToUInt(const char* source, int base)
  137. {
  138. if (!source)
  139. return 0;
  140. if (base < 2 || base > 36)
  141. base = 0;
  142. return (unsigned)strtoul(source, 0, base);
  143. }
  144. float ToFloat(const String& source)
  145. {
  146. return ToFloat(source.CString());
  147. }
  148. float ToFloat(const char* source)
  149. {
  150. if (!source)
  151. return 0;
  152. return (float)strtod_c_locale(source, 0);
  153. }
  154. double ToDouble(const String& source)
  155. {
  156. return ToDouble(source.CString());
  157. }
  158. double ToDouble(const char* source)
  159. {
  160. if (!source)
  161. return 0;
  162. return strtod_c_locale(source, 0);
  163. }
  164. Color ToColor(const String& source)
  165. {
  166. return ToColor(source.CString());
  167. }
  168. Color ToColor(const char* source)
  169. {
  170. Color ret;
  171. unsigned elements = CountElements(source, ' ');
  172. if (elements < 3)
  173. return ret;
  174. char* ptr = (char*)source;
  175. ret.r_ = (float)strtod_c_locale(ptr, &ptr);
  176. ret.g_ = (float)strtod_c_locale(ptr, &ptr);
  177. ret.b_ = (float)strtod_c_locale(ptr, &ptr);
  178. if (elements > 3)
  179. ret.a_ = (float)strtod_c_locale(ptr, &ptr);
  180. return ret;
  181. }
  182. IntRect ToIntRect(const String& source)
  183. {
  184. return ToIntRect(source.CString());
  185. }
  186. IntRect ToIntRect(const char* source)
  187. {
  188. IntRect ret(IntRect::ZERO);
  189. unsigned elements = CountElements(source, ' ');
  190. if (elements < 4)
  191. return ret;
  192. char* ptr = (char*)source;
  193. ret.left_ = (int)strtol(ptr, &ptr, 10);
  194. ret.top_ = (int)strtol(ptr, &ptr, 10);
  195. ret.right_ = (int)strtol(ptr, &ptr, 10);
  196. ret.bottom_ = (int)strtol(ptr, &ptr, 10);
  197. return ret;
  198. }
  199. IntVector2 ToIntVector2(const String& source)
  200. {
  201. return ToIntVector2(source.CString());
  202. }
  203. IntVector2 ToIntVector2(const char* source)
  204. {
  205. IntVector2 ret(IntVector2::ZERO);
  206. unsigned elements = CountElements(source, ' ');
  207. if (elements < 2)
  208. return ret;
  209. char* ptr = (char*)source;
  210. ret.x_ = (int)strtol(ptr, &ptr, 10);
  211. ret.y_ = (int)strtol(ptr, &ptr, 10);
  212. return ret;
  213. }
  214. Rect ToRect(const String& source)
  215. {
  216. return ToRect(source.CString());
  217. }
  218. Rect ToRect(const char* source)
  219. {
  220. Rect ret(Rect::ZERO);
  221. unsigned elements = CountElements(source, ' ');
  222. if (elements < 4)
  223. return ret;
  224. char* ptr = (char*)source;
  225. ret.min_.x_ = (float)strtod_c_locale(ptr, &ptr);
  226. ret.min_.y_ = (float)strtod_c_locale(ptr, &ptr);
  227. ret.max_.x_ = (float)strtod_c_locale(ptr, &ptr);
  228. ret.max_.y_ = (float)strtod_c_locale(ptr, &ptr);
  229. return ret;
  230. }
  231. Quaternion ToQuaternion(const String& source)
  232. {
  233. return ToQuaternion(source.CString());
  234. }
  235. Quaternion ToQuaternion(const char* source)
  236. {
  237. unsigned elements = CountElements(source, ' ');
  238. char* ptr = (char*)source;
  239. if (elements < 3)
  240. return Quaternion::IDENTITY;
  241. else if (elements < 4)
  242. {
  243. // 3 coords specified: conversion from Euler angles
  244. float x, y, z;
  245. x = (float)strtod_c_locale(ptr, &ptr);
  246. y = (float)strtod_c_locale(ptr, &ptr);
  247. z = (float)strtod_c_locale(ptr, &ptr);
  248. return Quaternion(x, y, z);
  249. }
  250. else
  251. {
  252. // 4 coords specified: full quaternion
  253. Quaternion ret;
  254. ret.w_ = (float)strtod_c_locale(ptr, &ptr);
  255. ret.x_ = (float)strtod_c_locale(ptr, &ptr);
  256. ret.y_ = (float)strtod_c_locale(ptr, &ptr);
  257. ret.z_ = (float)strtod_c_locale(ptr, &ptr);
  258. return ret;
  259. }
  260. }
  261. Vector2 ToVector2(const String& source)
  262. {
  263. return ToVector2(source.CString());
  264. }
  265. Vector2 ToVector2(const char* source)
  266. {
  267. Vector2 ret(Vector2::ZERO);
  268. unsigned elements = CountElements(source, ' ');
  269. if (elements < 2)
  270. return ret;
  271. char* ptr = (char*)source;
  272. ret.x_ = (float)strtod_c_locale(ptr, &ptr);
  273. ret.y_ = (float)strtod_c_locale(ptr, &ptr);
  274. return ret;
  275. }
  276. Vector3 ToVector3(const String& source)
  277. {
  278. return ToVector3(source.CString());
  279. }
  280. Vector3 ToVector3(const char* source)
  281. {
  282. Vector3 ret(Vector3::ZERO);
  283. unsigned elements = CountElements(source, ' ');
  284. if (elements < 3)
  285. return ret;
  286. char* ptr = (char*)source;
  287. ret.x_ = (float)strtod_c_locale(ptr, &ptr);
  288. ret.y_ = (float)strtod_c_locale(ptr, &ptr);
  289. ret.z_ = (float)strtod_c_locale(ptr, &ptr);
  290. return ret;
  291. }
  292. Vector4 ToVector4(const String& source, bool allowMissingCoords)
  293. {
  294. return ToVector4(source.CString(), allowMissingCoords);
  295. }
  296. Vector4 ToVector4(const char* source, bool allowMissingCoords)
  297. {
  298. Vector4 ret(Vector4::ZERO);
  299. unsigned elements = CountElements(source, ' ');
  300. char* ptr = (char*)source;
  301. if (!allowMissingCoords)
  302. {
  303. if (elements < 4)
  304. return ret;
  305. ret.x_ = (float)strtod_c_locale(ptr, &ptr);
  306. ret.y_ = (float)strtod_c_locale(ptr, &ptr);
  307. ret.z_ = (float)strtod_c_locale(ptr, &ptr);
  308. ret.w_ = (float)strtod_c_locale(ptr, &ptr);
  309. return ret;
  310. }
  311. else
  312. {
  313. if (elements > 0)
  314. ret.x_ = (float)strtod_c_locale(ptr, &ptr);
  315. if (elements > 1)
  316. ret.y_ = (float)strtod_c_locale(ptr, &ptr);
  317. if (elements > 2)
  318. ret.z_ = (float)strtod_c_locale(ptr, &ptr);
  319. if (elements > 3)
  320. ret.w_ = (float)strtod_c_locale(ptr, &ptr);
  321. return ret;
  322. }
  323. }
  324. Variant ToVectorVariant(const String& source)
  325. {
  326. return ToVectorVariant(source.CString());
  327. }
  328. Variant ToVectorVariant(const char* source)
  329. {
  330. Variant ret;
  331. unsigned elements = CountElements(source, ' ');
  332. switch (elements)
  333. {
  334. case 1:
  335. ret.FromString(VAR_FLOAT, source);
  336. break;
  337. case 2:
  338. ret.FromString(VAR_VECTOR2, source);
  339. break;
  340. case 3:
  341. ret.FromString(VAR_VECTOR3, source);
  342. break;
  343. case 4:
  344. ret.FromString(VAR_VECTOR4, source);
  345. break;
  346. case 9:
  347. ret.FromString(VAR_MATRIX3, source);
  348. break;
  349. case 12:
  350. ret.FromString(VAR_MATRIX3X4, source);
  351. break;
  352. case 16:
  353. ret.FromString(VAR_MATRIX4, source);
  354. break;
  355. default:
  356. // Illegal input. Return variant remains empty
  357. break;
  358. }
  359. return ret;
  360. }
  361. Matrix3 ToMatrix3(const String& source)
  362. {
  363. return ToMatrix3(source.CString());
  364. }
  365. Matrix3 ToMatrix3(const char* source)
  366. {
  367. Matrix3 ret(Matrix3::ZERO);
  368. unsigned elements = CountElements(source, ' ');
  369. if (elements < 9)
  370. return ret;
  371. char* ptr = (char*)source;
  372. ret.m00_ = (float)strtod_c_locale(ptr, &ptr);
  373. ret.m01_ = (float)strtod_c_locale(ptr, &ptr);
  374. ret.m02_ = (float)strtod_c_locale(ptr, &ptr);
  375. ret.m10_ = (float)strtod_c_locale(ptr, &ptr);
  376. ret.m11_ = (float)strtod_c_locale(ptr, &ptr);
  377. ret.m12_ = (float)strtod_c_locale(ptr, &ptr);
  378. ret.m20_ = (float)strtod_c_locale(ptr, &ptr);
  379. ret.m21_ = (float)strtod_c_locale(ptr, &ptr);
  380. ret.m22_ = (float)strtod_c_locale(ptr, &ptr);
  381. return ret;
  382. }
  383. Matrix3x4 ToMatrix3x4(const String& source)
  384. {
  385. return ToMatrix3x4(source.CString());
  386. }
  387. Matrix3x4 ToMatrix3x4(const char* source)
  388. {
  389. Matrix3x4 ret(Matrix3x4::ZERO);
  390. unsigned elements = CountElements(source, ' ');
  391. if (elements < 12)
  392. return ret;
  393. char* ptr = (char*)source;
  394. ret.m00_ = (float)strtod_c_locale(ptr, &ptr);
  395. ret.m01_ = (float)strtod_c_locale(ptr, &ptr);
  396. ret.m02_ = (float)strtod_c_locale(ptr, &ptr);
  397. ret.m03_ = (float)strtod_c_locale(ptr, &ptr);
  398. ret.m10_ = (float)strtod_c_locale(ptr, &ptr);
  399. ret.m11_ = (float)strtod_c_locale(ptr, &ptr);
  400. ret.m12_ = (float)strtod_c_locale(ptr, &ptr);
  401. ret.m13_ = (float)strtod_c_locale(ptr, &ptr);
  402. ret.m20_ = (float)strtod_c_locale(ptr, &ptr);
  403. ret.m21_ = (float)strtod_c_locale(ptr, &ptr);
  404. ret.m22_ = (float)strtod_c_locale(ptr, &ptr);
  405. ret.m23_ = (float)strtod_c_locale(ptr, &ptr);
  406. return ret;
  407. }
  408. Matrix4 ToMatrix4(const String& source)
  409. {
  410. return ToMatrix4(source.CString());
  411. }
  412. Matrix4 ToMatrix4(const char* source)
  413. {
  414. Matrix4 ret(Matrix4::ZERO);
  415. unsigned elements = CountElements(source, ' ');
  416. if (elements < 16)
  417. return ret;
  418. char* ptr = (char*)source;
  419. ret.m00_ = (float)strtod_c_locale(ptr, &ptr);
  420. ret.m01_ = (float)strtod_c_locale(ptr, &ptr);
  421. ret.m02_ = (float)strtod_c_locale(ptr, &ptr);
  422. ret.m03_ = (float)strtod_c_locale(ptr, &ptr);
  423. ret.m10_ = (float)strtod_c_locale(ptr, &ptr);
  424. ret.m11_ = (float)strtod_c_locale(ptr, &ptr);
  425. ret.m12_ = (float)strtod_c_locale(ptr, &ptr);
  426. ret.m13_ = (float)strtod_c_locale(ptr, &ptr);
  427. ret.m20_ = (float)strtod_c_locale(ptr, &ptr);
  428. ret.m21_ = (float)strtod_c_locale(ptr, &ptr);
  429. ret.m22_ = (float)strtod_c_locale(ptr, &ptr);
  430. ret.m23_ = (float)strtod_c_locale(ptr, &ptr);
  431. ret.m30_ = (float)strtod_c_locale(ptr, &ptr);
  432. ret.m31_ = (float)strtod_c_locale(ptr, &ptr);
  433. ret.m32_ = (float)strtod_c_locale(ptr, &ptr);
  434. ret.m33_ = (float)strtod_c_locale(ptr, &ptr);
  435. return ret;
  436. }
  437. String ToString(void* value)
  438. {
  439. return ToStringHex((unsigned)(size_t)value);
  440. }
  441. String ToStringHex(unsigned value)
  442. {
  443. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  444. sprintf(tempBuffer, "%08x", value);
  445. return String(tempBuffer);
  446. }
  447. void BufferToString(String& dest, const void* data, unsigned size)
  448. {
  449. // Precalculate needed string size
  450. const unsigned char* bytes = (const unsigned char*)data;
  451. unsigned length = 0;
  452. for (unsigned i = 0; i < size; ++i)
  453. {
  454. // Room for separator
  455. if (i)
  456. ++length;
  457. // Room for the value
  458. if (bytes[i] < 10)
  459. ++length;
  460. else if (bytes[i] < 100)
  461. length += 2;
  462. else
  463. length += 3;
  464. }
  465. dest.Resize(length);
  466. unsigned index = 0;
  467. // Convert values
  468. for (unsigned i = 0; i < size; ++i)
  469. {
  470. if (i)
  471. dest[index++] = ' ';
  472. if (bytes[i] < 10)
  473. {
  474. dest[index++] = '0' + bytes[i];
  475. }
  476. else if (bytes[i] < 100)
  477. {
  478. dest[index++] = (char)('0' + bytes[i] / 10);
  479. dest[index++] = (char)('0' + bytes[i] % 10);
  480. }
  481. else
  482. {
  483. dest[index++] = (char)('0' + bytes[i] / 100);
  484. dest[index++] = (char)('0' + bytes[i] % 100 / 10);
  485. dest[index++] = (char)('0' + bytes[i] % 10);
  486. }
  487. }
  488. }
  489. void StringToBuffer(PODVector<unsigned char>& dest, const String& source)
  490. {
  491. StringToBuffer(dest, source.CString());
  492. }
  493. void StringToBuffer(PODVector<unsigned char>& dest, const char* source)
  494. {
  495. if (!source)
  496. {
  497. dest.Clear();
  498. return;
  499. }
  500. unsigned size = CountElements(source, ' ');
  501. dest.Resize(size);
  502. bool inSpace = true;
  503. unsigned index = 0;
  504. unsigned value = 0;
  505. // Parse values
  506. const char* ptr = source;
  507. while (*ptr)
  508. {
  509. if (inSpace && *ptr != ' ')
  510. {
  511. inSpace = false;
  512. value = (unsigned)(*ptr - '0');
  513. }
  514. else if (!inSpace && *ptr != ' ')
  515. {
  516. value *= 10;
  517. value += *ptr - '0';
  518. }
  519. else if (!inSpace && *ptr == ' ')
  520. {
  521. dest[index++] = (unsigned char)value;
  522. inSpace = true;
  523. }
  524. ++ptr;
  525. }
  526. // Write the final value
  527. if (!inSpace && index < size)
  528. dest[index] = (unsigned char)value;
  529. }
  530. unsigned GetStringListIndex(const String& value, const String* strings, unsigned defaultIndex, bool caseSensitive)
  531. {
  532. return GetStringListIndex(value.CString(), strings, defaultIndex, caseSensitive);
  533. }
  534. unsigned GetStringListIndex(const char* value, const String* strings, unsigned defaultIndex, bool caseSensitive)
  535. {
  536. unsigned i = 0;
  537. while (!strings[i].Empty())
  538. {
  539. if (!strings[i].Compare(value, caseSensitive))
  540. return i;
  541. ++i;
  542. }
  543. return defaultIndex;
  544. }
  545. unsigned GetStringListIndex(const char* value, const char** strings, unsigned defaultIndex, bool caseSensitive)
  546. {
  547. unsigned i = 0;
  548. while (strings[i])
  549. {
  550. if (!String::Compare(value, strings[i], caseSensitive))
  551. return i;
  552. ++i;
  553. }
  554. return defaultIndex;
  555. }
  556. String ToString(const char* formatString, ...)
  557. {
  558. String ret;
  559. va_list args;
  560. va_start(args, formatString);
  561. ret.AppendWithFormatArgs(formatString, args);
  562. va_end(args);
  563. return ret;
  564. }
  565. bool IsAlpha(unsigned ch)
  566. {
  567. return ch < 256 ? isalpha(ch) != 0 : false;
  568. }
  569. bool IsDigit(unsigned ch)
  570. {
  571. return ch < 256 ? isdigit(ch) != 0 : false;
  572. }
  573. unsigned ToUpper(unsigned ch)
  574. {
  575. return (unsigned)toupper(ch);
  576. }
  577. unsigned ToLower(unsigned ch)
  578. {
  579. return (unsigned)tolower(ch);
  580. }
  581. String GetFileSizeString(unsigned long long memorySize)
  582. {
  583. static const char* memorySizeStrings = "kMGTPE";
  584. String output;
  585. if (memorySize < 1024)
  586. {
  587. output = String(memorySize) + " b";
  588. }
  589. else
  590. {
  591. const int exponent = (int)(log((double)memorySize) / log(1024.0));
  592. const double majorValue = ((double)memorySize) / pow(1024.0, exponent);
  593. char buffer[64];
  594. memset(buffer, 0, 64);
  595. sprintf(buffer, "%.1f", majorValue);
  596. output = buffer;
  597. output += " ";
  598. output += memorySizeStrings[exponent - 1];
  599. }
  600. return output;
  601. }
  602. // ATOMIC BEGIN
  603. String ToStringVariadic(const char* formatString, va_list args)
  604. {
  605. String ret;
  606. ret.AppendWithFormatArgs(formatString, args);
  607. return ret;
  608. }
  609. // ATOMIC END
  610. }