BsString.h 28 KB

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