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 {
00040
00041 inline std::string form( const char * format, ... )
00042 __attribute__ ((format (printf, 1, 2)));
00043
00053 inline std::string form( const char * format, ... ) {
00054 char * buf = 0;
00055 std::string val;
00056
00057 va_list ap;
00058 va_start( ap, format );
00059
00060 #if 1
00061 vasprintf( &buf, format, ap );
00062 if ( buf ) {
00063 val = buf;
00064 free( buf );
00065 }
00066 #else
00067
00068
00069
00070 va_list ap1;
00071 va_start( ap1, format );
00072 buf = new char[vsnprintf( NULL, 0, format, ap ) + 1];
00073 vsprintf( buf, format, ap1 );
00074 val = buf;
00075 delete [] buf;
00076 va_end( ap1 );
00077 #endif
00078
00079 va_end( ap );
00080 return val;
00081 }
00082
00093 inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
00094 inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
00095 inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
00096 inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
00097 inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
00098 inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
00099 inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
00100 inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
00101
00112 inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
00113 inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
00114 inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
00115 inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
00116 inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
00117 inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
00118 inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
00119 inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
00120
00131 inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
00132 inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
00133 inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
00134 inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
00135 inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
00136 inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
00137 inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
00138 inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
00139
00164 extern std::string getline( std::istream & str, const bool trimed = false );
00165
00196 extern unsigned split( const std::string line_r,
00197 std::vector<std::string> & words_r,
00198 const std::string & sep_t = " \t",
00199 const bool singlesep_r = false );
00200
00204 extern std::string join( const std::vector<std::string> & words_r,
00205 const std::string & sep_r = " " );
00206
00207
00216 inline std::list<std::string> splitToLines( const std::string text_r, const std::string & sep_r = "\n" )
00217 {
00218 std::vector<std::string> lines;
00219 stringutil::split( text_r, lines, "\n", true );
00220 std::list<std::string> ret;
00221 for ( unsigned i = 0; i < lines.size(); ++i ) {
00222 ret.push_back( lines[i] );
00223 }
00224 return ret;
00225 }
00226
00244 extern std::string stripFirstWord( std::string & value, const bool ltrim_first = false );
00245
00249 extern std::string ltrim( const std::string & s );
00250 extern std::string rtrim( const std::string & s );
00251 inline std::string trim( const std::string & s ) { return ltrim( rtrim( s ) ); }
00252
00256 extern std::string toLower( const std::string & s );
00257 extern std::string toUpper( const std::string & s );
00258
00262 extern std::ostream & dumpOn( std::ostream & str, const std::list<std::string> & l, const bool numbered = false );
00263 extern std::ostream & dumpOn( std::ostream & str, const std::vector<std::string> & l, const bool numbered = false );
00264
00266 }
00268
00269 #endif // stringutil_h