OgreString.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #include "OgreString.h"
  25. namespace Ogre {
  26. //-----------------------------------------------------------------------
  27. const String StringUtil::BLANK;
  28. //-----------------------------------------------------------------------
  29. void StringUtil::trim(String& str, bool left, bool right)
  30. {
  31. /*
  32. size_t lspaces, rspaces, len = length(), i;
  33. lspaces = rspaces = 0;
  34. if( left )
  35. {
  36. // Find spaces / tabs on the left
  37. for( i = 0;
  38. i < len && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r');
  39. ++lspaces, ++i );
  40. }
  41. if( right && lspaces < len )
  42. {
  43. // Find spaces / tabs on the right
  44. for( i = len - 1;
  45. i >= 0 && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r');
  46. rspaces++, i-- );
  47. }
  48. *this = substr(lspaces, len-lspaces-rspaces);
  49. */
  50. static const String delims = " \t\r";
  51. if(right)
  52. str.erase(str.find_last_not_of(delims)+1); // trim right
  53. if(left)
  54. str.erase(0, str.find_first_not_of(delims)); // trim left
  55. }
  56. //-----------------------------------------------------------------------
  57. std::vector<Ogre::String> StringUtil::split( const String& str, const String& delims, unsigned int maxSplits)
  58. {
  59. std::vector<Ogre::String> ret;
  60. // Pre-allocate some space for performance
  61. ret.reserve(maxSplits ? maxSplits+1 : 10); // 10 is guessed capacity for most case
  62. unsigned int numSplits = 0;
  63. // Use STL methods
  64. size_t start, pos;
  65. start = 0;
  66. do
  67. {
  68. pos = str.find_first_of(delims, start);
  69. if (pos == start)
  70. {
  71. // Do nothing
  72. start = pos + 1;
  73. }
  74. else if (pos == String::npos || (maxSplits && numSplits == maxSplits))
  75. {
  76. // Copy the rest of the string
  77. ret.push_back( str.substr(start) );
  78. break;
  79. }
  80. else
  81. {
  82. // Copy up to delimiter
  83. ret.push_back( str.substr(start, pos - start) );
  84. start = pos + 1;
  85. }
  86. // parse up to next real data
  87. start = str.find_first_not_of(delims, start);
  88. ++numSplits;
  89. } while (pos != String::npos);
  90. return ret;
  91. }
  92. //-----------------------------------------------------------------------
  93. std::vector<Ogre::String> StringUtil::tokenise( const String& str, const String& singleDelims, const String& doubleDelims, unsigned int maxSplits)
  94. {
  95. std::vector<Ogre::String> ret;
  96. // Pre-allocate some space for performance
  97. ret.reserve(maxSplits ? maxSplits+1 : 10); // 10 is guessed capacity for most case
  98. unsigned int numSplits = 0;
  99. String delims = singleDelims + doubleDelims;
  100. // Use STL methods
  101. size_t start, pos;
  102. char curDoubleDelim = 0;
  103. start = 0;
  104. do
  105. {
  106. if (curDoubleDelim != 0)
  107. {
  108. pos = str.find(curDoubleDelim, start);
  109. }
  110. else
  111. {
  112. pos = str.find_first_of(delims, start);
  113. }
  114. if (pos == start)
  115. {
  116. char curDelim = str.at(pos);
  117. if (doubleDelims.find_first_of(curDelim) != String::npos)
  118. {
  119. curDoubleDelim = curDelim;
  120. }
  121. // Do nothing
  122. start = pos + 1;
  123. }
  124. else if (pos == String::npos || (maxSplits && numSplits == maxSplits))
  125. {
  126. if (curDoubleDelim != 0)
  127. {
  128. //Missing closer. Warn or throw exception?
  129. }
  130. // Copy the rest of the string
  131. ret.push_back( str.substr(start) );
  132. break;
  133. }
  134. else
  135. {
  136. if (curDoubleDelim != 0)
  137. {
  138. curDoubleDelim = 0;
  139. }
  140. // Copy up to delimiter
  141. ret.push_back( str.substr(start, pos - start) );
  142. start = pos + 1;
  143. }
  144. if (curDoubleDelim == 0)
  145. {
  146. // parse up to next real data
  147. start = str.find_first_not_of(singleDelims, start);
  148. }
  149. ++numSplits;
  150. } while (pos != String::npos);
  151. return ret;
  152. }
  153. //-----------------------------------------------------------------------
  154. void StringUtil::toLowerCase(String& str)
  155. {
  156. std::transform(
  157. str.begin(),
  158. str.end(),
  159. str.begin(),
  160. tolower);
  161. }
  162. //-----------------------------------------------------------------------
  163. void StringUtil::toUpperCase(String& str)
  164. {
  165. std::transform(
  166. str.begin(),
  167. str.end(),
  168. str.begin(),
  169. toupper);
  170. }
  171. //-----------------------------------------------------------------------
  172. bool StringUtil::startsWith(const String& str, const String& pattern, bool lowerCase)
  173. {
  174. size_t thisLen = str.length();
  175. size_t patternLen = pattern.length();
  176. if (thisLen < patternLen || patternLen == 0)
  177. return false;
  178. String startOfThis = str.substr(0, patternLen);
  179. if (lowerCase)
  180. StringUtil::toLowerCase(startOfThis);
  181. return (startOfThis == pattern);
  182. }
  183. //-----------------------------------------------------------------------
  184. bool StringUtil::endsWith(const String& str, const String& pattern, bool lowerCase)
  185. {
  186. size_t thisLen = str.length();
  187. size_t patternLen = pattern.length();
  188. if (thisLen < patternLen || patternLen == 0)
  189. return false;
  190. String endOfThis = str.substr(thisLen - patternLen, patternLen);
  191. if (lowerCase)
  192. StringUtil::toLowerCase(endOfThis);
  193. return (endOfThis == pattern);
  194. }
  195. //-----------------------------------------------------------------------
  196. String StringUtil::standardisePath(const String& init)
  197. {
  198. String path = init;
  199. std::replace( path.begin(), path.end(), '\\', '/' );
  200. if( path[path.length() - 1] != '/' )
  201. path += '/';
  202. return path;
  203. }
  204. //-----------------------------------------------------------------------
  205. void StringUtil::splitFilename(const String& qualifiedName,
  206. String& outBasename, String& outPath)
  207. {
  208. String path = qualifiedName;
  209. // Replace \ with / first
  210. std::replace( path.begin(), path.end(), '\\', '/' );
  211. // split based on final /
  212. size_t i = path.find_last_of('/');
  213. if (i == String::npos)
  214. {
  215. outPath.clear();
  216. outBasename = qualifiedName;
  217. }
  218. else
  219. {
  220. outBasename = path.substr(i+1, path.size() - i - 1);
  221. outPath = path.substr(0, i+1);
  222. }
  223. }
  224. //-----------------------------------------------------------------------
  225. void StringUtil::splitBaseFilename(const Ogre::String& fullName,
  226. Ogre::String& outBasename, Ogre::String& outExtention)
  227. {
  228. size_t i = fullName.find_last_of(".");
  229. if (i == Ogre::String::npos)
  230. {
  231. outExtention.clear();
  232. outBasename = fullName;
  233. }
  234. else
  235. {
  236. outExtention = fullName.substr(i+1);
  237. outBasename = fullName.substr(0, i);
  238. }
  239. }
  240. // ----------------------------------------------------------------------------------------------------------------------------------------------
  241. void StringUtil::splitFullFilename( const Ogre::String& qualifiedName,
  242. Ogre::String& outBasename, Ogre::String& outExtention, Ogre::String& outPath )
  243. {
  244. Ogre::String fullName;
  245. splitFilename( qualifiedName, fullName, outPath );
  246. splitBaseFilename( fullName, outBasename, outExtention );
  247. }
  248. //-----------------------------------------------------------------------
  249. bool StringUtil::match(const String& str, const String& pattern, bool caseSensitive)
  250. {
  251. String tmpStr = str;
  252. String tmpPattern = pattern;
  253. if (!caseSensitive)
  254. {
  255. StringUtil::toLowerCase(tmpStr);
  256. StringUtil::toLowerCase(tmpPattern);
  257. }
  258. String::const_iterator strIt = tmpStr.begin();
  259. String::const_iterator patIt = tmpPattern.begin();
  260. String::const_iterator lastWildCardIt = tmpPattern.end();
  261. while (strIt != tmpStr.end() && patIt != tmpPattern.end())
  262. {
  263. if (*patIt == '*')
  264. {
  265. lastWildCardIt = patIt;
  266. // Skip over looking for next character
  267. ++patIt;
  268. if (patIt == tmpPattern.end())
  269. {
  270. // Skip right to the end since * matches the entire rest of the string
  271. strIt = tmpStr.end();
  272. }
  273. else
  274. {
  275. // scan until we find next pattern character
  276. while(strIt != tmpStr.end() && *strIt != *patIt)
  277. ++strIt;
  278. }
  279. }
  280. else
  281. {
  282. if (*patIt != *strIt)
  283. {
  284. if (lastWildCardIt != tmpPattern.end())
  285. {
  286. // The last wildcard can match this incorrect sequence
  287. // rewind pattern to wildcard and keep searching
  288. patIt = lastWildCardIt;
  289. lastWildCardIt = tmpPattern.end();
  290. }
  291. else
  292. {
  293. // no wildwards left
  294. return false;
  295. }
  296. }
  297. else
  298. {
  299. ++patIt;
  300. ++strIt;
  301. }
  302. }
  303. }
  304. // If we reached the end of both the pattern and the string, we succeeded
  305. if (patIt == tmpPattern.end() && strIt == tmpStr.end())
  306. {
  307. return true;
  308. }
  309. else
  310. {
  311. return false;
  312. }
  313. }
  314. //-----------------------------------------------------------------------
  315. const String StringUtil::replaceAll(const String& source, const String& replaceWhat, const String& replaceWithWhat)
  316. {
  317. String result = source;
  318. String::size_type pos = 0;
  319. while(1)
  320. {
  321. pos = result.find(replaceWhat,pos);
  322. if (pos == String::npos) break;
  323. result.replace(pos,replaceWhat.size(),replaceWithWhat);
  324. pos += replaceWithWhat.size();
  325. }
  326. return result;
  327. }
  328. }