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  __attribute__ ((format (printf, 1, 0)));
51 
52 inline std::string vform( const char * format, va_list ap ) {
53  char * buf = 0;
54  std::string val;
55 
56  int numprinted = vasprintf( &buf, format, ap );
57  if ( numprinted >= 0 ) {
58  val = buf;
59  free( buf );
60  }
61  return val;
62 }
63 
64 inline std::string form( const char * format, ... )
65  __attribute__ ((format (printf, 1, 2)));
66 
76 inline std::string form( const char * format, ... ) {
77  std::string val;
78 
79  va_list ap;
80  va_start( ap, format );
81 
82  val = vform(format, ap);
83 
84  va_end( ap );
85  return val;
86 }
87 
88 
89 
100 inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
101 inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
102 inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
103 inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
104 inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
105 inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
106 inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
107 inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
108 inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
109 inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
110 
121 inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
122 inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
123 inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
124 inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
125 inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
126 inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
127 inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
128 inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
129 inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
130 inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
131 
142 inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
143 inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
144 inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
145 inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
146 inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
147 inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
148 inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
149 inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
150 inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
151 inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
152 
156 template<typename _It>
157  inline _It strtonum( const std::string & str );
158 
159 template<>
160  inline short strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
161 template<>
162  inline int strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
163 template<>
164  inline long strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
165 template<>
166  inline long long strtonum( const std::string & str ) { return ::strtoll ( str.c_str(), NULL, 0 ); }
167 
168 template<>
169  inline unsigned short strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
170 template<>
171  inline unsigned strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
172 template<>
173  inline unsigned long strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
174 template<>
175  inline unsigned long long strtonum( const std::string & str ) { return ::strtoull( str.c_str(), NULL, 0 ); }
176 
180 template<typename _It>
181  inline _It strtonum( const std::string & str, _It & i ) { return i = strtonum<_It>( str ); }
182 
207 extern std::string getline( std::istream & str, bool trim = false );
208 
213 extern std::string getline( std::istream & str, const Trim trim_r );
214 
245 extern unsigned split( const std::string line_r,
246  std::vector<std::string> & words_r,
247  const std::string & sep_t = " \t",
248  const bool singlesep_r = false );
249 
253 extern std::string join( const std::vector<std::string> & words_r,
254  const std::string & sep_r = " " );
255 
256 
265 inline std::list<std::string> splitToLines( const std::string text_r, const std::string & sep_r = "\n" )
266 {
267  std::vector<std::string> lines;
268  stringutil::split( text_r, lines, sep_r, true );
269  std::list<std::string> ret;
270  for ( unsigned i = 0; i < lines.size(); ++i ) {
271  ret.push_back( lines[i] );
272  }
273  return ret;
274 }
275 
293 extern std::string stripFirstWord( std::string & value, const bool ltrim_first = false );
294 
298 extern std::string ltrim( const std::string & s );
299 extern std::string rtrim( const std::string & s );
300 inline std::string trim( const std::string & s, const Trim trim_r = TRIM ) {
301  switch ( trim_r ) {
302  case L_TRIM:
303  return ltrim( s );
304  case R_TRIM:
305  return rtrim( s );
306  case TRIM:
307  return ltrim( rtrim( s ) );
308  case NO_TRIM:
309  break;
310  }
311  return s;
312 }
313 
317 extern std::string toLower( const std::string & s );
318 extern std::string toUpper( const std::string & s );
319 
323 extern std::ostream & dumpOn( std::ostream & str, const std::list<std::string> & l, const bool numbered = false );
324 extern std::ostream & dumpOn( std::ostream & str, const std::vector<std::string> & l, const bool numbered = false );
325 
327 } // namespace stringutil
329 
330 #endif // stringutil_h
std::string trim(const std::string &s, const Trim trim_r=TRIM)
Definition: stringutil.h:300
#define str
Definition: scanner.cc:1003
Definition: stringutil.h:43
std::string hexstring(char n, int w=4)
Definition: stringutil.h:121
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
std::string format(const char *format,...) __attribute__((format(printf
Definition: IniParser.cc:1030
Definition: stringutil.h:44
std::list< std::string > splitToLines(const std::string text_r, const std::string &sep_r="\n")
Definition: stringutil.h:265
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:100
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:160
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:142
Definition: stringutil.h:39
std::string vform(const char *format, va_list ap) __attribute__((format(printf
Definition: stringutil.h:52
std::string form(const char *format,...) __attribute__((format(printf
Definition: stringutil.h:76
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.11