irrString.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine" and the "irrXML" project.
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
  4. #pragma once
  5. #include "irrTypes.h"
  6. #include <string>
  7. #include <string_view>
  8. #include <algorithm>
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <cwchar>
  12. /* HACK: import these string methods from MT's util/string.h */
  13. extern std::wstring utf8_to_wide(std::string_view input);
  14. extern std::string wide_to_utf8(std::wstring_view input);
  15. /* */
  16. namespace irr
  17. {
  18. namespace core
  19. {
  20. //! Very simple string class with some useful features.
  21. /** string<c8> and string<wchar_t> both accept Unicode AND ASCII/Latin-1,
  22. so you can assign Unicode to string<c8> and ASCII/Latin-1 to string<wchar_t>
  23. (and the other way round) if you want to.
  24. However, note that the conversation between both is not done using any encoding.
  25. This means that c8 strings are treated as ASCII/Latin-1, not UTF-8, and
  26. are simply expanded to the equivalent wchar_t, while Unicode/wchar_t
  27. characters are truncated to 8-bit ASCII/Latin-1 characters, discarding all
  28. other information in the wchar_t.
  29. Helper functions for converting between UTF-8 and wchar_t are provided
  30. outside the string class for explicit use.
  31. */
  32. // forward declarations
  33. template <typename T>
  34. class string;
  35. //! Typedef for character strings
  36. typedef string<c8> stringc;
  37. //! Typedef for wide character strings
  38. typedef string<wchar_t> stringw;
  39. //! Returns a character converted to lower case
  40. static inline u32 locale_lower(u32 x)
  41. {
  42. // ansi
  43. return x >= 'A' && x <= 'Z' ? x + 0x20 : x;
  44. }
  45. //! Returns a character converted to upper case
  46. static inline u32 locale_upper(u32 x)
  47. {
  48. // ansi
  49. return x >= 'a' && x <= 'z' ? x + ('A' - 'a') : x;
  50. }
  51. template <typename T>
  52. class string
  53. {
  54. using stl_type = std::basic_string<T>;
  55. public:
  56. typedef T char_type;
  57. //! Default constructor
  58. string()
  59. {
  60. }
  61. //! Copy constructor
  62. string(const string<T> &other)
  63. {
  64. *this = other;
  65. }
  66. string(const stl_type &str) : str(str) {}
  67. string(stl_type &&str) : str(std::move(str)) {}
  68. //! Constructor from other string types
  69. template <class B>
  70. string(const string<B> &other)
  71. {
  72. *this = other;
  73. }
  74. //! Constructs a string from a float
  75. explicit string(const double number)
  76. {
  77. c8 tmpbuf[32];
  78. snprintf_irr(tmpbuf, sizeof(tmpbuf), "%0.6f", number);
  79. str = tmpbuf;
  80. }
  81. //! Constructs a string from an int
  82. explicit string(int number)
  83. {
  84. str = std::to_string(number);
  85. }
  86. //! Constructs a string from an unsigned int
  87. explicit string(unsigned int number)
  88. {
  89. str = std::to_string(number);
  90. }
  91. //! Constructs a string from a long
  92. explicit string(long number)
  93. {
  94. str = std::to_string(number);
  95. }
  96. //! Constructs a string from an unsigned long
  97. explicit string(unsigned long number)
  98. {
  99. str = std::to_string(number);
  100. }
  101. //! Constructor for copying a string from a pointer with a given length
  102. template <class B>
  103. string(const B *const c, u32 length)
  104. {
  105. if (!c)
  106. return;
  107. str.resize(length);
  108. for (u32 l = 0; l < length; ++l)
  109. str[l] = (T)c[l];
  110. }
  111. //! Constructor for Unicode and ASCII strings
  112. template <class B>
  113. string(const B *const c)
  114. {
  115. *this = c;
  116. }
  117. //! Destructor
  118. ~string()
  119. {
  120. }
  121. //! Assignment operator
  122. string<T> &operator=(const string<T> &other)
  123. {
  124. if (this == &other)
  125. return *this;
  126. str = other.str;
  127. return *this;
  128. }
  129. //! Assignment operator for other string types
  130. template <class B>
  131. string<T> &operator=(const string<B> &other)
  132. {
  133. *this = other.c_str();
  134. return *this;
  135. }
  136. //! Assignment operator for strings, ASCII and Unicode
  137. template <class B>
  138. string<T> &operator=(const B *const c)
  139. {
  140. if (!c) {
  141. clear();
  142. return *this;
  143. }
  144. // no longer allowed!
  145. _IRR_DEBUG_BREAK_IF((void *)c == (void *)c_str());
  146. u32 len = calclen(c);
  147. str.resize(len);
  148. for (u32 l = 0; l < len; ++l)
  149. str[l] = (T)c[l];
  150. return *this;
  151. }
  152. //! Append operator for other strings
  153. string<T> operator+(const string<T> &other) const
  154. {
  155. string<T> tmp(*this);
  156. tmp.append(other);
  157. return tmp;
  158. }
  159. //! Append operator for strings, ASCII and Unicode
  160. template <class B>
  161. string<T> operator+(const B *const c) const
  162. {
  163. string<T> tmp(*this);
  164. tmp.append(c);
  165. return tmp;
  166. }
  167. //! Direct access operator
  168. T &operator[](const u32 index)
  169. {
  170. return str[index];
  171. }
  172. //! Direct access operator
  173. const T &operator[](const u32 index) const
  174. {
  175. return str[index];
  176. }
  177. //! Equality operator
  178. bool operator==(const T *const other) const
  179. {
  180. if (!other)
  181. return false;
  182. return !cmp(c_str(), other);
  183. }
  184. //! Equality operator
  185. bool operator==(const string<T> &other) const
  186. {
  187. return str == other.str;
  188. }
  189. //! Is smaller comparator
  190. bool operator<(const string<T> &other) const
  191. {
  192. return str < other.str;
  193. }
  194. //! Inequality operator
  195. bool operator!=(const T *const other) const
  196. {
  197. return !(*this == other);
  198. }
  199. //! Inequality operator
  200. bool operator!=(const string<T> &other) const
  201. {
  202. return !(*this == other);
  203. }
  204. //! Returns length of the string's content
  205. /** \return Length of the string's content in characters, excluding
  206. the trailing NUL. */
  207. u32 size() const
  208. {
  209. return static_cast<u32>(str.size());
  210. }
  211. //! Informs if the string is empty or not.
  212. //! \return True if the string is empty, false if not.
  213. bool empty() const
  214. {
  215. return str.empty();
  216. }
  217. void clear(bool releaseMemory = true)
  218. {
  219. if (releaseMemory) {
  220. stl_type empty;
  221. std::swap(str, empty);
  222. } else {
  223. str.clear();
  224. }
  225. }
  226. //! Returns character string
  227. /** \return pointer to C-style NUL terminated string. */
  228. const T *c_str() const
  229. {
  230. return str.c_str();
  231. }
  232. //! Makes the string lower case.
  233. string<T> &make_lower()
  234. {
  235. std::transform(str.begin(), str.end(), str.begin(), [](const T &c) {
  236. return locale_lower(c);
  237. });
  238. return *this;
  239. }
  240. //! Makes the string upper case.
  241. string<T> &make_upper()
  242. {
  243. std::transform(str.begin(), str.end(), str.begin(), [](const T &c) {
  244. return locale_upper(c);
  245. });
  246. return *this;
  247. }
  248. //! Compares the strings ignoring case.
  249. /** \param other: Other string to compare.
  250. \return True if the strings are equal ignoring case. */
  251. bool equals_ignore_case(const string<T> &other) const
  252. {
  253. const T *array = c_str();
  254. for (u32 i = 0; array[i] && other[i]; ++i)
  255. if (locale_lower(array[i]) != locale_lower(other[i]))
  256. return false;
  257. return size() == other.size();
  258. }
  259. //! Compares the strings ignoring case.
  260. /** \param other: Other string to compare.
  261. \param sourcePos: where to start to compare in the string
  262. \return True if the strings are equal ignoring case. */
  263. bool equals_substring_ignore_case(const string<T> &other, const u32 sourcePos = 0) const
  264. {
  265. if (sourcePos >= size() + 1)
  266. return false;
  267. const T *array = c_str();
  268. u32 i;
  269. for (i = 0; array[sourcePos + i] && other[i]; ++i)
  270. if (locale_lower(array[sourcePos + i]) != locale_lower(other[i]))
  271. return false;
  272. return array[sourcePos + i] == 0 && other[i] == 0;
  273. }
  274. //! Compares the strings ignoring case.
  275. /** \param other: Other string to compare.
  276. \return True if this string is smaller ignoring case. */
  277. bool lower_ignore_case(const string<T> &other) const
  278. {
  279. const T *array = c_str();
  280. for (u32 i = 0; array[i] && other[i]; ++i) {
  281. s32 diff = (s32)locale_lower(array[i]) - (s32)locale_lower(other[i]);
  282. if (diff)
  283. return diff < 0;
  284. }
  285. return size() < other.size();
  286. }
  287. //! compares the first n characters of the strings
  288. /** \param other Other string to compare.
  289. \param n Number of characters to compare
  290. \return True if the n first characters of both strings are equal. */
  291. bool equalsn(const string<T> &other, u32 n) const
  292. {
  293. const T *array = c_str();
  294. u32 i;
  295. for (i = 0; i < n && array[i] && other[i]; ++i)
  296. if (array[i] != other[i])
  297. return false;
  298. // if one (or both) of the strings was smaller then they
  299. // are only equal if they have the same length
  300. return (i == n) || (size() == other.size());
  301. }
  302. //! compares the first n characters of the strings
  303. /** \param str Other string to compare.
  304. \param n Number of characters to compare
  305. \return True if the n first characters of both strings are equal. */
  306. bool equalsn(const T *const other, u32 n) const
  307. {
  308. if (!other)
  309. return false;
  310. const T *array = c_str();
  311. u32 i;
  312. for (i = 0; i < n && array[i] && other[i]; ++i)
  313. if (array[i] != other[i])
  314. return false;
  315. // if one (or both) of the strings was smaller then they
  316. // are only equal if they have the same length
  317. return (i == n) || (array[i] == 0 && other[i] == 0);
  318. }
  319. //! Appends a character to this string
  320. /** \param character: Character to append. */
  321. string<T> &append(T character)
  322. {
  323. str.append(1, character);
  324. return *this;
  325. }
  326. //! Appends a char string to this string
  327. /** \param other: Char string to append. */
  328. /** \param length: The length of the string to append. */
  329. string<T> &append(const T *const other, u32 length = 0xffffffff)
  330. {
  331. if (!other)
  332. return *this;
  333. u32 len = calclen(other);
  334. if (len > length)
  335. len = length;
  336. str.append(other, len);
  337. return *this;
  338. }
  339. //! Appends a string to this string
  340. /** \param other: String to append. */
  341. string<T> &append(const string<T> &other)
  342. {
  343. str.append(other.str);
  344. return *this;
  345. }
  346. //! Appends a string of the length l to this string.
  347. /** \param other: other String to append to this string.
  348. \param length: How much characters of the other string to add to this one. */
  349. string<T> &append(const string<T> &other, u32 length)
  350. {
  351. if (other.size() < length)
  352. append(other);
  353. else
  354. str.append(other.c_str(), length);
  355. return *this;
  356. }
  357. //! Insert a certain amount of characters into the string before the given index
  358. //\param pos Insert the characters before this index
  359. //\param s String to insert. Must be at least of size n
  360. //\param n Number of characters from string s to use.
  361. string<T> &insert(u32 pos, const T *s, u32 n)
  362. {
  363. if (pos < size() + 1) {
  364. str.insert(pos, s, n);
  365. }
  366. return *this;
  367. }
  368. //! Reserves some memory.
  369. /** \param count: Amount of characters to reserve, including
  370. the trailing NUL. */
  371. void reserve(u32 count)
  372. {
  373. if (count == 0)
  374. return;
  375. str.reserve(count - 1);
  376. }
  377. //! finds first occurrence of character in string
  378. /** \param c: Character to search for.
  379. \return Position where the character has been found,
  380. or -1 if not found. */
  381. s32 findFirst(T c) const
  382. {
  383. auto r = str.find(c);
  384. return pos_from_stl(r);
  385. }
  386. //! finds first occurrence of a character of a list in string
  387. /** \param c: List of characters to find. For example if the method
  388. should find the first occurrence of 'a' or 'b', this parameter should be "ab".
  389. \param count: Amount of characters in the list. Usually,
  390. this should be strlen(c)
  391. \return Position where one of the characters has been found,
  392. or -1 if not found. */
  393. s32 findFirstChar(const T *const c, u32 count = 1) const
  394. {
  395. if (!c || !count)
  396. return -1;
  397. auto r = str.find_first_of(c, 0, count);
  398. return pos_from_stl(r);
  399. }
  400. //! Finds first position of a character not in a given list.
  401. /** \param c: List of characters not to find. For example if the method
  402. should find the first occurrence of a character not 'a' or 'b', this parameter should be "ab".
  403. \param count: Amount of characters in the list. Usually,
  404. this should be strlen(c)
  405. \return Position where the character has been found,
  406. or -1 if not found. */
  407. s32 findFirstCharNotInList(const T *const c, u32 count = 1) const
  408. {
  409. if (!c || !count)
  410. return -1;
  411. auto r = str.find_first_not_of(c, 0, count);
  412. return pos_from_stl(r);
  413. }
  414. //! Finds last position of a character not in a given list.
  415. /** \param c: List of characters not to find. For example if the method
  416. should find the first occurrence of a character not 'a' or 'b', this parameter should be "ab".
  417. \param count: Amount of characters in the list. Usually,
  418. this should be strlen(c)
  419. \return Position where the character has been found,
  420. or -1 if not found. */
  421. s32 findLastCharNotInList(const T *const c, u32 count = 1) const
  422. {
  423. if (!c || !count)
  424. return -1;
  425. auto r = str.find_last_not_of(c, npos, count);
  426. return pos_from_stl(r);
  427. }
  428. //! finds next occurrence of character in string
  429. /** \param c: Character to search for.
  430. \param startPos: Position in string to start searching.
  431. \return Position where the character has been found,
  432. or -1 if not found. */
  433. s32 findNext(T c, u32 startPos) const
  434. {
  435. auto r = str.find(c, startPos);
  436. return pos_from_stl(r);
  437. }
  438. //! finds last occurrence of character in string
  439. /** \param c: Character to search for.
  440. \param start: start to search reverse ( default = -1, on end )
  441. \return Position where the character has been found,
  442. or -1 if not found. */
  443. s32 findLast(T c, s32 start = -1) const
  444. {
  445. auto r = str.rfind(c, pos_to_stl(start));
  446. return pos_from_stl(r);
  447. }
  448. //! finds last occurrence of a character of a list in string
  449. /** \param c: List of strings to find. For example if the method
  450. should find the last occurrence of 'a' or 'b', this parameter should be "ab".
  451. \param count: Amount of characters in the list. Usually,
  452. this should be strlen(c)
  453. \return Position where one of the characters has been found,
  454. or -1 if not found. */
  455. s32 findLastChar(const T *const c, u32 count = 1) const
  456. {
  457. if (!c || !count)
  458. return -1;
  459. auto r = str.find_last_of(c, npos, count);
  460. return pos_from_stl(r);
  461. }
  462. //! finds another string in this string
  463. /** \param str: Another string
  464. \param start: Start position of the search
  465. \return Positions where the string has been found,
  466. or -1 if not found. */
  467. s32 find(const T *const other, const u32 start = 0) const
  468. {
  469. if (other && *other) {
  470. auto r = str.find(other, start);
  471. return pos_from_stl(r);
  472. }
  473. return -1;
  474. }
  475. //! Returns a substring
  476. /** \param begin Start of substring.
  477. \param length Length of substring.
  478. \param make_lower copy only lower case */
  479. string<T> subString(u32 begin, s32 length, bool make_lower = false) const
  480. {
  481. // if start after string
  482. // or no proper substring length
  483. if ((length <= 0) || (begin >= size()))
  484. return string<T>("");
  485. string<T> o = str.substr(begin, length);
  486. if (make_lower)
  487. o.make_lower();
  488. return o;
  489. }
  490. //! Appends a character to this string
  491. /** \param c Character to append. */
  492. string<T> &operator+=(T c)
  493. {
  494. append(c);
  495. return *this;
  496. }
  497. //! Appends a char string to this string
  498. /** \param c Char string to append. */
  499. string<T> &operator+=(const T *const c)
  500. {
  501. append(c);
  502. return *this;
  503. }
  504. //! Appends a string to this string
  505. /** \param other String to append. */
  506. string<T> &operator+=(const string<T> &other)
  507. {
  508. append(other);
  509. return *this;
  510. }
  511. //! Appends a string representation of a number to this string
  512. /** \param i Number to append. */
  513. string<T> &operator+=(const int i)
  514. {
  515. append(string<T>(i));
  516. return *this;
  517. }
  518. //! Appends a string representation of a number to this string
  519. /** \param i Number to append. */
  520. string<T> &operator+=(const unsigned int i)
  521. {
  522. append(string<T>(i));
  523. return *this;
  524. }
  525. //! Appends a string representation of a number to this string
  526. /** \param i Number to append. */
  527. string<T> &operator+=(const long i)
  528. {
  529. append(string<T>(i));
  530. return *this;
  531. }
  532. //! Appends a string representation of a number to this string
  533. /** \param i Number to append. */
  534. string<T> &operator+=(const unsigned long i)
  535. {
  536. append(string<T>(i));
  537. return *this;
  538. }
  539. //! Appends a string representation of a number to this string
  540. /** \param i Number to append. */
  541. string<T> &operator+=(const double i)
  542. {
  543. append(string<T>(i));
  544. return *this;
  545. }
  546. //! Appends a string representation of a number to this string
  547. /** \param i Number to append. */
  548. string<T> &operator+=(const float i)
  549. {
  550. append(string<T>(i));
  551. return *this;
  552. }
  553. //! Replaces all characters of a special type with another one
  554. /** \param toReplace Character to replace.
  555. \param replaceWith Character replacing the old one. */
  556. string<T> &replace(T toReplace, T replaceWith)
  557. {
  558. std::replace(str.begin(), str.end(), toReplace, replaceWith);
  559. return *this;
  560. }
  561. //! Replaces all instances of a string with another one.
  562. /** \param toReplace The string to replace.
  563. \param replaceWith The string replacing the old one. */
  564. string<T> &replace(const string<T> &toReplace, const string<T> &replaceWith)
  565. {
  566. size_type pos = 0;
  567. while ((pos = str.find(toReplace.str, pos)) != npos) {
  568. str.replace(pos, toReplace.size(), replaceWith.str);
  569. pos += replaceWith.size();
  570. }
  571. return *this;
  572. }
  573. //! Removes a character from a string.
  574. /** \param c: Character to remove. */
  575. string<T> &remove(T c)
  576. {
  577. str.erase(std::remove(str.begin(), str.end(), c), str.end());
  578. return *this;
  579. }
  580. //! Removes a string from the string.
  581. /** \param toRemove: String to remove. */
  582. string<T> &remove(const string<T> &toRemove)
  583. {
  584. u32 size = toRemove.size();
  585. if (size == 0)
  586. return *this;
  587. u32 pos = 0;
  588. u32 found = 0;
  589. for (u32 i = 0; i < str.size(); ++i) {
  590. u32 j = 0;
  591. while (j < size) {
  592. if (str[i + j] != toRemove[j])
  593. break;
  594. ++j;
  595. }
  596. if (j == size) {
  597. found += size;
  598. i += size - 1;
  599. continue;
  600. }
  601. str[pos++] = str[i];
  602. }
  603. str.resize(str.size() - found);
  604. return *this;
  605. }
  606. //! Removes characters from a string.
  607. /** \param characters: Characters to remove. */
  608. string<T> &removeChars(const string<T> &characters)
  609. {
  610. if (characters.size() == 0)
  611. return *this;
  612. for (u32 i = 0; i < characters.size(); i++)
  613. remove(characters[i]);
  614. return *this;
  615. }
  616. //! Trims the string.
  617. /** Removes the specified characters (by default, Latin-1 whitespace)
  618. from the beginning and the end of the string. */
  619. string<T> &trim(const string<T> &whitespace = " \t\n\r")
  620. {
  621. // find start and end of the substring without the specified characters
  622. const s32 begin = findFirstCharNotInList(whitespace.c_str(), whitespace.size());
  623. if (begin == -1)
  624. return (*this = "");
  625. const s32 end = findLastCharNotInList(whitespace.c_str(), whitespace.size());
  626. return (*this = subString(begin, (end + 1) - begin));
  627. }
  628. //! Erases a character from the string.
  629. /** May be slow, because all elements
  630. following after the erased element have to be copied.
  631. \param index: Index of element to be erased. */
  632. string<T> &erase(u32 index)
  633. {
  634. str.erase(str.begin() + index);
  635. return *this;
  636. }
  637. //! verify the existing string.
  638. string<T> &validate()
  639. {
  640. // truncate to existing null
  641. u32 len = calclen(c_str());
  642. if (len != size())
  643. str.resize(len);
  644. return *this;
  645. }
  646. //! gets the last char of a string or null
  647. T lastChar() const
  648. {
  649. return !str.empty() ? str.back() : 0;
  650. }
  651. //! Split string into parts (tokens).
  652. /** This method will split a string at certain delimiter characters
  653. into the container passed in as reference. The type of the container
  654. has to be given as template parameter. It must provide a push_back and
  655. a size method.
  656. \param ret The result container. Tokens are added, the container is not cleared.
  657. \param delimiter C-style string of delimiter characters
  658. \param countDelimiters Number of delimiter characters
  659. \param ignoreEmptyTokens Flag to avoid empty substrings in the result
  660. container. If two delimiters occur without a character in between or an
  661. empty substring would be placed in the result. Or if a delimiter is the last
  662. character an empty substring would be added at the end. If this flag is set,
  663. only non-empty strings are stored.
  664. \param keepSeparators Flag which allows to add the separator to the
  665. result string. If this flag is true, the concatenation of the
  666. substrings results in the original string. Otherwise, only the
  667. characters between the delimiters are returned.
  668. \return The number of resulting substrings
  669. */
  670. template <class container>
  671. u32 split(container &ret, const T *const delimiter, u32 countDelimiters = 1, bool ignoreEmptyTokens = true, bool keepSeparators = false) const
  672. {
  673. if (!delimiter)
  674. return 0;
  675. const u32 oldSize = static_cast<u32>(ret.size());
  676. u32 tokenStartIdx = 0;
  677. for (u32 i = 0; i < size() + 1; ++i) {
  678. for (u32 j = 0; j < countDelimiters; ++j) {
  679. if (str[i] == delimiter[j]) {
  680. if (i - tokenStartIdx > 0)
  681. ret.push_back(string<T>(&str[tokenStartIdx], i - tokenStartIdx));
  682. else if (!ignoreEmptyTokens)
  683. ret.push_back(string<T>());
  684. if (keepSeparators) {
  685. ret.push_back(string<T>(&str[i], 1));
  686. }
  687. tokenStartIdx = i + 1;
  688. break;
  689. }
  690. }
  691. }
  692. if (size() > tokenStartIdx)
  693. ret.push_back(string<T>(&str[tokenStartIdx], size() - tokenStartIdx));
  694. else if (!ignoreEmptyTokens)
  695. ret.push_back(string<T>());
  696. return static_cast<u32>(ret.size() - oldSize);
  697. }
  698. // This function should not be used and is only kept for "CGUIFileOpenDialog::pathToStringW".
  699. friend size_t multibyteToWString(stringw &destination, const stringc &source);
  700. friend size_t utf8ToWString(stringw &destination, const char *source);
  701. friend size_t wStringToUTF8(stringc &destination, const wchar_t *source);
  702. private:
  703. //! strlen wrapper
  704. template <typename U>
  705. static inline u32 calclen(const U *p)
  706. {
  707. u32 len = 0;
  708. while (*p++)
  709. len++;
  710. return len;
  711. }
  712. static inline u32 calclen(const char *p)
  713. {
  714. return static_cast<u32>(strlen(p));
  715. }
  716. static inline u32 calclen(const wchar_t *p)
  717. {
  718. return static_cast<u32>(wcslen(p));
  719. }
  720. //! strcmp wrapper
  721. template <typename U>
  722. static inline int cmp(const U *p, const U *p2)
  723. {
  724. while (*p && *p == *p2)
  725. p++, p2++;
  726. return (int)*p - (int)*p2;
  727. }
  728. static inline int cmp(const char *p, const char *p2)
  729. {
  730. return strcmp(p, p2);
  731. }
  732. static inline int cmp(const wchar_t *p, const wchar_t *p2)
  733. {
  734. return wcscmp(p, p2);
  735. }
  736. typedef typename stl_type::size_type size_type;
  737. static const size_type npos = stl_type::npos;
  738. static inline s32 pos_from_stl(size_type pos)
  739. {
  740. return pos == npos ? -1 : (s32)pos;
  741. }
  742. static inline size_type pos_to_stl(s32 pos)
  743. {
  744. return pos == -1 ? npos : (size_type)pos;
  745. }
  746. stl_type str;
  747. };
  748. //! Convert multibyte string to wide-character string
  749. /** Wrapper around mbstowcs from standard library, but directly using Irrlicht string class.
  750. What the function does exactly depends on the LC_CTYPE of the current c locale.
  751. \param destination Wide-character string receiving the converted source
  752. \param source multibyte string
  753. \return The number of wide characters written to destination, not including the eventual terminating null character or -1 when conversion failed
  754. This function should not be used and is only kept for "CGUIFileOpenDialog::pathToStringW". */
  755. inline size_t multibyteToWString(stringw &destination, const core::stringc &source)
  756. {
  757. u32 sourceSize = source.size();
  758. if (sourceSize) {
  759. destination.str.resize(sourceSize + 1);
  760. #if defined(_MSC_VER)
  761. #pragma warning(push)
  762. #pragma warning(disable : 4996) // 'mbstowcs': This function or variable may be unsafe. Consider using mbstowcs_s instead.
  763. #endif
  764. const size_t written = mbstowcs(&destination[0], source.c_str(), (size_t)sourceSize);
  765. #if defined(_MSC_VER)
  766. #pragma warning(pop)
  767. #endif
  768. if (written != (size_t)-1) {
  769. destination.str.resize(written);
  770. } else {
  771. // Likely character which got converted until the invalid character was encountered are in destination now.
  772. // And it seems even 0-terminated, but I found no documentation anywhere that this (the 0-termination) is guaranteed :-(
  773. destination.clear();
  774. }
  775. return written;
  776. } else {
  777. destination.clear();
  778. return 0;
  779. }
  780. }
  781. inline size_t utf8ToWString(stringw &destination, const char *source)
  782. {
  783. destination = utf8_to_wide(source);
  784. return destination.size();
  785. }
  786. inline size_t utf8ToWString(stringw &destination, const stringc &source)
  787. {
  788. return utf8ToWString(destination, source.c_str());
  789. }
  790. inline size_t wStringToUTF8(stringc &destination, const wchar_t *source)
  791. {
  792. destination = wide_to_utf8(source);
  793. return destination.size();
  794. }
  795. inline size_t wStringToUTF8(stringc &destination, const stringw &source)
  796. {
  797. return wStringToUTF8(destination, source.c_str());
  798. }
  799. } // end namespace core
  800. } // end namespace irr