2
0

BsString.h 29 KB

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