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 ///////////////////////////////////////////////////////////////////
47 { /////////////////////////////////////////////////////////////////
49 /** Convenience \c char* constructible from \c std::string and \c char*,
50 * it maps \c (char*)0 to an empty string.
53 * bool hasPrefix( const std::string & str_r, const std::string & prefix_r )
54 * { return( ::strncmp( str_r.c_str(), prefix_r.c_str(), prefix_r.size() ) == 0 ); }
57 * Called with a plain \c char* as argument, the \c std::string is created form
58 * for nothing. The implementation actually does not use the \c std::string.
60 * Best would be to implement \c hasPrefix for each combination of \c char*
61 * and \c std::string arguments:
64 * bool hasPrefix( const std::string & str_r, const std::string & prefix_r )
65 * { return( ::strncmp( str_r.c_str(), prefix_r.c_str(), prefix_r.size() ) == 0 ); }
67 * bool hasPrefix( const std::string & str_r, const char * prefix_r )
68 * { return( !prefix_r || ::strncmp( str_r.c_str(), prefix_r, ::strlen(prefix_r) ) == 0 ); }
70 * bool hasPrefix( const char * str_r, const std::string & prefix_r )
71 * { return( str_r ? ::strncmp( str_r, prefix_r.c_str(), prefix_r.size() ) == 0 : prefix_r.empty() ); }
73 * bool hasPrefix( const char * str_r, const char * prefix_r )
74 * { return( str && prefix_r ? ::strncmp( str_r, prefix_r, ::strlen(prefix_r) ) == 0
75 * : !((str_r && *str_r) || (prefix_r && *prefix_r)); }
78 * This is where \ref C_Str can help. Constructible from \c std::string and \c char*,
79 * it \e reduces the \c std::string to it's \c char*. At the same time it converts
80 * \c (char*)0 into an \c "" string.
83 * bool hasPrefix( const C_Str & str_r, const C_Str & prefix_r )
84 * { return( ::strncmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }
90 typedef std::string::size_type size_type;
93 C_Str() : _val( 0 ), _sze( 0 ) {}
94 C_Str( char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
95 C_Str( const char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
96 C_Str( const std::string & str_r ) : _val( str_r.c_str() ), _sze( str_r.size() ) {}
99 bool isNull() const { return !_val; }
100 bool empty() const { return !(_val && *_val); }
101 size_type size() const
103 if ( _sze == std::string::npos )
104 { _sze = _val ? ::strlen( _val ) : 0; }
108 operator const char *() const { return c_str(); }
109 const char * c_str() const { return _val ? _val : ""; }
112 const char *const _val;
113 mutable size_type _sze;
116 /** \relates C_Str Stream output */
117 inline std::ostream & operator<<( std::ostream & str, const C_Str & obj )
118 { return str << obj.c_str(); }
120 ///////////////////////////////////////////////////////////////////
121 /** String related utilities and \ref ZYPP_STR_REGEX.
122 \see \ref ZYPP_STR_REGEX
125 { /////////////////////////////////////////////////////////////////
127 ///////////////////////////////////////////////////////////////////
129 * Global asString() that works with std::string too
131 inline std::string asString( const std::string &t )
134 inline std::string asString( const char * t )
137 inline std::string asString( char * t )
141 inline std::string asString( const _T &t )
142 { return t.asString(); }
145 inline std::string asString( const intrusive_ptr<_T> &p )
146 { return p->asString(); }
149 inline std::string asString( const weak_ptr<_T> &p )
150 { return p->asString(); }
153 inline std::string asString( const bool &t )
154 { return t ? "+" : "-"; }
156 ///////////////////////////////////////////////////////////////////
157 /** Printf style construction of std::string. */
158 std::string form( const char * format, ... )
159 __attribute__ ((format (printf, 1, 2)));
161 ///////////////////////////////////////////////////////////////////
162 /** Return string describing the \a error_r code.
163 * Like ::strerror, but the numerical value is included in
164 * the string as well.
166 std::string strerror( int errno_r );
168 ///////////////////////////////////////////////////////////////////
169 /** Assert \c free called for allocated <tt>char *</tt>.
173 * vasprintf( &safe._buf, format, ap );
174 * return safe.asString();
182 SafeBuf() : _buf( 0 ) {}
183 ~SafeBuf() { if ( _buf ) free( _buf ); }
184 std::string asString() const
185 { return _buf ? std::string(_buf) : std::string(); }
188 ///////////////////////////////////////////////////////////////////
189 /** Convenient building of std::string via std::ostream::operator<<.
190 * Basically this is an \ref ostringstream which is autocenvertible
191 * into a \ref string.
193 * void fnc( const std::string & txt_r );
194 * fnc( str::Str() << "Hello " << 13 );
196 * std::string txt( str::Str() << 45 );
202 Str & operator<<( const _Tp & val )
203 { _str << val; return *this; }
205 operator std::string() const
206 { return _str.str(); }
208 std::ostringstream _str;
211 ///////////////////////////////////////////////////////////////////
212 /** \name String representation of number.
214 * Optional second argument sets the minimal string width (' ' padded).
215 * Negative values will cause the number to be left adjusted within the string.
217 * Default width is 0.
219 * numstring(42) -> "42"
220 * numstring(42, 4) -> " 42"
221 * numstring(42,-4) -> "42 "
225 inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
226 inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
227 inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
228 inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
229 inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
230 inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
231 inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
232 inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
233 inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
234 inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
236 template<> inline std::string asString( const char & t ) { return numstring( t ); }
237 template<> inline std::string asString( const unsigned char & t ) { return numstring( t ); }
238 template<> inline std::string asString( const short & t ) { return numstring( t ); }
239 template<> inline std::string asString( const unsigned short & t ) { return numstring( t ); }
240 template<> inline std::string asString( const int & t ) { return numstring( t ); }
241 template<> inline std::string asString( const unsigned & t ) { return numstring( t ); }
242 template<> inline std::string asString( const long & t ) { return numstring( t ); }
243 template<> inline std::string asString( const unsigned long & t ) { return numstring( t ); }
244 template<> inline std::string asString( const long long & t ) { return numstring( t ); }
245 template<> inline std::string asString( const unsigned long long & t ) { return numstring( t ); }
248 ///////////////////////////////////////////////////////////////////
249 /** \name String representation of number as hex value with leading '0x'.
250 * Optional second argument sets the minimal
251 * string width (0 padded). Negative values will cause the number to be left adjusted
252 * within the string. Default width is 10 (4 for char).
254 * hexstring(42) -> "0x0000002a"
255 * hexstring(42, 4) -> "0x2a"
256 * hexstring(42,-4) -> "0x2a"
260 inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
261 inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
262 inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
263 inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
264 inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
265 inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
266 inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
267 inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
268 inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
269 inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
272 ///////////////////////////////////////////////////////////////////
273 /** \name String representation of number as octal value with leading '0'.
274 * Optional second argument sets the minimal
275 * string width (0 padded). Negative values will cause the number to be left adjusted
276 * within the string. Default width is 5 (4 for char).
278 * octstring(42) -> "00052"
279 * octstring(42, 4) -> "0052"
280 * octstring(42,-4) -> "052 "
284 inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
285 inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
286 inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
287 inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
288 inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
289 inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
290 inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
291 inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
292 inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
293 inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
296 ///////////////////////////////////////////////////////////////////
297 /** Parsing numbers from string.
300 /** String to integer type determined by template arg.
301 * \note Only specializations are defined.
303 * time_t t = strtonum<time_t>( "42" );
306 template<typename _It>
307 _It strtonum( const C_Str & str );
310 inline short strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
312 inline int strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
314 inline long strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
316 inline long long strtonum( const C_Str & str ) { return ::strtoll ( str, NULL, 0 ); }
319 inline unsigned short strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
321 inline unsigned strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
323 inline unsigned long strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
325 inline unsigned long long strtonum( const C_Str & str ) { return ::strtoull( str, NULL, 0 ); }
327 /** String to integer type detemined 2nd function arg \a i.
329 * time_t t; strtonum( "42", t );
332 template<typename _It>
333 inline _It strtonum( const C_Str & str, _It & i )
334 { return i = strtonum<_It>( str ); }
337 ///////////////////////////////////////////////////////////////////
338 /** Parsing boolean from string.
341 /** Return \c true if str is <tt>1, true, yes, on</tt> (or a nonzero number). */
342 bool strToTrue( const C_Str & str );
344 /** Return \c false if str is <tt>0, false, no, off</tt>. */
345 bool strToFalse( const C_Str & str );
347 /** Parse \c str into a bool depending on the default value.
348 * If the \c default is true, look for a legal \c false string.
349 * If the \c default is false, look for a legal \c true string.
351 inline bool strToBool( const C_Str & str, bool default_r )
352 { return( default_r ? strToFalse( str ) : strToTrue( str ) ); }
354 /** Parse \c str into a bool if it's a legal \c true or \c false string.
355 * If \c str is not a recognized \c true or \c false string, \a return_r
358 inline bool strToBoolNodefault( const C_Str & str, bool & return_r )
360 if ( strToTrue( str ) ) return (return_r = true);
361 if ( !strToFalse( str ) ) return (return_r = false);
368 * \short Return a string with all occurrences of \c from_r replaced with \c to_r.
370 std::string gsub( const std::string & str_r, const std::string & from_r, const std::string & to_r );
372 /** \overload A function is called on demand to compute each replacement value.
374 std::string gsubFun( const std::string & str_r, const std::string & from_r, function<std::string()> to_r );
377 * \short Replace all occurrences of \c from_r with \c to_r in \c str_r (inplace).
378 * A reference to \c str_r is also returned for convenience.
380 std::string & replaceAll( std::string & str_r, const std::string & from_r, const std::string & to_r );
382 /** \overload A function is called on demand to compute each replacement value.
384 std::string & replaceAllFun( std::string & str_r, const std::string & from_r, function<std::string()> to_r );
387 ///////////////////////////////////////////////////////////////////
390 /** Split \a line_r into words.
391 * Any sequence of characters in \a sepchars_r is treated as
392 * delimiter. The words are passed to OutputIterator \a result_r.
394 * std::vector<std::string> words;
395 * str::split( "some line", std::back_inserter(words) )
399 template<class _OutputIterator>
400 unsigned split( const C_Str & line_r,
401 _OutputIterator result_r,
402 const C_Str & sepchars_r = " \t" )
404 const char * beg = line_r;
405 const char * cur = beg;
406 // skip leading sepchars
407 while ( *cur && ::strchr( sepchars_r, *cur ) )
410 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
413 while( *cur && !::strchr( sepchars_r, *cur ) )
416 *result_r = std::string( beg, cur-beg );
418 while ( *cur && ::strchr( sepchars_r, *cur ) )
424 /** Split \a line_r into words with respect to escape delimeters.
425 * Any sequence of characters in \a sepchars_r is treated as
426 * delimiter if not inside "" or "" or escaped by \, but not \\.
427 * The words are passed to OutputIterator \a result_r.
429 * \see \ref splitEscaped
432 * std::vector<std::string> words;
433 * str::splitEscaped( "some line", std::back_inserter(words) )
437 * example splitted strings
438 * normal line -> 2 elements ( "normal", "line" )
439 * escaped\ line -> 1 element( "escaped line" )
440 * "quoted line" -> 1 element same as above
441 * 'quoted line' -> 1 element same as above
442 * "escaped quote\'" -> 1 element ( "escaped quote'" )
444 * \param line_r The string to parse.
446 * \param sepchars_r String of separator characters.
447 * \param withEmpty Whether to include empty fields between separators in the result.
451 template<class _OutputIterator>
452 unsigned splitEscaped( const C_Str & line_r,
453 _OutputIterator result_r,
454 const C_Str & sepchars_r = " \t",
455 bool withEmpty = false)
457 const char * beg = line_r;
458 const char * cur = beg;
461 // skip leading sepchars
462 while ( *cur && ::strchr( sepchars_r, *cur ) )
472 // there were only sepchars in the string
473 if (!*cur && withEmpty)
479 // after the leading sepchars
480 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
482 if ( *cur == '"' || *cur == '\'' )
484 char closeChar = *cur;
489 while ( *cur && *cur != closeChar)
493 return ret; //TODO parsing exception no closing quote
496 const char * esc = cur-1;
497 while ( esc != beg && *esc == '\\' )
502 cont = (escCount % 2 == 1); // find some non escaped escape char
506 std::string s( beg+1, cur-beg-2 ); //without quotes
507 //transform escaped escape
508 replaceAll( s, "\\\\", "\\" );
509 //transform escaped quotes (only same as open
510 char tmpn[2] = { closeChar, 0 };
511 char tmpo[3] = { '\\', closeChar, 0 };
512 replaceAll( s, tmpo, tmpn );
519 while( *cur && !::strchr( sepchars_r, *cur ) )
521 //ignore char after backslash
529 std::string s( beg, cur-beg );
530 //transform escaped escape
531 replaceAll( s, "\\\\", "\\" );
533 const char *delimeter = sepchars_r;
536 std::string ds("\\");
537 const char tmp[2] = { *delimeter, '\0' };
538 std::string del(tmp);
540 replaceAll( s, ds, del );
547 if ( *cur && ::strchr( sepchars_r, *cur ) )
549 while ( *cur && ::strchr( sepchars_r, *cur ) )
558 // the last was a separator => one more field
559 if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
568 /** Split \a line_r into fields.
569 * Any single character in \a sepchars_r is treated as a
570 * field separator. The words are passed to OutputIterator
576 * ":a" -> words 2 ||a|
577 * "a:" -> words 2 |a||
578 * ":a:" -> words 3 ||a||
583 * std::vector<std::string> words;
584 * str::split( "some line", std::back_inserter(words) )
588 template<class _OutputIterator>
589 unsigned splitFields( const C_Str & line_r,
590 _OutputIterator result_r,
591 const C_Str & sepchars_r = ":" )
593 const char * beg = line_r;
594 const char * cur = beg;
596 for ( beg = cur; *beg; beg = cur, ++result_r )
599 while( *cur && !::strchr( sepchars_r, *cur ) )
602 *result_r = std::string( beg, cur-beg );
608 if ( ! *cur ) // ending with sepchar
610 *result_r = std::string(); // add final empty field
620 * Split \a line_r into fields handling also escaped separators.
623 * \see splitEscaped()
625 template<class _OutputIterator>
626 unsigned splitFieldsEscaped( const C_Str & line_r,
627 _OutputIterator result_r,
628 const C_Str & sepchars_r = ":" )
631 splitEscaped( line_r, result_r, sepchars_r, true /* withEmpty */ );
636 ///////////////////////////////////////////////////////////////////
639 /** Join strings using separator \a sep_r (defaults to BLANK). */
640 template <class _Iterator>
641 std::string join( _Iterator begin, _Iterator end,
642 const C_Str & sep_r = " " )
645 for ( _Iterator iter = begin; iter != end; ++ iter )
649 res += asString(*iter);
654 /** Join strings using separator \a sep_r (defaults to BLANK). */
655 template <class _Container>
656 std::string join( const _Container & cont_r,
657 const C_Str & sep_r = " " )
658 { return join( cont_r.begin(), cont_r.end(), sep_r ); }
660 /** Join strings using separator \a sep_r, quoting or escaping the values.
661 * Separator defaults to BLANK. Use \ref splitEscaped to restore the
664 template <class _Iterator>
665 std::string joinEscaped( _Iterator begin, _Iterator end,
666 const char sep_r = ' ' )
668 std::vector<char> buf;
669 for ( _Iterator iter = begin; iter != end; ++ iter )
672 buf.push_back( sep_r );
676 // empty string goes ""
677 buf.push_back( '"' );
678 buf.push_back( '"' );
682 std::string toadd( asString(*iter) );
683 for_( ch, toadd.begin(), toadd.end() )
690 buf.push_back( '\\' );
691 buf.push_back( *ch );
695 buf.push_back( '\\' );
696 buf.push_back( *ch );
701 return std::string( buf.begin(), buf.end() );
706 ///////////////////////////////////////////////////////////////////
710 * Escape desired character \a c using a backslash.
712 * For use when printing \a c separated values, and where
713 * \ref joinEscaped() is too heavy.
715 std::string escape( const C_Str & str_r, const char c = ' ' );
717 /** Escape \a next_r and append it to \a str_r using separator \a sep_r. */
718 inline void appendEscaped( std::string & str_r, const C_Str & next_r, const char sep_r = ' ' )
720 if ( ! str_r.empty() )
722 if ( next_r.empty() )
725 str_r += escape( next_r, sep_r );
731 ///////////////////////////////////////////////////////////////////
732 ///////////////////////////////////////////////////////////////////
734 * Encode all characters other than [a-zA-Z0-9] as %XX.
735 * This includes the % character itself, which becomes %25.
738 /** Encode all characters other than [a-zA-Z0-9] as %XX.
739 * This includes the % character itself, which becomes %25.
741 std::string hexencode( const C_Str & str_r );
742 /** Decode hexencoded %XX sequences. */
743 std::string hexdecode( const C_Str & str_r );
745 ///////////////////////////////////////////////////////////////////
747 /** \name Case conversion. */
749 /** Return lowercase version of \a s
752 std::string toLower( const std::string & s );
754 inline std::string toLower( const char * s )
755 { return( s ? toLower( std::string(s) ) : std::string() ); }
757 /** Return uppercase version of \a s
760 std::string toUpper( const std::string & s );
762 inline std::string toUpper( const char * s )
763 { return( s ? toUpper( std::string(s) ) : std::string() ); }
767 /** \name Case insensitive comparison. */
769 inline int compareCI( const C_Str & lhs, const C_Str & rhs )
770 { return ::strcasecmp( lhs, rhs ); }
773 /** \name Locate substring. */
775 /** Locate substring case sensitive. */
776 inline bool contains( const C_Str & str_r, const C_Str & val_r )
777 { return ::strstr( str_r, val_r ); }
778 /** Locate substring case insensitive. */
779 inline bool containsCI( const C_Str & str_r, const C_Str & val_r )
780 { return ::strcasestr( str_r, val_r ); }
783 ///////////////////////////////////////////////////////////////////
784 /** \name Trimming whitepace.
785 * \todo optimize l/r trim.
788 /** To define how to trim. */
793 TRIM = (L_TRIM|R_TRIM)
796 std::string trim( const std::string & s, const Trim trim_r = TRIM );
798 inline std::string ltrim( const std::string & s )
799 { return trim( s, L_TRIM ); }
801 inline std::string rtrim( const std::string & s )
802 { return trim( s, R_TRIM ); }
805 std::string stripFirstWord( std::string & line, const bool ltrim_first );
807 std::string stripLastWord( std::string & line, const bool rtrim_first );
809 /** Return stream content up to (but not returning) the next newline.
810 * \see \ref receiveUpTo
812 std::string getline( std::istream & str, bool trim = false );
814 /** Return stream content up to (but not returning) the next newline.
815 * \see \ref receiveUpTo
817 std::string getline( std::istream & str, const Trim trim_r );
819 /** Return stream content up to the next ocurrence of \c delim_r or EOF
820 * \c delim_r, if found, is always read from the stream. Whether it is
821 * also returned in the string depends on \c returnDelim_r.
822 * If the stream status is \c good, \c delim_r was found in the stream.
823 * If we reached EOF while looking for \c delim_r, \c eof is set; and
824 * also \c fail, if we did not read any data before.
826 std::string receiveUpTo( std::istream & str, const char delim_r, bool returnDelim_r = false );
828 ///////////////////////////////////////////////////////////////////
830 /** \name String prefix/suffix handling.
833 /** Return whether \a str_r has prefix \a prefix_r. */
834 inline bool hasPrefix( const C_Str & str_r, const C_Str & prefix_r )
835 { return( ::strncmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }
837 /** Strip a \a prefix_r from \a str_r and return the resulting string. */
838 inline std::string stripPrefix( const C_Str & str_r, const C_Str & prefix_r )
839 { return( hasPrefix( str_r, prefix_r ) ? str_r + prefix_r.size() : str_r.c_str() ); }
841 /** Return whether \a str_r has suffix \a suffix_r. */
842 inline bool hasSuffix( const C_Str & str_r, const C_Str & suffix_r )
843 { return( str_r.size() >= suffix_r.size() && ::strncmp( str_r + str_r.size() - suffix_r.size() , suffix_r, suffix_r.size() ) == 0 ); }
845 /** Strip a \a suffix_r from \a str_r and return the resulting string. */
846 inline std::string stripSuffix( const C_Str & str_r, const C_Str & suffix_r )
848 if ( hasSuffix( str_r, suffix_r ) )
849 return std::string( str_r, str_r.size() - suffix_r.size() );
850 return str_r.c_str();
852 /** Return size of the common prefix of \a lhs and \a rhs. */
853 inline std::string::size_type commonPrefix( const C_Str & lhs, const C_Str & rhs )
855 const char * lp = lhs.c_str();
856 const char * rp = rhs.c_str();
857 std::string::size_type ret = 0;
858 while ( *lp == *rp && *lp != '\0' )
859 { ++lp, ++rp, ++ret; }
863 /** alias for \ref hasPrefix */
864 inline bool startsWith( const C_Str & str_r, const C_Str & prefix_r )
865 { return hasPrefix( str_r, prefix_r ); }
866 /** alias for \ref hasSuffix */
867 inline bool endsWith( const C_Str & str_r, const C_Str & prefix_r )
868 { return hasSuffix( str_r, prefix_r ); }
870 /////////////////////////////////////////////////////////////////
872 ///////////////////////////////////////////////////////////////////
873 /////////////////////////////////////////////////////////////////
875 ///////////////////////////////////////////////////////////////////
876 #endif // ZYPP_BASE_STRING_H