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 
00220     template<class _OutputIterator>
00221       unsigned split( const std::string & line_r,
00222                       _OutputIterator     result_r,
00223                       const std::string & sepchars_r = " \t" )
00224       {
00225         const char * beg = line_r.c_str();
00226         const char * cur = beg;
00227         // skip leading sepchars
00228         while ( sepchars_r.find( *cur ) != std::string::npos )
00229           ++cur;
00230         unsigned ret = 0;
00231         for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
00232           {
00233             // skip non sepchars
00234             while( *cur && sepchars_r.find( *cur ) == std::string::npos )
00235               ++cur;
00236             // build string
00237             *result_r = std::string( beg, cur-beg );
00238             // skip sepchars
00239             while ( sepchars_r.find( *cur ) != std::string::npos )
00240               ++cur;
00241           }
00242         return ret;
00243       }
00245 
00247 
00250     template <class _Iterator>
00251       std::string join( _Iterator begin, _Iterator end,
00252                         const std::string & sep_r = " " )
00253       {
00254         std::string res;
00255         for ( _Iterator iter = begin; iter != end; ++ iter )
00256           {
00257             if ( iter != begin )
00258               res += sep_r;
00259             res += *iter;
00260           }
00261         return res;
00262       }
00263 
00265     template <class _Container>
00266       std::string join( const _Container & cont_r,
00267                         const std::string & sep_r = " " )
00268       { return join( cont_r.begin(), cont_r.end(), sep_r ); }
00270 
00271 
00273 
00278     std::string toLower( const std::string & s );
00279 
00283     std::string toUpper( const std::string & s );
00285 
00287 
00292     enum Trim {
00293       NO_TRIM = 0x00,
00294       L_TRIM  = 0x01,
00295       R_TRIM  = 0x02,
00296       TRIM    = (L_TRIM|R_TRIM)
00297     };
00298 
00299     std::string  trim( const std::string & s, const Trim trim_r = TRIM );
00300 
00301     inline std::string ltrim( const std::string & s )
00302     { return trim( s, L_TRIM ); }
00303 
00304     inline std::string rtrim( const std::string & s )
00305     { return trim( s, R_TRIM ); }
00307 
00308     std::string stripFirstWord( std::string & line, const bool ltrim_first );
00309 
00310     std::string getline( std::istream & str, bool trim = false );
00311 
00312     std::string getline( std::istream & str, const Trim trim_r );
00313 
00315   } // namespace str
00318 } // namespace zypp
00320 #endif // ZYPP_BASE_STRING_H

Generated on Thu Jul 6 00:07:20 2006 for zypp by  doxygen 1.4.6