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 
00226 
00232     inline std::string & replace_all( std::string & str, const std::string & from, const std::string & to )
00233     {
00234       std::string::size_type pos = 0;
00235       while( pos < str.length() && (pos = str.find( from, pos )) != std::string::npos )
00236       {
00237         str.replace( pos, from.size(), to );
00238         pos += to.size();
00239       }
00240       return str;
00241     }
00242 
00244 
00255     template<class _OutputIterator>
00256       unsigned split( const std::string & line_r,
00257                       _OutputIterator     result_r,
00258                       const std::string & sepchars_r = " \t" )
00259       {
00260         const char * beg = line_r.c_str();
00261         const char * cur = beg;
00262         // skip leading sepchars
00263         while ( sepchars_r.find( *cur ) != std::string::npos )
00264           ++cur;
00265         unsigned ret = 0;
00266         for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
00267           {
00268             // skip non sepchars
00269             while( *cur && sepchars_r.find( *cur ) == std::string::npos )
00270               ++cur;
00271             // build string
00272             *result_r = std::string( beg, cur-beg );
00273             // skip sepchars
00274             while ( sepchars_r.find( *cur ) != std::string::npos )
00275               ++cur;
00276           }
00277         return ret;
00278       }
00279 
00300     template<class _OutputIterator>
00301       unsigned splitFields( const std::string &   line_r,
00302                             _OutputIterator       result_r,
00303                             const std::string &   sepchars_r = ":" )
00304       {
00305         const char * beg = line_r.c_str();
00306         const char * cur = beg;
00307         unsigned ret = 0;
00308         for ( beg = cur; *beg; beg = cur, ++result_r )
00309           {
00310             // skip non sepchars
00311             while( *cur && !::strchr( sepchars_r.c_str(), *cur ) )
00312               ++cur;
00313             // build string
00314             *result_r = std::string( beg, cur-beg );
00315             ++ret;
00316             // skip sepchar
00317             if ( *cur )
00318             {
00319               ++cur;
00320               if ( ! *cur )                // ending with sepchar
00321               {
00322                 *result_r = std::string(); // add final empty field
00323                 ++ret;
00324                 break;
00325               }
00326             }
00327           }
00328         return ret;
00329       }
00331 
00333 
00336     template <class _Iterator>
00337       std::string join( _Iterator begin, _Iterator end,
00338                         const std::string & sep_r = " " )
00339       {
00340         std::string res;
00341         for ( _Iterator iter = begin; iter != end; ++ iter )
00342           {
00343             if ( iter != begin )
00344               res += sep_r;
00345             res += *iter;
00346           }
00347         return res;
00348       }
00349 
00351     template <class _Container>
00352       std::string join( const _Container & cont_r,
00353                         const std::string & sep_r = " " )
00354       { return join( cont_r.begin(), cont_r.end(), sep_r ); }
00356 
00358 
00366     std::string hexencode( const std::string & str_r );
00368     std::string hexdecode( const std::string & str_r );
00370 
00371 
00373 
00378     std::string toLower( const std::string & s );
00379 
00383     std::string toUpper( const std::string & s );
00385 
00387 
00392     enum Trim {
00393       NO_TRIM = 0x00,
00394       L_TRIM  = 0x01,
00395       R_TRIM  = 0x02,
00396       TRIM    = (L_TRIM|R_TRIM)
00397     };
00398 
00399     std::string  trim( const std::string & s, const Trim trim_r = TRIM );
00400 
00401     inline std::string ltrim( const std::string & s )
00402     { return trim( s, L_TRIM ); }
00403 
00404     inline std::string rtrim( const std::string & s )
00405     { return trim( s, R_TRIM ); }
00407 
00408     std::string stripFirstWord( std::string & line, const bool ltrim_first );
00409 
00410     std::string getline( std::istream & str, bool trim = false );
00411 
00412     std::string getline( std::istream & str, const Trim trim_r );
00413 
00415 
00420     inline bool hasPrefix( const std::string & str_r, const std::string & prefix_r )
00421     { return( str_r.substr( 0, prefix_r.size() ) == prefix_r ); }
00422 
00424     inline std::string stripPrefix( const std::string & str_r, const std::string & prefix_r )
00425     { return( hasPrefix( str_r, prefix_r ) ? str_r.substr( prefix_r.size() ) : str_r ); }
00427 
00429   } // namespace str
00432 } // namespace zypp
00434 #endif // ZYPP_BASE_STRING_H

Generated on Sat Sep 5 12:05:06 2009 for zypp by  doxygen 1.4.6