BsString.h 29 KB

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