BsString.h 29 KB

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