Changeset 219 for trunk


Ignore:
Timestamp:
12/17/13 22:02:23 (12 years ago)
Author:
epyon
Message:
  • nv::string - extended string ops
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/string.hh

    r201 r219  
    77
    88#include <string>
     9#include <algorithm>
    910#include <cstring>
    1011#include <sstream>
     
    120121        }
    121122
     123        inline bool trim( std::string& str )
     124        {
     125                size_t endpos = str.find_last_not_of(" \r\n\t");
     126                size_t startpos = str.find_first_not_of(" \r\n\t");
     127
     128                if ( string::npos != endpos || string::npos != startpos )
     129                {
     130                        if ( string::npos == startpos ) startpos = 0;
     131                        if ( string::npos != endpos )   endpos = endpos+1-startpos;
     132                        str = str.substr( startpos, endpos );
     133                        return true;
     134                }
     135                return false;
     136        }
     137
     138        inline bool rtrim( std::string& str )
     139        {
     140                size_t endpos = str.find_last_not_of(" \r\n\t");
     141                if ( string::npos != endpos )
     142                {
     143                        str = str.substr( 0, endpos+1 );
     144                        return true;
     145                }
     146                return false;
     147        }
     148
     149        inline bool ltrim( std::string& str )
     150        {
     151                size_t startpos = str.find_first_not_of(" \r\n\t");
     152                if( string::npos != startpos )
     153                {
     154                        str = str.substr( startpos );
     155                        return true;
     156                }
     157                return false;
     158        }
     159
     160        inline std::string trimmed( const std::string& str )
     161        {
     162                size_t endpos = str.find_last_not_of(" \r\n\t");
     163                size_t startpos = str.find_first_not_of(" \r\n\t");
     164
     165                if ( string::npos != endpos || string::npos != startpos )
     166                {
     167                        if ( string::npos == startpos ) startpos = 0;
     168                        if ( string::npos != endpos )   endpos = endpos+1-startpos;
     169                        return str.substr( startpos, endpos );
     170                }
     171                return str;
     172        }
     173
     174        inline std::string rtrimmed( const std::string& str )
     175        {
     176                size_t endpos = str.find_last_not_of(" \r\n\t");
     177                if ( string::npos != endpos )
     178                {
     179                        return str.substr( 0, endpos+1 );
     180                }
     181                return str;
     182        }
     183
     184        inline std::string ltrimmed( const std::string& str )
     185        {
     186                size_t startpos = str.find_first_not_of(" \r\n\t");
     187                if( string::npos != startpos )
     188                {
     189                        return str.substr( startpos );
     190                }
     191                return str;
     192        }
     193
    122194        inline bool ends_with( const std::string& s, const std::string & ending )
    123195        {
     
    129201        }
    130202
     203        inline std::string& remove_chars( std::string& s, const std::string& chars )
     204        {
     205                s.erase(remove_if(s.begin(), s.end(),
     206                        [&chars](const char& c) {
     207                                return chars.find(c) != std::string::npos;
     208                        }), s.end());
     209                return s;
     210        }
     211
     212        inline std::string remove_chars_copy( std::string s, const std::string& chars )
     213        {
     214                return remove_chars(s, chars);
     215        }
     216
    131217        template< typename T >
    132218        struct string_length
Note: See TracChangeset for help on using the changeset viewer.