BsString.h 29 KB

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