yast2-core
stringutil.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | |
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
8 | |
9 | core system |
10 | (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12 
13  File: stringutil.h
14 
15  Author: Michael Andres <ma@suse.de>
16  Maintainer: Michael Andres <ma@suse.de>
17 
18  Purpose: Contains 'std::string form(const char * format, ...)' for
19  printf style creation of strings and some more string utility
20  functions.
21 
22 /-*/
23 #ifndef stringutil_h
24 #define stringutil_h
25 
26 #include <cstdio>
27 #include <cstdarg>
28 #include <cstdlib>
29 
30 #include <iosfwd>
31 #include <vector>
32 #include <string>
33 #include <list>
34 
38 namespace stringutil {
40 ;
41 
42 enum Trim {
43  NO_TRIM = 0x00,
44  L_TRIM = 0x01,
45  R_TRIM = 0x02,
47 };
48 
49 inline std::string form( const char * format, ... )
50  __attribute__ ((format (printf, 1, 2)));
51 
61 inline std::string form( const char * format, ... ) {
62  char * buf = 0;
63  std::string val;
64 
65  va_list ap;
66  va_start( ap, format );
67 
68 #if 1
69  int numprinted = vasprintf( &buf, format, ap );
70  if ( numprinted >= 0 ) {
71  val = buf;
72  free( buf );
73  }
74 #else
75  // Don't know wheter we actually nedd two va_lists, one to
76  // evaluate the buffer size needed, and one to actually fill
77  // the buffer. Maybe there's a save way to reuse a va_lists.
78  va_list ap1;
79  va_start( ap1, format );
80  buf = new char[vsnprintf( NULL, 0, format, ap ) + 1];
81  vsprintf( buf, format, ap1 );
82  val = buf;
83  delete [] buf;
84  va_end( ap1 );
85 #endif
86 
87  va_end( ap );
88  return val;
89 }
90 
101 inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
102 inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
103 inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
104 inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
105 inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
106 inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
107 inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
108 inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
109 inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
110 inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
111 
122 inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
123 inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
124 inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
125 inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
126 inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
127 inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
128 inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
129 inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
130 inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
131 inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
132 
143 inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
144 inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
145 inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
146 inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
147 inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
148 inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
149 inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
150 inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
151 inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
152 inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
153 
157 template<typename _It>
158  inline _It strtonum( const std::string & str );
159 
160 template<>
161  inline short strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
162 template<>
163  inline int strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
164 template<>
165  inline long strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
166 template<>
167  inline long long strtonum( const std::string & str ) { return ::strtoll ( str.c_str(), NULL, 0 ); }
168 
169 template<>
170  inline unsigned short strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
171 template<>
172  inline unsigned strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
173 template<>
174  inline unsigned long strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
175 template<>
176  inline unsigned long long strtonum( const std::string & str ) { return ::strtoull( str.c_str(), NULL, 0 ); }
177 
181 template<typename _It>
182  inline _It strtonum( const std::string & str, _It & i ) { return i = strtonum<_It>( str ); }
183 
208 extern std::string getline( std::istream & str, bool trim = false );
209 
214 extern std::string getline( std::istream & str, const Trim trim_r );
215 
246 extern unsigned split( const std::string line_r,
247  std::vector<std::string> & words_r,
248  const std::string & sep_t = " \t",
249  const bool singlesep_r = false );
250 
254 extern std::string join( const std::vector<std::string> & words_r,
255  const std::string & sep_r = " " );
256 
257 
266 inline std::list<std::string> splitToLines( const std::string text_r, const std::string & sep_r = "\n" )
267 {
268  std::vector<std::string> lines;
269  stringutil::split( text_r, lines, sep_r, true );
270  std::list<std::string> ret;
271  for ( unsigned i = 0; i < lines.size(); ++i ) {
272  ret.push_back( lines[i] );
273  }
274  return ret;
275 }
276 
294 extern std::string stripFirstWord( std::string & value, const bool ltrim_first = false );
295 
299 extern std::string ltrim( const std::string & s );
300 extern std::string rtrim( const std::string & s );
301 inline std::string trim( const std::string & s, const Trim trim_r = TRIM ) {
302  switch ( trim_r ) {
303  case L_TRIM:
304  return ltrim( s );
305  case R_TRIM:
306  return rtrim( s );
307  case TRIM:
308  return ltrim( rtrim( s ) );
309  case NO_TRIM:
310  break;
311  }
312  return s;
313 }
314 
318 extern std::string toLower( const std::string & s );
319 extern std::string toUpper( const std::string & s );
320 
324 extern std::ostream & dumpOn( std::ostream & str, const std::list<std::string> & l, const bool numbered = false );
325 extern std::ostream & dumpOn( std::ostream & str, const std::vector<std::string> & l, const bool numbered = false );
326 
328 } // namespace stringutil
330 
331 #endif // stringutil_h
std::string trim(const std::string &s, const Trim trim_r=TRIM)
Definition: stringutil.h:301
#define str
Definition: scanner.cc:997
Definition: stringutil.h:43
std::string hexstring(char n, int w=4)
Definition: stringutil.h:122
std::string getline(std::istream &str, bool trim=false)
read one line from a stream Return one line read from istream. Afterwards the streampos is behind the...
Definition: stringutil.cc:60
Definition: stringutil.h:44
std::list< std::string > splitToLines(const std::string text_r, const std::string &sep_r="\n")
Definition: stringutil.h:266
std::string rtrim(const std::string &s)
Definition: stringutil.cc:207
std::string numstring(char n, int w=0)
Definition: stringutil.h:101
std::string ltrim(const std::string &s)
Definition: stringutil.cc:187
unsigned split(const std::string line_r, std::vector< std::string > &words_r, const std::string &sep_t=" \t", const bool singlesep_r=false)
Definition: stringutil.cc:73
_It strtonum(const std::string &str)
Definition: stringutil.h:161
std::string join(const std::vector< std::string > &words_r, const std::string &sep_r=" ")
Definition: stringutil.cc:128
std::string toUpper(const std::string &s)
Definition: stringutil.cc:248
std::string toLower(const std::string &s)
Definition: stringutil.cc:227
std::string octstring(char n, int w=4)
Definition: stringutil.h:143
std::string format(const char *format,...)
Definition: IniParser.cc:1027
std::string form(const char *format,...) __attribute__((format(printf
Definition: stringutil.h:61
std::string stripFirstWord(std::string &value, const bool ltrim_first=false)
Definition: stringutil.cc:151
Definition: stringutil.h:45
Trim
Definition: stringutil.h:42
std::ostream & dumpOn(std::ostream &str, const std::list< std::string > &l, const bool numbered=false)
Definition: stringutil.cc:269
Definition: stringutil.h:46

Generated on a sunny day for yast2-core by doxygen 1.8.5