CmString.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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. /** Returns a StringVector that contains all the substrings delimited
  69. by the characters in the passed <code>delims</code> argument.
  70. @param
  71. delims A list of delimiter characters to split by
  72. @param
  73. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  74. parameters is > 0, the splitting process will stop after this many splits, left to right.
  75. */
  76. static Vector<String>::type split(const String& str, const String& delims = "\t\n ", unsigned int maxSplits = 0);
  77. /** Returns a StringVector that contains all the substrings delimited
  78. by the characters in the passed <code>delims</code> argument.
  79. @param
  80. delims A list of delimiter characters to split by
  81. @param
  82. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  83. parameters is > 0, the splitting process will stop after this many splits, left to right.
  84. */
  85. static Vector<WString>::type split(const WString& str, const WString& delims = L"\t\n ", unsigned int maxSplits = 0);
  86. /** Returns a StringVector that contains all the substrings delimited
  87. by the characters in the passed <code>delims</code> argument,
  88. or in the <code>doubleDelims</code> argument, which is used to include (normal)
  89. delimeters in the tokenised string. For example, "strings like this".
  90. @param
  91. delims A list of delimiter characters to split by
  92. @param
  93. delims A list of double delimeters characters to tokenise by
  94. @param
  95. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  96. parameters is > 0, the splitting process will stop after this many splits, left to right.
  97. */
  98. static Vector<String>::type tokenise(const String& str, const String& delims = "\t\n ", const String& doubleDelims = "\"", unsigned int maxSplits = 0);
  99. /** Returns a StringVector that contains all the substrings delimited
  100. by the characters in the passed <code>delims</code> argument,
  101. or in the <code>doubleDelims</code> argument, which is used to include (normal)
  102. delimeters in the tokenised string. For example, "strings like this".
  103. @param
  104. delims A list of delimiter characters to split by
  105. @param
  106. delims A list of double delimeters characters to tokenise by
  107. @param
  108. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  109. parameters is > 0, the splitting process will stop after this many splits, left to right.
  110. */
  111. static Vector<WString>::type tokenise(const WString& str, const WString& delims = L"\t\n ", const WString& doubleDelims = L"\"", unsigned int maxSplits = 0);
  112. /** Lower-cases all the characters in the string.
  113. */
  114. static void toLowerCase(String& str);
  115. /** Lower-cases all the characters in the string.
  116. */
  117. static void toLowerCase(WString& str);
  118. /** Upper-cases all the characters in the string.
  119. */
  120. static void toUpperCase(String& str);
  121. /** Upper-cases all the characters in the string.
  122. */
  123. static void toUpperCase(WString& str);
  124. /** Returns whether the string begins with the pattern passed in.
  125. @param pattern The pattern to compare with.
  126. @param lowerCase If true, the start of the string will be lower cased before
  127. comparison, pattern should also be in lower case.
  128. */
  129. static bool startsWith(const String& str, const String& pattern, bool lowerCase = true);
  130. /** Returns whether the string begins with the pattern passed in.
  131. @param pattern The pattern to compare with.
  132. @param lowerCase If true, the start of the string will be lower cased before
  133. comparison, pattern should also be in lower case.
  134. */
  135. static bool startsWith(const WString& str, const WString& pattern, bool lowerCase = true);
  136. /** Returns whether the string ends with the pattern passed in.
  137. @param pattern The pattern to compare with.
  138. @param lowerCase If true, the end of the string will be lower cased before
  139. comparison, pattern should also be in lower case.
  140. */
  141. static bool endsWith(const String& str, const String& pattern, bool lowerCase = true);
  142. /** Returns whether the string ends with the pattern passed in.
  143. @param pattern The pattern to compare with.
  144. @param lowerCase If true, the end of the string will be lower cased before
  145. comparison, pattern should also be in lower case.
  146. */
  147. static bool endsWith(const WString& str, const WString& pattern, bool lowerCase = true);
  148. /** Simple pattern-matching routine allowing a wildcard pattern.
  149. @param str String to test
  150. @param pattern Pattern to match against; can include simple '*' wildcards
  151. @param caseSensitive Whether the match is case sensitive or not
  152. */
  153. static bool match(const String& str, const String& pattern, bool caseSensitive = true);
  154. /** Simple pattern-matching routine allowing a wildcard pattern.
  155. @param str String to test
  156. @param pattern Pattern to match against; can include simple '*' wildcards
  157. @param caseSensitive Whether the match is case sensitive or not
  158. */
  159. static bool match(const WString& str, const WString& pattern, bool caseSensitive = true);
  160. /** Replace all instances of a sub-string with a another sub-string.
  161. @param source Source string
  162. @param replaceWhat Sub-string to find and replace
  163. @param replaceWithWhat Sub-string to replace with (the new sub-string)
  164. @returns An updated string with the sub-string replaced
  165. */
  166. static const String replaceAll(const String& source, const String& replaceWhat, const String& replaceWithWhat);
  167. /** Replace all instances of a sub-string with a another sub-string.
  168. @param source Source string
  169. @param replaceWhat Sub-string to find and replace
  170. @param replaceWithWhat Sub-string to replace with (the new sub-string)
  171. @returns An updated string with the sub-string replaced
  172. */
  173. static const WString replaceAll(const WString& source, const WString& replaceWhat, const WString& replaceWithWhat);
  174. /// Constant blank string, useful for returning by ref where local does not exist
  175. static const String BLANK;
  176. /// Constant blank string, useful for returning by ref where local does not exist
  177. static const WString WBLANK;
  178. private:
  179. template <class T>
  180. static typename Vector<typename BasicString<T>::type>::type splitInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& delims, unsigned int maxSplits)
  181. {
  182. Vector<BasicString<T>::type>::type ret;
  183. // Pre-allocate some space for performance
  184. ret.reserve(maxSplits ? maxSplits+1 : 10); // 10 is guessed capacity for most case
  185. unsigned int numSplits = 0;
  186. // Use STL methods
  187. size_t start, pos;
  188. start = 0;
  189. do
  190. {
  191. pos = str.find_first_of(delims, start);
  192. if (pos == start)
  193. {
  194. // Do nothing
  195. start = pos + 1;
  196. }
  197. else if (pos == BasicString<T>::type::npos || (maxSplits && numSplits == maxSplits))
  198. {
  199. // Copy the rest of the string
  200. ret.push_back(str.substr(start));
  201. break;
  202. }
  203. else
  204. {
  205. // Copy up to delimiter
  206. ret.push_back(str.substr(start, pos - start));
  207. start = pos + 1;
  208. }
  209. // parse up to next real data
  210. start = str.find_first_not_of(delims, start);
  211. ++numSplits;
  212. } while (pos != BasicString<T>::type::npos);
  213. return ret;
  214. }
  215. template <class T>
  216. static typename Vector<typename BasicString<T>::type>::type tokeniseInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& singleDelims,
  217. const typename BasicString<T>::type& doubleDelims, unsigned int maxSplits)
  218. {
  219. Vector<BasicString<T>::type>::type ret;
  220. // Pre-allocate some space for performance
  221. ret.reserve(maxSplits ? maxSplits + 1 : 10); // 10 is guessed capacity for most case
  222. unsigned int numSplits = 0;
  223. BasicString<T>::type delims = singleDelims + doubleDelims;
  224. // Use STL methods
  225. size_t start, pos;
  226. T curDoubleDelim = 0;
  227. start = 0;
  228. do
  229. {
  230. if (curDoubleDelim != 0)
  231. {
  232. pos = str.find(curDoubleDelim, start);
  233. }
  234. else
  235. {
  236. pos = str.find_first_of(delims, start);
  237. }
  238. if (pos == start)
  239. {
  240. T curDelim = str.at(pos);
  241. if (doubleDelims.find_first_of(curDelim) != BasicString<T>::type::npos)
  242. {
  243. curDoubleDelim = curDelim;
  244. }
  245. // Do nothing
  246. start = pos + 1;
  247. }
  248. else if (pos == BasicString<T>::type::npos || (maxSplits && numSplits == maxSplits))
  249. {
  250. if (curDoubleDelim != 0)
  251. {
  252. //Missing closer. Warn or throw exception?
  253. }
  254. // Copy the rest of the string
  255. ret.push_back( str.substr(start) );
  256. break;
  257. }
  258. else
  259. {
  260. if (curDoubleDelim != 0)
  261. {
  262. curDoubleDelim = 0;
  263. }
  264. // Copy up to delimiter
  265. ret.push_back( str.substr(start, pos - start) );
  266. start = pos + 1;
  267. }
  268. if (curDoubleDelim == 0)
  269. {
  270. // parse up to next real data
  271. start = str.find_first_not_of(singleDelims, start);
  272. }
  273. ++numSplits;
  274. } while (pos != BasicString<T>::type::npos);
  275. return ret;
  276. }
  277. template <class T>
  278. static bool startsWithInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& pattern, bool lowerCase)
  279. {
  280. size_t thisLen = str.length();
  281. size_t patternLen = pattern.length();
  282. if (thisLen < patternLen || patternLen == 0)
  283. return false;
  284. BasicString<T>::type startOfThis = str.substr(0, patternLen);
  285. if (lowerCase)
  286. StringUtil::toLowerCase(startOfThis);
  287. return (startOfThis == pattern);
  288. }
  289. template <class T>
  290. static bool endsWithInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& pattern, bool lowerCase)
  291. {
  292. size_t thisLen = str.length();
  293. size_t patternLen = pattern.length();
  294. if (thisLen < patternLen || patternLen == 0)
  295. return false;
  296. BasicString<T>::type endOfThis = str.substr(thisLen - patternLen, patternLen);
  297. if (lowerCase)
  298. StringUtil::toLowerCase(endOfThis);
  299. return (endOfThis == pattern);
  300. }
  301. template <class T>
  302. static bool matchInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& pattern, bool caseSensitive)
  303. {
  304. BasicString<T>::type tmpStr = str;
  305. BasicString<T>::type tmpPattern = pattern;
  306. if (!caseSensitive)
  307. {
  308. StringUtil::toLowerCase(tmpStr);
  309. StringUtil::toLowerCase(tmpPattern);
  310. }
  311. BasicString<T>::type::const_iterator strIt = tmpStr.begin();
  312. BasicString<T>::type::const_iterator patIt = tmpPattern.begin();
  313. BasicString<T>::type::const_iterator lastWildCardIt = tmpPattern.end();
  314. while (strIt != tmpStr.end() && patIt != tmpPattern.end())
  315. {
  316. if (*patIt == '*')
  317. {
  318. lastWildCardIt = patIt;
  319. // Skip over looking for next character
  320. ++patIt;
  321. if (patIt == tmpPattern.end())
  322. {
  323. // Skip right to the end since * matches the entire rest of the string
  324. strIt = tmpStr.end();
  325. }
  326. else
  327. {
  328. // scan until we find next pattern character
  329. while(strIt != tmpStr.end() && *strIt != *patIt)
  330. ++strIt;
  331. }
  332. }
  333. else
  334. {
  335. if (*patIt != *strIt)
  336. {
  337. if (lastWildCardIt != tmpPattern.end())
  338. {
  339. // The last wildcard can match this incorrect sequence
  340. // rewind pattern to wildcard and keep searching
  341. patIt = lastWildCardIt;
  342. lastWildCardIt = tmpPattern.end();
  343. }
  344. else
  345. {
  346. // no wildwards left
  347. return false;
  348. }
  349. }
  350. else
  351. {
  352. ++patIt;
  353. ++strIt;
  354. }
  355. }
  356. }
  357. // If we reached the end of both the pattern and the string, we succeeded
  358. if (patIt == tmpPattern.end() && strIt == tmpStr.end())
  359. return true;
  360. else
  361. return false;
  362. }
  363. template <class T>
  364. static const typename BasicString<T>::type replaceAllInternal(const typename BasicString<T>::type& source,
  365. const typename BasicString<T>::type& replaceWhat, const typename BasicString<T>::type& replaceWithWhat)
  366. {
  367. BasicString<T>::type result = source;
  368. BasicString<T>::type::size_type pos = 0;
  369. while(1)
  370. {
  371. pos = result.find(replaceWhat,pos);
  372. if (pos == BasicString<T>::type::npos) break;
  373. result.replace(pos,replaceWhat.size(), replaceWithWhat);
  374. pos += replaceWithWhat.size();
  375. }
  376. return result;
  377. }
  378. };
  379. /**
  380. * @brief Converts a narrow string to a wide string.
  381. */
  382. CM_UTILITY_EXPORT WString toWString(const String& source);
  383. /** Converts a float to a WString. */
  384. CM_UTILITY_EXPORT WString toWString(float val, unsigned short precision = 6,
  385. unsigned short width = 0, char fill = ' ',
  386. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  387. /** Converts a double to a WString. */
  388. CM_UTILITY_EXPORT WString toWString(double val, unsigned short precision = 6,
  389. unsigned short width = 0, char fill = ' ',
  390. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  391. /** Converts a Radian to a WString. */
  392. CM_UTILITY_EXPORT WString toWString(Radian val, unsigned short precision = 6,
  393. unsigned short width = 0, char fill = ' ',
  394. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  395. /** Converts a Degree to a WString. */
  396. CM_UTILITY_EXPORT WString toWString(Degree val, unsigned short precision = 6,
  397. unsigned short width = 0, char fill = ' ',
  398. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  399. /** Converts an int to a WString. */
  400. CM_UTILITY_EXPORT WString toWString(int val, unsigned short width = 0,
  401. char fill = ' ',
  402. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  403. /** Converts an unsigned int to a WString. */
  404. CM_UTILITY_EXPORT WString toWString(unsigned int val,
  405. unsigned short width = 0, char fill = ' ',
  406. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  407. /** Converts a long to a WString. */
  408. CM_UTILITY_EXPORT WString toWString(long val,
  409. unsigned short width = 0, char fill = ' ',
  410. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  411. /** Converts an unsigned long to a WString. */
  412. CM_UTILITY_EXPORT WString toWString(unsigned long val,
  413. unsigned short width = 0, char fill = ' ',
  414. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  415. /** Converts an INT64 to a WString. */
  416. CM_UTILITY_EXPORT WString toWString(INT64 val,
  417. unsigned short width = 0, char fill = ' ',
  418. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  419. /** Converts an UINT64 to a WString. */
  420. CM_UTILITY_EXPORT WString toWString(UINT64 val,
  421. unsigned short width = 0, char fill = ' ',
  422. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  423. /** Converts a boolean to a WString.
  424. @param yesNo If set to true, result is 'yes' or 'no' instead of 'true' or 'false'
  425. */
  426. CM_UTILITY_EXPORT WString toWString(bool val, bool yesNo = false);
  427. /** Converts a Vector2 to a WString.
  428. @remarks
  429. Format is "x y" (i.e. 2x float values, space delimited)
  430. */
  431. CM_UTILITY_EXPORT WString toWString(const Vector2& val);
  432. /** Converts a Vector3 to a WString.
  433. @remarks
  434. Format is "x y z" (i.e. 3x float values, space delimited)
  435. */
  436. CM_UTILITY_EXPORT WString toWString(const Vector3& val);
  437. /** Converts a Vector4 to a WString.
  438. @remarks
  439. Format is "x y z w" (i.e. 4x float values, space delimited)
  440. */
  441. CM_UTILITY_EXPORT WString toWString(const Vector4& val);
  442. /** Converts a Matrix3 to a WString.
  443. @remarks
  444. Format is "00 01 02 10 11 12 20 21 22" where '01' means row 0 column 1 etc.
  445. */
  446. CM_UTILITY_EXPORT WString toWString(const Matrix3& val);
  447. /** Converts a Matrix4 to a WString.
  448. @remarks
  449. Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33" where
  450. '01' means row 0 column 1 etc.
  451. */
  452. CM_UTILITY_EXPORT WString toWString(const Matrix4& val);
  453. /** Converts a Quaternion to a WString.
  454. @remarks
  455. Format is "w x y z" (i.e. 4x float values, space delimited)
  456. */
  457. CM_UTILITY_EXPORT WString toWString(const Quaternion& val);
  458. /** Converts a ColourValue to a WString.
  459. @remarks
  460. Format is "r g b a" (i.e. 4x float values, space delimited).
  461. */
  462. CM_UTILITY_EXPORT WString toWString(const Color& val);
  463. /** Converts a StringVector to a WString.
  464. @remarks
  465. Strings must not contain spaces since space is used as a delimiter in
  466. the output.
  467. */
  468. CM_UTILITY_EXPORT WString toWString(const Vector<CamelotFramework::WString>::type& val);
  469. /**
  470. * @brief Converts a wide string to a narrow string.
  471. */
  472. CM_UTILITY_EXPORT String toString(const WString& source);
  473. /** Converts a float to a String. */
  474. CM_UTILITY_EXPORT String toString(float val, unsigned short precision = 6,
  475. unsigned short width = 0, char fill = ' ',
  476. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  477. /** Converts a double to a String. */
  478. CM_UTILITY_EXPORT String toString(double val, unsigned short precision = 6,
  479. unsigned short width = 0, char fill = ' ',
  480. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  481. /** Converts a Radian to a String. */
  482. CM_UTILITY_EXPORT String toString(Radian val, unsigned short precision = 6,
  483. unsigned short width = 0, char fill = ' ',
  484. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  485. /** Converts a Degree to a String. */
  486. CM_UTILITY_EXPORT String toString(Degree val, unsigned short precision = 6,
  487. unsigned short width = 0, char fill = ' ',
  488. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  489. /** Converts an int to a String. */
  490. CM_UTILITY_EXPORT String toString(int val, unsigned short width = 0,
  491. char fill = ' ',
  492. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  493. /** Converts an unsigned int to a String. */
  494. CM_UTILITY_EXPORT String toString(unsigned int val,
  495. unsigned short width = 0, char fill = ' ',
  496. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  497. /** Converts a long to a String. */
  498. CM_UTILITY_EXPORT String toString(long val,
  499. unsigned short width = 0, char fill = ' ',
  500. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  501. /** Converts an unsigned long to a String. */
  502. CM_UTILITY_EXPORT String toString(unsigned long val,
  503. unsigned short width = 0, char fill = ' ',
  504. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  505. /** Converts an unsigned long to a String. */
  506. CM_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 unsigned long to a String. */
  510. CM_UTILITY_EXPORT String toString(UINT64 val,
  511. unsigned short width = 0, char fill = ' ',
  512. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  513. /** Converts a boolean to a String.
  514. @param yesNo If set to true, result is 'yes' or 'no' instead of 'true' or 'false'
  515. */
  516. CM_UTILITY_EXPORT String toString(bool val, bool yesNo = false);
  517. /** Converts a Vector2 to a String.
  518. @remarks
  519. Format is "x y" (i.e. 2x float values, space delimited)
  520. */
  521. CM_UTILITY_EXPORT String toString(const Vector2& val);
  522. /** Converts a Vector3 to a String.
  523. @remarks
  524. Format is "x y z" (i.e. 3x float values, space delimited)
  525. */
  526. CM_UTILITY_EXPORT String toString(const Vector3& val);
  527. /** Converts a Vector4 to a String.
  528. @remarks
  529. Format is "x y z w" (i.e. 4x float values, space delimited)
  530. */
  531. CM_UTILITY_EXPORT String toString(const Vector4& val);
  532. /** Converts a Matrix3 to a String.
  533. @remarks
  534. Format is "00 01 02 10 11 12 20 21 22" where '01' means row 0 column 1 etc.
  535. */
  536. CM_UTILITY_EXPORT String toString(const Matrix3& val);
  537. /** Converts a Matrix4 to a String.
  538. @remarks
  539. Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33" where
  540. '01' means row 0 column 1 etc.
  541. */
  542. CM_UTILITY_EXPORT String toString(const Matrix4& val);
  543. /** Converts a Quaternion to a String.
  544. @remarks
  545. Format is "w x y z" (i.e. 4x float values, space delimited)
  546. */
  547. CM_UTILITY_EXPORT String toString(const Quaternion& val);
  548. /** Converts a ColourValue to a String.
  549. @remarks
  550. Format is "r g b a" (i.e. 4x float values, space delimited).
  551. */
  552. CM_UTILITY_EXPORT String toString(const Color& val);
  553. /** Converts a StringVector to a string.
  554. @remarks
  555. Strings must not contain spaces since space is used as a delimiter in
  556. the output.
  557. */
  558. CM_UTILITY_EXPORT String toString(const Vector<CamelotFramework::String>::type& val);
  559. /** Converts a String to a float.
  560. @returns
  561. 0.0 if the value could not be parsed, otherwise the float version of the String.
  562. */
  563. CM_UTILITY_EXPORT float parseFloat(const String& val, float defaultValue = 0);
  564. /** Converts a String to a whole number.
  565. @returns
  566. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  567. */
  568. CM_UTILITY_EXPORT int parseInt(const String& val, int defaultValue = 0);
  569. /** Converts a String to a whole number.
  570. @returns
  571. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  572. */
  573. CM_UTILITY_EXPORT unsigned int parseUnsignedInt(const String& val, unsigned int defaultValue = 0);
  574. /** Converts a String to a whole number.
  575. @returns
  576. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  577. */
  578. CM_UTILITY_EXPORT long parseLong(const String& val, long defaultValue = 0);
  579. /** Converts a String to a whole number.
  580. @returns
  581. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  582. */
  583. CM_UTILITY_EXPORT unsigned long parseUnsignedLong(const String& val, unsigned long defaultValue = 0);
  584. /** Converts a String to a boolean.
  585. @remarks
  586. Returns true if case-insensitive match of the start of the string
  587. matches "true", "yes" or "1", false otherwise.
  588. */
  589. CM_UTILITY_EXPORT bool parseBool(const String& val, bool defaultValue = 0);
  590. /** Checks the String is a valid number value. */
  591. CM_UTILITY_EXPORT bool isNumber(const String& val);
  592. /** Converts a String to a float.
  593. @returns
  594. 0.0 if the value could not be parsed, otherwise the float version of the String.
  595. */
  596. CM_UTILITY_EXPORT float parseFloat(const WString& val, float defaultValue = 0);
  597. /** Converts a String to a whole number.
  598. @returns
  599. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  600. */
  601. CM_UTILITY_EXPORT int parseInt(const WString& val, int defaultValue = 0);
  602. /** Converts a String to a whole number.
  603. @returns
  604. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  605. */
  606. CM_UTILITY_EXPORT unsigned int parseUnsignedInt(const WString& val, unsigned int defaultValue = 0);
  607. /** Converts a String to a whole number.
  608. @returns
  609. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  610. */
  611. CM_UTILITY_EXPORT long parseLong(const WString& val, long defaultValue = 0);
  612. /** Converts a String to a whole number.
  613. @returns
  614. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  615. */
  616. CM_UTILITY_EXPORT unsigned long parseUnsignedLong(const WString& val, unsigned long defaultValue = 0);
  617. /** Converts a String to a boolean.
  618. @remarks
  619. Returns true if case-insensitive match of the start of the string
  620. matches "true", "yes" or "1", false otherwise.
  621. */
  622. CM_UTILITY_EXPORT bool parseBool(const WString& val, bool defaultValue = 0);
  623. /** Checks the String is a valid number value. */
  624. CM_UTILITY_EXPORT bool isNumber(const WString& val);
  625. /** @} */
  626. void CM_UTILITY_EXPORT __string_throwDataOverflowException();
  627. /**
  628. * @brief Strings need to copy their data in a slightly more intricate way than just memcpy.
  629. */
  630. template<> struct RTTIPlainType<String>
  631. {
  632. enum { id = 20 }; enum { hasDynamicSize = 1 };
  633. static void toMemory(const String& data, char* memory)
  634. {
  635. UINT32 size = getDynamicSize(data);
  636. memcpy(memory, &size, sizeof(UINT32));
  637. memory += sizeof(UINT32);
  638. size -= sizeof(UINT32);
  639. memcpy(memory, data.data(), size);
  640. }
  641. static UINT32 fromMemory(String& data, char* memory)
  642. {
  643. UINT32 size;
  644. memcpy(&size, memory, sizeof(UINT32));
  645. memory += sizeof(UINT32);
  646. UINT32 stringSize = size - sizeof(UINT32);
  647. char* buffer = (char*)cm_alloc<ScratchAlloc>(stringSize + 1);
  648. memcpy(buffer, memory, stringSize);
  649. buffer[stringSize] = '\0';
  650. data = String(buffer);
  651. cm_free<ScratchAlloc>(buffer);
  652. return size;
  653. }
  654. static UINT32 getDynamicSize(const String& data)
  655. {
  656. UINT64 dataSize = data.size() * sizeof(String::value_type) + sizeof(UINT32);
  657. #if CM_DEBUG_MODE
  658. if(dataSize > std::numeric_limits<UINT32>::max())
  659. {
  660. __string_throwDataOverflowException();
  661. }
  662. #endif
  663. return (UINT32)dataSize;
  664. }
  665. };
  666. /**
  667. * @brief Strings need to copy their data in a slightly more intricate way than just memcpy.
  668. */
  669. template<> struct RTTIPlainType<WString>
  670. {
  671. enum { id = TID_WString }; enum { hasDynamicSize = 1 };
  672. static void toMemory(const WString& data, char* memory)
  673. {
  674. UINT32 size = getDynamicSize(data);
  675. memcpy(memory, &size, sizeof(UINT32));
  676. memory += sizeof(UINT32);
  677. size -= sizeof(UINT32);
  678. memcpy(memory, data.data(), size);
  679. }
  680. static UINT32 fromMemory(WString& data, char* memory)
  681. {
  682. UINT32 size;
  683. memcpy(&size, memory, sizeof(UINT32));
  684. memory += sizeof(UINT32);
  685. UINT32 stringSize = size - sizeof(UINT32);
  686. WString::value_type* buffer = (WString::value_type*)cm_alloc<ScratchAlloc>(stringSize + 1);
  687. memcpy(buffer, memory, stringSize);
  688. buffer[stringSize] = '\0';
  689. data = WString(buffer);
  690. cm_free<ScratchAlloc>(buffer);
  691. return size;
  692. }
  693. static UINT32 getDynamicSize(const WString& data)
  694. {
  695. UINT64 dataSize = data.size() * sizeof(WString::value_type) + sizeof(UINT32);
  696. #if CM_DEBUG_MODE
  697. if(dataSize > std::numeric_limits<UINT32>::max())
  698. {
  699. __string_throwDataOverflowException();
  700. }
  701. #endif
  702. return (UINT32)dataSize;
  703. }
  704. };
  705. } // namespace CamelotFramework
  706. template<>
  707. struct std::hash<CamelotFramework::String>
  708. {
  709. size_t operator()(const CamelotFramework::String& string) const
  710. {
  711. size_t hash = 0;
  712. for(size_t i = 0; i < string.size(); i++)
  713. hash = 65599 * hash + string[i];
  714. return hash ^ (hash >> 16);
  715. }
  716. };
  717. template<>
  718. struct std::hash<CamelotFramework::WString>
  719. {
  720. size_t operator()(const CamelotFramework::WString& string) const
  721. {
  722. size_t hash = 0;
  723. for(size_t i = 0; i < string.size(); i++)
  724. hash = 65599 * hash + string[i];
  725. return hash ^ (hash >> 16);
  726. }
  727. };
  728. #include "CmHString.h"