BsString.h 27 KB

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