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     String String::BLANK = String("");
00033     //-----------------------------------------------------------------------
00034     void String::trim(bool left, bool right)
00035     {
00036         size_t lspaces, rspaces, len = length(), i;
00037 
00038         lspaces = rspaces = 0;
00039 
00040         if( left )
00041         {
00042             // Find spaces / tabs on the left
00043             for( i = 0;
00044                 i < len && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r');
00045                 ++lspaces, ++i );
00046         }
00047         
00048         if( right && lspaces < len )
00049         {
00050             // Find spaces / tabs on the right
00051             for( i = len - 1;
00052                 i >= 0 && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r');
00053                 rspaces++, i-- );
00054         }
00055 
00056         *this = substr(lspaces, len-lspaces-rspaces);
00057     }
00058 
00059     //-----------------------------------------------------------------------
00060     std::vector<String> String::split( const String& delims, unsigned int maxSplits) const
00061     {
00062         // static unsigned dl;
00063         std::vector<String> ret;
00064         unsigned int numSplits = 0;
00065 
00066         // Use STL methods 
00067         size_t start, pos;
00068         start = 0;
00069         do 
00070         {
00071             pos = find_first_of(delims, start);
00072             if (pos == start)
00073             {
00074                 // Do nothing
00075                 start = pos + 1;
00076             }
00077             else if (pos == npos || (maxSplits && numSplits == maxSplits))
00078             {
00079                 // Copy the rest of the string
00080                 ret.push_back( substr(start) );
00081                 break;
00082             }
00083             else
00084             {
00085                 // Copy up to delimiter
00086                 ret.push_back( substr(start, pos - start) );
00087                 start = pos + 1;
00088             }
00089             // parse up to next real data
00090             start = find_first_not_of(delims, start);
00091             ++numSplits;
00092 
00093         } while (pos != npos);
00094 
00095 
00096 
00097         return ret;
00098     }
00099 
00100     //-----------------------------------------------------------------------
00101     String String::toLowerCase(void) 
00102     {
00103         std::transform(
00104             begin(),
00105             end(),
00106             begin(),
00107             static_cast<int(*)(int)>(::tolower) );
00108 
00109         return *this;
00110     }
00111 
00112     //-----------------------------------------------------------------------
00113     String String::toUpperCase(void) 
00114     {
00115         std::transform(
00116             begin(),
00117             end(),
00118             begin(),
00119             static_cast<int(*)(int)>(::toupper) );
00120 
00121         return *this;
00122     }
00123     //-----------------------------------------------------------------------
00124     Real String::toReal(void) const
00125     {
00126         return (Real)atof(this->c_str());
00127     }
00128     //-----------------------------------------------------------------------
00129     bool String::startsWith(const String& pattern, bool lowerCase) const
00130     {
00131         size_t thisLen = this->length();
00132         size_t patternLen = pattern.length();
00133         if (thisLen < patternLen || patternLen == 0)
00134             return false;
00135 
00136         String startOfThis = substr(0, patternLen);
00137         if (lowerCase)
00138             startOfThis.toLowerCase();
00139 
00140         return (startOfThis == pattern);
00141     }
00142     //-----------------------------------------------------------------------
00143     bool String::endsWith(const String& pattern, bool lowerCase) const
00144     {
00145         size_t thisLen = this->length();
00146         size_t patternLen = pattern.length();
00147         if (thisLen < patternLen || patternLen == 0)
00148             return false;
00149 
00150         String endOfThis = substr(thisLen - patternLen, patternLen);
00151         if (lowerCase)
00152             endOfThis.toLowerCase();
00153 
00154         return (endOfThis == pattern);
00155     }
00156     //-----------------------------------------------------------------------
00157 
00158 }
00159 

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:28 2004