BsString.h 28 KB

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