CmString.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. #ifndef _String_H__
  25. #define _String_H__
  26. #include "CmPrerequisitesUtil.h"
  27. // If we're using the GCC 3.1 C++ Std lib
  28. #if CM_COMPILER == CM_COMPILER_GNUC && CM_COMP_VER >= 310 && !defined(STLPORT)
  29. // For gcc 4.3 see http://gcc.gnu.org/gcc-4.3/changes.html
  30. # if CM_COMP_VER >= 430
  31. # include <tr1/unordered_map>
  32. # else
  33. # include <ext/hash_map>
  34. namespace __gnu_cxx
  35. {
  36. template <> struct hash< CamelotFramework::_StringBase >
  37. {
  38. size_t operator()( const CamelotFramework::_StringBase _stringBase ) const
  39. {
  40. /* This is the PRO-STL way, but it seems to cause problems with VC7.1
  41. and in some other cases (although I can't recreate it)
  42. hash<const char*> H;
  43. return H(_stringBase.c_str());
  44. */
  45. /** This is our custom way */
  46. register size_t ret = 0;
  47. for( CamelotFramework::_StringBase::const_iterator it = _stringBase.begin(); it != _stringBase.end(); ++it )
  48. ret = 5 * ret + *it;
  49. return ret;
  50. }
  51. };
  52. }
  53. # endif
  54. #endif
  55. namespace CamelotFramework
  56. {
  57. template <typename T>
  58. struct BasicString
  59. {
  60. typedef typename std::basic_string<T, std::char_traits<T>, StdAlloc<T>> type;
  61. };
  62. template <typename T>
  63. struct BasicStringStream
  64. {
  65. typedef typename std::basic_stringstream<T, std::char_traits<T>, StdAlloc<T>> type;
  66. };
  67. typedef BasicString<wchar_t>::type WString;
  68. typedef BasicString<char>::type String;
  69. typedef BasicStringStream<wchar_t>::type WStringStream;
  70. typedef BasicStringStream<char>::type StringStream;
  71. /** \addtogroup Core
  72. * @{
  73. */
  74. /** \addtogroup General
  75. * @{
  76. */
  77. /** Utility class for manipulating Strings. */
  78. class CM_UTILITY_EXPORT StringUtil
  79. {
  80. public:
  81. /** Removes any whitespace characters, be it standard space or
  82. TABs and so on.
  83. @remarks
  84. The user may specify wether they want to trim only the
  85. beginning or the end of the String ( the default action is
  86. to trim both).
  87. */
  88. static void trim(String& str, bool left = true, bool right = true);
  89. /** Removes any whitespace characters, be it standard space or
  90. TABs and so on.
  91. @remarks
  92. The user may specify wether they want to trim only the
  93. beginning or the end of the String ( the default action is
  94. to trim both).
  95. */
  96. static void trim(WString& str, bool left = true, bool right = true);
  97. /** Returns a StringVector that contains all the substrings delimited
  98. by the characters in the passed <code>delims</code> argument.
  99. @param
  100. delims A list of delimiter characters to split by
  101. @param
  102. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  103. parameters is > 0, the splitting process will stop after this many splits, left to right.
  104. */
  105. static Vector<String>::type split(const String& str, const String& delims = "\t\n ", unsigned int maxSplits = 0);
  106. /** Returns a StringVector that contains all the substrings delimited
  107. by the characters in the passed <code>delims</code> argument.
  108. @param
  109. delims A list of delimiter characters to split by
  110. @param
  111. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  112. parameters is > 0, the splitting process will stop after this many splits, left to right.
  113. */
  114. static Vector<WString>::type split(const WString& str, const WString& delims = L"\t\n ", unsigned int maxSplits = 0);
  115. /** Returns a StringVector that contains all the substrings delimited
  116. by the characters in the passed <code>delims</code> argument,
  117. or in the <code>doubleDelims</code> argument, which is used to include (normal)
  118. delimeters in the tokenised string. For example, "strings like this".
  119. @param
  120. delims A list of delimiter characters to split by
  121. @param
  122. delims A list of double delimeters characters to tokenise by
  123. @param
  124. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  125. parameters is > 0, the splitting process will stop after this many splits, left to right.
  126. */
  127. static Vector<String>::type tokenise(const String& str, const String& delims = "\t\n ", const String& doubleDelims = "\"", unsigned int maxSplits = 0);
  128. /** Returns a StringVector that contains all the substrings delimited
  129. by the characters in the passed <code>delims</code> argument,
  130. or in the <code>doubleDelims</code> argument, which is used to include (normal)
  131. delimeters in the tokenised string. For example, "strings like this".
  132. @param
  133. delims A list of delimiter characters to split by
  134. @param
  135. delims A list of double delimeters characters to tokenise by
  136. @param
  137. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  138. parameters is > 0, the splitting process will stop after this many splits, left to right.
  139. */
  140. static Vector<WString>::type tokenise(const WString& str, const WString& delims = L"\t\n ", const WString& doubleDelims = L"\"", unsigned int maxSplits = 0);
  141. /** Lower-cases all the characters in the string.
  142. */
  143. static void toLowerCase(String& str);
  144. /** Lower-cases all the characters in the string.
  145. */
  146. static void toLowerCase(WString& str);
  147. /** Upper-cases all the characters in the string.
  148. */
  149. static void toUpperCase(String& str);
  150. /** Upper-cases all the characters in the string.
  151. */
  152. static void toUpperCase(WString& str);
  153. /** Returns whether the string begins with the pattern passed in.
  154. @param pattern The pattern to compare with.
  155. @param lowerCase If true, the start of the string will be lower cased before
  156. comparison, pattern should also be in lower case.
  157. */
  158. static bool startsWith(const String& str, const String& pattern, bool lowerCase = true);
  159. /** Returns whether the string begins with the pattern passed in.
  160. @param pattern The pattern to compare with.
  161. @param lowerCase If true, the start of the string will be lower cased before
  162. comparison, pattern should also be in lower case.
  163. */
  164. static bool startsWith(const WString& str, const WString& pattern, bool lowerCase = true);
  165. /** Returns whether the string ends with the pattern passed in.
  166. @param pattern The pattern to compare with.
  167. @param lowerCase If true, the end of the string will be lower cased before
  168. comparison, pattern should also be in lower case.
  169. */
  170. static bool endsWith(const String& str, const String& pattern, bool lowerCase = true);
  171. /** Returns whether the string ends with the pattern passed in.
  172. @param pattern The pattern to compare with.
  173. @param lowerCase If true, the end of the string will be lower cased before
  174. comparison, pattern should also be in lower case.
  175. */
  176. static bool endsWith(const WString& str, const WString& pattern, bool lowerCase = true);
  177. /** Simple pattern-matching routine allowing a wildcard pattern.
  178. @param str String to test
  179. @param pattern Pattern to match against; can include simple '*' wildcards
  180. @param caseSensitive Whether the match is case sensitive or not
  181. */
  182. static bool match(const String& str, const String& pattern, bool caseSensitive = true);
  183. /** Simple pattern-matching routine allowing a wildcard pattern.
  184. @param str String to test
  185. @param pattern Pattern to match against; can include simple '*' wildcards
  186. @param caseSensitive Whether the match is case sensitive or not
  187. */
  188. static bool match(const WString& str, const WString& pattern, bool caseSensitive = true);
  189. /** Replace all instances of a sub-string with a another sub-string.
  190. @param source Source string
  191. @param replaceWhat Sub-string to find and replace
  192. @param replaceWithWhat Sub-string to replace with (the new sub-string)
  193. @returns An updated string with the sub-string replaced
  194. */
  195. static const String replaceAll(const String& source, const String& replaceWhat, const String& replaceWithWhat);
  196. /** Replace all instances of a sub-string with a another sub-string.
  197. @param source Source string
  198. @param replaceWhat Sub-string to find and replace
  199. @param replaceWithWhat Sub-string to replace with (the new sub-string)
  200. @returns An updated string with the sub-string replaced
  201. */
  202. static const WString replaceAll(const WString& source, const WString& replaceWhat, const WString& replaceWithWhat);
  203. /// Constant blank string, useful for returning by ref where local does not exist
  204. static const String BLANK;
  205. /// Constant blank string, useful for returning by ref where local does not exist
  206. static const WString WBLANK;
  207. private:
  208. template <class T>
  209. static typename Vector<typename BasicString<T>::type>::type splitInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& delims, unsigned int maxSplits)
  210. {
  211. Vector<BasicString<T>::type>::type ret;
  212. // Pre-allocate some space for performance
  213. ret.reserve(maxSplits ? maxSplits+1 : 10); // 10 is guessed capacity for most case
  214. unsigned int numSplits = 0;
  215. // Use STL methods
  216. size_t start, pos;
  217. start = 0;
  218. do
  219. {
  220. pos = str.find_first_of(delims, start);
  221. if (pos == start)
  222. {
  223. // Do nothing
  224. start = pos + 1;
  225. }
  226. else if (pos == BasicString<T>::type::npos || (maxSplits && numSplits == maxSplits))
  227. {
  228. // Copy the rest of the string
  229. ret.push_back(str.substr(start));
  230. break;
  231. }
  232. else
  233. {
  234. // Copy up to delimiter
  235. ret.push_back(str.substr(start, pos - start));
  236. start = pos + 1;
  237. }
  238. // parse up to next real data
  239. start = str.find_first_not_of(delims, start);
  240. ++numSplits;
  241. } while (pos != BasicString<T>::type::npos);
  242. return ret;
  243. }
  244. template <class T>
  245. static typename Vector<typename BasicString<T>::type>::type tokeniseInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& singleDelims,
  246. const typename BasicString<T>::type& doubleDelims, unsigned int maxSplits)
  247. {
  248. Vector<BasicString<T>::type>::type ret;
  249. // Pre-allocate some space for performance
  250. ret.reserve(maxSplits ? maxSplits + 1 : 10); // 10 is guessed capacity for most case
  251. unsigned int numSplits = 0;
  252. BasicString<T>::type delims = singleDelims + doubleDelims;
  253. // Use STL methods
  254. size_t start, pos;
  255. T curDoubleDelim = 0;
  256. start = 0;
  257. do
  258. {
  259. if (curDoubleDelim != 0)
  260. {
  261. pos = str.find(curDoubleDelim, start);
  262. }
  263. else
  264. {
  265. pos = str.find_first_of(delims, start);
  266. }
  267. if (pos == start)
  268. {
  269. T curDelim = str.at(pos);
  270. if (doubleDelims.find_first_of(curDelim) != BasicString<T>::type::npos)
  271. {
  272. curDoubleDelim = curDelim;
  273. }
  274. // Do nothing
  275. start = pos + 1;
  276. }
  277. else if (pos == BasicString<T>::type::npos || (maxSplits && numSplits == maxSplits))
  278. {
  279. if (curDoubleDelim != 0)
  280. {
  281. //Missing closer. Warn or throw exception?
  282. }
  283. // Copy the rest of the string
  284. ret.push_back( str.substr(start) );
  285. break;
  286. }
  287. else
  288. {
  289. if (curDoubleDelim != 0)
  290. {
  291. curDoubleDelim = 0;
  292. }
  293. // Copy up to delimiter
  294. ret.push_back( str.substr(start, pos - start) );
  295. start = pos + 1;
  296. }
  297. if (curDoubleDelim == 0)
  298. {
  299. // parse up to next real data
  300. start = str.find_first_not_of(singleDelims, start);
  301. }
  302. ++numSplits;
  303. } while (pos != BasicString<T>::type::npos);
  304. return ret;
  305. }
  306. template <class T>
  307. static bool startsWithInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& pattern, bool lowerCase)
  308. {
  309. size_t thisLen = str.length();
  310. size_t patternLen = pattern.length();
  311. if (thisLen < patternLen || patternLen == 0)
  312. return false;
  313. BasicString<T>::type startOfThis = str.substr(0, patternLen);
  314. if (lowerCase)
  315. StringUtil::toLowerCase(startOfThis);
  316. return (startOfThis == pattern);
  317. }
  318. template <class T>
  319. static bool endsWithInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& pattern, bool lowerCase)
  320. {
  321. size_t thisLen = str.length();
  322. size_t patternLen = pattern.length();
  323. if (thisLen < patternLen || patternLen == 0)
  324. return false;
  325. BasicString<T>::type endOfThis = str.substr(thisLen - patternLen, patternLen);
  326. if (lowerCase)
  327. StringUtil::toLowerCase(endOfThis);
  328. return (endOfThis == pattern);
  329. }
  330. template <class T>
  331. static bool matchInternal(const typename BasicString<T>::type& str, const typename BasicString<T>::type& pattern, bool caseSensitive)
  332. {
  333. BasicString<T>::type tmpStr = str;
  334. BasicString<T>::type tmpPattern = pattern;
  335. if (!caseSensitive)
  336. {
  337. StringUtil::toLowerCase(tmpStr);
  338. StringUtil::toLowerCase(tmpPattern);
  339. }
  340. BasicString<T>::type::const_iterator strIt = tmpStr.begin();
  341. BasicString<T>::type::const_iterator patIt = tmpPattern.begin();
  342. BasicString<T>::type::const_iterator lastWildCardIt = tmpPattern.end();
  343. while (strIt != tmpStr.end() && patIt != tmpPattern.end())
  344. {
  345. if (*patIt == '*')
  346. {
  347. lastWildCardIt = patIt;
  348. // Skip over looking for next character
  349. ++patIt;
  350. if (patIt == tmpPattern.end())
  351. {
  352. // Skip right to the end since * matches the entire rest of the string
  353. strIt = tmpStr.end();
  354. }
  355. else
  356. {
  357. // scan until we find next pattern character
  358. while(strIt != tmpStr.end() && *strIt != *patIt)
  359. ++strIt;
  360. }
  361. }
  362. else
  363. {
  364. if (*patIt != *strIt)
  365. {
  366. if (lastWildCardIt != tmpPattern.end())
  367. {
  368. // The last wildcard can match this incorrect sequence
  369. // rewind pattern to wildcard and keep searching
  370. patIt = lastWildCardIt;
  371. lastWildCardIt = tmpPattern.end();
  372. }
  373. else
  374. {
  375. // no wildwards left
  376. return false;
  377. }
  378. }
  379. else
  380. {
  381. ++patIt;
  382. ++strIt;
  383. }
  384. }
  385. }
  386. // If we reached the end of both the pattern and the string, we succeeded
  387. if (patIt == tmpPattern.end() && strIt == tmpStr.end())
  388. return true;
  389. else
  390. return false;
  391. }
  392. template <class T>
  393. static const typename BasicString<T>::type replaceAllInternal(const typename BasicString<T>::type& source,
  394. const typename BasicString<T>::type& replaceWhat, const typename BasicString<T>::type& replaceWithWhat)
  395. {
  396. BasicString<T>::type result = source;
  397. BasicString<T>::type::size_type pos = 0;
  398. while(1)
  399. {
  400. pos = result.find(replaceWhat,pos);
  401. if (pos == BasicString<T>::type::npos) break;
  402. result.replace(pos,replaceWhat.size(), replaceWithWhat);
  403. pos += replaceWithWhat.size();
  404. }
  405. return result;
  406. }
  407. };
  408. /**
  409. * @brief Converts a narrow string to a wide string.
  410. */
  411. CM_UTILITY_EXPORT WString toWString(const String& source);
  412. /**
  413. * @brief Converts a wide string to a narrow string.
  414. */
  415. CM_UTILITY_EXPORT WString toWString(const String& source);
  416. /** Converts a float to a String. */
  417. CM_UTILITY_EXPORT String toString(float val, unsigned short precision = 6,
  418. unsigned short width = 0, char fill = ' ',
  419. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  420. /** Converts a Radian to a String. */
  421. CM_UTILITY_EXPORT String toString(Radian val, unsigned short precision = 6,
  422. unsigned short width = 0, char fill = ' ',
  423. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  424. /** Converts a Degree to a String. */
  425. CM_UTILITY_EXPORT String toString(Degree val, unsigned short precision = 6,
  426. unsigned short width = 0, char fill = ' ',
  427. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  428. /** Converts an int to a String. */
  429. CM_UTILITY_EXPORT String toString(int val, unsigned short width = 0,
  430. char fill = ' ',
  431. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  432. /** Converts an unsigned int to a String. */
  433. CM_UTILITY_EXPORT String toString(unsigned int val,
  434. unsigned short width = 0, char fill = ' ',
  435. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  436. /** Converts a long to a String. */
  437. CM_UTILITY_EXPORT String toString(long val,
  438. unsigned short width = 0, char fill = ' ',
  439. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  440. /** Converts an unsigned long to a String. */
  441. CM_UTILITY_EXPORT String toString(unsigned long val,
  442. unsigned short width = 0, char fill = ' ',
  443. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  444. /** Converts a boolean to a String.
  445. @param yesNo If set to true, result is 'yes' or 'no' instead of 'true' or 'false'
  446. */
  447. CM_UTILITY_EXPORT String toString(bool val, bool yesNo = false);
  448. /** Converts a Vector2 to a String.
  449. @remarks
  450. Format is "x y" (i.e. 2x float values, space delimited)
  451. */
  452. CM_UTILITY_EXPORT String toString(const Vector2& val);
  453. /** Converts a Vector3 to a String.
  454. @remarks
  455. Format is "x y z" (i.e. 3x float values, space delimited)
  456. */
  457. CM_UTILITY_EXPORT String toString(const Vector3& val);
  458. /** Converts a Vector4 to a String.
  459. @remarks
  460. Format is "x y z w" (i.e. 4x float values, space delimited)
  461. */
  462. CM_UTILITY_EXPORT String toString(const Vector4& val);
  463. /** Converts a Matrix3 to a String.
  464. @remarks
  465. Format is "00 01 02 10 11 12 20 21 22" where '01' means row 0 column 1 etc.
  466. */
  467. CM_UTILITY_EXPORT String toString(const Matrix3& val);
  468. /** Converts a Matrix4 to a String.
  469. @remarks
  470. Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33" where
  471. '01' means row 0 column 1 etc.
  472. */
  473. CM_UTILITY_EXPORT String toString(const Matrix4& val);
  474. /** Converts a Quaternion to a String.
  475. @remarks
  476. Format is "w x y z" (i.e. 4x float values, space delimited)
  477. */
  478. CM_UTILITY_EXPORT String toString(const Quaternion& val);
  479. /** Converts a ColourValue to a String.
  480. @remarks
  481. Format is "r g b a" (i.e. 4x float values, space delimited).
  482. */
  483. CM_UTILITY_EXPORT String toString(const Color& val);
  484. /** Converts a StringVector to a string.
  485. @remarks
  486. Strings must not contain spaces since space is used as a delimiter in
  487. the output.
  488. */
  489. CM_UTILITY_EXPORT String toString(const Vector<CamelotFramework::String>::type& val);
  490. /** Converts a String to a float.
  491. @returns
  492. 0.0 if the value could not be parsed, otherwise the float version of the String.
  493. */
  494. CM_UTILITY_EXPORT float parseFloat(const String& val, float defaultValue = 0);
  495. /** Converts a String to a whole number.
  496. @returns
  497. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  498. */
  499. CM_UTILITY_EXPORT int parseInt(const String& val, int defaultValue = 0);
  500. /** Converts a String to a whole number.
  501. @returns
  502. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  503. */
  504. CM_UTILITY_EXPORT unsigned int parseUnsignedInt(const String& val, unsigned int defaultValue = 0);
  505. /** Converts a String to a whole number.
  506. @returns
  507. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  508. */
  509. CM_UTILITY_EXPORT long parseLong(const String& val, long defaultValue = 0);
  510. /** Converts a String to a whole number.
  511. @returns
  512. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  513. */
  514. CM_UTILITY_EXPORT unsigned long parseUnsignedLong(const String& val, unsigned long defaultValue = 0);
  515. /** Converts a String to a boolean.
  516. @remarks
  517. Returns true if case-insensitive match of the start of the string
  518. matches "true", "yes" or "1", false otherwise.
  519. */
  520. CM_UTILITY_EXPORT bool parseBool(const String& val, bool defaultValue = 0);
  521. /** Checks the String is a valid number value. */
  522. CM_UTILITY_EXPORT bool isNumber(const String& val);
  523. /** @} */
  524. void CM_UTILITY_EXPORT __string_throwDataOverflowException();
  525. /**
  526. * @brief Strings need to copy their data in a slightly more intricate way than just memcpy.
  527. */
  528. template<> struct RTTIPlainType<String>
  529. {
  530. enum { id = 20 }; enum { hasDynamicSize = 1 };
  531. static void toMemory(const String& data, char* memory)
  532. {
  533. UINT32 size = getDynamicSize(data);
  534. memcpy(memory, &size, sizeof(UINT32));
  535. memory += sizeof(UINT32);
  536. size -= sizeof(UINT32);
  537. memcpy(memory, data.data(), size);
  538. }
  539. static UINT32 fromMemory(String& data, char* memory)
  540. {
  541. UINT32 size;
  542. memcpy(&size, memory, sizeof(UINT32));
  543. memory += sizeof(UINT32);
  544. UINT32 stringSize = size - sizeof(UINT32);
  545. char* buffer = (char*)cm_alloc<ScratchAlloc>(stringSize + 1);
  546. memcpy(buffer, memory, stringSize);
  547. buffer[stringSize] = '\0';
  548. data = String(buffer);
  549. cm_free<ScratchAlloc>(buffer);
  550. return size;
  551. }
  552. static UINT32 getDynamicSize(const String& data)
  553. {
  554. UINT64 dataSize = data.size() * sizeof(String::value_type) + sizeof(UINT32);
  555. #if CM_DEBUG_MODE
  556. if(dataSize > std::numeric_limits<UINT32>::max())
  557. {
  558. __string_throwDataOverflowException();
  559. }
  560. #endif
  561. return (UINT32)dataSize;
  562. }
  563. };
  564. /**
  565. * @brief Strings need to copy their data in a slightly more intricate way than just memcpy.
  566. */
  567. template<> struct RTTIPlainType<WString>
  568. {
  569. enum { id = TID_WString }; enum { hasDynamicSize = 1 };
  570. static void toMemory(const WString& data, char* memory)
  571. {
  572. UINT32 size = getDynamicSize(data);
  573. memcpy(memory, &size, sizeof(UINT32));
  574. memory += sizeof(UINT32);
  575. size -= sizeof(UINT32);
  576. memcpy(memory, data.data(), size);
  577. }
  578. static UINT32 fromMemory(WString& data, char* memory)
  579. {
  580. UINT32 size;
  581. memcpy(&size, memory, sizeof(UINT32));
  582. memory += sizeof(UINT32);
  583. UINT32 stringSize = size - sizeof(UINT32);
  584. WString::value_type* buffer = (WString::value_type*)cm_alloc<ScratchAlloc>(stringSize + 1);
  585. memcpy(buffer, memory, stringSize);
  586. buffer[stringSize] = '\0';
  587. data = WString(buffer);
  588. cm_free<ScratchAlloc>(buffer);
  589. return size;
  590. }
  591. static UINT32 getDynamicSize(const WString& data)
  592. {
  593. UINT64 dataSize = data.size() * sizeof(WString::value_type) + sizeof(UINT32);
  594. #if CM_DEBUG_MODE
  595. if(dataSize > std::numeric_limits<UINT32>::max())
  596. {
  597. __string_throwDataOverflowException();
  598. }
  599. #endif
  600. return (UINT32)dataSize;
  601. }
  602. };
  603. } // namespace CamelotFramework
  604. #endif // _String_H__