OgreString.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 "OgrePrerequisites.h"
  27. // If we're using the GCC 3.1 C++ Std lib
  28. #if OGRE_COMPILER == OGRE_COMPILER_GNUC && OGRE_COMP_VER >= 310 && !defined(STLPORT)
  29. // For gcc 4.3 see http://gcc.gnu.org/gcc-4.3/changes.html
  30. # if OGRE_COMP_VER >= 430
  31. # include <tr1/unordered_map>
  32. # else
  33. # include <ext/hash_map>
  34. namespace __gnu_cxx
  35. {
  36. template <> struct hash< Ogre::_StringBase >
  37. {
  38. size_t operator()( const Ogre::_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( Ogre::_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 Ogre {
  56. /** \addtogroup Core
  57. * @{
  58. */
  59. /** \addtogroup General
  60. * @{
  61. */
  62. /** Utility class for manipulating Strings. */
  63. class _OgreExport StringUtil
  64. {
  65. public:
  66. typedef StringStream StrStreamType;
  67. /** Removes any whitespace characters, be it standard space or
  68. TABs and so on.
  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, bool left = true, bool right = true );
  75. /** Returns a StringVector that contains all the substrings delimited
  76. by the characters in the passed <code>delims</code> argument.
  77. @param
  78. delims A list of delimiter characters to split by
  79. @param
  80. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  81. parameters is > 0, the splitting process will stop after this many splits, left to right.
  82. */
  83. static vector<String>::type split( const String& str, const String& delims = "\t\n ", unsigned int maxSplits = 0);
  84. /** Returns a StringVector that contains all the substrings delimited
  85. by the characters in the passed <code>delims</code> argument,
  86. or in the <code>doubleDelims</code> argument, which is used to include (normal)
  87. delimeters in the tokenised string. For example, "strings like this".
  88. @param
  89. delims A list of delimiter characters to split by
  90. @param
  91. delims A list of double delimeters characters to tokenise by
  92. @param
  93. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  94. parameters is > 0, the splitting process will stop after this many splits, left to right.
  95. */
  96. static vector<String>::type tokenise( const String& str, const String& delims = "\t\n ", const String& doubleDelims = "\"", unsigned int maxSplits = 0);
  97. /** Lower-cases all the characters in the string.
  98. */
  99. static void toLowerCase( String& str );
  100. /** Upper-cases all the characters in the string.
  101. */
  102. static void toUpperCase( String& str );
  103. /** Returns whether the string begins with the pattern passed in.
  104. @param pattern The pattern to compare with.
  105. @param lowerCase If true, the start of the string will be lower cased before
  106. comparison, pattern should also be in lower case.
  107. */
  108. static bool startsWith(const String& str, const String& pattern, bool lowerCase = true);
  109. /** Returns whether the string ends with the pattern passed in.
  110. @param pattern The pattern to compare with.
  111. @param lowerCase If true, the end of the string will be lower cased before
  112. comparison, pattern should also be in lower case.
  113. */
  114. static bool endsWith(const String& str, const String& pattern, bool lowerCase = true);
  115. /** Method for standardising paths - use forward slashes only, end with slash.
  116. */
  117. static String standardisePath( const String &init);
  118. /** Method for splitting a fully qualified filename into the base name
  119. and path.
  120. @remarks
  121. Path is standardised as in standardisePath
  122. */
  123. static void splitFilename(const String& qualifiedName,
  124. String& outBasename, String& outPath);
  125. /** Method for splitting a fully qualified filename into the base name,
  126. extension and path.
  127. @remarks
  128. Path is standardised as in standardisePath
  129. */
  130. static void splitFullFilename(const Ogre::String& qualifiedName,
  131. Ogre::String& outBasename, Ogre::String& outExtention,
  132. Ogre::String& outPath);
  133. /** Method for splitting a filename into the base name
  134. and extension.
  135. */
  136. static void splitBaseFilename(const Ogre::String& fullName,
  137. Ogre::String& outBasename, Ogre::String& outExtention);
  138. /** Simple pattern-matching routine allowing a wildcard pattern.
  139. @param str String to test
  140. @param pattern Pattern to match against; can include simple '*' wildcards
  141. @param caseSensitive Whether the match is case sensitive or not
  142. */
  143. static bool match(const String& str, const String& pattern, bool caseSensitive = true);
  144. /** replace all instances of a sub-string with a another sub-string.
  145. @param source Source string
  146. @param replaceWhat Sub-string to find and replace
  147. @param replaceWithWhat Sub-string to replace with (the new sub-string)
  148. @returns An updated string with the sub-string replaced
  149. */
  150. static const String replaceAll(const String& source, const String& replaceWhat, const String& replaceWithWhat);
  151. /// Constant blank string, useful for returning by ref where local does not exist
  152. static const String BLANK;
  153. };
  154. #if OGRE_COMPILER == OGRE_COMPILER_GNUC && OGRE_COMP_VER >= 310 && !defined(STLPORT)
  155. # if OGRE_COMP_VER < 430
  156. typedef ::__gnu_cxx::hash< _StringBase > _StringHash;
  157. # else
  158. typedef ::std::tr1::hash< _StringBase > _StringHash;
  159. # endif
  160. #elif OGRE_COMPILER == OGRE_COMPILER_MSVC && OGRE_COMP_VER >= 1600 && !defined(STLPORT) // VC++ 10.0
  161. typedef ::std::tr1::hash< _StringBase > _StringHash;
  162. #elif !defined( _STLP_HASH_FUN_H )
  163. typedef stdext::hash_compare< _StringBase, std::less< _StringBase > > _StringHash;
  164. #else
  165. typedef std::hash< _StringBase > _StringHash;
  166. #endif
  167. /** @} */
  168. /** @} */
  169. } // namespace Ogre
  170. #endif // _String_H__