CmString.h 28 KB

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