irrString.h 34 KB

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