OgreString.cpp 11 KB

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