BsString.h 28 KB

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