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 
00233 
00239     inline std::string & replace_all( std::string & str, const std::string & from, const std::string & to )
00240     {
00241       std::string::size_type pos = 0;
00242       while( pos < str.length() && (pos = str.find( from, pos )) != std::string::npos )
00243       {
00244         str.replace( pos, from.size(), to );
00245         pos += to.size();
00246       }
00247       return str;
00248     }
00249 
00251 
00262     template<class _OutputIterator>
00263       unsigned split( const std::string & line_r,
00264                       _OutputIterator     result_r,
00265                       const std::string & sepchars_r = " \t" )
00266       {
00267         const char * beg = line_r.c_str();
00268         const char * cur = beg;
00269         // skip leading sepchars
00270         while ( sepchars_r.find( *cur ) != std::string::npos )
00271           ++cur;
00272         unsigned ret = 0;
00273         for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
00274           {
00275             // skip non sepchars
00276             while( *cur && sepchars_r.find( *cur ) == std::string::npos )
00277               ++cur;
00278             // build string
00279             *result_r = std::string( beg, cur-beg );
00280             // skip sepchars
00281             while ( cur != beg && sepchars_r.find( *cur ) != std::string::npos )
00282               ++cur;
00283           }
00284         return ret;
00285       }
00286 
00307     template<class _OutputIterator>
00308       unsigned splitFields( const std::string &   line_r,
00309                             _OutputIterator       result_r,
00310                             const std::string &   sepchars_r = ":" )
00311       {
00312         const char * beg = line_r.c_str();
00313         const char * cur = beg;
00314         unsigned ret = 0;
00315         for ( beg = cur; *beg; beg = cur, ++result_r )
00316           {
00317             // skip non sepchars
00318             while( *cur && !::strchr( sepchars_r.c_str(), *cur ) )
00319               ++cur;
00320             // build string
00321             *result_r = std::string( beg, cur-beg );
00322             ++ret;
00323             // skip sepchar
00324             if ( *cur )
00325             {
00326               ++cur;
00327               if ( ! *cur )                // ending with sepchar
00328               {
00329                 *result_r = std::string(); // add final empty field
00330                 ++ret;
00331                 break;
00332               }
00333             }
00334           }
00335         return ret;
00336       }
00338 
00340 
00343     template <class _Iterator>
00344       std::string join( _Iterator begin, _Iterator end,
00345                         const std::string & sep_r = " " )
00346       {
00347         std::string res;
00348         for ( _Iterator iter = begin; iter != end; ++ iter )
00349           {
00350             if ( iter != begin )
00351               res += sep_r;
00352             res += asString(*iter);
00353           }
00354         return res;
00355       }
00356 
00358     template <class _Container>
00359       std::string join( const _Container & cont_r,
00360                         const std::string & sep_r = " " )
00361       { return join( cont_r.begin(), cont_r.end(), sep_r ); }
00363 
00364 
00366 
00371     std::string toLower( const std::string & s );
00372 
00376     std::string toUpper( const std::string & s );
00378 
00380 
00385     enum Trim {
00386       NO_TRIM = 0x00,
00387       L_TRIM  = 0x01,
00388       R_TRIM  = 0x02,
00389       TRIM    = (L_TRIM|R_TRIM)
00390     };
00391 
00392     std::string  trim( const std::string & s, const Trim trim_r = TRIM );
00393 
00394     inline std::string ltrim( const std::string & s )
00395     { return trim( s, L_TRIM ); }
00396 
00397     inline std::string rtrim( const std::string & s )
00398     { return trim( s, R_TRIM ); }
00400 
00401     std::string stripFirstWord( std::string & line, const bool ltrim_first );
00402 
00403     std::string getline( std::istream & str, bool trim = false );
00404 
00405     std::string getline( std::istream & str, const Trim trim_r );
00406 
00407     inline bool startsWith(const std::string& s, const char* str) { return s.find(str) == 0; }
00408     inline bool endsWith(const std::string& s, const char* str) { return s.find(str) == s.size() - strlen(str); }
00409     inline bool contains(const std::string& s, const char* str) { return s.find(str) != std::string::npos; }
00410 
00416     std::string gsub( const std::string& sData, const std::string& sFrom, const std::string& sTo);
00417 
00418 
00420 
00425     inline bool hasPrefix( const std::string & str_r, const std::string & prefix_r )
00426     { return( str_r.substr( 0, prefix_r.size() ) == prefix_r ); }
00427 
00429     inline std::string stripPrefix( const std::string & str_r, const std::string & prefix_r )
00430     { return( hasPrefix( str_r, prefix_r ) ? str_r.substr( prefix_r.size() ) : str_r ); }
00432 
00433   } // namespace str
00436 } // namespace zypp
00438 #endif // ZYPP_BASE_STRING_H

Generated on Tue Oct 21 02:32:55 2008 for libzypp by  doxygen 1.5.3