00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef stringutil_h
00024 #define stringutil_h
00025
00026 #include <cstdio>
00027 #include <cstdarg>
00028
00029 #include <iosfwd>
00030 #include <vector>
00031 #include <string>
00032 #include <list>
00033
00037
00038 namespace stringutil {
00039 ;
00040
00041 enum Trim {
00042 NO_TRIM = 0x00,
00043 L_TRIM = 0x01,
00044 R_TRIM = 0x02,
00045 TRIM = (L_TRIM|R_TRIM)
00046 };
00047
00048 inline std::string form( const char * format, ... )
00049 __attribute__ ((format (printf, 1, 2)));
00050
00060 inline std::string form( const char * format, ... ) {
00061 char * buf = 0;
00062 std::string val;
00063
00064 va_list ap;
00065 va_start( ap, format );
00066
00067 #if 1
00068 vasprintf( &buf, format, ap );
00069 if ( buf ) {
00070 val = buf;
00071 free( buf );
00072 }
00073 #else
00074
00075
00076
00077 va_list ap1;
00078 va_start( ap1, format );
00079 buf = new char[vsnprintf( NULL, 0, format, ap ) + 1];
00080 vsprintf( buf, format, ap1 );
00081 val = buf;
00082 delete [] buf;
00083 va_end( ap1 );
00084 #endif
00085
00086 va_end( ap );
00087 return val;
00088 }
00089
00100 inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
00101 inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
00102 inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
00103 inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
00104 inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
00105 inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
00106 inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
00107 inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
00108
00119 inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
00120 inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
00121 inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
00122 inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
00123 inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
00124 inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
00125 inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
00126 inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
00127
00138 inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
00139 inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
00140 inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
00141 inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
00142 inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
00143 inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
00144 inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
00145 inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
00146
00171 extern std::string getline( std::istream & str, bool trim = false );
00172
00177 extern std::string getline( std::istream & str, const Trim trim_r );
00178
00209 extern unsigned split( const std::string line_r,
00210 std::vector<std::string> & words_r,
00211 const std::string & sep_t = " \t",
00212 const bool singlesep_r = false );
00213
00217 extern std::string join( const std::vector<std::string> & words_r,
00218 const std::string & sep_r = " " );
00219
00220
00229 inline std::list<std::string> splitToLines( const std::string text_r, const std::string & sep_r = "\n" )
00230 {
00231 std::vector<std::string> lines;
00232 stringutil::split( text_r, lines, "\n", true );
00233 std::list<std::string> ret;
00234 for ( unsigned i = 0; i < lines.size(); ++i ) {
00235 ret.push_back( lines[i] );
00236 }
00237 return ret;
00238 }
00239
00257 extern std::string stripFirstWord( std::string & value, const bool ltrim_first = false );
00258
00262 extern std::string ltrim( const std::string & s );
00263 extern std::string rtrim( const std::string & s );
00264 inline std::string trim( const std::string & s, const Trim trim_r = TRIM ) {
00265 switch ( trim_r ) {
00266 case L_TRIM:
00267 return ltrim( s );
00268 case R_TRIM:
00269 return rtrim( s );
00270 case TRIM:
00271 return ltrim( rtrim( s ) );
00272 case NO_TRIM:
00273 break;
00274 }
00275 return s;
00276 }
00277
00281 extern std::string toLower( const std::string & s );
00282 extern std::string toUpper( const std::string & s );
00283
00287 extern std::ostream & dumpOn( std::ostream & str, const std::list<std::string> & l, const bool numbered = false );
00288 extern std::ostream & dumpOn( std::ostream & str, const std::vector<std::string> & l, const bool numbered = false );
00289
00291 }
00293
00294 #endif // stringutil_h