BsString.h 28 KB

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