irrString.h 28 KB

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