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 
00208 
00214     inline std::string & replace_all( std::string & str, const std::string & from, const std::string & to )
00215     {
00216       std::string::size_type pos = 0;
00217       while( pos < str.length() && (pos = str.find( from, pos )) != std::string::npos )
00218       {
00219         str.replace( pos, from.size(), to );
00220         pos += to.size();
00221       }
00222       return str;
00223     }
00224 
00226 
00237     template<class _OutputIterator>
00238       unsigned split( const std::string & line_r,
00239                       _OutputIterator     result_r,
00240                       const std::string & sepchars_r = " \t" )
00241       {
00242         const char * beg = line_r.c_str();
00243         const char * cur = beg;
00244         // skip leading sepchars
00245         while ( sepchars_r.find( *cur ) != std::string::npos )
00246           ++cur;
00247         unsigned ret = 0;
00248         for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
00249           {
00250             // skip non sepchars
00251             while( *cur && sepchars_r.find( *cur ) == std::string::npos )
00252               ++cur;
00253             // build string
00254             *result_r = std::string( beg, cur-beg );
00255             // skip sepchars
00256             while ( sepchars_r.find( *cur ) != std::string::npos )
00257               ++cur;
00258           }
00259         return ret;
00260       }
00261 
00282     template<class _OutputIterator>
00283       unsigned splitFields( const std::string &   line_r,
00284                             _OutputIterator       result_r,
00285                             const std::string &   sepchars_r = ":" )
00286       {
00287         const char * beg = line_r.c_str();
00288         const char * cur = beg;
00289         unsigned ret = 0;
00290         for ( beg = cur; *beg; beg = cur, ++result_r )
00291           {
00292             // skip non sepchars
00293             while( *cur && !::strchr( sepchars_r.c_str(), *cur ) )
00294               ++cur;
00295             // build string
00296             *result_r = std::string( beg, cur-beg );
00297             ++ret;
00298             // skip sepchar
00299             if ( *cur )
00300             {
00301               ++cur;
00302               if ( ! *cur )                // ending with sepchar
00303               {
00304                 *result_r = std::string(); // add final empty field
00305                 ++ret;
00306                 break;
00307               }
00308             }
00309           }
00310         return ret;
00311       }
00313 
00315 
00318     template <class _Iterator>
00319       std::string join( _Iterator begin, _Iterator end,
00320                         const std::string & sep_r = " " )
00321       {
00322         std::string res;
00323         for ( _Iterator iter = begin; iter != end; ++ iter )
00324           {
00325             if ( iter != begin )
00326               res += sep_r;
00327             res += *iter;
00328           }
00329         return res;
00330       }
00331 
00333     template <class _Container>
00334       std::string join( const _Container & cont_r,
00335                         const std::string & sep_r = " " )
00336       { return join( cont_r.begin(), cont_r.end(), sep_r ); }
00338 
00339 
00341 
00346     std::string toLower( const std::string & s );
00347 
00351     std::string toUpper( const std::string & s );
00353 
00355 
00360     enum Trim {
00361       NO_TRIM = 0x00,
00362       L_TRIM  = 0x01,
00363       R_TRIM  = 0x02,
00364       TRIM    = (L_TRIM|R_TRIM)
00365     };
00366 
00367     std::string  trim( const std::string & s, const Trim trim_r = TRIM );
00368 
00369     inline std::string ltrim( const std::string & s )
00370     { return trim( s, L_TRIM ); }
00371 
00372     inline std::string rtrim( const std::string & s )
00373     { return trim( s, R_TRIM ); }
00375 
00376     std::string stripFirstWord( std::string & line, const bool ltrim_first );
00377 
00378     std::string getline( std::istream & str, bool trim = false );
00379 
00380     std::string getline( std::istream & str, const Trim trim_r );
00381 
00383 
00388     inline bool hasPrefix( const std::string & str_r, const std::string & prefix_r )
00389     { return( str_r.substr( 0, prefix_r.size() ) == prefix_r ); }
00390 
00392     inline std::string stripPrefix( const std::string & str_r, const std::string & prefix_r )
00393     { return( hasPrefix( str_r, prefix_r ) ? str_r.substr( prefix_r.size() ) : str_r ); }
00395 
00397   } // namespace str
00400 } // namespace zypp
00402 #endif // ZYPP_BASE_STRING_H

Generated on Fri Jul 4 16:57:56 2008 for zypp by  doxygen 1.5.0