irrString.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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. #ifndef IRR_STRING_H_INCLUDED
  5. #define IRR_STRING_H_INCLUDED
  6. #include "irrTypes.h"
  7. #include "irrAllocator.h"
  8. #include "irrMath.h"
  9. #include <string.h>
  10. namespace irr
  11. {
  12. namespace core
  13. {
  14. //! Very simple string class with some useful features.
  15. /** string<c8> and string<wchar_t> both accept Unicode AND ASCII/Latin-1,
  16. so you can assign Unicode to string<c8> and ASCII/Latin-1 to string<wchar_t>
  17. (and the other way round) if you want to.
  18. However, note that the conversation between both is not done using any encoding.
  19. This means that c8 strings are treated as ASCII/Latin-1, not UTF-8, and
  20. are simply expanded to the equivalent wchar_t, while Unicode/wchar_t
  21. characters are truncated to 8-bit ASCII/Latin-1 characters, discarding all
  22. other information in the wchar_t.
  23. Helper functions for converting between UTF-8 and wchar_t are provided
  24. outside the string class for explicit use.
  25. */
  26. // forward declarations
  27. template <typename T, typename TAlloc = irrAllocator<T> >
  28. class string;
  29. static size_t multibyteToWString(string<wchar_t>& destination, const char* source, u32 sourceSize);
  30. inline s32 isdigit(s32 c);
  31. enum eLocaleID
  32. {
  33. IRR_LOCALE_ANSI = 0,
  34. IRR_LOCALE_GERMAN = 1
  35. };
  36. static eLocaleID locale_current = IRR_LOCALE_ANSI;
  37. static inline void locale_set ( eLocaleID id )
  38. {
  39. locale_current = id;
  40. }
  41. //! Returns a character converted to lower case
  42. static inline u32 locale_lower ( u32 x )
  43. {
  44. switch ( locale_current )
  45. {
  46. case IRR_LOCALE_GERMAN:
  47. case IRR_LOCALE_ANSI:
  48. break;
  49. }
  50. // ansi
  51. return x >= 'A' && x <= 'Z' ? x + 0x20 : x;
  52. }
  53. //! Returns a character converted to upper case
  54. static inline u32 locale_upper ( u32 x )
  55. {
  56. switch ( locale_current )
  57. {
  58. case IRR_LOCALE_GERMAN:
  59. case IRR_LOCALE_ANSI:
  60. break;
  61. }
  62. // ansi
  63. return x >= 'a' && x <= 'z' ? x + ( 'A' - 'a' ) : x;
  64. }
  65. //! Convert this utf-8-encoded string to the platform's wchar.
  66. /** The resulting string is always NULL-terminated and well-formed.
  67. \param len The size of the output buffer in bytes.
  68. */
  69. IRRLICHT_API void utf8ToWchar(const char *in, wchar_t *out, const u64 len);
  70. //! Convert this wchar string to utf-8.
  71. /** The resulting string is always NULL-terminated and well-formed.
  72. \param len The size of the output buffer in bytes.
  73. */
  74. IRRLICHT_API void wcharToUtf8(const wchar_t *in, char *out, const u64 len);
  75. template <typename T, typename TAlloc>
  76. class string
  77. {
  78. public:
  79. typedef T char_type;
  80. //! Default constructor
  81. string()
  82. : array(0), allocated(1), used(1)
  83. {
  84. array = allocator.allocate(1); // new T[1];
  85. array[0] = 0;
  86. }
  87. //! Constructor
  88. string(const string<T,TAlloc>& other)
  89. : array(0), allocated(0), used(0)
  90. {
  91. *this = other;
  92. }
  93. //! Constructor from other string types
  94. template <class B, class A>
  95. string(const string<B, A>& other)
  96. : array(0), allocated(0), used(0)
  97. {
  98. *this = other;
  99. }
  100. //! Constructs a string from a float
  101. explicit string(const double number)
  102. : array(0), allocated(0), used(0)
  103. {
  104. c8 tmpbuf[255];
  105. snprintf_irr(tmpbuf, 255, "%0.6f", number);
  106. *this = tmpbuf;
  107. }
  108. //! Constructs a string from an int
  109. explicit string(int number)
  110. : array(0), allocated(0), used(0)
  111. {
  112. // store if negative and make positive
  113. bool negative = false;
  114. if (number < 0)
  115. {
  116. number *= -1;
  117. negative = true;
  118. }
  119. // temporary buffer for 16 numbers
  120. c8 tmpbuf[16]={0};
  121. u32 idx = 15;
  122. // special case '0'
  123. if (!number)
  124. {
  125. tmpbuf[14] = '0';
  126. *this = &tmpbuf[14];
  127. return;
  128. }
  129. // add numbers
  130. while(number && idx)
  131. {
  132. --idx;
  133. tmpbuf[idx] = (c8)('0' + (number % 10));
  134. number /= 10;
  135. }
  136. // add sign
  137. if (negative)
  138. {
  139. --idx;
  140. tmpbuf[idx] = '-';
  141. }
  142. *this = &tmpbuf[idx];
  143. }
  144. //! Constructs a string from an unsigned int
  145. explicit string(unsigned int number)
  146. : array(0), allocated(0), used(0)
  147. {
  148. // temporary buffer for 16 numbers
  149. c8 tmpbuf[16]={0};
  150. u32 idx = 15;
  151. // special case '0'
  152. if (!number)
  153. {
  154. tmpbuf[14] = '0';
  155. *this = &tmpbuf[14];
  156. return;
  157. }
  158. // add numbers
  159. while(number && idx)
  160. {
  161. --idx;
  162. tmpbuf[idx] = (c8)('0' + (number % 10));
  163. number /= 10;
  164. }
  165. *this = &tmpbuf[idx];
  166. }
  167. //! Constructs a string from a long
  168. explicit string(long number)
  169. : array(0), allocated(0), used(0)
  170. {
  171. // store if negative and make positive
  172. bool negative = false;
  173. if (number < 0)
  174. {
  175. number *= -1;
  176. negative = true;
  177. }
  178. // temporary buffer for 16 numbers
  179. c8 tmpbuf[16]={0};
  180. u32 idx = 15;
  181. // special case '0'
  182. if (!number)
  183. {
  184. tmpbuf[14] = '0';
  185. *this = &tmpbuf[14];
  186. return;
  187. }
  188. // add numbers
  189. while(number && idx)
  190. {
  191. --idx;
  192. tmpbuf[idx] = (c8)('0' + (number % 10));
  193. number /= 10;
  194. }
  195. // add sign
  196. if (negative)
  197. {
  198. --idx;
  199. tmpbuf[idx] = '-';
  200. }
  201. *this = &tmpbuf[idx];
  202. }
  203. //! Constructs a string from an unsigned long
  204. explicit string(unsigned long number)
  205. : array(0), allocated(0), used(0)
  206. {
  207. // temporary buffer for 16 numbers
  208. c8 tmpbuf[16]={0};
  209. u32 idx = 15;
  210. // special case '0'
  211. if (!number)
  212. {
  213. tmpbuf[14] = '0';
  214. *this = &tmpbuf[14];
  215. return;
  216. }
  217. // add numbers
  218. while(number && idx)
  219. {
  220. --idx;
  221. tmpbuf[idx] = (c8)('0' + (number % 10));
  222. number /= 10;
  223. }
  224. *this = &tmpbuf[idx];
  225. }
  226. //! Constructor for copying a string from a pointer with a given length
  227. template <class B>
  228. string(const B* const c, u32 length)
  229. : array(0), allocated(0), used(0)
  230. {
  231. if (!c)
  232. {
  233. // correctly init the string to an empty one
  234. *this="";
  235. return;
  236. }
  237. allocated = used = length+1;
  238. array = allocator.allocate(used); // new T[used];
  239. for (u32 l = 0; l<length; ++l)
  240. array[l] = (T)c[l];
  241. array[length] = 0;
  242. }
  243. //! Constructor for Unicode and ASCII strings
  244. template <class B>
  245. string(const B* const c)
  246. : array(0), allocated(0), used(0)
  247. {
  248. *this = c;
  249. }
  250. //! Destructor
  251. ~string()
  252. {
  253. allocator.deallocate(array); // delete [] array;
  254. }
  255. //! Assignment operator
  256. string<T,TAlloc>& operator=(const string<T,TAlloc>& other)
  257. {
  258. if (this == &other)
  259. return *this;
  260. used = other.size()+1;
  261. if (used>allocated)
  262. {
  263. allocator.deallocate(array); // delete [] array;
  264. allocated = used;
  265. array = allocator.allocate(used); //new T[used];
  266. }
  267. const T* p = other.c_str();
  268. for (u32 i=0; i<used; ++i, ++p)
  269. array[i] = *p;
  270. return *this;
  271. }
  272. //! Assignment operator for other string types
  273. template <class B, class A>
  274. string<T,TAlloc>& operator=(const string<B,A>& other)
  275. {
  276. *this = other.c_str();
  277. return *this;
  278. }
  279. //! Assignment operator for strings, ASCII and Unicode
  280. template <class B>
  281. string<T,TAlloc>& operator=(const B* const c)
  282. {
  283. if (!c)
  284. {
  285. if (!array)
  286. {
  287. array = allocator.allocate(1); //new T[1];
  288. allocated = 1;
  289. }
  290. used = 1;
  291. array[0] = 0x0;
  292. return *this;
  293. }
  294. if ((void*)c == (void*)array)
  295. return *this;
  296. u32 len = 0;
  297. const B* p = c;
  298. do
  299. {
  300. ++len;
  301. } while(*p++);
  302. // we'll keep the old string for a while, because the new
  303. // string could be a part of the current string.
  304. T* oldArray = array;
  305. used = len;
  306. if (used>allocated)
  307. {
  308. allocated = used;
  309. array = allocator.allocate(used); //new T[used];
  310. }
  311. for (u32 l = 0; l<len; ++l)
  312. array[l] = (T)c[l];
  313. if (oldArray != array)
  314. allocator.deallocate(oldArray); // delete [] oldArray;
  315. return *this;
  316. }
  317. //! Append operator for other strings
  318. string<T,TAlloc> operator+(const string<T,TAlloc>& other) const
  319. {
  320. string<T,TAlloc> str(*this);
  321. str.append(other);
  322. return str;
  323. }
  324. //! Append operator for strings, ASCII and Unicode
  325. template <class B>
  326. string<T,TAlloc> operator+(const B* const c) const
  327. {
  328. string<T,TAlloc> str(*this);
  329. str.append(c);
  330. return str;
  331. }
  332. //! Direct access operator
  333. T& operator [](const u32 index)
  334. {
  335. IRR_DEBUG_BREAK_IF(index>=used) // bad index
  336. return array[index];
  337. }
  338. //! Direct access operator
  339. const T& operator [](const u32 index) const
  340. {
  341. IRR_DEBUG_BREAK_IF(index>=used) // bad index
  342. return array[index];
  343. }
  344. //! Equality operator
  345. bool operator==(const T* const str) const
  346. {
  347. if (!str)
  348. return false;
  349. u32 i;
  350. for (i=0; array[i] && str[i]; ++i)
  351. if (array[i] != str[i])
  352. return false;
  353. return (!array[i] && !str[i]);
  354. }
  355. //! Equality operator
  356. bool operator==(const string<T,TAlloc>& other) const
  357. {
  358. for (u32 i=0; array[i] && other.array[i]; ++i)
  359. if (array[i] != other.array[i])
  360. return false;
  361. return used == other.used;
  362. }
  363. //! Is smaller comparator
  364. bool operator<(const string<T,TAlloc>& other) const
  365. {
  366. for (u32 i=0; array[i] && other.array[i]; ++i)
  367. {
  368. const s32 diff = array[i] - other.array[i];
  369. if (diff)
  370. return (diff < 0);
  371. }
  372. return (used < other.used);
  373. }
  374. //! Inequality operator
  375. bool operator!=(const T* const str) const
  376. {
  377. return !(*this == str);
  378. }
  379. //! Inequality operator
  380. bool operator!=(const string<T,TAlloc>& other) const
  381. {
  382. return !(*this == other);
  383. }
  384. //! Returns length of the string's content
  385. /** \return Length of the string's content in characters, excluding
  386. the trailing NUL. */
  387. u32 size() const
  388. {
  389. return used-1;
  390. }
  391. //! Informs if the string is empty or not.
  392. //! \return True if the string is empty, false if not.
  393. bool empty() const
  394. {
  395. return (size() == 0);
  396. }
  397. void clear(bool releaseMemory=true)
  398. {
  399. if ( releaseMemory )
  400. {
  401. reallocate(1);
  402. }
  403. array[0] = 0;
  404. used = 1;
  405. }
  406. //! Returns character string
  407. /** \return pointer to C-style NUL terminated string. */
  408. const T* c_str() const
  409. {
  410. return array;
  411. }
  412. //! Makes the string lower case.
  413. string<T,TAlloc>& make_lower()
  414. {
  415. for (u32 i=0; array[i]; ++i)
  416. array[i] = locale_lower ( array[i] );
  417. return *this;
  418. }
  419. //! Makes the string upper case.
  420. string<T,TAlloc>& make_upper()
  421. {
  422. for (u32 i=0; array[i]; ++i)
  423. array[i] = locale_upper ( array[i] );
  424. return *this;
  425. }
  426. //! Compares the strings ignoring case.
  427. /** \param other: Other string to compare.
  428. \return True if the strings are equal ignoring case. */
  429. bool equals_ignore_case(const string<T,TAlloc>& other) const
  430. {
  431. for(u32 i=0; array[i] && other[i]; ++i)
  432. if (locale_lower( array[i]) != locale_lower(other[i]))
  433. return false;
  434. return used == other.used;
  435. }
  436. //! Compares the strings ignoring case.
  437. /** \param other: Other string to compare.
  438. \param sourcePos: where to start to compare in the string
  439. \return True if the strings are equal ignoring case. */
  440. bool equals_substring_ignore_case(const string<T,TAlloc>&other, const s32 sourcePos = 0 ) const
  441. {
  442. if ( (u32) sourcePos >= used )
  443. return false;
  444. u32 i;
  445. for( i=0; array[sourcePos + i] && other[i]; ++i)
  446. if (locale_lower( array[sourcePos + i]) != locale_lower(other[i]))
  447. return false;
  448. return array[sourcePos + i] == 0 && other[i] == 0;
  449. }
  450. //! Compares the strings ignoring case.
  451. /** \param other: Other string to compare.
  452. \return True if this string is smaller ignoring case. */
  453. bool lower_ignore_case(const string<T,TAlloc>& other) const
  454. {
  455. for(u32 i=0; array[i] && other.array[i]; ++i)
  456. {
  457. s32 diff = (s32) locale_lower ( array[i] ) - (s32) locale_lower ( other.array[i] );
  458. if ( diff )
  459. return diff < 0;
  460. }
  461. return used < other.used;
  462. }
  463. //! compares the first n characters of the strings
  464. /** \param other Other string to compare.
  465. \param n Number of characters to compare
  466. \return True if the n first characters of both strings are equal. */
  467. bool equalsn(const string<T,TAlloc>& other, u32 n) const
  468. {
  469. u32 i;
  470. for(i=0; i < n && array[i] && other[i]; ++i)
  471. if (array[i] != other[i])
  472. return false;
  473. // if one (or both) of the strings was smaller then they
  474. // are only equal if they have the same length
  475. return (i == n) || (used == other.used);
  476. }
  477. //! compares the first n characters of the strings
  478. /** \param str Other string to compare.
  479. \param n Number of characters to compare
  480. \return True if the n first characters of both strings are equal. */
  481. bool equalsn(const T* const str, u32 n) const
  482. {
  483. if (!str)
  484. return false;
  485. u32 i;
  486. for(i=0; i < n && array[i] && str[i]; ++i)
  487. if (array[i] != str[i])
  488. return false;
  489. // if one (or both) of the strings was smaller then they
  490. // are only equal if they have the same length
  491. return (i == n) || (array[i] == 0 && str[i] == 0);
  492. }
  493. //! Appends a character to this string
  494. /** \param character: Character to append. */
  495. string<T,TAlloc>& append(T character)
  496. {
  497. if (used + 1 > allocated)
  498. reallocate(used + 1);
  499. ++used;
  500. array[used-2] = character;
  501. array[used-1] = 0;
  502. return *this;
  503. }
  504. //! Appends a char string to this string
  505. /** \param other: Char string to append. */
  506. /** \param length: The length of the string to append. */
  507. string<T,TAlloc>& append(const T* const other, u32 length=0xffffffff)
  508. {
  509. if (!other)
  510. return *this;
  511. u32 len = 0;
  512. const T* p = other;
  513. while(*p)
  514. {
  515. ++len;
  516. ++p;
  517. }
  518. if (len > length)
  519. len = length;
  520. if (used + len > allocated)
  521. reallocate(used + len);
  522. --used;
  523. ++len;
  524. for (u32 l=0; l<len; ++l)
  525. array[l+used] = *(other+l);
  526. used += len;
  527. return *this;
  528. }
  529. //! Appends a string to this string
  530. /** \param other: String to append. */
  531. string<T,TAlloc>& append(const string<T,TAlloc>& other)
  532. {
  533. if (other.size() == 0)
  534. return *this;
  535. --used;
  536. const u32 len = other.size()+1;
  537. if (used + len > allocated)
  538. reallocate(used + len);
  539. for (u32 l=0; l<len; ++l)
  540. array[used+l] = other[l];
  541. used += len;
  542. return *this;
  543. }
  544. //! Appends a string of the length l to this string.
  545. /** \param other: other String to append to this string.
  546. \param length: How much characters of the other string to add to this one. */
  547. string<T,TAlloc>& append(const string<T,TAlloc>& other, u32 length)
  548. {
  549. if (other.size() == 0)
  550. return *this;
  551. if (other.size() < length)
  552. {
  553. append(other);
  554. return *this;
  555. }
  556. if (used + length > allocated)
  557. reallocate(used + length);
  558. --used;
  559. for (u32 l=0; l<length; ++l)
  560. array[l+used] = other[l];
  561. used += length;
  562. // ensure proper termination
  563. array[used]=0;
  564. ++used;
  565. return *this;
  566. }
  567. //! Insert a certain amount of characters into the string before the given index
  568. //\param pos Insert the characters before this index
  569. //\param s String to insert. Must be at least of size n
  570. //\param n Number of characters from string s to use.
  571. string<T,TAlloc>& insert(u32 pos, const char* s, u32 n)
  572. {
  573. if ( pos < used )
  574. {
  575. reserve(used+n);
  576. // move stuff behind insert point
  577. const u32 end = used+n-1;
  578. for (u32 i=0; i<used-pos; ++i)
  579. {
  580. array[end-i] = array[end-(i+n)];
  581. }
  582. used += n;
  583. for (u32 i=0; i<n; ++i)
  584. {
  585. array[pos+i] = s[i];
  586. }
  587. }
  588. return *this;
  589. }
  590. //! Reserves some memory.
  591. /** \param count: Amount of characters to reserve. */
  592. void reserve(u32 count)
  593. {
  594. if (count < allocated)
  595. return;
  596. reallocate(count);
  597. }
  598. //! finds first occurrence of character in string
  599. /** \param c: Character to search for.
  600. \return Position where the character has been found,
  601. or -1 if not found. */
  602. s32 findFirst(T c) const
  603. {
  604. for (u32 i=0; i<used-1; ++i)
  605. if (array[i] == c)
  606. return i;
  607. return -1;
  608. }
  609. //! finds first occurrence of a character of a list in string
  610. /** \param c: List of characters to find. For example if the method
  611. should find the first occurrence of 'a' or 'b', this parameter should be "ab".
  612. \param count: Amount of characters in the list. Usually,
  613. this should be strlen(c)
  614. \return Position where one of the characters has been found,
  615. or -1 if not found. */
  616. s32 findFirstChar(const T* const c, u32 count=1) const
  617. {
  618. if (!c || !count)
  619. return -1;
  620. for (u32 i=0; i<used-1; ++i)
  621. for (u32 j=0; j<count; ++j)
  622. if (array[i] == c[j])
  623. return i;
  624. return -1;
  625. }
  626. //! Finds first position of a character not in a given list.
  627. /** \param c: List of characters not to find. For example if the method
  628. should find the first occurrence of a character not 'a' or 'b', this parameter should be "ab".
  629. \param count: Amount of characters in the list. Usually,
  630. this should be strlen(c)
  631. \return Position where the character has been found,
  632. or -1 if not found. */
  633. template <class B>
  634. s32 findFirstCharNotInList(const B* const c, u32 count=1) const
  635. {
  636. if (!c || !count)
  637. return -1;
  638. for (u32 i=0; i<used-1; ++i)
  639. {
  640. u32 j;
  641. for (j=0; j<count; ++j)
  642. if (array[i] == c[j])
  643. break;
  644. if (j==count)
  645. return i;
  646. }
  647. return -1;
  648. }
  649. //! Finds last position of a character not in a given list.
  650. /** \param c: List of characters not to find. For example if the method
  651. should find the first occurrence of a character not 'a' or 'b', this parameter should be "ab".
  652. \param count: Amount of characters in the list. Usually,
  653. this should be strlen(c)
  654. \return Position where the character has been found,
  655. or -1 if not found. */
  656. template <class B>
  657. s32 findLastCharNotInList(const B* const c, u32 count=1) const
  658. {
  659. if (!c || !count)
  660. return -1;
  661. for (s32 i=(s32)(used-2); i>=0; --i)
  662. {
  663. u32 j;
  664. for (j=0; j<count; ++j)
  665. if (array[i] == c[j])
  666. break;
  667. if (j==count)
  668. return i;
  669. }
  670. return -1;
  671. }
  672. //! finds next occurrence of character in string
  673. /** \param c: Character to search for.
  674. \param startPos: Position in string to start searching.
  675. \return Position where the character has been found,
  676. or -1 if not found. */
  677. s32 findNext(T c, u32 startPos) const
  678. {
  679. for (u32 i=startPos; i<used-1; ++i)
  680. if (array[i] == c)
  681. return i;
  682. return -1;
  683. }
  684. //! finds last occurrence of character in string
  685. /** \param c: Character to search for.
  686. \param start: start to search reverse ( default = -1, on end )
  687. \return Position where the character has been found,
  688. or -1 if not found. */
  689. s32 findLast(T c, s32 start = -1) const
  690. {
  691. start = core::clamp ( start < 0 ? (s32)(used) - 2 : start, 0, (s32)(used) - 2 );
  692. for (s32 i=start; i>=0; --i)
  693. if (array[i] == c)
  694. return i;
  695. return -1;
  696. }
  697. //! finds last occurrence of a character of a list in string
  698. /** \param c: List of strings to find. For example if the method
  699. should find the last occurrence of 'a' or 'b', this parameter should be "ab".
  700. \param count: Amount of characters in the list. Usually,
  701. this should be strlen(c)
  702. \return Position where one of the characters has been found,
  703. or -1 if not found. */
  704. s32 findLastChar(const T* const c, u32 count=1) const
  705. {
  706. if (!c || !count)
  707. return -1;
  708. for (s32 i=(s32)used-2; i>=0; --i)
  709. for (u32 j=0; j<count; ++j)
  710. if (array[i] == c[j])
  711. return i;
  712. return -1;
  713. }
  714. //! finds another string in this string
  715. /** \param str: Another string
  716. \param start: Start position of the search
  717. \return Positions where the string has been found,
  718. or -1 if not found. */
  719. template <class B>
  720. s32 find(const B* const str, const u32 start = 0) const
  721. {
  722. if (str && *str)
  723. {
  724. u32 len = 0;
  725. while (str[len])
  726. ++len;
  727. if (len > used-1)
  728. return -1;
  729. for (u32 i=start; i<used-len; ++i)
  730. {
  731. u32 j=0;
  732. while(str[j] && array[i+j] == str[j])
  733. ++j;
  734. if (!str[j])
  735. return i;
  736. }
  737. }
  738. return -1;
  739. }
  740. //! Returns a substring
  741. /** \param begin Start of substring.
  742. \param length Length of substring.
  743. \param make_lower copy only lower case */
  744. string<T> subString(u32 begin, s32 length, bool make_lower = false ) const
  745. {
  746. // clamp length to maximal value
  747. if ((length+begin) > size())
  748. length = size()-begin;
  749. // if start after string
  750. // or no proper substring length
  751. if ((length <= 0) || (begin>=size()))
  752. return string<T>("");
  753. string<T> o;
  754. o.reserve(length+1);
  755. if ( !make_lower )
  756. {
  757. for (s32 i=0; i<length; ++i)
  758. o.array[i] = array[i+begin];
  759. }
  760. else
  761. {
  762. for (s32 i=0; i<length; ++i)
  763. o.array[i] = locale_lower ( array[i+begin] );
  764. }
  765. o.array[length] = 0;
  766. o.used = length + 1;
  767. return o;
  768. }
  769. //! Appends a character to this string
  770. /** \param c Character to append. */
  771. string<T,TAlloc>& operator += (T c)
  772. {
  773. append(c);
  774. return *this;
  775. }
  776. //! Appends a char string to this string
  777. /** \param c Char string to append. */
  778. string<T,TAlloc>& operator += (const T* const c)
  779. {
  780. append(c);
  781. return *this;
  782. }
  783. //! Appends a string to this string
  784. /** \param other String to append. */
  785. string<T,TAlloc>& operator += (const string<T,TAlloc>& other)
  786. {
  787. append(other);
  788. return *this;
  789. }
  790. //! Appends a string representation of a number to this string
  791. /** \param i Number to append. */
  792. string<T,TAlloc>& operator += (const int i)
  793. {
  794. append(string<T,TAlloc>(i));
  795. return *this;
  796. }
  797. //! Appends a string representation of a number to this string
  798. /** \param i Number to append. */
  799. string<T,TAlloc>& operator += (const unsigned int i)
  800. {
  801. append(string<T,TAlloc>(i));
  802. return *this;
  803. }
  804. //! Appends a string representation of a number to this string
  805. /** \param i Number to append. */
  806. string<T,TAlloc>& operator += (const long i)
  807. {
  808. append(string<T,TAlloc>(i));
  809. return *this;
  810. }
  811. //! Appends a string representation of a number to this string
  812. /** \param i Number to append. */
  813. string<T,TAlloc>& operator += (const unsigned long i)
  814. {
  815. append(string<T,TAlloc>(i));
  816. return *this;
  817. }
  818. //! Appends a string representation of a number to this string
  819. /** \param i Number to append. */
  820. string<T,TAlloc>& operator += (const double i)
  821. {
  822. append(string<T,TAlloc>(i));
  823. return *this;
  824. }
  825. //! Appends a string representation of a number to this string
  826. /** \param i Number to append. */
  827. string<T,TAlloc>& operator += (const float i)
  828. {
  829. append(string<T,TAlloc>(i));
  830. return *this;
  831. }
  832. //! Replaces all characters of a special type with another one
  833. /** \param toReplace Character to replace.
  834. \param replaceWith Character replacing the old one. */
  835. string<T,TAlloc>& replace(T toReplace, T replaceWith)
  836. {
  837. for (u32 i=0; i<used-1; ++i)
  838. if (array[i] == toReplace)
  839. array[i] = replaceWith;
  840. return *this;
  841. }
  842. //! Replaces all instances of a string with another one.
  843. /** \param toReplace The string to replace.
  844. \param replaceWith The string replacing the old one. */
  845. string<T,TAlloc>& replace(const string<T,TAlloc>& toReplace, const string<T,TAlloc>& replaceWith)
  846. {
  847. if (toReplace.size() == 0)
  848. return *this;
  849. const T* other = toReplace.c_str();
  850. const T* replace = replaceWith.c_str();
  851. const u32 other_size = toReplace.size();
  852. const u32 replace_size = replaceWith.size();
  853. // Determine the delta. The algorithm will change depending on the delta.
  854. s32 delta = replace_size - other_size;
  855. // A character for character replace. The string will not shrink or grow.
  856. if (delta == 0)
  857. {
  858. s32 pos = 0;
  859. while ((pos = find(other, pos)) != -1)
  860. {
  861. for (u32 i = 0; i < replace_size; ++i)
  862. array[pos + i] = replace[i];
  863. ++pos;
  864. }
  865. return *this;
  866. }
  867. // We are going to be removing some characters. The string will shrink.
  868. if (delta < 0)
  869. {
  870. u32 i = 0;
  871. for (u32 pos = 0; pos < used; ++i, ++pos)
  872. {
  873. // Is this potentially a match?
  874. if (array[pos] == *other)
  875. {
  876. // Check to see if we have a match.
  877. u32 j;
  878. for (j = 0; j < other_size; ++j)
  879. {
  880. if (array[pos + j] != other[j])
  881. break;
  882. }
  883. // If we have a match, replace characters.
  884. if (j == other_size)
  885. {
  886. for (j = 0; j < replace_size; ++j)
  887. array[i + j] = replace[j];
  888. i += replace_size - 1;
  889. pos += other_size - 1;
  890. continue;
  891. }
  892. }
  893. // No match found, just copy characters.
  894. array[i] = array[pos];
  895. }
  896. array[i-1] = 0;
  897. used = i;
  898. return *this;
  899. }
  900. // We are going to be adding characters, so the string size will increase.
  901. // Count the number of times toReplace exists in the string so we can allocate the new size.
  902. u32 find_count = 0;
  903. s32 pos = 0;
  904. while ((pos = find(other, pos)) != -1)
  905. {
  906. ++find_count;
  907. ++pos;
  908. }
  909. // Re-allocate the string now, if needed.
  910. u32 len = delta * find_count;
  911. if (used + len > allocated)
  912. reallocate(used + len);
  913. // Start replacing.
  914. pos = 0;
  915. while ((pos = find(other, pos)) != -1)
  916. {
  917. T* start = array + pos + other_size - 1;
  918. T* ptr = array + used - 1;
  919. T* end = array + delta + used -1;
  920. // Shift characters to make room for the string.
  921. while (ptr != start)
  922. {
  923. *end = *ptr;
  924. --ptr;
  925. --end;
  926. }
  927. // Add the new string now.
  928. for (u32 i = 0; i < replace_size; ++i)
  929. array[pos + i] = replace[i];
  930. pos += replace_size;
  931. used += delta;
  932. }
  933. return *this;
  934. }
  935. //! Removes characters from a string.
  936. /** \param c: Character to remove. */
  937. string<T,TAlloc>& remove(T c)
  938. {
  939. u32 pos = 0;
  940. u32 found = 0;
  941. for (u32 i=0; i<used-1; ++i)
  942. {
  943. if (array[i] == c)
  944. {
  945. ++found;
  946. continue;
  947. }
  948. array[pos++] = array[i];
  949. }
  950. used -= found;
  951. array[used-1] = 0;
  952. return *this;
  953. }
  954. //! Removes a string from the string.
  955. /** \param toRemove: String to remove. */
  956. string<T,TAlloc>& remove(const string<T,TAlloc>& toRemove)
  957. {
  958. u32 size = toRemove.size();
  959. if ( size == 0 )
  960. return *this;
  961. u32 pos = 0;
  962. u32 found = 0;
  963. for (u32 i=0; i<used-1; ++i)
  964. {
  965. u32 j = 0;
  966. while (j < size)
  967. {
  968. if (array[i + j] != toRemove[j])
  969. break;
  970. ++j;
  971. }
  972. if (j == size)
  973. {
  974. found += size;
  975. i += size - 1;
  976. continue;
  977. }
  978. array[pos++] = array[i];
  979. }
  980. used -= found;
  981. array[used-1] = 0;
  982. return *this;
  983. }
  984. //! Removes characters from a string.
  985. /** \param characters: Characters to remove. */
  986. string<T,TAlloc>& removeChars(const string<T,TAlloc> & characters)
  987. {
  988. if (characters.size() == 0)
  989. return *this;
  990. u32 pos = 0;
  991. u32 found = 0;
  992. for (u32 i=0; i<used-1; ++i)
  993. {
  994. // Don't use characters.findFirst as it finds the \0,
  995. // causing used to become incorrect.
  996. bool docontinue = false;
  997. for (u32 j=0; j<characters.size(); ++j)
  998. {
  999. if (characters[j] == array[i])
  1000. {
  1001. ++found;
  1002. docontinue = true;
  1003. break;
  1004. }
  1005. }
  1006. if (docontinue)
  1007. continue;
  1008. array[pos++] = array[i];
  1009. }
  1010. used -= found;
  1011. array[used-1] = 0;
  1012. return *this;
  1013. }
  1014. //! Trims the string.
  1015. /** Removes the specified characters (by default, Latin-1 whitespace)
  1016. from the beginning and the end of the string. */
  1017. string<T,TAlloc>& trim(const string<T,TAlloc> & whitespace = " \t\n\r")
  1018. {
  1019. // find start and end of the substring without the specified characters
  1020. const s32 begin = findFirstCharNotInList(whitespace.c_str(), whitespace.used);
  1021. if (begin == -1)
  1022. return (*this="");
  1023. const s32 end = findLastCharNotInList(whitespace.c_str(), whitespace.used);
  1024. return (*this = subString(begin, (end +1) - begin));
  1025. }
  1026. //! Erase 0's at the end when a string ends with a floating point number
  1027. /** After generating strings from floats we often end up with strings
  1028. ending up with lots of zeros which don't add any value. Erase 'em all.
  1029. Examples: "0.100000" becomes "0.1"
  1030. "10.000000" becomes "10"
  1031. "foo 3.140000" becomes "foo 3.14"
  1032. "no_num.000" stays "no_num.000"
  1033. "1." stays "1."
  1034. */
  1035. string<T,TAlloc>& eraseTrailingFloatZeros(char decimalPoint='.')
  1036. {
  1037. s32 i=findLastCharNotInList("0", 1);
  1038. if ( i > 0 && (u32)i < used-2 ) // non 0 must be found and not last char (also used is at least 2 when i > 0)
  1039. {
  1040. u32 eraseStart=i+1;
  1041. u32 dot=0;
  1042. if( core::isdigit(array[i]) )
  1043. {
  1044. while( --i>0 && core::isdigit(array[i]) );
  1045. if ( array[i] == decimalPoint )
  1046. dot = i;
  1047. }
  1048. else if ( array[i] == decimalPoint )
  1049. {
  1050. dot = i;
  1051. eraseStart = i;
  1052. }
  1053. if ( dot > 0 && core::isdigit(array[dot-1]) )
  1054. {
  1055. array[eraseStart] = 0;
  1056. used = eraseStart+1;
  1057. }
  1058. }
  1059. return *this;
  1060. }
  1061. //! Erases a character from the string.
  1062. /** May be slow, because all elements
  1063. following after the erased element have to be copied.
  1064. \param index: Index of element to be erased. */
  1065. string<T,TAlloc>& erase(u32 index)
  1066. {
  1067. IRR_DEBUG_BREAK_IF(index>=used) // access violation
  1068. for (u32 i=index+1; i<used; ++i)
  1069. array[i-1] = array[i];
  1070. --used;
  1071. return *this;
  1072. }
  1073. //! verify the existing string.
  1074. string<T,TAlloc>& validate()
  1075. {
  1076. // terminate on existing null
  1077. for (u32 i=0; i<allocated; ++i)
  1078. {
  1079. if (array[i] == 0)
  1080. {
  1081. used = i + 1;
  1082. return *this;
  1083. }
  1084. }
  1085. // terminate
  1086. if ( allocated > 0 )
  1087. {
  1088. used = allocated;
  1089. array[used-1] = 0;
  1090. }
  1091. else
  1092. {
  1093. used = 0;
  1094. }
  1095. return *this;
  1096. }
  1097. //! gets the last char of a string or null
  1098. T lastChar() const
  1099. {
  1100. return used > 1 ? array[used-2] : 0;
  1101. }
  1102. //! Split string into parts (tokens).
  1103. /** This method will split a string at certain delimiter characters
  1104. into the container passed in as reference. The type of the container
  1105. has to be given as template parameter. It must provide a push_back and
  1106. a size method.
  1107. \param ret The result container. Tokens are added, the container is not cleared.
  1108. \param delimiter C-style string of delimiter characters
  1109. \param countDelimiters Number of delimiter characters
  1110. \param ignoreEmptyTokens Flag to avoid empty substrings in the result
  1111. container. If two delimiters occur without a character in between or an
  1112. empty substring would be placed in the result. Or if a delimiter is the last
  1113. character an empty substring would be added at the end. If this flag is set,
  1114. only non-empty strings are stored.
  1115. \param keepSeparators Flag which allows to add the separator to the
  1116. result string. If this flag is true, the concatenation of the
  1117. substrings results in the original string. Otherwise, only the
  1118. characters between the delimiters are returned.
  1119. \return The number of resulting substrings
  1120. */
  1121. template<class container>
  1122. u32 split(container& ret, const T* const delimiter, u32 countDelimiters=1, bool ignoreEmptyTokens=true, bool keepSeparators=false) const
  1123. {
  1124. if (!delimiter)
  1125. return 0;
  1126. const u32 oldSize=ret.size();
  1127. u32 tokenStartIdx = 0;
  1128. for (u32 i=0; i<used; ++i)
  1129. {
  1130. for (u32 j=0; j<countDelimiters; ++j)
  1131. {
  1132. if (array[i] == delimiter[j])
  1133. {
  1134. if (i - tokenStartIdx > 0)
  1135. ret.push_back(string<T,TAlloc>(&array[tokenStartIdx], i - tokenStartIdx));
  1136. else if ( !ignoreEmptyTokens )
  1137. ret.push_back(string<T,TAlloc>());
  1138. if ( keepSeparators )
  1139. {
  1140. ret.push_back(string<T,TAlloc>(&array[i], 1));
  1141. }
  1142. tokenStartIdx = i+1;
  1143. break;
  1144. }
  1145. }
  1146. }
  1147. if ((used - 1) > tokenStartIdx)
  1148. ret.push_back(string<T,TAlloc>(&array[tokenStartIdx], (used - 1) - tokenStartIdx));
  1149. else if ( !ignoreEmptyTokens )
  1150. ret.push_back(string<T,TAlloc>());
  1151. return ret.size()-oldSize;
  1152. }
  1153. friend size_t multibyteToWString(string<wchar_t>& destination, const char* source, u32 sourceSize);
  1154. private:
  1155. //! Reallocate the array, make it bigger or smaller
  1156. void reallocate(u32 new_size)
  1157. {
  1158. T* old_array = array;
  1159. array = allocator.allocate(new_size); //new T[new_size];
  1160. allocated = new_size;
  1161. const u32 amount = used < new_size ? used : new_size;
  1162. for (u32 i=0; i<amount; ++i)
  1163. array[i] = old_array[i];
  1164. if (allocated < used)
  1165. used = allocated;
  1166. allocator.deallocate(old_array); // delete [] old_array;
  1167. }
  1168. //--- member variables
  1169. T* array;
  1170. u32 allocated;
  1171. u32 used;
  1172. TAlloc allocator;
  1173. };
  1174. //! Typedef for character strings
  1175. typedef string<c8> stringc;
  1176. //! Typedef for wide character strings
  1177. typedef string<wchar_t> stringw;
  1178. //! Convert multibyte string to wide-character string
  1179. /** Wrapper around mbstowcs from standard library, but directly using Irrlicht string class.
  1180. What the function does exactly depends on the LC_CTYPE of the current c locale.
  1181. \param destination Wide-character string receiving the converted source
  1182. \param source multibyte string
  1183. \return The number of wide characters written to destination, not including the eventual terminating null character or -1 when conversion failed */
  1184. static inline size_t multibyteToWString(string<wchar_t>& destination, const core::string<c8>& source)
  1185. {
  1186. return multibyteToWString(destination, source.c_str(), (u32)source.size());
  1187. }
  1188. //! Convert multibyte string to wide-character string
  1189. /** Wrapper around mbstowcs from standard library, but directly writing to Irrlicht string class.
  1190. What the function does exactly depends on the LC_CTYPE of the current c locale.
  1191. \param destination Wide-character string receiving the converted source
  1192. \param source multibyte string
  1193. \return The number of wide characters written to destination, not including the eventual terminating null character or -1 when conversion failed. */
  1194. static inline size_t multibyteToWString(string<wchar_t>& destination, const char* source)
  1195. {
  1196. const u32 s = source ? (u32)strlen(source) : 0;
  1197. return multibyteToWString(destination, source, s);
  1198. }
  1199. //! Internally used by the other multibyteToWString functions
  1200. static size_t multibyteToWString(string<wchar_t>& destination, const char* source, u32 sourceSize)
  1201. {
  1202. if ( sourceSize )
  1203. {
  1204. destination.reserve(sourceSize+1);
  1205. #if defined(_MSC_VER)
  1206. #pragma warning(push)
  1207. #pragma warning(disable: 4996) // 'mbstowcs': This function or variable may be unsafe. Consider using mbstowcs_s instead.
  1208. #endif
  1209. const size_t written = mbstowcs(destination.array, source, (size_t)sourceSize);
  1210. #if defined(_MSC_VER)
  1211. #pragma warning(pop)
  1212. #endif
  1213. if ( written != (size_t)-1 )
  1214. {
  1215. destination.used = (u32)written+1;
  1216. destination.array[destination.used-1] = 0;
  1217. }
  1218. else
  1219. {
  1220. // Likely character which got converted until the invalid character was encountered are in destination now.
  1221. // And it seems even 0-terminated, but I found no documentation anywhere that this (the 0-termination) is guaranteed :-(
  1222. destination.clear();
  1223. }
  1224. return written;
  1225. }
  1226. else
  1227. {
  1228. destination.clear();
  1229. return 0;
  1230. }
  1231. }
  1232. } // end namespace core
  1233. } // end namespace irr
  1234. #endif