BsString.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. #pragma once
  2. namespace BansheeEngine
  3. {
  4. /**
  5. * @brief Basic string that uses Banshee memory allocators.
  6. */
  7. template <typename T>
  8. using BasicString = std::basic_string < T, std::char_traits<T>, StdAlloc<T> > ;
  9. /**
  10. * @brief Basic string stream that uses Banshee memory allocators.
  11. */
  12. template <typename T>
  13. using BasicStringStream = std::basic_stringstream < T, std::char_traits<T>, StdAlloc<T> > ;
  14. /**
  15. * @brief Wide string used primarily for handling Unicode text.
  16. */
  17. typedef BasicString<wchar_t> WString;
  18. /**
  19. * @brief Narrow string used primarily for handling ASCII text.
  20. */
  21. typedef BasicString<char> String;
  22. /**
  23. * @brief Wide string stream used for primarily for constructing
  24. * strings consisting of Unicode text.
  25. */
  26. typedef BasicStringStream<wchar_t> WStringStream;
  27. /**
  28. * @brief Wide string stream used for primarily for constructing
  29. * strings consisting of ASCII text.
  30. */
  31. typedef BasicStringStream<char> StringStream;
  32. }
  33. #include "BsStringFormat.h"
  34. namespace BansheeEngine
  35. {
  36. /**
  37. * @brief Utility class for manipulating Strings.
  38. */
  39. class BS_UTILITY_EXPORT StringUtil
  40. {
  41. public:
  42. /**
  43. * @brief Removes any whitespace characters from beginning or end of the string.
  44. */
  45. static void trim(String& str, bool left = true, bool right = true);
  46. /**
  47. * @copydoc StringUtil::trim(String&, bool, bool)
  48. */
  49. static void trim(WString& str, bool left = true, bool right = true);
  50. /**
  51. * @brief Removes specified characters from beginning or end of the string.
  52. */
  53. static void trim(String& str, const String& delims, bool left = true, bool right = true);
  54. /**
  55. * @copydoc StringUtil::trim(String&, const String&, bool, bool)
  56. */
  57. static void trim(WString& str, const WString& delims, bool left = true, bool right = true);
  58. /**
  59. * @brief Returns a vector of strings containing all the substrings delimited
  60. * by the provided delimiter characters.
  61. *
  62. * @param str The string to split.
  63. * @param delims (optional) Delimiter characters to split the string by. They will not
  64. * be included in resulting substrings.
  65. * @param maxSplits (optional) The maximum number of splits to perform (0 for unlimited splits). If this
  66. * parameters is > 0, the splitting process will stop after this many splits, left to right.
  67. */
  68. static Vector<String> split(const String& str, const String& delims = "\t\n ", unsigned int maxSplits = 0);
  69. /**
  70. * @copydoc StringUtil::split(const String&, const String&, unsigned int)
  71. */
  72. static Vector<WString> split(const WString& str, const WString& delims = L"\t\n ", unsigned int maxSplits = 0);
  73. /**
  74. * @brief Returns a vector of strings containing all the substrings delimited
  75. * by the provided delimiter characters, or the double delimiters used for including
  76. * normal delimiter characters in the tokenized string.
  77. *
  78. * @param str The string to split.
  79. * @param delims (optional) Delimiter characters to split the string by. They will not
  80. * be included in resulting substrings.
  81. * @params doubleDelims (optional) Delimiter character you may use to surround other normal delimiters, in order
  82. * to include them in the tokensized string.
  83. * @param maxSplits (optional) The maximum number of splits to perform (0 for unlimited splits). If this
  84. * parameters is > 0, the splitting process will stop after this many splits, left to right.
  85. */
  86. static Vector<String> tokenise(const String& str, const String& delims = "\t\n ", const String& doubleDelims = "\"", unsigned int maxSplits = 0);
  87. /**
  88. * @copydoc StringUtil::tokenise(const String&, const String&, const String&, unsigned int)
  89. */
  90. static Vector<WString> tokenise(const WString& str, const WString& delims = L"\t\n ", const WString& doubleDelims = L"\"", unsigned int maxSplits = 0);
  91. /**
  92. * @brief Converts all the characters in the string to lower case.
  93. */
  94. static void toLowerCase(String& str);
  95. /**
  96. * @brief Converts all the characters in the string to lower case.
  97. */
  98. static void toLowerCase(WString& str);
  99. /**
  100. * @brief Converts all the characters in the string to upper case.
  101. */
  102. static void toUpperCase(String& str);
  103. /**
  104. * @brief Converts all the characters in the string to upper case.
  105. */
  106. static void toUpperCase(WString& str);
  107. /**
  108. * @brief Returns whether the string begins with the pattern passed in.
  109. *
  110. * @param str String to compare.
  111. * @param pattern Pattern to compare with.
  112. * @param lowerCase (optional) If true, the start of the string will be lower cased before
  113. * comparison, and the pattern should also be in lower case.
  114. */
  115. static bool startsWith(const String& str, const String& pattern, bool lowerCase = true);
  116. /**
  117. * @copydoc startsWidth(const String&, const String&, bool)
  118. */
  119. static bool startsWith(const WString& str, const WString& pattern, bool lowerCase = true);
  120. /**
  121. * @brief Returns whether the string end with the pattern passed in.
  122. *
  123. * @param str String to compare.
  124. * @param pattern Pattern to compare with.
  125. * @param lowerCase (optional) If true, the start of the string will be lower cased before
  126. * comparison, and the pattern should also be in lower case.
  127. */
  128. static bool endsWith(const String& str, const String& pattern, bool lowerCase = true);
  129. /**
  130. * @copydoc endsWith(const String&, const String&, bool)
  131. */
  132. static bool endsWith(const WString& str, const WString& pattern, bool lowerCase = true);
  133. /**
  134. * @brief Returns true if the string matches the provided pattern. Pattern may use
  135. * a "*" wildcard for matching any characters.
  136. *
  137. * @param str The string to test.
  138. * @param pattern Patterns to look for.
  139. * @param caseSensitive (optional) Should the match be case sensitive or not.
  140. */
  141. static bool match(const String& str, const String& pattern, bool caseSensitive = true);
  142. /**
  143. * @copydoc match(const String&, const String&, bool)
  144. */
  145. static bool match(const WString& str, const WString& pattern, bool caseSensitive = true);
  146. /**
  147. * @brief Replace all instances of a substring with a another substring.
  148. *
  149. * @param source String to search.
  150. * @param replaceWhat Substring to find and replace
  151. * @param replaceWithWhat Substring to replace with (the new sub-string)
  152. *
  153. * @return An updated string with the substrings replaced.
  154. */
  155. static const String replaceAll(const String& source, const String& replaceWhat, const String& replaceWithWhat);
  156. /**
  157. * @copydoc replaceAll(const String&, const String&, const String&)
  158. */
  159. static const WString replaceAll(const WString& source, const WString& replaceWhat, const WString& replaceWithWhat);
  160. /**
  161. * @brief Compares two strings. Returns 0 if the two compare equal, <0 if the value of the left
  162. * string is lower than of the right string, or >0 if the value of the left string is
  163. * higher than the right string.
  164. *
  165. * @param lhs Left string to compare.
  166. * @param rhs Right string to compare.
  167. * @param caseSensitive If true the comparison will consider uppercase and lowercase
  168. * characters different.
  169. */
  170. template <class T>
  171. static int compare(const BasicString<T>& lhs, const BasicString<T>& rhs, bool caseSensitive = true)
  172. {
  173. if (caseSensitive)
  174. return (int)lhs.compare(rhs);
  175. int size = (int)std::min(lhs.size(), rhs.size());
  176. for (int i = 0; i < size; i++)
  177. {
  178. if (toupper(lhs[i]) < toupper(rhs[i])) return -1;
  179. if (toupper(lhs[i]) > toupper(rhs[i])) return 1;
  180. }
  181. return (lhs.size() < rhs.size() ? -1 : (lhs.size() == rhs.size() ? 0 : 1));
  182. }
  183. /**
  184. * @copydoc StringFormat::format
  185. */
  186. template<class T, class... Args>
  187. static BasicString<T> format(const BasicString<T>& source, Args&& ...args)
  188. {
  189. return StringFormat::format(source.c_str(), std::forward<Args>(args)...);
  190. }
  191. /**
  192. * @copydoc StringFormat::format
  193. */
  194. template<class T, class... Args>
  195. static BasicString<T> format(const T* source, Args&& ...args)
  196. {
  197. return StringFormat::format(source, std::forward<Args>(args)...);
  198. }
  199. /**
  200. * @brief Constant blank string, useful for returning by ref where local does not exist.
  201. */
  202. static const String BLANK;
  203. /**
  204. * @brief Constant blank wide string, useful for returning by ref where local does not exist.
  205. */
  206. static const WString WBLANK;
  207. private:
  208. template <class T>
  209. static Vector<BasicString<T>> splitInternal(const BasicString<T>& str, const BasicString<T>& delims, unsigned int maxSplits)
  210. {
  211. Vector<BasicString<T>> ret;
  212. // Pre-allocate some space for performance
  213. ret.reserve(maxSplits ? maxSplits+1 : 10); // 10 is guessed capacity for most case
  214. unsigned int numSplits = 0;
  215. // Use STL methods
  216. size_t start, pos;
  217. start = 0;
  218. do
  219. {
  220. pos = str.find_first_of(delims, start);
  221. if (pos == start)
  222. {
  223. // Do nothing
  224. start = pos + 1;
  225. }
  226. else if (pos == BasicString<T>::npos || (maxSplits && numSplits == maxSplits))
  227. {
  228. // Copy the rest of the string
  229. ret.push_back(str.substr(start));
  230. break;
  231. }
  232. else
  233. {
  234. // Copy up to delimiter
  235. ret.push_back(str.substr(start, pos - start));
  236. start = pos + 1;
  237. }
  238. // parse up to next real data
  239. start = str.find_first_not_of(delims, start);
  240. ++numSplits;
  241. } while (pos != BasicString<T>::npos);
  242. return ret;
  243. }
  244. template <class T>
  245. static Vector<BasicString<T>> tokeniseInternal(const BasicString<T>& str, const BasicString<T>& singleDelims,
  246. const BasicString<T>& doubleDelims, unsigned int maxSplits)
  247. {
  248. Vector<BasicString<T>> ret;
  249. // Pre-allocate some space for performance
  250. ret.reserve(maxSplits ? maxSplits + 1 : 10); // 10 is guessed capacity for most case
  251. unsigned int numSplits = 0;
  252. BasicString<T> delims = singleDelims + doubleDelims;
  253. // Use STL methods
  254. size_t start, pos;
  255. T curDoubleDelim = 0;
  256. start = 0;
  257. do
  258. {
  259. if (curDoubleDelim != 0)
  260. {
  261. pos = str.find(curDoubleDelim, start);
  262. }
  263. else
  264. {
  265. pos = str.find_first_of(delims, start);
  266. }
  267. if (pos == start)
  268. {
  269. T curDelim = str.at(pos);
  270. if (doubleDelims.find_first_of(curDelim) != BasicString<T>::npos)
  271. {
  272. curDoubleDelim = curDelim;
  273. }
  274. // Do nothing
  275. start = pos + 1;
  276. }
  277. else if (pos == BasicString<T>::npos || (maxSplits && numSplits == maxSplits))
  278. {
  279. if (curDoubleDelim != 0)
  280. {
  281. //Missing closer. Warn or throw exception?
  282. }
  283. // Copy the rest of the string
  284. ret.push_back( str.substr(start) );
  285. break;
  286. }
  287. else
  288. {
  289. if (curDoubleDelim != 0)
  290. {
  291. curDoubleDelim = 0;
  292. }
  293. // Copy up to delimiter
  294. ret.push_back( str.substr(start, pos - start) );
  295. start = pos + 1;
  296. }
  297. if (curDoubleDelim == 0)
  298. {
  299. // parse up to next real data
  300. start = str.find_first_not_of(singleDelims, start);
  301. }
  302. ++numSplits;
  303. } while (pos != BasicString<T>::npos);
  304. return ret;
  305. }
  306. template <class T>
  307. static bool startsWithInternal(const BasicString<T>& str, const BasicString<T>& pattern, bool lowerCase)
  308. {
  309. size_t thisLen = str.length();
  310. size_t patternLen = pattern.length();
  311. if (thisLen < patternLen || patternLen == 0)
  312. return false;
  313. BasicString<T> startOfThis = str.substr(0, patternLen);
  314. if (lowerCase)
  315. StringUtil::toLowerCase(startOfThis);
  316. return (startOfThis == pattern);
  317. }
  318. template <class T>
  319. static bool endsWithInternal(const BasicString<T>& str, const BasicString<T>& pattern, bool lowerCase)
  320. {
  321. size_t thisLen = str.length();
  322. size_t patternLen = pattern.length();
  323. if (thisLen < patternLen || patternLen == 0)
  324. return false;
  325. BasicString<T> endOfThis = str.substr(thisLen - patternLen, patternLen);
  326. if (lowerCase)
  327. StringUtil::toLowerCase(endOfThis);
  328. return (endOfThis == pattern);
  329. }
  330. template <class T>
  331. static bool matchInternal(const BasicString<T>& str, const BasicString<T>& pattern, bool caseSensitive)
  332. {
  333. BasicString<T> tmpStr = str;
  334. BasicString<T> tmpPattern = pattern;
  335. if (!caseSensitive)
  336. {
  337. StringUtil::toLowerCase(tmpStr);
  338. StringUtil::toLowerCase(tmpPattern);
  339. }
  340. BasicString<T>::const_iterator strIt = tmpStr.begin();
  341. BasicString<T>::const_iterator patIt = tmpPattern.begin();
  342. BasicString<T>::const_iterator lastWildCardIt = tmpPattern.end();
  343. while (strIt != tmpStr.end() && patIt != tmpPattern.end())
  344. {
  345. if (*patIt == '*')
  346. {
  347. lastWildCardIt = patIt;
  348. // Skip over looking for next character
  349. ++patIt;
  350. if (patIt == tmpPattern.end())
  351. {
  352. // Skip right to the end since * matches the entire rest of the string
  353. strIt = tmpStr.end();
  354. }
  355. else
  356. {
  357. // scan until we find next pattern character
  358. while(strIt != tmpStr.end() && *strIt != *patIt)
  359. ++strIt;
  360. }
  361. }
  362. else
  363. {
  364. if (*patIt != *strIt)
  365. {
  366. if (lastWildCardIt != tmpPattern.end())
  367. {
  368. // The last wildcard can match this incorrect sequence
  369. // rewind pattern to wildcard and keep searching
  370. patIt = lastWildCardIt;
  371. lastWildCardIt = tmpPattern.end();
  372. }
  373. else
  374. {
  375. // no wildwards left
  376. return false;
  377. }
  378. }
  379. else
  380. {
  381. ++patIt;
  382. ++strIt;
  383. }
  384. }
  385. }
  386. // If we reached the end of both the pattern and the string, we succeeded
  387. if (patIt == tmpPattern.end() && strIt == tmpStr.end())
  388. return true;
  389. else
  390. return false;
  391. }
  392. template <class T>
  393. static BasicString<T> replaceAllInternal(const BasicString<T>& source,
  394. const BasicString<T>& replaceWhat, const BasicString<T>& replaceWithWhat)
  395. {
  396. BasicString<T> result = source;
  397. BasicString<T>::size_type pos = 0;
  398. while(1)
  399. {
  400. pos = result.find(replaceWhat,pos);
  401. if (pos == BasicString<T>::npos) break;
  402. result.replace(pos,replaceWhat.size(), replaceWithWhat);
  403. pos += replaceWithWhat.size();
  404. }
  405. return result;
  406. }
  407. };
  408. /**
  409. * @brief Converts a narrow string to a wide string.
  410. */
  411. BS_UTILITY_EXPORT WString toWString(const String& source);
  412. /**
  413. * @brief Converts a narrow string to a wide string.
  414. */
  415. BS_UTILITY_EXPORT WString toWString(const char* source);
  416. /**
  417. * @brief Converts a float to a string.
  418. */
  419. BS_UTILITY_EXPORT WString toWString(float val, unsigned short precision = 6,
  420. unsigned short width = 0, char fill = ' ',
  421. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  422. /**
  423. * @brief Converts a double to a string.
  424. */
  425. BS_UTILITY_EXPORT WString toWString(double val, unsigned short precision = 6,
  426. unsigned short width = 0, char fill = ' ',
  427. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  428. /**
  429. * @brief Converts a Radian to a string.
  430. */
  431. BS_UTILITY_EXPORT WString toWString(Radian val, unsigned short precision = 6,
  432. unsigned short width = 0, char fill = ' ',
  433. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  434. /**
  435. * @brief Converts a Degree to a string.
  436. */
  437. BS_UTILITY_EXPORT WString toWString(Degree val, unsigned short precision = 6,
  438. unsigned short width = 0, char fill = ' ',
  439. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  440. /**
  441. * @brief Converts an int to a string.
  442. */
  443. BS_UTILITY_EXPORT WString toWString(int val, unsigned short width = 0,
  444. char fill = ' ',
  445. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  446. /**
  447. * @brief Converts an unsigned int to a string.
  448. */
  449. BS_UTILITY_EXPORT WString toWString(unsigned int val,
  450. unsigned short width = 0, char fill = ' ',
  451. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  452. /**
  453. * @brief Converts an 64bit integer to a string.
  454. */
  455. BS_UTILITY_EXPORT WString toWString(INT64 val,
  456. unsigned short width = 0, char fill = ' ',
  457. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  458. /**
  459. * @brief Converts an 64bit unsigned to a string.
  460. */
  461. BS_UTILITY_EXPORT WString toWString(UINT64 val,
  462. unsigned short width = 0, char fill = ' ',
  463. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  464. /**
  465. * @brief Converts an narrow char unsigned to a string.
  466. */
  467. BS_UTILITY_EXPORT WString toWString(char val,
  468. unsigned short width = 0, char fill = ' ',
  469. std::ios::fmtflags flags = std::ios::fmtflags(0));
  470. /**
  471. * @brief Converts an wide bit char unsigned to a string.
  472. */
  473. BS_UTILITY_EXPORT WString toWString(wchar_t val,
  474. unsigned short width = 0, char fill = ' ',
  475. std::ios::fmtflags flags = std::ios::fmtflags(0));
  476. /**
  477. * @brief Converts a boolean to a string.
  478. *
  479. * @param val Value to convert.
  480. * @param yesNo (optional) If set to true, result is "yes" or "no" instead of "true" or "false".
  481. */
  482. BS_UTILITY_EXPORT WString toWString(bool val, bool yesNo = false);
  483. /**
  484. * @brief Converts a 2 dimensional vector to a string.
  485. *
  486. * @note Format is "x y".
  487. */
  488. BS_UTILITY_EXPORT WString toWString(const Vector2& val);
  489. /**
  490. * @brief Converts a 2 dimensional integer vector to a string.
  491. *
  492. * @note Format is "x y".
  493. */
  494. BS_UTILITY_EXPORT WString toWString(const Vector2I& val);
  495. /**
  496. * @brief Converts a 3 dimensional vector to a string.
  497. *
  498. * @note Format is "x y z".
  499. */
  500. BS_UTILITY_EXPORT WString toWString(const Vector3& val);
  501. /**
  502. * @brief Converts a 4 dimensional vector to a string.
  503. *
  504. * @note Format is "x y z w".
  505. */
  506. BS_UTILITY_EXPORT WString toWString(const Vector4& val);
  507. /**
  508. * @brief Converts a 3x3 matrix to a string.
  509. *
  510. * @note Format is "00 01 02 10 11 12 20 21 22".
  511. */
  512. BS_UTILITY_EXPORT WString toWString(const Matrix3& val);
  513. /**
  514. * @brief Converts a 4x4 matrix to a string.
  515. *
  516. * @note Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33".
  517. */
  518. BS_UTILITY_EXPORT WString toWString(const Matrix4& val);
  519. /**
  520. * @brief Converts a Quaternion to a string.
  521. *
  522. * @note Format is "w x y z".
  523. */
  524. BS_UTILITY_EXPORT WString toWString(const Quaternion& val);
  525. /**
  526. * @brief Converts a color to a string.
  527. *
  528. * @note Format is "r g b a".
  529. */
  530. BS_UTILITY_EXPORT WString toWString(const Color& val);
  531. /**
  532. * @brief Converts a vector of strings into a single string where the substrings are
  533. * delimited by spaces.
  534. */
  535. BS_UTILITY_EXPORT WString toWString(const Vector<BansheeEngine::WString>& val);
  536. /**
  537. * @brief Converts a wide string to a narrow string.
  538. */
  539. BS_UTILITY_EXPORT String toString(const WString& source);
  540. /**
  541. * @brief Converts a wide string to a narrow string.
  542. */
  543. BS_UTILITY_EXPORT String toString(const wchar_t* source);
  544. /**
  545. * @brief Converts a float to a string.
  546. */
  547. BS_UTILITY_EXPORT String toString(float val, unsigned short precision = 6,
  548. unsigned short width = 0, char fill = ' ',
  549. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  550. /**
  551. * @brief Converts a double to a string.
  552. */
  553. BS_UTILITY_EXPORT String toString(double val, unsigned short precision = 6,
  554. unsigned short width = 0, char fill = ' ',
  555. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  556. /**
  557. * @brief Converts a Radian to a string.
  558. */
  559. BS_UTILITY_EXPORT String toString(Radian val, unsigned short precision = 6,
  560. unsigned short width = 0, char fill = ' ',
  561. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  562. /**
  563. * @brief Converts a Degree to a string.
  564. */
  565. BS_UTILITY_EXPORT String toString(Degree val, unsigned short precision = 6,
  566. unsigned short width = 0, char fill = ' ',
  567. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  568. /**
  569. * @brief Converts an int to a string.
  570. */
  571. BS_UTILITY_EXPORT String toString(int val, unsigned short width = 0,
  572. char fill = ' ',
  573. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  574. /**
  575. * @brief Converts an unsigned int to a string.
  576. */
  577. BS_UTILITY_EXPORT String toString(unsigned int val,
  578. unsigned short width = 0, char fill = ' ',
  579. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  580. /**
  581. * @brief Converts a 64bit int to a string.
  582. */
  583. BS_UTILITY_EXPORT String toString(INT64 val,
  584. unsigned short width = 0, char fill = ' ',
  585. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  586. /**
  587. * @brief Converts an 64bit unsigned int to a string.
  588. */
  589. BS_UTILITY_EXPORT String toString(UINT64 val,
  590. unsigned short width = 0, char fill = ' ',
  591. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  592. /**
  593. * @brief Converts a boolean to a string.
  594. *
  595. * @param val true to value.
  596. * @param yesNo (optional) If set to true, result is "yes" or "no" instead of "true" or "false".
  597. */
  598. BS_UTILITY_EXPORT String toString(bool val, bool yesNo = false);
  599. /**
  600. * @brief Converts a 2 dimensional vector to a string.
  601. *
  602. * @note Format is "x y".
  603. */
  604. BS_UTILITY_EXPORT String toString(const Vector2& val);
  605. /**
  606. * @brief Converts a 2 dimensional integer vector to a string.
  607. *
  608. * @note Format is "x y".
  609. */
  610. BS_UTILITY_EXPORT String toString(const Vector2I& val);
  611. /**
  612. * @brief Converts a 3 dimensional vector to a string.
  613. *
  614. * @note Format is "x y z".
  615. */
  616. BS_UTILITY_EXPORT String toString(const Vector3& val);
  617. /**
  618. * @brief Converts a 4 dimensional vector to a string.
  619. *
  620. * @note Format is "x y z w".
  621. */
  622. BS_UTILITY_EXPORT String toString(const Vector4& val);
  623. /**
  624. * @brief Converts a 3x3 matrix to a string.
  625. *
  626. * @note Format is "00 01 02 10 11 12 20 21 22".
  627. */
  628. BS_UTILITY_EXPORT String toString(const Matrix3& val);
  629. /**
  630. * @brief Converts a 4x4 matrix to a string.
  631. *
  632. * @note Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33".
  633. */
  634. BS_UTILITY_EXPORT String toString(const Matrix4& val);
  635. /**
  636. * @brief Converts a Quaternion to a string.
  637. *
  638. * @note Format is "w x y z".
  639. */
  640. BS_UTILITY_EXPORT String toString(const Quaternion& val);
  641. /**
  642. * @brief Converts a color to a string.
  643. *
  644. * @note Format is "r g b a".
  645. */
  646. BS_UTILITY_EXPORT String toString(const Color& val);
  647. /**
  648. * @brief Converts a vector of strings into a single string where the substrings are
  649. * delimited by spaces.
  650. */
  651. BS_UTILITY_EXPORT String toString(const Vector<BansheeEngine::String>& val);
  652. /**
  653. * @brief Converts a String to a float.
  654. *
  655. * @note 0.0f if the value could not be parsed, otherwise the numeric version of the string.
  656. */
  657. BS_UTILITY_EXPORT float parseFloat(const String& val, float defaultValue = 0);
  658. /**
  659. * @brief Converts a String to a whole number.
  660. *
  661. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  662. */
  663. BS_UTILITY_EXPORT int parseInt(const String& val, int defaultValue = 0);
  664. /**
  665. * @brief Converts a String to a whole number.
  666. *
  667. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  668. */
  669. BS_UTILITY_EXPORT unsigned int parseUnsignedInt(const String& val, unsigned int defaultValue = 0);
  670. /**
  671. * @brief Converts a String to a whole number.
  672. *
  673. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  674. */
  675. BS_UTILITY_EXPORT long parseLong(const String& val, long defaultValue = 0);
  676. /**
  677. * @brief Converts a String to a whole number.
  678. *
  679. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  680. */
  681. BS_UTILITY_EXPORT unsigned long parseUnsignedLong(const String& val, unsigned long defaultValue = 0);
  682. /**
  683. * @brief Converts a String to a boolean.
  684. *
  685. * @note Returns true if case-insensitive match of the start of the string
  686. * matches "true", "yes" or "1", false otherwise.
  687. */
  688. BS_UTILITY_EXPORT bool parseBool(const String& val, bool defaultValue = 0);
  689. /**
  690. * @brief Checks the String is a valid number value.
  691. */
  692. BS_UTILITY_EXPORT bool isNumber(const String& val);
  693. /**
  694. * @brief Converts a WString to a float.
  695. *
  696. * @note 0.0f if the value could not be parsed, otherwise the numeric version of the string.
  697. */
  698. BS_UTILITY_EXPORT float parseFloat(const WString& val, float defaultValue = 0);
  699. /**
  700. * @brief Converts a WString to a whole number.
  701. *
  702. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  703. */
  704. BS_UTILITY_EXPORT int parseInt(const WString& val, int defaultValue = 0);
  705. /**
  706. * @brief Converts a WString to a whole number.
  707. *
  708. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  709. */
  710. BS_UTILITY_EXPORT unsigned int parseUnsignedInt(const WString& val, unsigned int defaultValue = 0);
  711. /**
  712. * @brief Converts a WString to a whole number.
  713. *
  714. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  715. */
  716. BS_UTILITY_EXPORT long parseLong(const WString& val, long defaultValue = 0);
  717. /**
  718. * @brief Converts a WString to a whole number.
  719. *
  720. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  721. */
  722. BS_UTILITY_EXPORT unsigned long parseUnsignedLong(const WString& val, unsigned long defaultValue = 0);
  723. /**
  724. * @brief Converts a WString to a boolean.
  725. *
  726. * @note Returns true if case-insensitive match of the start of the string
  727. * matches "true", "yes" or "1", false otherwise.
  728. */
  729. BS_UTILITY_EXPORT bool parseBool(const WString& val, bool defaultValue = 0);
  730. /**
  731. * @brief Checks the WString is a valid number value.
  732. */
  733. BS_UTILITY_EXPORT bool isNumber(const WString& val);
  734. /**
  735. * @brief Helper method that throws an exception regarding a data overflow.
  736. */
  737. void BS_UTILITY_EXPORT __string_throwDataOverflowException();
  738. /**
  739. * @brief RTTIPlainType specialization for String that allows strings be serialized as
  740. * value types.
  741. *
  742. * @see RTTIPlainType
  743. */
  744. template<> struct RTTIPlainType<String>
  745. {
  746. enum { id = 20 }; enum { hasDynamicSize = 1 };
  747. static void toMemory(const String& data, char* memory)
  748. {
  749. UINT32 size = getDynamicSize(data);
  750. memcpy(memory, &size, sizeof(UINT32));
  751. memory += sizeof(UINT32);
  752. size -= sizeof(UINT32);
  753. memcpy(memory, data.data(), size);
  754. }
  755. static UINT32 fromMemory(String& data, char* memory)
  756. {
  757. UINT32 size;
  758. memcpy(&size, memory, sizeof(UINT32));
  759. memory += sizeof(UINT32);
  760. UINT32 stringSize = size - sizeof(UINT32);
  761. char* buffer = (char*)bs_alloc(stringSize + 1);
  762. memcpy(buffer, memory, stringSize);
  763. buffer[stringSize] = '\0';
  764. data = String(buffer);
  765. bs_free(buffer);
  766. return size;
  767. }
  768. static UINT32 getDynamicSize(const String& data)
  769. {
  770. UINT64 dataSize = data.size() * sizeof(String::value_type) + sizeof(UINT32);
  771. #if BS_DEBUG_MODE
  772. if(dataSize > std::numeric_limits<UINT32>::max())
  773. {
  774. __string_throwDataOverflowException();
  775. }
  776. #endif
  777. return (UINT32)dataSize;
  778. }
  779. };
  780. /**
  781. * @brief RTTIPlainType specialization for WString that allows strings be serialized as
  782. * value types.
  783. *
  784. * @see RTTIPlainType
  785. */
  786. template<> struct RTTIPlainType<WString>
  787. {
  788. enum { id = TID_WString }; enum { hasDynamicSize = 1 };
  789. static void toMemory(const WString& data, char* memory)
  790. {
  791. UINT32 size = getDynamicSize(data);
  792. memcpy(memory, &size, sizeof(UINT32));
  793. memory += sizeof(UINT32);
  794. size -= sizeof(UINT32);
  795. memcpy(memory, data.data(), size);
  796. }
  797. static UINT32 fromMemory(WString& data, char* memory)
  798. {
  799. UINT32 size;
  800. memcpy(&size, memory, sizeof(UINT32));
  801. memory += sizeof(UINT32);
  802. UINT32 stringSize = size - sizeof(UINT32);
  803. WString::value_type* buffer = (WString::value_type*)bs_alloc(stringSize + sizeof(WString::value_type));
  804. memcpy(buffer, memory, stringSize);
  805. UINT32 numChars = stringSize / sizeof(WString::value_type);
  806. buffer[numChars] = L'\0';
  807. data = WString(buffer);
  808. bs_free(buffer);
  809. return size;
  810. }
  811. static UINT32 getDynamicSize(const WString& data)
  812. {
  813. UINT64 dataSize = data.size() * sizeof(WString::value_type) + sizeof(UINT32);
  814. #if BS_DEBUG_MODE
  815. if(dataSize > std::numeric_limits<UINT32>::max())
  816. {
  817. __string_throwDataOverflowException();
  818. }
  819. #endif
  820. return (UINT32)dataSize;
  821. }
  822. };
  823. }
  824. /**
  825. * @brief Hash value generator for String.
  826. */
  827. template<>
  828. struct std::hash<BansheeEngine::String>
  829. {
  830. size_t operator()(const BansheeEngine::String& string) const
  831. {
  832. size_t hash = 0;
  833. for(size_t i = 0; i < string.size(); i++)
  834. hash = 65599 * hash + string[i];
  835. return hash ^ (hash >> 16);
  836. }
  837. };
  838. /**
  839. * @brief Hash value generator for WString.
  840. */
  841. template<>
  842. struct std::hash<BansheeEngine::WString>
  843. {
  844. size_t operator()(const BansheeEngine::WString& string) const
  845. {
  846. size_t hash = 0;
  847. for(size_t i = 0; i < string.size(); i++)
  848. hash = 65599 * hash + string[i];
  849. return hash ^ (hash >> 16);
  850. }
  851. };