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 
00018 #include "zypp/base/PtrTypes.h"
00019 
00021 namespace zypp
00022 { 
00023 
00024 
00027   namespace str
00028   { 
00029 
00031 
00034     template<class _T>
00035         inline std::string asString( const _T &t )
00036         { return t.asString(); }
00037 
00038     template<class _T>
00039         inline std::string asString( const intrusive_ptr<_T> &p )
00040         { return p->asString(); }
00041 
00042     template<class _T>
00043         inline std::string asString( const weak_ptr<_T> &p )
00044         { return p->asString(); }
00045 
00046     template<>
00047         inline std::string asString( const std::string &t )
00048         { return t; }
00049 
00051 
00052     std::string form( const char * format, ... )
00053     __attribute__ ((format (printf, 1, 2)));
00054 
00056 
00060     std::string strerror( int errno_r );
00061 
00063 
00073     struct SafeBuf
00074     {
00075       char * _buf;
00076       SafeBuf() : _buf( 0 ) {}
00077       ~SafeBuf() { if ( _buf ) free( _buf ); }
00078       std::string asString() const
00079       { return _buf ? std::string(_buf) : std::string(); }
00080     };
00081 
00083 
00100 
00101 
00114     inline std::string numstring( char n,               int w = 0 ) { return form( "%*hhd",  w, n ); }
00115     inline std::string numstring( unsigned char n,      int w = 0 ) { return form( "%*hhu",  w, n ); }
00116     inline std::string numstring( short n,              int w = 0 ) { return form( "%*hd",   w, n ); }
00117     inline std::string numstring( unsigned short n,     int w = 0 ) { return form( "%*hu",   w, n ); }
00118     inline std::string numstring( int n,                int w = 0 ) { return form( "%*d",    w, n ); }
00119     inline std::string numstring( unsigned n,           int w = 0 ) { return form( "%*u",    w, n ); }
00120     inline std::string numstring( long n,               int w = 0 ) { return form( "%*ld",   w, n ); }
00121     inline std::string numstring( unsigned long n,      int w = 0 ) { return form( "%*lu",   w, n ); }
00122     inline std::string numstring( long long n,          int w = 0 ) { return form( "%*lld",  w, n ); }
00123     inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu",  w, n ); }
00125 
00127 
00138     inline std::string hexstring( char n,               int w = 4 ) { return form( "%#0*hhx", w, n ); }
00139     inline std::string hexstring( unsigned char n,      int w = 4 ) { return form( "%#0*hhx", w, n ); }
00140     inline std::string hexstring( short n,              int w = 10 ){ return form( "%#0*hx",  w, n ); }
00141     inline std::string hexstring( unsigned short n,     int w = 10 ){ return form( "%#0*hx",  w, n ); }
00142     inline std::string hexstring( int n,                int w = 10 ){ return form( "%#0*x",   w, n ); }
00143     inline std::string hexstring( unsigned n,           int w = 10 ){ return form( "%#0*x",   w, n ); }
00144     inline std::string hexstring( long n,               int w = 10 ){ return form( "%#0*lx",  w, n ); }
00145     inline std::string hexstring( unsigned long n,      int w = 10 ){ return form( "%#0*lx",  w, n ); }
00146     inline std::string hexstring( long long n,          int w = 0 ) { return form( "%#0*llx", w, n ); }
00147     inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
00149 
00151 
00162     inline std::string octstring( char n,               int w = 4 ) { return form( "%#0*hho",  w, n ); }
00163     inline std::string octstring( unsigned char n,      int w = 4 ) { return form( "%#0*hho",  w, n ); }
00164     inline std::string octstring( short n,              int w = 5 ) { return form( "%#0*ho",   w, n ); }
00165     inline std::string octstring( unsigned short n,     int w = 5 ) { return form( "%#0*ho",   w, n ); }
00166     inline std::string octstring( int n,                int w = 5 ) { return form( "%#0*o",    w, n ); }
00167     inline std::string octstring( unsigned n,           int w = 5 ) { return form( "%#0*o",    w, n ); }
00168     inline std::string octstring( long n,               int w = 5 ) { return form( "%#0*lo",   w, n ); }
00169     inline std::string octstring( unsigned long n,      int w = 5 ) { return form( "%#0*lo",   w, n ); }
00170     inline std::string octstring( long long n,          int w = 0 ) { return form( "%#0*llo",  w, n ); }
00171     inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo",  w, n ); }
00173 
00175 
00184     template<typename _It>
00185       _It strtonum( const std::string & str );
00186 
00187     template<>
00188       inline short              strtonum( const std::string & str ) { return ::strtol  ( str.c_str(), NULL, 0 ); }
00189     template<>
00190       inline int                strtonum( const std::string & str ) { return ::strtol  ( str.c_str(), NULL, 0 ); }
00191     template<>
00192       inline long               strtonum( const std::string & str ) { return ::strtol  ( str.c_str(), NULL, 0 ); }
00193     template<>
00194       inline long long          strtonum( const std::string & str ) { return ::strtoll ( str.c_str(), NULL, 0 ); }
00195 
00196     template<>
00197       inline unsigned short     strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00198     template<>
00199       inline unsigned           strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00200     template<>
00201       inline unsigned long      strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
00202     template<>
00203       inline unsigned long long strtonum( const std::string & str ) { return ::strtoull( str.c_str(), NULL, 0 ); }
00204 
00210     template<typename _It>
00211       inline _It strtonum( const std::string & str, _It & i )
00212       { return i = strtonum<_It>( str ); }
00214 
00216 
00220     bool strToTrue( const std::string & str );
00221 
00223     bool strToFalse( const std::string & str );
00224 
00229     inline bool strToBool( const std::string & str, bool default_r )
00230     { return( default_r ? strToFalse( str ) : strToTrue( str ) ); }
00232 
00234 
00245     template<class _OutputIterator>
00246       unsigned split( const std::string & line_r,
00247                       _OutputIterator     result_r,
00248                       const std::string & sepchars_r = " \t" )
00249       {
00250         const char * beg = line_r.c_str();
00251         const char * cur = beg;
00252         // skip leading sepchars
00253         while ( sepchars_r.find( *cur ) != std::string::npos )
00254           ++cur;
00255         unsigned ret = 0;
00256         for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
00257           {
00258             // skip non sepchars
00259             while( *cur && sepchars_r.find( *cur ) == std::string::npos )
00260               ++cur;
00261             // build string
00262             *result_r = std::string( beg, cur-beg );
00263             // skip sepchars
00264             while ( cur != beg && sepchars_r.find( *cur ) != std::string::npos )
00265               ++cur;
00266           }
00267         return ret;
00268       }
00270 
00272 
00275     template <class _Iterator>
00276       std::string join( _Iterator begin, _Iterator end,
00277                         const std::string & sep_r = " " )
00278       {
00279         std::string res;
00280         for ( _Iterator iter = begin; iter != end; ++ iter )
00281           {
00282             if ( iter != begin )
00283               res += sep_r;
00284             res += asString(*iter);
00285           }
00286         return res;
00287       }
00288 
00290     template <class _Container>
00291       std::string join( const _Container & cont_r,
00292                         const std::string & sep_r = " " )
00293       { return join( cont_r.begin(), cont_r.end(), sep_r ); }
00295 
00296 
00298 
00303     std::string toLower( const std::string & s );
00304 
00308     std::string toUpper( const std::string & s );
00310 
00312 
00317     enum Trim {
00318       NO_TRIM = 0x00,
00319       L_TRIM  = 0x01,
00320       R_TRIM  = 0x02,
00321       TRIM    = (L_TRIM|R_TRIM)
00322     };
00323 
00324     std::string  trim( const std::string & s, const Trim trim_r = TRIM );
00325 
00326     inline std::string ltrim( const std::string & s )
00327     { return trim( s, L_TRIM ); }
00328 
00329     inline std::string rtrim( const std::string & s )
00330     { return trim( s, R_TRIM ); }
00332 
00333     std::string stripFirstWord( std::string & line, const bool ltrim_first );
00334 
00335     std::string getline( std::istream & str, bool trim = false );
00336 
00337     std::string getline( std::istream & str, const Trim trim_r );
00338 
00339     inline bool startsWith(const std::string& s, const char* str) { return s.find(str) == 0; }
00340     inline bool endsWith(const std::string& s, const char* str) { return s.find(str) == s.size() - strlen(str); }
00341     inline bool contains(const std::string& s, const char* str) { return s.find(str) != std::string::npos; }
00342 
00348     std::string gsub( const std::string& sData, const std::string& sFrom, const std::string& sTo);
00349 
00350 
00352 
00357     inline bool hasPrefix( const std::string & str_r, const std::string & prefix_r )
00358     { return( str_r.substr( 0, prefix_r.size() ) == prefix_r ); }
00359 
00361     inline std::string stripPrefix( const std::string & str_r, const std::string & prefix_r )
00362     { return( hasPrefix( str_r, prefix_r ) ? str_r.substr( prefix_r.size() ) : str_r ); }
00364 
00365   } // namespace str
00368 } // namespace zypp
00370 #endif // ZYPP_BASE_STRING_H

Generated on Tue Sep 25 19:22:59 2007 for libzypp by  doxygen 1.5.3