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 vform( const char * format, va_list ap ) {
50  char * buf = 0;
51  std::string val;
52 
53  int numprinted = vasprintf( &buf, format, ap );
54  if ( numprinted >= 0 ) {
55  val = buf;
56  free( buf );
57  }
58  return val;
59 }
60 
61 inline std::string form( const char * format, ... )
62  __attribute__ ((format (printf, 1, 2)));
63 
73 inline std::string form( const char * format, ... ) {
74  std::string val;
75 
76  va_list ap;
77  va_start( ap, format );
78 
79  val = vform(format, ap);
80 
81  va_end( ap );
82  return val;
83 }
84 
85 
86 
97 inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
98 inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
99 inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
100 inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
101 inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
102 inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
103 inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
104 inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
105 inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
106 inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
107 
118 inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
119 inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
120 inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
121 inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
122 inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
123 inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
124 inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
125 inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
126 inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
127 inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
128 
139 inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
140 inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
141 inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
142 inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
143 inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
144 inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
145 inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
146 inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
147 inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
148 inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
149 
153 template<typename _It>
154  inline _It strtonum( const std::string & str );
155 
156 template<>
157  inline short strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
158 template<>
159  inline int strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
160 template<>
161  inline long strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
162 template<>
163  inline long long strtonum( const std::string & str ) { return ::strtoll ( str.c_str(), NULL, 0 ); }
164 
165 template<>
166  inline unsigned short strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
167 template<>
168  inline unsigned strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
169 template<>
170  inline unsigned long strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
171 template<>
172  inline unsigned long long strtonum( const std::string & str ) { return ::strtoull( str.c_str(), NULL, 0 ); }
173 
177 template<typename _It>
178  inline _It strtonum( const std::string & str, _It & i ) { return i = strtonum<_It>( str ); }
179 
204 extern std::string getline( std::istream & str, bool trim = false );
205 
210 extern std::string getline( std::istream & str, const Trim trim_r );
211 
242 extern unsigned split( const std::string line_r,
243  std::vector<std::string> & words_r,
244  const std::string & sep_t = " \t",
245  const bool singlesep_r = false );
246 
250 extern std::string join( const std::vector<std::string> & words_r,
251  const std::string & sep_r = " " );
252 
253 
262 inline std::list<std::string> splitToLines( const std::string text_r, const std::string & sep_r = "\n" )
263 {
264  std::vector<std::string> lines;
265  stringutil::split( text_r, lines, sep_r, true );
266  std::list<std::string> ret;
267  for ( unsigned i = 0; i < lines.size(); ++i ) {
268  ret.push_back( lines[i] );
269  }
270  return ret;
271 }
272 
290 extern std::string stripFirstWord( std::string & value, const bool ltrim_first = false );
291 
295 extern std::string ltrim( const std::string & s );
296 extern std::string rtrim( const std::string & s );
297 inline std::string trim( const std::string & s, const Trim trim_r = TRIM ) {
298  switch ( trim_r ) {
299  case L_TRIM:
300  return ltrim( s );
301  case R_TRIM:
302  return rtrim( s );
303  case TRIM:
304  return ltrim( rtrim( s ) );
305  case NO_TRIM:
306  break;
307  }
308  return s;
309 }
310 
314 extern std::string toLower( const std::string & s );
315 extern std::string toUpper( const std::string & s );
316 
320 extern std::ostream & dumpOn( std::ostream & str, const std::list<std::string> & l, const bool numbered = false );
321 extern std::ostream & dumpOn( std::ostream & str, const std::vector<std::string> & l, const bool numbered = false );
322 
324 } // namespace stringutil
326 
327 #endif // stringutil_h
std::string trim(const std::string &s, const Trim trim_r=TRIM)
Definition: stringutil.h:297
#define str
Definition: scanner.cc:997
Definition: stringutil.h:43
std::string hexstring(char n, int w=4)
Definition: stringutil.h:118
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:262
std::string vform(const char *format, va_list ap)
Definition: stringutil.h:49
static int variable_not_used __attribute__((unused))
std::string rtrim(const std::string &s)
Definition: stringutil.cc:207
std::string numstring(char n, int w=0)
Definition: stringutil.h:97
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:157
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:139
std::string format(const char *format,...)
Definition: IniParser.cc:1027
std::string form(const char *format,...) __attribute__((format(printf
Definition: stringutil.h:73
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.6