BsString.h 27 KB

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