CmString.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #pragma once
  25. #include "CmPrerequisitesUtil.h"
  26. namespace CamelotFramework
  27. {
  28. template <typename T>
  29. struct BasicString
  30. {
  31. typedef typename std::basic_string<T, std::char_traits<T>, StdAlloc<T>> type;
  32. };
  33. template <typename T>
  34. struct BasicStringStream
  35. {
  36. typedef typename std::basic_stringstream<T, std::char_traits<T>, StdAlloc<T>> type;
  37. };
  38. typedef BasicString<wchar_t>::type WString;
  39. typedef BasicString<char>::type String;
  40. typedef BasicStringStream<wchar_t>::type WStringStream;
  41. typedef BasicStringStream<char>::type StringStream;
  42. /** \addtogroup Core
  43. * @{
  44. */
  45. /** \addtogroup General
  46. * @{
  47. */
  48. /** Utility class for manipulating Strings. */
  49. class CM_UTILITY_EXPORT StringUtil
  50. {
  51. public:
  52. /** Removes any whitespace characters, be it standard space or
  53. TABs and so on.
  54. @remarks
  55. The user may specify wether they want to trim only the
  56. beginning or the end of the String ( the default action is
  57. to trim both).
  58. */
  59. static void trim(String& str, bool left = true, bool right = true);
  60. /** Removes any whitespace characters, be it standard space or
  61. TABs and so on.
  62. @remarks
  63. The user may specify wether they want to trim only the
  64. beginning or the end of the String ( the default action is
  65. to trim both).
  66. */
  67. static void trim(WString& str, bool left = true, bool right = true);
  68. /** Removes specified characters.
  69. @remarks
  70. The user may specify wether they want to trim only the
  71. beginning or the end of the String ( the default action is
  72. to trim both).
  73. */
  74. static void trim(String& str, const String& delims, bool left = true, bool right = true);
  75. /** Removes specified characters.
  76. @remarks
  77. The user may specify wether they want to trim only the
  78. beginning or the end of the String ( the default action is
  79. to trim both).
  80. */
  81. static void trim(WString& str, const WString& delims, bool left = true, bool right = true);
  82. /** Returns a StringVector that contains all the substrings delimited
  83. by the characters in the passed <code>delims</code> argument.
  84. @param
  85. delims A list of delimiter characters to split by
  86. @param
  87. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  88. parameters is > 0, the splitting process will stop after this many splits, left to right.
  89. */
  90. static Vector<String>::type split(const String& str, const String& delims = "\t\n ", unsigned int maxSplits = 0);
  91. /** Returns a StringVector that contains all the substrings delimited
  92. by the characters in the passed <code>delims</code> argument.
  93. @param
  94. delims A list of delimiter characters to split by
  95. @param
  96. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  97. parameters is > 0, the splitting process will stop after this many splits, left to right.
  98. */
  99. static Vector<WString>::type split(const WString& str, const WString& delims = L"\t\n ", unsigned int maxSplits = 0);
  100. /** Returns a StringVector that contains all the substrings delimited
  101. by the characters in the passed <code>delims</code> argument,
  102. or in the <code>doubleDelims</code> argument, which is used to include (normal)
  103. delimeters in the tokenised string. For example, "strings like this".
  104. @param
  105. delims A list of delimiter characters to split by
  106. @param
  107. delims A list of double delimeters characters to tokenise by
  108. @param
  109. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  110. parameters is > 0, the splitting process will stop after this many splits, left to right.
  111. */
  112. static Vector<String>::type tokenise(const String& str, const String& delims = "\t\n ", const String& doubleDelims = "\"", unsigned int maxSplits = 0);
  113. /** Returns a StringVector that contains all the substrings delimited
  114. by the characters in the passed <code>delims</code> argument,
  115. or in the <code>doubleDelims</code> argument, which is used to include (normal)
  116. delimeters in the tokenised string. For example, "strings like this".
  117. @param
  118. delims A list of delimiter characters to split by
  119. @param
  120. delims A list of double delimeters characters to tokenise by
  121. @param
  122. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  123. parameters is > 0, the splitting process will stop after this many splits, left to right.
  124. */
  125. static Vector<WString>::type tokenise(const WString& str, const WString& delims = L"\t\n ", const WString& doubleDelims = L"\"", unsigned int maxSplits = 0);
  126. /** Lower-cases all the characters in the string.
  127. */
  128. static void toLowerCase(String& str);
  129. /** Lower-cases all the characters in the string.
  130. */
  131. static void toLowerCase(WString& str);
  132. /** Upper-cases all the characters in the string.
  133. */
  134. static void toUpperCase(String& str);
  135. /** Upper-cases all the characters in the string.
  136. */
  137. static void toUpperCase(WString& str);
  138. /** Returns whether the string begins with the pattern passed in.
  139. @param pattern The pattern to compare with.
  140. @param lowerCase If true, the start of the string will be lower cased before
  141. comparison, pattern should also be in lower case.
  142. */
  143. static bool startsWith(const String& str, const String& pattern, bool lowerCase = true);
  144. /** Returns whether the string begins with the pattern passed in.
  145. @param pattern The pattern to compare with.
  146. @param lowerCase If true, the start of the string will be lower cased before
  147. comparison, pattern should also be in lower case.
  148. */
  149. static bool startsWith(const WString& str, const WString& pattern, bool lowerCase = true);
  150. /** Returns whether the string ends with the pattern passed in.
  151. @param pattern The pattern to compare with.
  152. @param lowerCase If true, the end of the string will be lower cased before
  153. comparison, pattern should also be in lower case.
  154. */
  155. static bool endsWith(const String& str, const String& pattern, bool lowerCase = true);
  156. /** Returns whether the string ends with the pattern passed in.
  157. @param pattern The pattern to compare with.
  158. @param lowerCase If true, the end of the string will be lower cased before
  159. comparison, pattern should also be in lower case.
  160. */
  161. static bool endsWith(const WString& str, const WString& pattern, bool lowerCase = true);
  162. /** Simple pattern-matching routine allowing a wildcard pattern.
  163. @param str String to test
  164. @param pattern Pattern to match against; can include simple '*' wildcards
  165. @param caseSensitive Whether the match is case sensitive or not
  166. */
  167. static bool match(const String& str, const String& pattern, bool caseSensitive = true);
  168. /** Simple pattern-matching routine allowing a wildcard pattern.
  169. @param str String to test
  170. @param pattern Pattern to match against; can include simple '*' wildcards
  171. @param caseSensitive Whether the match is case sensitive or not
  172. */
  173. static bool match(const WString& str, const WString& pattern, bool caseSensitive = true);
  174. /** Replace all instances of a sub-string with a another sub-string.
  175. @param source Source string
  176. @param replaceWhat Sub-string to find and replace
  177. @param replaceWithWhat Sub-string to replace with (the new sub-string)
  178. @returns An updated string with the sub-string replaced
  179. */
  180. static const String replaceAll(const String& source, const String& replaceWhat, const String& replaceWithWhat);
  181. /** Replace all instances of a sub-string with a another sub-string.
  182. @param source Source string
  183. @param replaceWhat Sub-string to find and replace
  184. @param replaceWithWhat Sub-string to replace with (the new sub-string)
  185. @returns An updated string with the sub-string replaced
  186. */
  187. static const WString replaceAll(const WString& source, const WString& replaceWhat, const WString& replaceWithWhat);
  188. /// Constant blank string, useful for returning by ref where local does not exist
  189. static const String BLANK;
  190. /// Constant blank string, useful for returning by ref where local does not exist
  191. static const WString WBLANK;
  192. private:
  193. template <class T>
  194. static typename Vector<typename BasicString<T>::type>::type splitInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& delims, unsigned int maxSplits)
  195. {
  196. Vector<BasicString<T>::type>::type 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. // Use STL methods
  201. size_t start, pos;
  202. start = 0;
  203. do
  204. {
  205. pos = str.find_first_of(delims, start);
  206. if (pos == start)
  207. {
  208. // Do nothing
  209. start = pos + 1;
  210. }
  211. else if (pos == BasicString<T>::type::npos || (maxSplits && numSplits == maxSplits))
  212. {
  213. // Copy the rest of the string
  214. ret.push_back(str.substr(start));
  215. break;
  216. }
  217. else
  218. {
  219. // Copy up to delimiter
  220. ret.push_back(str.substr(start, pos - start));
  221. start = pos + 1;
  222. }
  223. // parse up to next real data
  224. start = str.find_first_not_of(delims, start);
  225. ++numSplits;
  226. } while (pos != BasicString<T>::type::npos);
  227. return ret;
  228. }
  229. template <class T>
  230. static typename Vector<typename BasicString<T>::type>::type tokeniseInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& singleDelims,
  231. const typename BasicString<T>::type& doubleDelims, unsigned int maxSplits)
  232. {
  233. Vector<BasicString<T>::type>::type ret;
  234. // Pre-allocate some space for performance
  235. ret.reserve(maxSplits ? maxSplits + 1 : 10); // 10 is guessed capacity for most case
  236. unsigned int numSplits = 0;
  237. BasicString<T>::type delims = singleDelims + doubleDelims;
  238. // Use STL methods
  239. size_t start, pos;
  240. T curDoubleDelim = 0;
  241. start = 0;
  242. do
  243. {
  244. if (curDoubleDelim != 0)
  245. {
  246. pos = str.find(curDoubleDelim, start);
  247. }
  248. else
  249. {
  250. pos = str.find_first_of(delims, start);
  251. }
  252. if (pos == start)
  253. {
  254. T curDelim = str.at(pos);
  255. if (doubleDelims.find_first_of(curDelim) != BasicString<T>::type::npos)
  256. {
  257. curDoubleDelim = curDelim;
  258. }
  259. // Do nothing
  260. start = pos + 1;
  261. }
  262. else if (pos == BasicString<T>::type::npos || (maxSplits && numSplits == maxSplits))
  263. {
  264. if (curDoubleDelim != 0)
  265. {
  266. //Missing closer. Warn or throw exception?
  267. }
  268. // Copy the rest of the string
  269. ret.push_back( str.substr(start) );
  270. break;
  271. }
  272. else
  273. {
  274. if (curDoubleDelim != 0)
  275. {
  276. curDoubleDelim = 0;
  277. }
  278. // Copy up to delimiter
  279. ret.push_back( str.substr(start, pos - start) );
  280. start = pos + 1;
  281. }
  282. if (curDoubleDelim == 0)
  283. {
  284. // parse up to next real data
  285. start = str.find_first_not_of(singleDelims, start);
  286. }
  287. ++numSplits;
  288. } while (pos != BasicString<T>::type::npos);
  289. return ret;
  290. }
  291. template <class T>
  292. static bool startsWithInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& pattern, bool lowerCase)
  293. {
  294. size_t thisLen = str.length();
  295. size_t patternLen = pattern.length();
  296. if (thisLen < patternLen || patternLen == 0)
  297. return false;
  298. BasicString<T>::type startOfThis = str.substr(0, patternLen);
  299. if (lowerCase)
  300. StringUtil::toLowerCase(startOfThis);
  301. return (startOfThis == pattern);
  302. }
  303. template <class T>
  304. static bool endsWithInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& pattern, bool lowerCase)
  305. {
  306. size_t thisLen = str.length();
  307. size_t patternLen = pattern.length();
  308. if (thisLen < patternLen || patternLen == 0)
  309. return false;
  310. BasicString<T>::type endOfThis = str.substr(thisLen - patternLen, patternLen);
  311. if (lowerCase)
  312. StringUtil::toLowerCase(endOfThis);
  313. return (endOfThis == pattern);
  314. }
  315. template <class T>
  316. static bool matchInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& pattern, bool caseSensitive)
  317. {
  318. BasicString<T>::type tmpStr = str;
  319. BasicString<T>::type tmpPattern = pattern;
  320. if (!caseSensitive)
  321. {
  322. StringUtil::toLowerCase(tmpStr);
  323. StringUtil::toLowerCase(tmpPattern);
  324. }
  325. BasicString<T>::type::const_iterator strIt = tmpStr.begin();
  326. BasicString<T>::type::const_iterator patIt = tmpPattern.begin();
  327. BasicString<T>::type::const_iterator lastWildCardIt = tmpPattern.end();
  328. while (strIt != tmpStr.end() && patIt != tmpPattern.end())
  329. {
  330. if (*patIt == '*')
  331. {
  332. lastWildCardIt = patIt;
  333. // Skip over looking for next character
  334. ++patIt;
  335. if (patIt == tmpPattern.end())
  336. {
  337. // Skip right to the end since * matches the entire rest of the string
  338. strIt = tmpStr.end();
  339. }
  340. else
  341. {
  342. // scan until we find next pattern character
  343. while(strIt != tmpStr.end() && *strIt != *patIt)
  344. ++strIt;
  345. }
  346. }
  347. else
  348. {
  349. if (*patIt != *strIt)
  350. {
  351. if (lastWildCardIt != tmpPattern.end())
  352. {
  353. // The last wildcard can match this incorrect sequence
  354. // rewind pattern to wildcard and keep searching
  355. patIt = lastWildCardIt;
  356. lastWildCardIt = tmpPattern.end();
  357. }
  358. else
  359. {
  360. // no wildwards left
  361. return false;
  362. }
  363. }
  364. else
  365. {
  366. ++patIt;
  367. ++strIt;
  368. }
  369. }
  370. }
  371. // If we reached the end of both the pattern and the string, we succeeded
  372. if (patIt == tmpPattern.end() && strIt == tmpStr.end())
  373. return true;
  374. else
  375. return false;
  376. }
  377. template <class T>
  378. static const typename BasicString<T>::type replaceAllInternal(const typename BasicString<T>::type& source,
  379. const typename BasicString<T>::type& replaceWhat, const typename BasicString<T>::type& replaceWithWhat)
  380. {
  381. BasicString<T>::type result = source;
  382. BasicString<T>::type::size_type pos = 0;
  383. while(1)
  384. {
  385. pos = result.find(replaceWhat,pos);
  386. if (pos == BasicString<T>::type::npos) break;
  387. result.replace(pos,replaceWhat.size(), replaceWithWhat);
  388. pos += replaceWithWhat.size();
  389. }
  390. return result;
  391. }
  392. };
  393. /**
  394. * @brief Converts a narrow string to a wide string.
  395. */
  396. CM_UTILITY_EXPORT WString toWString(const String& source);
  397. /** Converts a float to a WString. */
  398. CM_UTILITY_EXPORT WString toWString(float val, unsigned short precision = 6,
  399. unsigned short width = 0, char fill = ' ',
  400. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  401. /** Converts a double to a WString. */
  402. CM_UTILITY_EXPORT WString toWString(double val, unsigned short precision = 6,
  403. unsigned short width = 0, char fill = ' ',
  404. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  405. /** Converts a Radian to a WString. */
  406. CM_UTILITY_EXPORT WString toWString(Radian val, unsigned short precision = 6,
  407. unsigned short width = 0, char fill = ' ',
  408. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  409. /** Converts a Degree to a WString. */
  410. CM_UTILITY_EXPORT WString toWString(Degree val, unsigned short precision = 6,
  411. unsigned short width = 0, char fill = ' ',
  412. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  413. /** Converts an int to a WString. */
  414. CM_UTILITY_EXPORT WString toWString(int val, unsigned short width = 0,
  415. char fill = ' ',
  416. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  417. /** Converts an unsigned int to a WString. */
  418. CM_UTILITY_EXPORT WString toWString(unsigned int val,
  419. unsigned short width = 0, char fill = ' ',
  420. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  421. /** Converts a long to a WString. */
  422. CM_UTILITY_EXPORT WString toWString(long val,
  423. unsigned short width = 0, char fill = ' ',
  424. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  425. /** Converts an unsigned long to a WString. */
  426. CM_UTILITY_EXPORT WString toWString(unsigned long val,
  427. unsigned short width = 0, char fill = ' ',
  428. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  429. /** Converts an INT64 to a WString. */
  430. CM_UTILITY_EXPORT WString toWString(INT64 val,
  431. unsigned short width = 0, char fill = ' ',
  432. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  433. /** Converts an UINT64 to a WString. */
  434. CM_UTILITY_EXPORT WString toWString(UINT64 val,
  435. unsigned short width = 0, char fill = ' ',
  436. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  437. /** Converts a boolean to a WString.
  438. @param yesNo If set to true, result is 'yes' or 'no' instead of 'true' or 'false'
  439. */
  440. CM_UTILITY_EXPORT WString toWString(bool val, bool yesNo = false);
  441. /** Converts a Vector2 to a WString.
  442. @remarks
  443. Format is "x y" (i.e. 2x float values, space delimited)
  444. */
  445. CM_UTILITY_EXPORT WString toWString(const Vector2& val);
  446. /** Converts a Vector3 to a WString.
  447. @remarks
  448. Format is "x y z" (i.e. 3x float values, space delimited)
  449. */
  450. CM_UTILITY_EXPORT WString toWString(const Vector3& val);
  451. /** Converts a Vector4 to a WString.
  452. @remarks
  453. Format is "x y z w" (i.e. 4x float values, space delimited)
  454. */
  455. CM_UTILITY_EXPORT WString toWString(const Vector4& val);
  456. /** Converts a Matrix3 to a WString.
  457. @remarks
  458. Format is "00 01 02 10 11 12 20 21 22" where '01' means row 0 column 1 etc.
  459. */
  460. CM_UTILITY_EXPORT WString toWString(const Matrix3& val);
  461. /** Converts a Matrix4 to a WString.
  462. @remarks
  463. Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33" where
  464. '01' means row 0 column 1 etc.
  465. */
  466. CM_UTILITY_EXPORT WString toWString(const Matrix4& val);
  467. /** Converts a Quaternion to a WString.
  468. @remarks
  469. Format is "w x y z" (i.e. 4x float values, space delimited)
  470. */
  471. CM_UTILITY_EXPORT WString toWString(const Quaternion& val);
  472. /** Converts a ColourValue to a WString.
  473. @remarks
  474. Format is "r g b a" (i.e. 4x float values, space delimited).
  475. */
  476. CM_UTILITY_EXPORT WString toWString(const Color& val);
  477. /** Converts a StringVector to a WString.
  478. @remarks
  479. Strings must not contain spaces since space is used as a delimiter in
  480. the output.
  481. */
  482. CM_UTILITY_EXPORT WString toWString(const Vector<CamelotFramework::WString>::type& val);
  483. /**
  484. * @brief Converts a wide string to a narrow string.
  485. */
  486. CM_UTILITY_EXPORT String toString(const WString& source);
  487. /** Converts a float to a String. */
  488. CM_UTILITY_EXPORT String toString(float val, unsigned short precision = 6,
  489. unsigned short width = 0, char fill = ' ',
  490. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  491. /** Converts a double to a String. */
  492. CM_UTILITY_EXPORT String toString(double val, unsigned short precision = 6,
  493. unsigned short width = 0, char fill = ' ',
  494. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  495. /** Converts a Radian to a String. */
  496. CM_UTILITY_EXPORT String toString(Radian val, unsigned short precision = 6,
  497. unsigned short width = 0, char fill = ' ',
  498. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  499. /** Converts a Degree to a String. */
  500. CM_UTILITY_EXPORT String toString(Degree val, unsigned short precision = 6,
  501. unsigned short width = 0, char fill = ' ',
  502. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  503. /** Converts an int to a String. */
  504. CM_UTILITY_EXPORT String toString(int val, unsigned short width = 0,
  505. char fill = ' ',
  506. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  507. /** Converts an unsigned int to a String. */
  508. CM_UTILITY_EXPORT String toString(unsigned int val,
  509. unsigned short width = 0, char fill = ' ',
  510. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  511. /** Converts a long to a String. */
  512. CM_UTILITY_EXPORT String toString(long val,
  513. unsigned short width = 0, char fill = ' ',
  514. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  515. /** Converts an unsigned long to a String. */
  516. CM_UTILITY_EXPORT String toString(unsigned long val,
  517. unsigned short width = 0, char fill = ' ',
  518. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  519. /** Converts an unsigned long to a String. */
  520. CM_UTILITY_EXPORT String toString(INT64 val,
  521. unsigned short width = 0, char fill = ' ',
  522. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  523. /** Converts an unsigned long to a String. */
  524. CM_UTILITY_EXPORT String toString(UINT64 val,
  525. unsigned short width = 0, char fill = ' ',
  526. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  527. /** Converts a boolean to a String.
  528. @param yesNo If set to true, result is 'yes' or 'no' instead of 'true' or 'false'
  529. */
  530. CM_UTILITY_EXPORT String toString(bool val, bool yesNo = false);
  531. /** Converts a Vector2 to a String.
  532. @remarks
  533. Format is "x y" (i.e. 2x float values, space delimited)
  534. */
  535. CM_UTILITY_EXPORT String toString(const Vector2& val);
  536. /** Converts a Vector3 to a String.
  537. @remarks
  538. Format is "x y z" (i.e. 3x float values, space delimited)
  539. */
  540. CM_UTILITY_EXPORT String toString(const Vector3& val);
  541. /** Converts a Vector4 to a String.
  542. @remarks
  543. Format is "x y z w" (i.e. 4x float values, space delimited)
  544. */
  545. CM_UTILITY_EXPORT String toString(const Vector4& val);
  546. /** Converts a Matrix3 to a String.
  547. @remarks
  548. Format is "00 01 02 10 11 12 20 21 22" where '01' means row 0 column 1 etc.
  549. */
  550. CM_UTILITY_EXPORT String toString(const Matrix3& val);
  551. /** Converts a Matrix4 to a String.
  552. @remarks
  553. Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33" where
  554. '01' means row 0 column 1 etc.
  555. */
  556. CM_UTILITY_EXPORT String toString(const Matrix4& val);
  557. /** Converts a Quaternion to a String.
  558. @remarks
  559. Format is "w x y z" (i.e. 4x float values, space delimited)
  560. */
  561. CM_UTILITY_EXPORT String toString(const Quaternion& val);
  562. /** Converts a ColourValue to a String.
  563. @remarks
  564. Format is "r g b a" (i.e. 4x float values, space delimited).
  565. */
  566. CM_UTILITY_EXPORT String toString(const Color& val);
  567. /** Converts a StringVector to a string.
  568. @remarks
  569. Strings must not contain spaces since space is used as a delimiter in
  570. the output.
  571. */
  572. CM_UTILITY_EXPORT String toString(const Vector<CamelotFramework::String>::type& val);
  573. /** Converts a String to a float.
  574. @returns
  575. 0.0 if the value could not be parsed, otherwise the float version of the String.
  576. */
  577. CM_UTILITY_EXPORT float parseFloat(const String& val, float defaultValue = 0);
  578. /** Converts a String to a whole number.
  579. @returns
  580. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  581. */
  582. CM_UTILITY_EXPORT int parseInt(const String& val, int defaultValue = 0);
  583. /** Converts a String to a whole number.
  584. @returns
  585. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  586. */
  587. CM_UTILITY_EXPORT unsigned int parseUnsignedInt(const String& val, unsigned int defaultValue = 0);
  588. /** Converts a String to a whole number.
  589. @returns
  590. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  591. */
  592. CM_UTILITY_EXPORT long parseLong(const String& val, long defaultValue = 0);
  593. /** Converts a String to a whole number.
  594. @returns
  595. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  596. */
  597. CM_UTILITY_EXPORT unsigned long parseUnsignedLong(const String& val, unsigned long defaultValue = 0);
  598. /** Converts a String to a boolean.
  599. @remarks
  600. Returns true if case-insensitive match of the start of the string
  601. matches "true", "yes" or "1", false otherwise.
  602. */
  603. CM_UTILITY_EXPORT bool parseBool(const String& val, bool defaultValue = 0);
  604. /** Checks the String is a valid number value. */
  605. CM_UTILITY_EXPORT bool isNumber(const String& val);
  606. /** Converts a String to a float.
  607. @returns
  608. 0.0 if the value could not be parsed, otherwise the float version of the String.
  609. */
  610. CM_UTILITY_EXPORT float parseFloat(const WString& val, float defaultValue = 0);
  611. /** Converts a String to a whole number.
  612. @returns
  613. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  614. */
  615. CM_UTILITY_EXPORT int parseInt(const WString& val, int defaultValue = 0);
  616. /** Converts a String to a whole number.
  617. @returns
  618. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  619. */
  620. CM_UTILITY_EXPORT unsigned int parseUnsignedInt(const WString& val, unsigned int defaultValue = 0);
  621. /** Converts a String to a whole number.
  622. @returns
  623. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  624. */
  625. CM_UTILITY_EXPORT long parseLong(const WString& val, long defaultValue = 0);
  626. /** Converts a String to a whole number.
  627. @returns
  628. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  629. */
  630. CM_UTILITY_EXPORT unsigned long parseUnsignedLong(const WString& val, unsigned long defaultValue = 0);
  631. /** Converts a String to a boolean.
  632. @remarks
  633. Returns true if case-insensitive match of the start of the string
  634. matches "true", "yes" or "1", false otherwise.
  635. */
  636. CM_UTILITY_EXPORT bool parseBool(const WString& val, bool defaultValue = 0);
  637. /** Checks the String is a valid number value. */
  638. CM_UTILITY_EXPORT bool isNumber(const WString& val);
  639. /** @} */
  640. void CM_UTILITY_EXPORT __string_throwDataOverflowException();
  641. /**
  642. * @brief Strings need to copy their data in a slightly more intricate way than just memcpy.
  643. */
  644. template<> struct RTTIPlainType<String>
  645. {
  646. enum { id = 20 }; enum { hasDynamicSize = 1 };
  647. static void toMemory(const String& data, char* memory)
  648. {
  649. UINT32 size = getDynamicSize(data);
  650. memcpy(memory, &size, sizeof(UINT32));
  651. memory += sizeof(UINT32);
  652. size -= sizeof(UINT32);
  653. memcpy(memory, data.data(), size);
  654. }
  655. static UINT32 fromMemory(String& data, char* memory)
  656. {
  657. UINT32 size;
  658. memcpy(&size, memory, sizeof(UINT32));
  659. memory += sizeof(UINT32);
  660. UINT32 stringSize = size - sizeof(UINT32);
  661. char* buffer = (char*)cm_alloc<ScratchAlloc>(stringSize + 1);
  662. memcpy(buffer, memory, stringSize);
  663. buffer[stringSize] = '\0';
  664. data = String(buffer);
  665. cm_free<ScratchAlloc>(buffer);
  666. return size;
  667. }
  668. static UINT32 getDynamicSize(const String& data)
  669. {
  670. UINT64 dataSize = data.size() * sizeof(String::value_type) + sizeof(UINT32);
  671. #if CM_DEBUG_MODE
  672. if(dataSize > std::numeric_limits<UINT32>::max())
  673. {
  674. __string_throwDataOverflowException();
  675. }
  676. #endif
  677. return (UINT32)dataSize;
  678. }
  679. };
  680. /**
  681. * @brief Strings need to copy their data in a slightly more intricate way than just memcpy.
  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*)cm_alloc<ScratchAlloc>(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. cm_free<ScratchAlloc>(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 CM_DEBUG_MODE
  712. if(dataSize > std::numeric_limits<UINT32>::max())
  713. {
  714. __string_throwDataOverflowException();
  715. }
  716. #endif
  717. return (UINT32)dataSize;
  718. }
  719. };
  720. } // namespace CamelotFramework
  721. template<>
  722. struct std::hash<CamelotFramework::String>
  723. {
  724. size_t operator()(const CamelotFramework::String& string) const
  725. {
  726. size_t hash = 0;
  727. for(size_t i = 0; i < string.size(); i++)
  728. hash = 65599 * hash + string[i];
  729. return hash ^ (hash >> 16);
  730. }
  731. };
  732. template<>
  733. struct std::hash<CamelotFramework::WString>
  734. {
  735. size_t operator()(const CamelotFramework::WString& string) const
  736. {
  737. size_t hash = 0;
  738. for(size_t i = 0; i < string.size(); i++)
  739. hash = 65599 * hash + string[i];
  740. return hash ^ (hash >> 16);
  741. }
  742. };
  743. #include "CmHString.h"