BsString.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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 narrow string to a wide string.
  391. */
  392. BS_UTILITY_EXPORT WString toWString(const char* source);
  393. /**
  394. * @brief Converts a float to a string.
  395. */
  396. BS_UTILITY_EXPORT WString toWString(float 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 double to a string.
  401. */
  402. BS_UTILITY_EXPORT WString toWString(double 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 a Radian to a string.
  407. */
  408. BS_UTILITY_EXPORT WString toWString(Radian val, unsigned short precision = 6,
  409. unsigned short width = 0, char fill = ' ',
  410. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  411. /**
  412. * @brief Converts a Degree to a string.
  413. */
  414. BS_UTILITY_EXPORT WString toWString(Degree val, unsigned short precision = 6,
  415. unsigned short width = 0, char fill = ' ',
  416. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  417. /**
  418. * @brief Converts an int to a string.
  419. */
  420. BS_UTILITY_EXPORT WString toWString(int val, unsigned short width = 0,
  421. char fill = ' ',
  422. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  423. /**
  424. * @brief Converts an unsigned int to a string.
  425. */
  426. BS_UTILITY_EXPORT WString toWString(unsigned int val,
  427. unsigned short width = 0, char fill = ' ',
  428. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  429. /**
  430. * @brief Converts an 64bit integer to a string.
  431. */
  432. BS_UTILITY_EXPORT WString toWString(INT64 val,
  433. unsigned short width = 0, char fill = ' ',
  434. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  435. /**
  436. * @brief Converts an 64bit unsigned to a string.
  437. */
  438. BS_UTILITY_EXPORT WString toWString(UINT64 val,
  439. unsigned short width = 0, char fill = ' ',
  440. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  441. /**
  442. * @brief Converts an narrow char unsigned to a string.
  443. */
  444. BS_UTILITY_EXPORT WString toWString(char val,
  445. unsigned short width = 0, char fill = ' ',
  446. std::ios::fmtflags flags = std::ios::fmtflags(0));
  447. /**
  448. * @brief Converts an wide bit char unsigned to a string.
  449. */
  450. BS_UTILITY_EXPORT WString toWString(wchar_t val,
  451. unsigned short width = 0, char fill = ' ',
  452. std::ios::fmtflags flags = std::ios::fmtflags(0));
  453. /**
  454. * @brief Converts a boolean to a string.
  455. *
  456. * @param val Value to convert.
  457. * @param yesNo (optional) If set to true, result is "yes" or "no" instead of "true" or "false".
  458. */
  459. BS_UTILITY_EXPORT WString toWString(bool val, bool yesNo = false);
  460. /**
  461. * @brief Converts a 2 dimensional vector to a string.
  462. *
  463. * @note Format is "x y".
  464. */
  465. BS_UTILITY_EXPORT WString toWString(const Vector2& val);
  466. /**
  467. * @brief Converts a 2 dimensional integer vector to a string.
  468. *
  469. * @note Format is "x y".
  470. */
  471. BS_UTILITY_EXPORT WString toWString(const Vector2I& val);
  472. /**
  473. * @brief Converts a 3 dimensional vector to a string.
  474. *
  475. * @note Format is "x y z".
  476. */
  477. BS_UTILITY_EXPORT WString toWString(const Vector3& val);
  478. /**
  479. * @brief Converts a 4 dimensional vector to a string.
  480. *
  481. * @note Format is "x y z w".
  482. */
  483. BS_UTILITY_EXPORT WString toWString(const Vector4& val);
  484. /**
  485. * @brief Converts a 3x3 matrix to a string.
  486. *
  487. * @note Format is "00 01 02 10 11 12 20 21 22".
  488. */
  489. BS_UTILITY_EXPORT WString toWString(const Matrix3& val);
  490. /**
  491. * @brief Converts a 4x4 matrix to a string.
  492. *
  493. * @note Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33".
  494. */
  495. BS_UTILITY_EXPORT WString toWString(const Matrix4& val);
  496. /**
  497. * @brief Converts a Quaternion to a string.
  498. *
  499. * @note Format is "w x y z".
  500. */
  501. BS_UTILITY_EXPORT WString toWString(const Quaternion& val);
  502. /**
  503. * @brief Converts a color to a string.
  504. *
  505. * @note Format is "r g b a".
  506. */
  507. BS_UTILITY_EXPORT WString toWString(const Color& val);
  508. /**
  509. * @brief Converts a vector of strings into a single string where the substrings are
  510. * delimited by spaces.
  511. */
  512. BS_UTILITY_EXPORT WString toWString(const Vector<BansheeEngine::WString>& val);
  513. /**
  514. * @brief Converts a wide string to a narrow string.
  515. */
  516. BS_UTILITY_EXPORT String toString(const WString& source);
  517. /**
  518. * @brief Converts a wide string to a narrow string.
  519. */
  520. BS_UTILITY_EXPORT String toString(const wchar_t* source);
  521. /**
  522. * @brief Converts a float to a string.
  523. */
  524. BS_UTILITY_EXPORT String toString(float val, unsigned short precision = 6,
  525. unsigned short width = 0, char fill = ' ',
  526. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  527. /**
  528. * @brief Converts a double to a string.
  529. */
  530. BS_UTILITY_EXPORT String toString(double val, unsigned short precision = 6,
  531. unsigned short width = 0, char fill = ' ',
  532. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  533. /**
  534. * @brief Converts a Radian to a string.
  535. */
  536. BS_UTILITY_EXPORT String toString(Radian val, unsigned short precision = 6,
  537. unsigned short width = 0, char fill = ' ',
  538. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  539. /**
  540. * @brief Converts a Degree to a string.
  541. */
  542. BS_UTILITY_EXPORT String toString(Degree val, unsigned short precision = 6,
  543. unsigned short width = 0, char fill = ' ',
  544. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  545. /**
  546. * @brief Converts an int to a string.
  547. */
  548. BS_UTILITY_EXPORT String toString(int val, unsigned short width = 0,
  549. char fill = ' ',
  550. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  551. /**
  552. * @brief Converts an unsigned int to a string.
  553. */
  554. BS_UTILITY_EXPORT String toString(unsigned int val,
  555. unsigned short width = 0, char fill = ' ',
  556. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  557. /**
  558. * @brief Converts a 64bit int to a string.
  559. */
  560. BS_UTILITY_EXPORT String toString(INT64 val,
  561. unsigned short width = 0, char fill = ' ',
  562. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  563. /**
  564. * @brief Converts an 64bit unsigned int to a string.
  565. */
  566. BS_UTILITY_EXPORT String toString(UINT64 val,
  567. unsigned short width = 0, char fill = ' ',
  568. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  569. /**
  570. * @brief Converts a boolean to a string.
  571. *
  572. * @param val true to value.
  573. * @param yesNo (optional) If set to true, result is "yes" or "no" instead of "true" or "false".
  574. */
  575. BS_UTILITY_EXPORT String toString(bool val, bool yesNo = false);
  576. /**
  577. * @brief Converts a 2 dimensional vector to a string.
  578. *
  579. * @note Format is "x y".
  580. */
  581. BS_UTILITY_EXPORT String toString(const Vector2& val);
  582. /**
  583. * @brief Converts a 2 dimensional integer vector to a string.
  584. *
  585. * @note Format is "x y".
  586. */
  587. BS_UTILITY_EXPORT String toString(const Vector2I& val);
  588. /**
  589. * @brief Converts a 3 dimensional vector to a string.
  590. *
  591. * @note Format is "x y z".
  592. */
  593. BS_UTILITY_EXPORT String toString(const Vector3& val);
  594. /**
  595. * @brief Converts a 4 dimensional vector to a string.
  596. *
  597. * @note Format is "x y z w".
  598. */
  599. BS_UTILITY_EXPORT String toString(const Vector4& val);
  600. /**
  601. * @brief Converts a 3x3 matrix to a string.
  602. *
  603. * @note Format is "00 01 02 10 11 12 20 21 22".
  604. */
  605. BS_UTILITY_EXPORT String toString(const Matrix3& val);
  606. /**
  607. * @brief Converts a 4x4 matrix to a string.
  608. *
  609. * @note Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33".
  610. */
  611. BS_UTILITY_EXPORT String toString(const Matrix4& val);
  612. /**
  613. * @brief Converts a Quaternion to a string.
  614. *
  615. * @note Format is "w x y z".
  616. */
  617. BS_UTILITY_EXPORT String toString(const Quaternion& val);
  618. /**
  619. * @brief Converts a color to a string.
  620. *
  621. * @note Format is "r g b a".
  622. */
  623. BS_UTILITY_EXPORT String toString(const Color& val);
  624. /**
  625. * @brief Converts a vector of strings into a single string where the substrings are
  626. * delimited by spaces.
  627. */
  628. BS_UTILITY_EXPORT String toString(const Vector<BansheeEngine::String>& val);
  629. /**
  630. * @brief Converts a String to a float.
  631. *
  632. * @note 0.0f if the value could not be parsed, otherwise the numeric version of the string.
  633. */
  634. BS_UTILITY_EXPORT float parseFloat(const String& val, float defaultValue = 0);
  635. /**
  636. * @brief Converts a String to a whole number.
  637. *
  638. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  639. */
  640. BS_UTILITY_EXPORT int parseInt(const String& val, int defaultValue = 0);
  641. /**
  642. * @brief Converts a String to a whole number.
  643. *
  644. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  645. */
  646. BS_UTILITY_EXPORT unsigned int parseUnsignedInt(const String& val, unsigned int defaultValue = 0);
  647. /**
  648. * @brief Converts a String to a whole number.
  649. *
  650. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  651. */
  652. BS_UTILITY_EXPORT long parseLong(const String& val, long defaultValue = 0);
  653. /**
  654. * @brief Converts a String to a whole number.
  655. *
  656. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  657. */
  658. BS_UTILITY_EXPORT unsigned long parseUnsignedLong(const String& val, unsigned long defaultValue = 0);
  659. /**
  660. * @brief Converts a String to a boolean.
  661. *
  662. * @note Returns true if case-insensitive match of the start of the string
  663. * matches "true", "yes" or "1", false otherwise.
  664. */
  665. BS_UTILITY_EXPORT bool parseBool(const String& val, bool defaultValue = 0);
  666. /**
  667. * @brief Checks the String is a valid number value.
  668. */
  669. BS_UTILITY_EXPORT bool isNumber(const String& val);
  670. /**
  671. * @brief Converts a WString to a float.
  672. *
  673. * @note 0.0f if the value could not be parsed, otherwise the numeric version of the string.
  674. */
  675. BS_UTILITY_EXPORT float parseFloat(const WString& val, float defaultValue = 0);
  676. /**
  677. * @brief Converts a WString to a whole number.
  678. *
  679. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  680. */
  681. BS_UTILITY_EXPORT int parseInt(const WString& val, int defaultValue = 0);
  682. /**
  683. * @brief Converts a WString to a whole number.
  684. *
  685. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  686. */
  687. BS_UTILITY_EXPORT unsigned int parseUnsignedInt(const WString& val, unsigned int defaultValue = 0);
  688. /**
  689. * @brief Converts a WString to a whole number.
  690. *
  691. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  692. */
  693. BS_UTILITY_EXPORT long parseLong(const WString& val, long defaultValue = 0);
  694. /**
  695. * @brief Converts a WString to a whole number.
  696. *
  697. * @note 0 if the value could not be parsed, otherwise the numeric version of the string.
  698. */
  699. BS_UTILITY_EXPORT unsigned long parseUnsignedLong(const WString& val, unsigned long defaultValue = 0);
  700. /**
  701. * @brief Converts a WString to a boolean.
  702. *
  703. * @note Returns true if case-insensitive match of the start of the string
  704. * matches "true", "yes" or "1", false otherwise.
  705. */
  706. BS_UTILITY_EXPORT bool parseBool(const WString& val, bool defaultValue = 0);
  707. /**
  708. * @brief Checks the WString is a valid number value.
  709. */
  710. BS_UTILITY_EXPORT bool isNumber(const WString& val);
  711. /**
  712. * @brief Helper method that throws an exception regarding a data overflow.
  713. */
  714. void BS_UTILITY_EXPORT __string_throwDataOverflowException();
  715. /**
  716. * @brief RTTIPlainType specialization for String that allows strings be serialized as
  717. * value types.
  718. *
  719. * @see RTTIPlainType
  720. */
  721. template<> struct RTTIPlainType<String>
  722. {
  723. enum { id = 20 }; enum { hasDynamicSize = 1 };
  724. static void toMemory(const String& data, char* memory)
  725. {
  726. UINT32 size = getDynamicSize(data);
  727. memcpy(memory, &size, sizeof(UINT32));
  728. memory += sizeof(UINT32);
  729. size -= sizeof(UINT32);
  730. memcpy(memory, data.data(), size);
  731. }
  732. static UINT32 fromMemory(String& data, char* memory)
  733. {
  734. UINT32 size;
  735. memcpy(&size, memory, sizeof(UINT32));
  736. memory += sizeof(UINT32);
  737. UINT32 stringSize = size - sizeof(UINT32);
  738. char* buffer = (char*)bs_alloc<ScratchAlloc>(stringSize + 1);
  739. memcpy(buffer, memory, stringSize);
  740. buffer[stringSize] = '\0';
  741. data = String(buffer);
  742. bs_free<ScratchAlloc>(buffer);
  743. return size;
  744. }
  745. static UINT32 getDynamicSize(const String& data)
  746. {
  747. UINT64 dataSize = data.size() * sizeof(String::value_type) + sizeof(UINT32);
  748. #if BS_DEBUG_MODE
  749. if(dataSize > std::numeric_limits<UINT32>::max())
  750. {
  751. __string_throwDataOverflowException();
  752. }
  753. #endif
  754. return (UINT32)dataSize;
  755. }
  756. };
  757. /**
  758. * @brief RTTIPlainType specialization for WString that allows strings be serialized as
  759. * value types.
  760. *
  761. * @see RTTIPlainType
  762. */
  763. template<> struct RTTIPlainType<WString>
  764. {
  765. enum { id = TID_WString }; enum { hasDynamicSize = 1 };
  766. static void toMemory(const WString& data, char* memory)
  767. {
  768. UINT32 size = getDynamicSize(data);
  769. memcpy(memory, &size, sizeof(UINT32));
  770. memory += sizeof(UINT32);
  771. size -= sizeof(UINT32);
  772. memcpy(memory, data.data(), size);
  773. }
  774. static UINT32 fromMemory(WString& data, char* memory)
  775. {
  776. UINT32 size;
  777. memcpy(&size, memory, sizeof(UINT32));
  778. memory += sizeof(UINT32);
  779. UINT32 stringSize = size - sizeof(UINT32);
  780. WString::value_type* buffer = (WString::value_type*)bs_alloc<ScratchAlloc>(stringSize + sizeof(WString::value_type));
  781. memcpy(buffer, memory, stringSize);
  782. UINT32 numChars = stringSize / sizeof(WString::value_type);
  783. buffer[numChars] = L'\0';
  784. data = WString(buffer);
  785. bs_free<ScratchAlloc>(buffer);
  786. return size;
  787. }
  788. static UINT32 getDynamicSize(const WString& data)
  789. {
  790. UINT64 dataSize = data.size() * sizeof(WString::value_type) + sizeof(UINT32);
  791. #if BS_DEBUG_MODE
  792. if(dataSize > std::numeric_limits<UINT32>::max())
  793. {
  794. __string_throwDataOverflowException();
  795. }
  796. #endif
  797. return (UINT32)dataSize;
  798. }
  799. };
  800. }
  801. /**
  802. * @brief Hash value generator for SString.
  803. */
  804. template<>
  805. struct std::hash<BansheeEngine::String>
  806. {
  807. size_t operator()(const BansheeEngine::String& string) const
  808. {
  809. size_t hash = 0;
  810. for(size_t i = 0; i < string.size(); i++)
  811. hash = 65599 * hash + string[i];
  812. return hash ^ (hash >> 16);
  813. }
  814. };
  815. /**
  816. * @brief Hash value generator for WString.
  817. */
  818. template<>
  819. struct std::hash<BansheeEngine::WString>
  820. {
  821. size_t operator()(const BansheeEngine::WString& string) const
  822. {
  823. size_t hash = 0;
  824. for(size_t i = 0; i < string.size(); i++)
  825. hash = 65599 * hash + string[i];
  826. return hash ^ (hash >> 16);
  827. }
  828. };
  829. #include "BsHString.h"