String.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_BASE_STRING_H
00013 #define ZYPP_BASE_STRING_H
00014 
00015 #include <iosfwd>
00016 #include <string>
00017 #include <boost/regex.hpp>
00018 
00019 #include "zypp/base/Easy.h"
00020 
00022 namespace zypp
00023 { 
00024 
00025 
00028   namespace str
00029   { 
00030 
00032 
00033     std::string form( const char * format, ... )
00034     __attribute__ ((format (printf, 1, 2)));
00035 
00037 
00041     std::string strerror( int errno_r );
00042 
00044 
00054     struct SafeBuf
00055     {
00056       char * _buf;
00057       SafeBuf() : _buf( 0 ) {}
00058       ~SafeBuf() { if ( _buf ) free( _buf ); }
00059       std::string asString() const
00060       { return _buf ? std::string(_buf) : std::string(); }
00061     };
00062 
00064 
00083     using boost::regex;
00084     using boost::regex_match;
00085     using boost::regex_search;
00086     using boost::regex_replace;
00087     using boost::match_results;
00088     using boost::match_extra;
00089     using boost::cmatch;
00090     using boost::wcmatch;
00091     using boost::smatch;
00092     using boost::wsmatch;
00094 
00096 
00109     inline std::string numstring( char n,               int w = 0 ) { return form( "%*hhd",  w, n ); }
00110     inline std::string numstring( unsigned char n,      int w = 0 ) { return form( "%*hhu",  w, n ); }
00111     inline std::string numstring( short n,              int w = 0 ) { return form( "%*hd",   w, n ); }
00112     inline std::string numstring( unsigned short n,     int w = 0 ) { return form( "%*hu",   w, n ); }
00113     inline std::string numstring( int n,                int w = 0 ) { return form( "%*d",    w, n ); }
00114     inline std::string numstring( unsigned n,           int w = 0 ) { return form( "%*u",    w, n ); }
00115     inline std::string numstring( long n,               int w = 0 ) { return form( "%*ld",   w, n ); }
00116     inline std::string numstring( unsigned long n,      int w = 0 ) { return form( "%*lu",   w, n ); }
00117     inline std::string numstring( long long n,          int w = 0 ) { return form( "%*lld",  w, n ); }
00118     inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu",  w, n ); }
00120 
00122 
00133     inline std::string hexstring( char n,               int w = 4 ) { return form( "%#0*hhx", w, n ); }
00134     inline std::string hexstring( unsigned char n,      int w = 4 ) { return form( "%#0*hhx", w, n ); }
00135     inline std::string hexstring( short n,              int w = 10 ){ return form( "%#0*hx",  w, n ); }
00136     inline std::string hexstring( unsigned short n,     int w = 10 ){ return form( "%#0*hx",  w, n ); }
00137     inline std::string hexstring( int n,                int w = 10 ){ return form( "%#0*x",   w, n ); }
00138     inline std::string hexstring( unsigned n,           int w = 10 ){ return form( "%#0*x",   w, n ); }
00139     inline std::string hexstring( long n,               int w = 10 ){ return form( "%#0*lx",  w, n ); }
00140     inline std::string hexstring( unsigned long n,      int w = 10 ){ return form( "%#0*lx",  w, n ); }
00141     inline std::string hexstring( long long n,          int w = 0 ) { return form( "%#0*llx", w, n ); }
00142     inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
00144 
00146 
00157     inline std::string octstring( char n,               int w = 4 ) { return form( "%#0*hho",  w, n ); }
00158     inline std::string octstring( unsigned char n,      int w = 4 ) { return form( "%#0*hho",  w, n ); }
00159     inline std::string octstring( short n,              int w = 5 ) { return form( "%#0*ho",   w, n ); }
00160     inline std::string octstring( unsigned short n,     int w = 5 ) { return form( "%#0*ho",   w, n ); }
00161     inline std::string octstring( int n,                int w = 5 ) { return form( "%#0*o",    w, n ); }
00162     inline std::string octstring( unsigned n,           int w = 5 ) { return form( "%#0*o",    w, n ); }
00163     inline std::string octstring( long n,               int w = 5 ) { return form( "%#0*lo",   w, n ); }
00164     inline std::string octstring( unsigned long n,      int w = 5 ) { return form( "%#0*lo",   w, n ); }
00165     inline std::string octstring( long long n,          int w = 0 ) { return form( "%#0*llo",  w, n ); }
00166     inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo",  w, n ); }
00168 
00170 
00179     template<typename _It>
00180       inline _It strtonum( const std::string & str );
00181 
00182     template<>
00183       inline short              strtonum( const std::string & str ) { return ::strtol  ( str.c_str(), NULL, 0 ); }
00184     template<>
00185       inline int                strtonum( const std::string & str ) { return ::strtol  ( str.c_str(), NULL, 0 ); }
00186     template<>
00187       inline long               strtonum( const std::string & str ) { return ::strtol  ( str.c_str(), NULL, 0 ); }
00188     template<>
00189       inline long long          strtonum( const std::string & str ) { return ::strtoll ( str.c_str(), NULL, 0 ); }
00190 
00191     template<>
00192       inline unsigned short     strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00193     template<>
00194       inline unsigned           strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00195     template<>
00196       inline unsigned long      strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00197     template<>
00198       inline unsigned long long strtonum( const std::string & str ) { return ::strtoull( str.c_str(), NULL, 0 ); }
00199 
00205     template<typename _It>
00206       inline _It strtonum( const std::string & str, _It & i )
00207       { return i = strtonum<_It>( str ); }
00209 
00211 
00215     bool strToTrue( const std::string & str );
00216 
00218     bool strToFalse( const std::string & str );
00219 
00224     inline bool strToBool( const std::string & str, bool default_r )
00225     { return( default_r ? strToFalse( str ) : strToTrue( str ) ); }
00227 
00228 
00234     inline std::string & replace_all( std::string & str, const std::string & from, const std::string & to )
00235     {
00236       std::string::size_type pos = 0;
00237       while( pos < str.length() && (pos = str.find( from, pos )) != std::string::npos )
00238       {
00239         str.replace( pos, from.size(), to );
00240         pos += to.size();
00241       }
00242       return str;
00243     }
00244 
00246 
00257     template<class _OutputIterator>
00258       unsigned split( const std::string & line_r,
00259                       _OutputIterator     result_r,
00260                       const std::string & sepchars_r = " \t" )
00261       {
00262         const char * beg = line_r.c_str();
00263         const char * cur = beg;
00264         // skip leading sepchars
00265         while ( sepchars_r.find( *cur ) != std::string::npos )
00266           ++cur;
00267         unsigned ret = 0;
00268         for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
00269           {
00270             // skip non sepchars
00271             while( *cur && sepchars_r.find( *cur ) == std::string::npos )
00272               ++cur;
00273             // build string
00274             *result_r = std::string( beg, cur-beg );
00275             // skip sepchars
00276             while ( sepchars_r.find( *cur ) != std::string::npos )
00277               ++cur;
00278           }
00279         return ret;
00280       }
00281 
00302     template<class _OutputIterator>
00303       unsigned splitFields( const std::string &   line_r,
00304                             _OutputIterator       result_r,
00305                             const std::string &   sepchars_r = ":" )
00306       {
00307         const char * beg = line_r.c_str();
00308         const char * cur = beg;
00309         unsigned ret = 0;
00310         for ( beg = cur; *beg; beg = cur, ++result_r )
00311           {
00312             // skip non sepchars
00313             while( *cur && !::strchr( sepchars_r.c_str(), *cur ) )
00314               ++cur;
00315             // build string
00316             *result_r = std::string( beg, cur-beg );
00317             ++ret;
00318             // skip sepchar
00319             if ( *cur )
00320             {
00321               ++cur;
00322               if ( ! *cur )                // ending with sepchar
00323               {
00324                 *result_r = std::string(); // add final empty field
00325                 ++ret;
00326                 break;
00327               }
00328             }
00329           }
00330         return ret;
00331       }
00333 
00335 
00338     template <class _Iterator>
00339       std::string join( _Iterator begin, _Iterator end,
00340                         const std::string & sep_r = " " )
00341       {
00342         std::string res;
00343         for ( _Iterator iter = begin; iter != end; ++ iter )
00344           {
00345             if ( iter != begin )
00346               res += sep_r;
00347             res += *iter;
00348           }
00349         return res;
00350       }
00351 
00353     template <class _Container>
00354       std::string join( const _Container & cont_r,
00355                         const std::string & sep_r = " " )
00356       { return join( cont_r.begin(), cont_r.end(), sep_r ); }
00358 
00360 
00368     std::string hexencode( const std::string & str_r );
00370     std::string hexdecode( const std::string & str_r );
00372 
00373 
00375 
00380     std::string toLower( const std::string & s );
00381 
00385     std::string toUpper( const std::string & s );
00387 
00389 
00394     enum Trim {
00395       NO_TRIM = 0x00,
00396       L_TRIM  = 0x01,
00397       R_TRIM  = 0x02,
00398       TRIM    = (L_TRIM|R_TRIM)
00399     };
00400 
00401     std::string  trim( const std::string & s, const Trim trim_r = TRIM );
00402 
00403     inline std::string ltrim( const std::string & s )
00404     { return trim( s, L_TRIM ); }
00405 
00406     inline std::string rtrim( const std::string & s )
00407     { return trim( s, R_TRIM ); }
00409 
00410     std::string stripFirstWord( std::string & line, const bool ltrim_first );
00411 
00412     std::string getline( std::istream & str, bool trim = false );
00413 
00414     std::string getline( std::istream & str, const Trim trim_r );
00415 
00417 
00422     inline bool hasPrefix( const std::string & str_r, const std::string & prefix_r )
00423     { return( str_r.substr( 0, prefix_r.size() ) == prefix_r ); }
00424 
00426     inline std::string stripPrefix( const std::string & str_r, const std::string & prefix_r )
00427     { return( hasPrefix( str_r, prefix_r ) ? str_r.substr( prefix_r.size() ) : str_r ); }
00428 
00430     inline bool hasSuffix( const std::string & str_r, const std::string & suffix_r )
00431     { return( str_r.size() >= suffix_r.size() && ::strncmp( str_r.c_str() + str_r.size() - suffix_r.size() , suffix_r.c_str(), suffix_r.size() ) == 0 ); }
00432 
00434     inline std::string stripSuffix( const std::string & str_r, const std::string & suffix_r )
00435     {
00436       if ( hasSuffix( str_r, suffix_r ) )
00437         return std::string( str_r.c_str(), str_r.size() - suffix_r.size() );
00438       return str_r;
00439     }
00441 
00443   } // namespace str
00446 } // namespace zypp
00448 #endif // ZYPP_BASE_STRING_H

Generated on Wed Feb 16 18:51:43 2011 for zypp by  doxygen 1.4.6