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 
00020 namespace zypp
00021 { 
00022 
00023 
00026   namespace str
00027   { 
00028 
00030 
00031     std::string form( const char * format, ... )
00032     __attribute__ ((format (printf, 1, 2)));
00033 
00035 
00039     std::string strerror( int errno_r );
00040 
00042 
00052     struct SafeBuf
00053     {
00054       char * _buf;
00055       SafeBuf() : _buf( 0 ) {}
00056       ~SafeBuf() { if ( _buf ) free( _buf ); }
00057       std::string asString() const
00058       { return _buf ? std::string(_buf) : std::string(); }
00059     };
00060 
00062 
00081     using boost::regex;
00082     using boost::regex_match;
00083     using boost::regex_search;
00084     using boost::regex_replace;
00085     using boost::match_results;
00086     using boost::match_extra;
00087     using boost::cmatch;
00088     using boost::wcmatch;
00089     using boost::smatch;
00090     using boost::wsmatch;
00092 
00094 
00107     inline std::string numstring( char n,               int w = 0 ) { return form( "%*hhd",  w, n ); }
00108     inline std::string numstring( unsigned char n,      int w = 0 ) { return form( "%*hhu",  w, n ); }
00109     inline std::string numstring( short n,              int w = 0 ) { return form( "%*hd",   w, n ); }
00110     inline std::string numstring( unsigned short n,     int w = 0 ) { return form( "%*hu",   w, n ); }
00111     inline std::string numstring( int n,                int w = 0 ) { return form( "%*d",    w, n ); }
00112     inline std::string numstring( unsigned n,           int w = 0 ) { return form( "%*u",    w, n ); }
00113     inline std::string numstring( long n,               int w = 0 ) { return form( "%*ld",   w, n ); }
00114     inline std::string numstring( unsigned long n,      int w = 0 ) { return form( "%*lu",   w, n ); }
00115     inline std::string numstring( long long n,          int w = 0 ) { return form( "%*lld",  w, n ); }
00116     inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu",  w, n ); }
00118 
00120 
00131     inline std::string hexstring( char n,               int w = 4 ) { return form( "%#0*hhx", w, n ); }
00132     inline std::string hexstring( unsigned char n,      int w = 4 ) { return form( "%#0*hhx", w, n ); }
00133     inline std::string hexstring( short n,              int w = 10 ){ return form( "%#0*hx",  w, n ); }
00134     inline std::string hexstring( unsigned short n,     int w = 10 ){ return form( "%#0*hx",  w, n ); }
00135     inline std::string hexstring( int n,                int w = 10 ){ return form( "%#0*x",   w, n ); }
00136     inline std::string hexstring( unsigned n,           int w = 10 ){ return form( "%#0*x",   w, n ); }
00137     inline std::string hexstring( long n,               int w = 10 ){ return form( "%#0*lx",  w, n ); }
00138     inline std::string hexstring( unsigned long n,      int w = 10 ){ return form( "%#0*lx",  w, n ); }
00139     inline std::string hexstring( long long n,          int w = 0 ) { return form( "%#0*llx", w, n ); }
00140     inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
00142 
00144 
00155     inline std::string octstring( char n,               int w = 4 ) { return form( "%#0*hho",  w, n ); }
00156     inline std::string octstring( unsigned char n,      int w = 4 ) { return form( "%#0*hho",  w, n ); }
00157     inline std::string octstring( short n,              int w = 5 ) { return form( "%#0*ho",   w, n ); }
00158     inline std::string octstring( unsigned short n,     int w = 5 ) { return form( "%#0*ho",   w, n ); }
00159     inline std::string octstring( int n,                int w = 5 ) { return form( "%#0*o",    w, n ); }
00160     inline std::string octstring( unsigned n,           int w = 5 ) { return form( "%#0*o",    w, n ); }
00161     inline std::string octstring( long n,               int w = 5 ) { return form( "%#0*lo",   w, n ); }
00162     inline std::string octstring( unsigned long n,      int w = 5 ) { return form( "%#0*lo",   w, n ); }
00163     inline std::string octstring( long long n,          int w = 0 ) { return form( "%#0*llo",  w, n ); }
00164     inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo",  w, n ); }
00166 
00168 
00177     template<typename _It>
00178       inline _It strtonum( const std::string & str );
00179 
00180     template<>
00181       inline short              strtonum( const std::string & str ) { return ::strtol  ( str.c_str(), NULL, 0 ); }
00182     template<>
00183       inline int                strtonum( const std::string & str ) { return ::strtol  ( str.c_str(), NULL, 0 ); }
00184     template<>
00185       inline long               strtonum( const std::string & str ) { return ::strtol  ( str.c_str(), NULL, 0 ); }
00186     template<>
00187       inline long long          strtonum( const std::string & str ) { return ::strtoll ( str.c_str(), NULL, 0 ); }
00188 
00189     template<>
00190       inline unsigned short     strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00191     template<>
00192       inline unsigned           strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00193     template<>
00194       inline unsigned long      strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00195     template<>
00196       inline unsigned long long strtonum( const std::string & str ) { return ::strtoull( str.c_str(), NULL, 0 ); }
00197 
00203     template<typename _It>
00204       inline _It strtonum( const std::string & str, _It & i )
00205       { return i = strtonum<_It>( str ); }
00207 
00209 
00213     bool strToTrue( const std::string & str );
00214 
00216     bool strToFalse( const std::string & str );
00217 
00222     inline bool strToBool( const std::string & str, bool default_r )
00223     { return( default_r ? strToFalse( str ) : strToTrue( str ) ); }
00225 
00227 
00238     template<class _OutputIterator>
00239       unsigned split( const std::string & line_r,
00240                       _OutputIterator     result_r,
00241                       const std::string & sepchars_r = " \t" )
00242       {
00243         const char * beg = line_r.c_str();
00244         const char * cur = beg;
00245         // skip leading sepchars
00246         while ( sepchars_r.find( *cur ) != std::string::npos )
00247           ++cur;
00248         unsigned ret = 0;
00249         for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
00250           {
00251             // skip non sepchars
00252             while( *cur && sepchars_r.find( *cur ) == std::string::npos )
00253               ++cur;
00254             // build string
00255             *result_r = std::string( beg, cur-beg );
00256             // skip sepchars
00257             while ( sepchars_r.find( *cur ) != std::string::npos )
00258               ++cur;
00259           }
00260         return ret;
00261       }
00263 
00265 
00268     template <class _Iterator>
00269       std::string join( _Iterator begin, _Iterator end,
00270                         const std::string & sep_r = " " )
00271       {
00272         std::string res;
00273         for ( _Iterator iter = begin; iter != end; ++ iter )
00274           {
00275             if ( iter != begin )
00276               res += sep_r;
00277             res += *iter;
00278           }
00279         return res;
00280       }
00281 
00283     template <class _Container>
00284       std::string join( const _Container & cont_r,
00285                         const std::string & sep_r = " " )
00286       { return join( cont_r.begin(), cont_r.end(), sep_r ); }
00288 
00289 
00291 
00296     std::string toLower( const std::string & s );
00297 
00301     std::string toUpper( const std::string & s );
00303 
00305 
00310     enum Trim {
00311       NO_TRIM = 0x00,
00312       L_TRIM  = 0x01,
00313       R_TRIM  = 0x02,
00314       TRIM    = (L_TRIM|R_TRIM)
00315     };
00316 
00317     std::string  trim( const std::string & s, const Trim trim_r = TRIM );
00318 
00319     inline std::string ltrim( const std::string & s )
00320     { return trim( s, L_TRIM ); }
00321 
00322     inline std::string rtrim( const std::string & s )
00323     { return trim( s, R_TRIM ); }
00325 
00326     std::string stripFirstWord( std::string & line, const bool ltrim_first );
00327 
00328     std::string getline( std::istream & str, bool trim = false );
00329 
00330     std::string getline( std::istream & str, const Trim trim_r );
00331 
00333 
00338     inline bool hasPrefix( const std::string & str_r, const std::string & prefix_r )
00339     { return( str_r.substr( 0, prefix_r.size() ) == prefix_r ); }
00340 
00342     inline std::string stripPrefix( const std::string & str_r, const std::string & prefix_r )
00343     { return( hasPrefix( str_r, prefix_r ) ? str_r.substr( prefix_r.size() ) : str_r ); }
00345 
00347   } // namespace str
00350 } // namespace zypp
00352 #endif // ZYPP_BASE_STRING_H

Generated on Thu Apr 24 02:24:48 2008 for zypp by  doxygen 1.4.6