irrString.h 24 KB

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