Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreString.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreStableHeaders.h"
00026 #include "OgreString.h"
00027 
00028 #include "OgreStringVector.h"
00029 
00030 namespace Ogre {
00031 
00032     //-----------------------------------------------------------------------
00033     const String StringUtil::BLANK = String("");
00034     //-----------------------------------------------------------------------
00035     void StringUtil::trim(String& str, bool left, bool right)
00036     {
00037         /*
00038         size_t lspaces, rspaces, len = length(), i;
00039 
00040         lspaces = rspaces = 0;
00041 
00042         if( left )
00043         {
00044             // Find spaces / tabs on the left
00045             for( i = 0;
00046                 i < len && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r');
00047                 ++lspaces, ++i );
00048         }
00049         
00050         if( right && lspaces < len )
00051         {
00052             // Find spaces / tabs on the right
00053             for( i = len - 1;
00054                 i >= 0 && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r');
00055                 rspaces++, i-- );
00056         }
00057 
00058         *this = substr(lspaces, len-lspaces-rspaces);
00059         */
00060         static const String delims = " \t\r";
00061         str.erase(str.find_last_not_of(delims)+1); // trim right
00062         str.erase(0, str.find_first_not_of(delims)); // trim left
00063     }
00064 
00065     //-----------------------------------------------------------------------
00066     std::vector<String> StringUtil::split( const String& str, const String& delims, unsigned int maxSplits)
00067     {
00068         // static unsigned dl;
00069         std::vector<String> ret;
00070         unsigned int numSplits = 0;
00071 
00072         // Use STL methods 
00073         size_t start, pos;
00074         start = 0;
00075         do 
00076         {
00077             pos = str.find_first_of(delims, start);
00078             if (pos == start)
00079             {
00080                 // Do nothing
00081                 start = pos + 1;
00082             }
00083             else if (pos == String::npos || (maxSplits && numSplits == maxSplits))
00084             {
00085                 // Copy the rest of the string
00086                 ret.push_back( str.substr(start) );
00087                 break;
00088             }
00089             else
00090             {
00091                 // Copy up to delimiter
00092                 ret.push_back( str.substr(start, pos - start) );
00093                 start = pos + 1;
00094             }
00095             // parse up to next real data
00096             start = str.find_first_not_of(delims, start);
00097             ++numSplits;
00098 
00099         } while (pos != String::npos);
00100 
00101 
00102 
00103         return ret;
00104     }
00105 
00106     //-----------------------------------------------------------------------
00107     void StringUtil::toLowerCase(String& str)
00108     {
00109         std::transform(
00110             str.begin(),
00111             str.end(),
00112             str.begin(),
00113             tolower);
00114     }
00115 
00116     //-----------------------------------------------------------------------
00117     void StringUtil::toUpperCase(String& str) 
00118     {
00119         std::transform(
00120             str.begin(),
00121             str.end(),
00122             str.begin(),
00123             toupper);
00124     }
00125     //-----------------------------------------------------------------------
00126     Real StringUtil::toReal(const String& str)
00127     {
00128         return (Real)atof(str.c_str());
00129     }
00130     //-----------------------------------------------------------------------
00131     bool StringUtil::startsWith(const String& str, const String& pattern, bool lowerCase)
00132     {
00133         size_t thisLen = str.length();
00134         size_t patternLen = pattern.length();
00135         if (thisLen < patternLen || patternLen == 0)
00136             return false;
00137 
00138         String startOfThis = str.substr(0, patternLen);
00139         if (lowerCase)
00140             StringUtil::toLowerCase(startOfThis);
00141 
00142         return (startOfThis == pattern);
00143     }
00144     //-----------------------------------------------------------------------
00145     bool StringUtil::endsWith(const String& str, const String& pattern, bool lowerCase)
00146     {
00147         size_t thisLen = str.length();
00148         size_t patternLen = pattern.length();
00149         if (thisLen < patternLen || patternLen == 0)
00150             return false;
00151 
00152         String endOfThis = str.substr(thisLen - patternLen, patternLen);
00153         if (lowerCase)
00154             StringUtil::toLowerCase(endOfThis);
00155 
00156         return (endOfThis == pattern);
00157     }
00158     //-----------------------------------------------------------------------
00159 
00160 }

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:47 2004