1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/String.h
12 #ifndef ZYPP_BASE_STRING_H
13 #define ZYPP_BASE_STRING_H
21 #include <boost/format.hpp>
23 #include "zypp/base/Easy.h"
24 #include "zypp/base/PtrTypes.h"
25 #include "zypp/base/Function.h"
28 ///////////////////////////////////////////////////////////////////
31 /** A formater with (N)o (A)rgument (C)heck.
32 * It won't complain about missing or excess arguments. Sometimes
33 * usefull when dealing with translations or classes providing a
36 inline format formatNAC( const std::string & string_r ) {
37 using namespace boost::io;
38 format fmter( string_r );
39 fmter.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
43 ///////////////////////////////////////////////////////////////////
45 ///////////////////////////////////////////////////////////////////
48 /** Request a human readable (translated) string representation of _Tp [_Tp.asUserString()]
49 * Classes may implement a default as member function.
52 std::string asUserString( const _Tp & val_r )
53 { return val_r.asUserString(); }
56 ///////////////////////////////////////////////////////////////////
58 ///////////////////////////////////////////////////////////////////
60 { /////////////////////////////////////////////////////////////////
62 /** Convenience \c char* constructible from \c std::string and \c char*,
63 * it maps \c (char*)0 to an empty string.
66 * bool hasPrefix( const std::string & str_r, const std::string & prefix_r )
67 * { return( ::strncmp( str_r.c_str(), prefix_r.c_str(), prefix_r.size() ) == 0 ); }
70 * Called with a plain \c char* as argument, the \c std::string is created form
71 * for nothing. The implementation actually does not use the \c std::string.
73 * Best would be to implement \c hasPrefix for each combination of \c char*
74 * and \c std::string arguments:
77 * bool hasPrefix( const std::string & str_r, const std::string & prefix_r )
78 * { return( ::strncmp( str_r.c_str(), prefix_r.c_str(), prefix_r.size() ) == 0 ); }
80 * bool hasPrefix( const std::string & str_r, const char * prefix_r )
81 * { return( !prefix_r || ::strncmp( str_r.c_str(), prefix_r, ::strlen(prefix_r) ) == 0 ); }
83 * bool hasPrefix( const char * str_r, const std::string & prefix_r )
84 * { return( str_r ? ::strncmp( str_r, prefix_r.c_str(), prefix_r.size() ) == 0 : prefix_r.empty() ); }
86 * bool hasPrefix( const char * str_r, const char * prefix_r )
87 * { return( str && prefix_r ? ::strncmp( str_r, prefix_r, ::strlen(prefix_r) ) == 0
88 * : !((str_r && *str_r) || (prefix_r && *prefix_r)); }
91 * This is where \ref C_Str can help. Constructible from \c std::string and \c char*,
92 * it \e reduces the \c std::string to it's \c char*. At the same time it converts
93 * \c (char*)0 into an \c "" string.
96 * bool hasPrefix( const C_Str & str_r, const C_Str & prefix_r )
97 * { return( ::strncmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }
103 typedef std::string::size_type size_type;
106 C_Str() : _val( 0 ), _sze( 0 ) {}
107 C_Str( char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
108 C_Str( const char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
109 C_Str( const std::string & str_r ) : _val( str_r.c_str() ), _sze( str_r.size() ) {}
112 bool isNull() const { return !_val; }
113 bool empty() const { return !(_val && *_val); }
114 size_type size() const
116 if ( _sze == std::string::npos )
117 { _sze = _val ? ::strlen( _val ) : 0; }
121 operator const char *() const { return c_str(); }
122 const char * c_str() const { return _val ? _val : ""; }
125 const char *const _val;
126 mutable size_type _sze;
129 /** \relates C_Str Stream output */
130 inline std::ostream & operator<<( std::ostream & str, const C_Str & obj )
131 { return str << obj.c_str(); }
133 ///////////////////////////////////////////////////////////////////
134 /** String related utilities and \ref ZYPP_STR_REGEX.
135 \see \ref ZYPP_STR_REGEX
138 { /////////////////////////////////////////////////////////////////
140 ///////////////////////////////////////////////////////////////////
142 * Global asString() that works with std::string too
144 inline std::string asString( const std::string &t )
147 #ifndef SWIG // Swig treats it as syntax error
148 inline std::string asString( std::string && t )
149 { return std::move(t); }
152 inline std::string asString( const char * t )
155 inline std::string asString( char * t )
159 inline std::string asString( const _T &t )
160 { return t.asString(); }
163 inline std::string asString( const intrusive_ptr<_T> &p )
164 { return p->asString(); }
167 inline std::string asString( const weak_ptr<_T> &p )
168 { return p->asString(); }
171 inline std::string asString( const bool &t )
172 { return t ? "true" : "false"; }
174 ///////////////////////////////////////////////////////////////////
175 /** Printf style construction of std::string. */
176 std::string form( const char * format, ... )
177 __attribute__ ((format (printf, 1, 2)));
179 ///////////////////////////////////////////////////////////////////
180 /** Return string describing the \a error_r code.
181 * Like ::strerror, but the numerical value is included in
182 * the string as well.
184 std::string strerror( int errno_r );
186 ///////////////////////////////////////////////////////////////////
187 /** Assert \c free called for allocated <tt>char *</tt>.
191 * vasprintf( &safe._buf, format, ap );
192 * return safe.asString();
200 SafeBuf() : _buf( 0 ) {}
201 ~SafeBuf() { if ( _buf ) free( _buf ); }
202 std::string asString() const
203 { return _buf ? std::string(_buf) : std::string(); }
206 ///////////////////////////////////////////////////////////////////
207 /** Convenient building of std::string via std::ostream::operator<<.
208 * Basically this is an \ref ostringstream which is autocenvertible
209 * into a \ref string.
211 * void fnc( const std::string & txt_r );
212 * fnc( str::Str() << "Hello " << 13 );
214 * std::string txt( str::Str() << 45 );
220 Str & operator<<( const _Tp & val )
221 { _str << val; return *this; }
223 operator std::string() const
224 { return _str.str(); }
226 std::ostream & stream()
230 { _str.str( std::string() ); }
232 std::ostringstream _str;
235 inline std::ostream & operator<<( std::ostream & str, const Str & obj )
236 { return str << (std::string)obj; }
238 ///////////////////////////////////////////////////////////////////
239 /** \name String representation of number.
241 * Optional second argument sets the minimal string width (' ' padded).
242 * Negative values will cause the number to be left adjusted within the string.
244 * Default width is 0.
246 * numstring(42) -> "42"
247 * numstring(42, 4) -> " 42"
248 * numstring(42,-4) -> "42 "
252 inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
253 inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
254 inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
255 inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
256 inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
257 inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
258 inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
259 inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
260 inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
261 inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
263 template<> inline std::string asString( const char & t ) { return numstring( t ); }
264 template<> inline std::string asString( const unsigned char & t ) { return numstring( t ); }
265 template<> inline std::string asString( const short & t ) { return numstring( t ); }
266 template<> inline std::string asString( const unsigned short & t ) { return numstring( t ); }
267 template<> inline std::string asString( const int & t ) { return numstring( t ); }
268 template<> inline std::string asString( const unsigned & t ) { return numstring( t ); }
269 template<> inline std::string asString( const long & t ) { return numstring( t ); }
270 template<> inline std::string asString( const unsigned long & t ) { return numstring( t ); }
271 template<> inline std::string asString( const long long & t ) { return numstring( t ); }
272 template<> inline std::string asString( const unsigned long long & t ) { return numstring( t ); }
275 ///////////////////////////////////////////////////////////////////
276 /** \name String representation of number as hex value with leading '0x'.
277 * Optional second argument sets the minimal
278 * string width (0 padded). Negative values will cause the number to be left adjusted
279 * within the string. Default width is 10 (4 for char).
281 * hexstring(42) -> "0x0000002a"
282 * hexstring(42, 4) -> "0x2a"
283 * hexstring(42,-4) -> "0x2a"
287 inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
288 inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
289 inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
290 inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
291 inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
292 inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
293 inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
294 inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
295 inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
296 inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
299 ///////////////////////////////////////////////////////////////////
300 /** \name String representation of number as octal value with leading '0'.
301 * Optional second argument sets the minimal
302 * string width (0 padded). Negative values will cause the number to be left adjusted
303 * within the string. Default width is 5 (4 for char).
305 * octstring(42) -> "00052"
306 * octstring(42, 4) -> "0052"
307 * octstring(42,-4) -> "052 "
311 inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
312 inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
313 inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
314 inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
315 inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
316 inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
317 inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
318 inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
319 inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
320 inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
323 ///////////////////////////////////////////////////////////////////
324 /** Parsing numbers from string.
327 /** String to integer type determined by template arg.
328 * \note Only specializations are defined.
330 * time_t t = strtonum<time_t>( "42" );
333 template<typename _It>
334 _It strtonum( const C_Str & str );
337 inline short strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
339 inline int strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
341 inline long strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
343 inline long long strtonum( const C_Str & str ) { return ::strtoll ( str, NULL, 0 ); }
346 inline unsigned short strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
348 inline unsigned strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
350 inline unsigned long strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
352 inline unsigned long long strtonum( const C_Str & str ) { return ::strtoull( str, NULL, 0 ); }
354 /** String to integer type detemined 2nd function arg \a i.
356 * time_t t; strtonum( "42", t );
359 template<typename _It>
360 inline _It strtonum( const C_Str & str, _It & i )
361 { return i = strtonum<_It>( str ); }
364 ///////////////////////////////////////////////////////////////////
365 /** Parsing boolean from string.
368 /** Return \c true if str is <tt>1, true, yes, on</tt> (or a nonzero number). */
369 bool strToTrue( const C_Str & str );
371 /** Return \c false if str is <tt>0, false, no, off</tt>. */
372 bool strToFalse( const C_Str & str );
374 /** Parse \c str into a bool depending on the default value.
375 * If the \c default is true, look for a legal \c false string.
376 * If the \c default is false, look for a legal \c true string.
378 inline bool strToBool( const C_Str & str, bool default_r )
379 { return( default_r ? strToFalse( str ) : strToTrue( str ) ); }
381 /** Parse \c str into a bool if it's a legal \c true or \c false string.
382 * If \c str is not a recognized \c true or \c false string, \a return_r
385 inline bool strToBoolNodefault( const C_Str & str, bool & return_r )
387 if ( strToTrue( str ) ) return (return_r = true);
388 if ( !strToFalse( str ) ) return (return_r = false);
395 * \short Return a string with all occurrences of \c from_r replaced with \c to_r.
397 std::string gsub( const std::string & str_r, const std::string & from_r, const std::string & to_r );
399 /** \overload A function is called on demand to compute each replacement value.
401 std::string gsubFun( const std::string & str_r, const std::string & from_r, function<std::string()> to_r );
404 * \short Replace all occurrences of \c from_r with \c to_r in \c str_r (inplace).
405 * A reference to \c str_r is also returned for convenience.
407 std::string & replaceAll( std::string & str_r, const std::string & from_r, const std::string & to_r );
409 /** \overload A function is called on demand to compute each replacement value.
411 std::string & replaceAllFun( std::string & str_r, const std::string & from_r, function<std::string()> to_r );
413 /** Enhance readability: insert gaps at regular distance
416 * Key Fingerprint: 22C07BA534178CD02EFE22AAB88B2FD43DBDC284
418 * Key Fingerprint: 22C07BA5 34178CD0 2EFE22AA B88B2FD4 3DBDC284
420 * Key Fingerprint: 22C0 7BA5 3417 8CD0 2EFE 22AA B88B 2FD4 3DBD C284
422 * Key Fingerprint: 22C0-7BA5-3417-8CD0-2EFE-22AA-B88B-2FD4-3DBD-C284
425 inline std::string gapify( std::string inp_r, std::string::size_type gap_r = 1, char gapchar = ' ' )
427 if ( gap_r && inp_r.size() > gap_r )
429 inp_r.reserve( inp_r.size() + (inp_r.size()-1)/gap_r );
430 for ( std::string::size_type pos = gap_r; pos < inp_r.size(); pos += gap_r+1 )
431 inp_r.insert( pos, 1, gapchar );
436 ///////////////////////////////////////////////////////////////////
439 /** Split \a line_r into words.
440 * Any sequence of characters in \a sepchars_r is treated as
441 * delimiter. The words are passed to OutputIterator \a result_r.
443 * std::vector<std::string> words;
444 * str::split( "some line", std::back_inserter(words) )
448 template<class _OutputIterator>
449 unsigned split( const C_Str & line_r,
450 _OutputIterator result_r,
451 const C_Str & sepchars_r = " \t" )
453 const char * beg = line_r;
454 const char * cur = beg;
455 // skip leading sepchars
456 while ( *cur && ::strchr( sepchars_r, *cur ) )
459 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
462 while( *cur && !::strchr( sepchars_r, *cur ) )
465 *result_r = std::string( beg, cur-beg );
467 while ( *cur && ::strchr( sepchars_r, *cur ) )
473 /** Split \a line_r into words with respect to escape delimeters.
474 * Any sequence of characters in \a sepchars_r is treated as
475 * delimiter if not inside "" or "" or escaped by \, but not \\.
476 * The words are passed to OutputIterator \a result_r.
478 * \see \ref splitEscaped
481 * std::vector<std::string> words;
482 * str::splitEscaped( "some line", std::back_inserter(words) )
486 * example splitted strings
487 * normal line -> 2 elements ( "normal", "line" )
488 * escaped\ line -> 1 element( "escaped line" )
489 * "quoted line" -> 1 element same as above
490 * 'quoted line' -> 1 element same as above
491 * "escaped quote\'" -> 1 element ( "escaped quote'" )
493 * \param line_r The string to parse.
495 * \param sepchars_r String of separator characters.
496 * \param withEmpty Whether to include empty fields between separators in the result.
500 template<class _OutputIterator>
501 unsigned splitEscaped( const C_Str & line_r,
502 _OutputIterator result_r,
503 const C_Str & sepchars_r = " \t",
504 bool withEmpty = false)
506 const char * beg = line_r;
507 const char * cur = beg;
510 // skip leading sepchars
511 while ( *cur && ::strchr( sepchars_r, *cur ) )
521 // there were only sepchars in the string
522 if (!*cur && withEmpty)
528 // after the leading sepchars
529 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
531 if ( *cur == '"' || *cur == '\'' )
533 char closeChar = *cur;
538 while ( *cur && *cur != closeChar)
542 return ret; //TODO parsing exception no closing quote
545 const char * esc = cur-1;
546 while ( esc != beg && *esc == '\\' )
551 cont = (escCount % 2 == 1); // find some non escaped escape char
555 std::string s( beg+1, cur-beg-2 ); //without quotes
556 //transform escaped escape
557 replaceAll( s, "\\\\", "\\" );
558 //transform escaped quotes (only same as open
559 char tmpn[2] = { closeChar, 0 };
560 char tmpo[3] = { '\\', closeChar, 0 };
561 replaceAll( s, tmpo, tmpn );
568 while( *cur && !::strchr( sepchars_r, *cur ) )
570 //ignore char after backslash
578 std::string s( beg, cur-beg );
579 //transform escaped escape
580 replaceAll( s, "\\\\", "\\" );
582 const char *delimeter = sepchars_r;
585 std::string ds("\\");
586 const char tmp[2] = { *delimeter, '\0' };
587 std::string del(tmp);
589 replaceAll( s, ds, del );
596 if ( *cur && ::strchr( sepchars_r, *cur ) )
598 while ( *cur && ::strchr( sepchars_r, *cur ) )
607 // the last was a separator => one more field
608 if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
617 /** Split \a line_r into fields.
618 * Any single character in \a sepchars_r is treated as a
619 * field separator unless \-escaped. The words are passed
626 * ":a" -> words 2 ||a|
627 * "a:" -> words 2 |a||
628 * ":a:" -> words 3 ||a||
633 * std::vector<std::string> words;
634 * str::split( "some line", std::back_inserter(words) )
638 template<class _OutputIterator>
639 unsigned splitFields( const C_Str & line_r,
640 _OutputIterator result_r,
641 const C_Str & sepchars_r = ":" )
643 const char * beg = line_r;
644 const char * cur = beg;
646 for ( beg = cur; *beg; beg = cur, ++result_r )
649 while( *cur && !::strchr( sepchars_r, *cur ) )
651 if ( *cur == '\\' && *(cur+1) )
656 *result_r = std::string( beg, cur-beg );
662 if ( ! *cur ) // ending with sepchar
664 *result_r = std::string(); // add final empty field
674 * Split \a line_r into fields handling also escaped separators.
677 * \see splitEscaped()
679 template<class _OutputIterator>
680 unsigned splitFieldsEscaped( const C_Str & line_r,
681 _OutputIterator result_r,
682 const C_Str & sepchars_r = ":" )
685 splitEscaped( line_r, result_r, sepchars_r, true /* withEmpty */ );
690 ///////////////////////////////////////////////////////////////////
693 /** Join strings using separator \a sep_r (defaults to BLANK). */
694 template <class _Iterator>
695 std::string join( _Iterator begin, _Iterator end,
696 const C_Str & sep_r = " " )
699 for ( _Iterator iter = begin; iter != end; ++ iter )
703 res += asString(*iter);
708 /** Join strings using separator \a sep_r (defaults to BLANK). */
709 template <class _Container>
710 std::string join( const _Container & cont_r,
711 const C_Str & sep_r = " " )
712 { return join( cont_r.begin(), cont_r.end(), sep_r ); }
714 /** Join strings using separator \a sep_r, quoting or escaping the values.
715 * Separator defaults to BLANK. Use \ref splitEscaped to restore the
718 template <class _Iterator>
719 std::string joinEscaped( _Iterator begin, _Iterator end,
720 const char sep_r = ' ' )
722 std::vector<char> buf;
723 for ( _Iterator iter = begin; iter != end; ++ iter )
726 buf.push_back( sep_r );
730 // empty string goes ""
731 buf.push_back( '"' );
732 buf.push_back( '"' );
736 std::string toadd( asString(*iter) );
737 for_( ch, toadd.begin(), toadd.end() )
744 buf.push_back( '\\' );
745 buf.push_back( *ch );
749 buf.push_back( '\\' );
750 buf.push_back( *ch );
755 return std::string( buf.begin(), buf.end() );
760 ///////////////////////////////////////////////////////////////////
763 /** Indent by string [" "] optionally wrap.
764 * Prints nothing for an empty string. Asserts a trainling '\n' on
765 * the last line. Optionally wrap lines at ' ' at a given length.
767 inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, const std::string & indent_r = " ", unsigned maxWitdh_r = 0 )
771 if ( indent_r.size() >= maxWitdh_r )
772 maxWitdh_r = 0; // nonsense: indent larger than line witdh
774 maxWitdh_r -= indent_r.size();
777 for ( const char * e = text_r.c_str(), * s = e; *e; s = ++e )
779 for ( ; *e && *e != '\n'; ++e ) ;/*searching*/
781 if ( maxWitdh_r && width > maxWitdh_r )
785 for ( e = s+width; e > s && *e != ' '; --e ) ;/*searching*/
787 width = e-s; // on a ' ', replaced by '\n'
789 e = s+width-1; // cut line;
792 str.write( s, width );
794 if ( !*e ) // on '\0'
799 /** \overload Indent by number of chars [' '] optionally wrap. */
800 inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, char indentch_r = ' ', unsigned maxWitdh_r = 0 )
801 { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
802 /** \overload Indent by number of chars [' '] wrap. */
803 inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r = ' ' )
804 { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
806 /** Prefix lines by string computed by function taking line begin/end [std::string(const char*, const char*)]
807 * Prints nothing for an empty string. Asserts a trainling '\n' on the last line.
809 inline std::ostream & autoPrefix( std::ostream & str, const std::string & text_r, function<std::string(const char*, const char*)> fnc_r )
811 for ( const char * e = text_r.c_str(); *e; ++e )
814 for ( ; *e && *e != '\n'; ++e ) /*searching*/;
815 str << fnc_r( s, e );
818 if ( !*e ) // on '\0'
823 /** \overload Prefix lines by string generated by function [std::string()] */
824 inline std::ostream & autoPrefix0( std::ostream & str, const std::string & text_r, function<std::string()> fnc_r )
826 auto wrap = [&fnc_r]( const char*, const char* )-> std::string {
829 return autoPrefix( str, text_r, wrap );
832 ///////////////////////////////////////////////////////////////////
836 * Escape desired character \a c using a backslash.
838 * For use when printing \a c separated values, and where
839 * \ref joinEscaped() is too heavy.
841 std::string escape( const C_Str & str_r, const char c = ' ' );
843 /** Escape \a next_r and append it to \a str_r using separator \a sep_r. */
844 inline void appendEscaped( std::string & str_r, const C_Str & next_r, const char sep_r = ' ' )
846 if ( ! str_r.empty() )
848 if ( next_r.empty() )
851 str_r += escape( next_r, sep_r );
857 ///////////////////////////////////////////////////////////////////
858 ///////////////////////////////////////////////////////////////////
860 * Encode all characters other than [a-zA-Z0-9] as %XX.
861 * This includes the % character itself, which becomes %25.
864 /** Encode all characters other than [a-zA-Z0-9] as %XX.
865 * This includes the % character itself, which becomes %25.
867 std::string hexencode( const C_Str & str_r );
868 /** Decode hexencoded %XX sequences. */
869 std::string hexdecode( const C_Str & str_r );
871 ///////////////////////////////////////////////////////////////////
873 /** \name Case conversion. */
875 /** Return lowercase version of \a s
878 std::string toLower( const std::string & s );
880 inline std::string toLower( const char * s )
881 { return( s ? toLower( std::string(s) ) : std::string() ); }
883 /** Return uppercase version of \a s
886 std::string toUpper( const std::string & s );
888 inline std::string toUpper( const char * s )
889 { return( s ? toUpper( std::string(s) ) : std::string() ); }
893 /** \name Case insensitive comparison. */
895 inline int compareCI( const C_Str & lhs, const C_Str & rhs )
896 { return ::strcasecmp( lhs, rhs ); }
899 /** \name Locate substring. */
901 /** Locate substring case sensitive. */
902 inline bool contains( const C_Str & str_r, const C_Str & val_r )
903 { return ::strstr( str_r, val_r ); }
904 /** Locate substring case insensitive. */
905 inline bool containsCI( const C_Str & str_r, const C_Str & val_r )
906 { return ::strcasestr( str_r, val_r ); }
909 ///////////////////////////////////////////////////////////////////
910 /** \name Trimming whitepace.
911 * \todo optimize l/r trim.
914 /** To define how to trim. */
919 TRIM = (L_TRIM|R_TRIM)
922 std::string trim( const std::string & s, const Trim trim_r = TRIM );
924 inline std::string ltrim( const std::string & s )
925 { return trim( s, L_TRIM ); }
927 inline std::string rtrim( const std::string & s )
928 { return trim( s, R_TRIM ); }
931 std::string stripFirstWord( std::string & line, const bool ltrim_first = true );
933 std::string stripLastWord( std::string & line, const bool rtrim_first = true );
935 /** Return stream content up to (but not returning) the next newline.
936 * \see \ref receiveUpTo
938 std::string getline( std::istream & str, bool trim = false );
940 /** Return stream content up to (but not returning) the next newline.
941 * \see \ref receiveUpTo
943 std::string getline( std::istream & str, const Trim trim_r );
945 /** Return stream content up to the next ocurrence of \c delim_r or EOF
946 * \c delim_r, if found, is always read from the stream. Whether it is
947 * also returned in the string depends on \c returnDelim_r.
948 * If the stream status is \c good, \c delim_r was found in the stream.
949 * If we reached EOF while looking for \c delim_r, \c eof is set; and
950 * also \c fail, if we did not read any data before.
952 std::string receiveUpTo( std::istream & str, const char delim_r, bool returnDelim_r = false );
954 ///////////////////////////////////////////////////////////////////
956 /** \name String prefix/suffix handling.
959 /** Return whether \a str_r has prefix \a prefix_r. */
960 inline bool hasPrefix( const C_Str & str_r, const C_Str & prefix_r )
961 { return( ::strncmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }
963 /** Strip a \a prefix_r from \a str_r and return the resulting string. */
964 inline std::string stripPrefix( const C_Str & str_r, const C_Str & prefix_r )
965 { return( hasPrefix( str_r, prefix_r ) ? str_r + prefix_r.size() : str_r.c_str() ); }
967 /** Return whether \a str_r has suffix \a suffix_r. */
968 inline bool hasSuffix( const C_Str & str_r, const C_Str & suffix_r )
969 { return( str_r.size() >= suffix_r.size() && ::strncmp( str_r + str_r.size() - suffix_r.size() , suffix_r, suffix_r.size() ) == 0 ); }
971 /** Strip a \a suffix_r from \a str_r and return the resulting string. */
972 inline std::string stripSuffix( const C_Str & str_r, const C_Str & suffix_r )
974 if ( hasSuffix( str_r, suffix_r ) )
975 return std::string( str_r, str_r.size() - suffix_r.size() );
976 return str_r.c_str();
978 /** Return size of the common prefix of \a lhs and \a rhs. */
979 inline std::string::size_type commonPrefix( const C_Str & lhs, const C_Str & rhs )
981 const char * lp = lhs.c_str();
982 const char * rp = rhs.c_str();
983 std::string::size_type ret = 0;
984 while ( *lp == *rp && *lp != '\0' )
985 { ++lp, ++rp, ++ret; }
989 /** alias for \ref hasPrefix */
990 inline bool startsWith( const C_Str & str_r, const C_Str & prefix_r )
991 { return hasPrefix( str_r, prefix_r ); }
992 /** alias for \ref hasSuffix */
993 inline bool endsWith( const C_Str & str_r, const C_Str & prefix_r )
994 { return hasSuffix( str_r, prefix_r ); }
996 /////////////////////////////////////////////////////////////////
998 ///////////////////////////////////////////////////////////////////
999 /////////////////////////////////////////////////////////////////
1001 ///////////////////////////////////////////////////////////////////
1002 #endif // ZYPP_BASE_STRING_H