irrString.h 24 KB

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