irrString.h 23 KB

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