1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/String.h
12 #ifndef ZYPP_BASE_STRING_H
13 #define ZYPP_BASE_STRING_H
17 #include <boost/regex.hpp>
19 ///////////////////////////////////////////////////////////////////
21 { /////////////////////////////////////////////////////////////////
22 ///////////////////////////////////////////////////////////////////
23 /** String related utilities and \ref ZYPP_STR_REGEX.
24 \see \ref ZYPP_STR_REGEX
27 { /////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////
30 /** Printf style construction of std::string. */
31 std::string form( const char * format, ... )
32 __attribute__ ((format (printf, 1, 2)));
34 ///////////////////////////////////////////////////////////////////
35 /** Return string describing the \a error_r code.
36 * Like ::strerror, but the numerical value is included in
39 std::string strerror( int errno_r );
41 ///////////////////////////////////////////////////////////////////
42 /** Assert \c free called for allocated <tt>char *</tt>.
46 * vasprintf( &safe._buf, format, ap );
47 * return safe.asString();
55 SafeBuf() : _buf( 0 ) {}
56 ~SafeBuf() { if ( _buf ) free( _buf ); }
57 std::string asString() const
58 { return _buf ? std::string(_buf) : std::string(); }
61 ///////////////////////////////////////////////////////////////////
62 /** \defgroup ZYPP_STR_REGEX Regular expressions
64 * Namespace zypp::str regular expressions \b using the
66 * \url http://www.boost.org/libs/regex/doc/index.html.
71 * \li \c regex_replace
72 * \li \c match_results
82 using boost::regex_match;
83 using boost::regex_search;
84 using boost::regex_replace;
85 using boost::match_results;
86 using boost::match_extra;
94 * helper to debug regular expressions matches
96 std::ostream & dumpRegexpResult( const boost::smatch &what, std::ostream & str );
98 ///////////////////////////////////////////////////////////////////
99 /** \name String representation of number.
101 * Optional second argument sets the minimal string width (' ' padded).
102 * Negative values will cause the number to be left adjusted within the string.
104 * Default width is 0.
106 * numstring(42) -> "42"
107 * numstring(42, 4) -> " 42"
108 * numstring(42,-4) -> "42 "
112 inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
113 inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
114 inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
115 inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
116 inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
117 inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
118 inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
119 inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
120 inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
121 inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
124 ///////////////////////////////////////////////////////////////////
125 /** \name String representation of number as hex value with leading '0x'.
126 * Optional second argument sets the minimal
127 * string width (0 padded). Negative values will cause the number to be left adjusted
128 * within the string. Default width is 10 (4 for char).
130 * hexstring(42) -> "0x0000002a"
131 * hexstring(42, 4) -> "0x2a"
132 * hexstring(42,-4) -> "0x2a"
136 inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
137 inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
138 inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
139 inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
140 inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
141 inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
142 inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
143 inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
144 inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
145 inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
148 ///////////////////////////////////////////////////////////////////
149 /** \name String representation of number as octal value with leading '0'.
150 * Optional second argument sets the minimal
151 * string width (0 padded). Negative values will cause the number to be left adjusted
152 * within the string. Default width is 5 (4 for char).
154 * octstring(42) -> "00052"
155 * octstring(42, 4) -> "0052"
156 * octstring(42,-4) -> "052 "
160 inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
161 inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
162 inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
163 inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
164 inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
165 inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
166 inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
167 inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
168 inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
169 inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
172 ///////////////////////////////////////////////////////////////////
173 /** Parsing numbers from string.
176 /** String to integer type determined by template arg.
177 * \note Only specializations are defined.
179 * time_t t = strtonum<time_t>( "42" );
182 template<typename _It>
183 _It strtonum( const std::string & str );
186 inline short strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
188 inline int strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
190 inline long strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
192 inline long long strtonum( const std::string & str ) { return ::strtoll ( str.c_str(), NULL, 0 ); }
195 inline unsigned short strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
197 inline unsigned strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
199 inline unsigned long strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
201 inline unsigned long long strtonum( const std::string & str ) { return ::strtoull( str.c_str(), NULL, 0 ); }
203 /** String to integer type detemined 2nd function arg \a i.
205 * time_t t; strtonum( "42", t );
208 template<typename _It>
209 inline _It strtonum( const std::string & str, _It & i )
210 { return i = strtonum<_It>( str ); }
213 ///////////////////////////////////////////////////////////////////
216 /** Split \a line_r into words.
217 * Any sequence of characters in \a sepchars_r is treated as
218 * delimiter. The words are passed to OutputIterator \a result_r.
220 * std::vector<std::string> words;
221 * str::split( "some line", std::back_inserter(words) )
225 template<class _OutputIterator>
226 unsigned split( const std::string & line_r,
227 _OutputIterator result_r,
228 const std::string & sepchars_r = " \t" )
230 const char * beg = line_r.c_str();
231 const char * cur = beg;
232 // skip leading sepchars
233 while ( sepchars_r.find( *cur ) != std::string::npos )
236 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
239 while( *cur && sepchars_r.find( *cur ) == std::string::npos )
242 *result_r = std::string( beg, cur-beg );
244 while ( sepchars_r.find( *cur ) != std::string::npos )
251 ///////////////////////////////////////////////////////////////////
254 /** Join strings using separator \a sep_r (defaults to BLANK). */
255 template <class _Iterator>
256 std::string join( _Iterator begin, _Iterator end,
257 const std::string & sep_r = " " )
260 for ( _Iterator iter = begin; iter != end; ++ iter )
269 /** Join strings using separator \a sep_r (defaults to BLANK). */
270 template <class _Container>
271 std::string join( const _Container & cont_r,
272 const std::string & sep_r = " " )
273 { return join( cont_r.begin(), cont_r.end(), sep_r ); }
277 ///////////////////////////////////////////////////////////////////
278 /** \name Case conversion. */
280 /** Return lowercase version of \a s
283 std::string toLower( const std::string & s );
285 /** Return uppercase version of \a s
288 std::string toUpper( const std::string & s );
291 ///////////////////////////////////////////////////////////////////
292 /** \name Trimming whitepace.
293 * \todo optimize l/r trim.
296 /** To define how to trim. */
301 TRIM = (L_TRIM|R_TRIM)
304 std::string trim( const std::string & s, const Trim trim_r = TRIM );
306 inline std::string ltrim( const std::string & s )
307 { return trim( s, L_TRIM ); }
309 inline std::string rtrim( const std::string & s )
310 { return trim( s, R_TRIM ); }
313 std::string stripFirstWord( std::string & line, const bool ltrim_first );
315 std::string getline( std::istream & str, bool trim = false );
317 std::string getline( std::istream & str, const Trim trim_r );
319 ///////////////////////////////////////////////////////////////////
321 /** \name String prefix handling.
324 /** Return whether \a str_r has prefix \a prefix_r. */
325 inline bool hasPrefix( const std::string & str_r, const std::string & prefix_r )
326 { return( str_r.substr( 0, prefix_r.size() ) == prefix_r ); }
328 /** Strip a \a prefix_r from \a str_r and return the resulting string. */
329 inline std::string stripPrefix( const std::string & str_r, const std::string & prefix_r )
330 { return( hasPrefix( str_r, prefix_r ) ? str_r.substr( prefix_r.size() ) : str_r ); }
333 /////////////////////////////////////////////////////////////////
335 ///////////////////////////////////////////////////////////////////
336 /////////////////////////////////////////////////////////////////
338 ///////////////////////////////////////////////////////////////////
339 #endif // ZYPP_BASE_STRING_H