# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

string_ex.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "stdgeorges.h"
00027 #include "nel/georges/string_ex.h"
00028 
00029 namespace NLGEORGES
00030 {
00031 
00033 // Construction/Destruction
00035 using namespace std;
00036 CStringEx::~CStringEx()
00037 {
00038 
00039 }
00040 
00041 void CStringEx::remove( const char _c )
00042 {
00043         if( empty() )
00044                 return;
00045         iterator it = begin();
00046         while( it != end() ) 
00047         {
00048                 if( (*it) == _c )
00049                         it = erase( it );
00050                 else
00051                         ++it;
00052         }
00053 }
00054 
00055 void CStringEx::remove()
00056 {
00057         remove(' ');
00058         remove('\t');
00059         remove('\n');
00060 }
00061 
00062 void    CStringEx::make_lower( )
00063 {
00064         for( iterator it = begin(); it != end(); ++it )
00065                 if( (*it >= 'A')&&(*it <= 'Z') )
00066                         *it += 'a'-'A';
00067 }
00068 
00069 void    CStringEx::make_upper( )
00070 {
00071         for( iterator it = begin(); it != end(); ++it )
00072                 if( (*it >= 'a')&&(*it <= 'z') )
00073                         *it += 'A'-'a';
00074 }
00075 
00076 void    CStringEx::trim_left( )
00077 {
00078         if( empty() )
00079                 return;
00080         iterator it;
00081         for( it = begin(); (it != end())&&( (*it==' ')||(*it=='\t')||(*it=='\n') ); ++it );
00082         erase( begin(), it );
00083 }
00084 
00085 void    CStringEx::trim_left( const char _c )
00086 {
00087         if( empty() )
00088                 return;
00089         iterator it;
00090         for( it = begin(); (it != end())&&( *it == _c ); ++it );
00091         erase( begin(), it );
00092 }
00093 
00094 void    CStringEx::trim_right( ) 
00095 {
00096         if( empty() )
00097                 return;
00098         iterator it = end();
00099         --it;
00100         while( it != begin() )
00101         {
00102                 iterator i = it--;
00103                 if( (*i==' ')||(*i=='\t')||(*i=='\n') )
00104                         erase( i );
00105                 else
00106                         break;
00107         }
00108         if( (*it==' ')||(*it=='\t')||(*it=='\n') )
00109                 erase( it );
00110 }
00111 
00112 void    CStringEx::trim_right( char c )
00113 {
00114         if( empty() )
00115                 return;
00116         iterator it = end();
00117         while( it != begin() )
00118         {
00119                 iterator i = it--;
00120                 if( *i == c )
00121                         erase( i );
00122                 else
00123                         break;
00124         }
00125         if( *it == c )
00126                 erase( it );
00127 }
00128 
00129 void CStringEx::trim()
00130 {
00131         trim_left();
00132         trim_right();
00133 }
00134 
00135 void CStringEx::purge()
00136 {
00137         make_lower();
00138         remove(' ');
00139         remove('\t');
00140         remove('\n');
00141 }
00142 
00143 void CStringEx::trim( const char _c )
00144 {
00145         trim_left( _c );
00146         trim_right( _c );
00147 }
00148 
00149 void    CStringEx::mid( const int nFirst )
00150 {
00151         CStringEx s( *this );
00152         erase();
00153         append( s.get_mid( nFirst ));
00154 }
00155 
00156 void    CStringEx::mid( const int nFirst, const int nCount )
00157 {
00158         CStringEx s( *this );
00159         erase();
00160         append( s.get_mid( nFirst, nCount ));
00161 }
00162 
00163 void    CStringEx::left( const int nCount )
00164 {
00165         CStringEx s( *this );
00166         erase();
00167         append( s.get_left( nCount ));
00168 }
00169 
00170 void    CStringEx::right( const int nCount )
00171 {
00172         CStringEx s( *this );
00173         erase();
00174         append( s.get_right( nCount ));
00175 }
00176 
00177 
00178 CStringEx CStringEx::get_remove( const char _c ) const
00179 {
00180         CStringEx s( *this );
00181         s.remove( _c );
00182         return( s );
00183 }
00184 
00185 CStringEx CStringEx::get_remove() const
00186 {
00187         CStringEx s( *this );
00188         s.remove();
00189         return( s );
00190 }
00191 
00192 CStringEx CStringEx::get_make_lower() const
00193 {
00194         CStringEx s( *this );
00195         s.make_lower();
00196         return( s );
00197 }
00198 
00199 CStringEx CStringEx::get_make_upper() const
00200 {
00201         CStringEx s( *this );
00202         s.make_upper();
00203         return( s );
00204 }
00205 
00206 CStringEx CStringEx::get_trim_left() const
00207 {
00208         CStringEx s( *this );
00209         s.trim_left();
00210         return( s );
00211 }
00212 
00213 CStringEx CStringEx::get_trim_left( const char _c ) const
00214 {
00215         CStringEx s( *this );
00216         s.trim_left( _c );
00217         return( s );
00218 }
00219 
00220 CStringEx CStringEx::get_trim_right() const
00221 {
00222         CStringEx s( *this );
00223         s.trim_right();
00224         return( s );
00225 }
00226 
00227 CStringEx CStringEx::get_trim_right( const char _c ) const
00228 {
00229         CStringEx s( *this );
00230         s.trim_right( _c );
00231         return( s );
00232 }
00233 
00234 CStringEx CStringEx::get_trim() const
00235 {
00236         CStringEx s( *this );
00237         s.trim();
00238         return( s );
00239 }
00240 
00241 CStringEx CStringEx::get_purge() const
00242 {
00243         CStringEx s( *this );
00244         s.purge();
00245         return( s );
00246 }
00247 
00248 CStringEx CStringEx::get_trim( const char _c ) const
00249 {
00250         CStringEx s( *this );
00251         s.trim( _c );
00252         return( s );
00253 }
00254 
00255 CStringEx       CStringEx::get_mid( const int nFirst ) const
00256 {
00257         if( !size() )
00258           {
00259             CStringEx object;
00260                 return( object );
00261           }
00262         return( get_right( size()-nFirst ) );
00263 }
00264 
00265 CStringEx       CStringEx::get_mid( const int nFirst, const int nCount ) const
00266 {
00267         if( !size() )
00268           {
00269             CStringEx object;
00270                 return( object );
00271           }
00272         return( substr( nFirst, nCount ) );
00273 }
00274 
00275 CStringEx       CStringEx::get_left( const int nCount ) const
00276 {
00277         if( !size() )
00278           {
00279             CStringEx object;
00280                 return( object );
00281           }
00282         return( substr( 0, nCount ) );
00283 }
00284 
00285 CStringEx       CStringEx::get_right( const int nCount ) const
00286 {
00287         if( !size() )
00288           {
00289             CStringEx object;
00290                 return( object );
00291           }
00292         return( substr( size()-nCount, nCount ) );
00293 }
00294 
00295 
00296 bool  CStringEx::operator <= ( const CStringEx& s ) const 
00297 {
00298         const_iterator it = begin();
00299         const_iterator is = s.begin();
00300         while( ( it != end() )&&( is != s.end() ) )
00301         {
00302                 if( *it != *is )
00303                         return( *it < *is );
00304                 it++;
00305                 is++;
00306         }
00307         return( ( it == end() )&&( is == s.end() ) );
00308 }
00309 
00310 bool  CStringEx::operator < ( const CStringEx& s ) const 
00311 {
00312         const_iterator it = begin();
00313         const_iterator is = s.begin();
00314         while( ( it != end() )&&( is != s.end() ) )
00315         {
00316                 if( *it != *is )
00317                         return( *it < *is );
00318                 it++;
00319                 is++;
00320         }
00321         return( is != s.end() );
00322 }
00323 
00324 int CStringEx::reverse_find( const char _c ) const
00325 {
00326         unsigned int i = length();
00327         const_iterator it = end();
00328         while( it != begin() )
00329         {
00330                 --it;
00331                 --i;
00332                 if( *it == _c )
00333                         return( i );
00334         }
00335         return( npos );
00336 }
00337 
00338 void CStringEx::format( const char* s, ... )
00339 {
00340         char *p = new char[256];
00341         va_list ap;
00342         va_start(ap, s);
00343         int x = vsprintf( p, s, ap);
00344         erase();
00345         append(p);
00346         delete[] p;
00347 }
00348 
00349 /*
00350    #include <stdio.h>
00351    #include <stdlib.h>
00352    #include <stdarg.h>
00353    char *newfmt(const char *fmt, ...)
00354    {
00355            char *p;
00356            va_list ap;
00357            if ((p = malloc(128)) == NULL)
00358                            return (NULL);
00359            va_start(ap, fmt);
00360            (void) vsnprintf(p, 128, fmt, ap);
00361            va_end(ap);
00362            return (p);
00363    }
00364 */
00365 
00366 }