libzypp  15.28.6
String.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_BASE_STRING_H
13 #define ZYPP_BASE_STRING_H
14 
15 #include <cstring>
16 
17 #include <iosfwd>
18 #include <vector>
19 #include <string>
20 #include <sstream>
21 #include <boost/format.hpp>
22 #include <boost/utility/string_ref.hpp>
23 
24 #include "zypp/base/Easy.h"
25 #include "zypp/base/PtrTypes.h"
26 #include "zypp/base/Function.h"
27 
29 namespace boost { namespace logic { class tribool; } }
30 namespace zypp { typedef boost::logic::tribool TriBool; }
32 
34 namespace zypp
35 {
39  template <class Tp>
40  std::string asUserString( const Tp & val_r )
41  { return val_r.asUserString(); }
42 
43 }// namespace zypp
45 
47 namespace zypp
48 {
49 
90  class C_Str
91  {
92  public:
94 
95  public:
96  C_Str() : _val( 0 ), _sze( 0 ) {}
97  C_Str( char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
98  C_Str( const char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
99  C_Str( const std::string & str_r ) : _val( str_r.c_str() ), _sze( str_r.size() ) {}
100  C_Str( const boost::string_ref & str_r ) : _val( str_r.data() ), _sze( str_r.size() ) {}
101 
102  public:
103  bool isNull() const { return !_val; }
104  bool empty() const { return !(_val && *_val); }
105  size_type size() const
106  {
107  if ( _sze == std::string::npos )
108  { _sze = _val ? ::strlen( _val ) : 0; }
109  return _sze;
110  };
111 
112  operator const char *() const { return c_str(); }
113  const char * c_str() const { return _val ? _val : ""; }
114 
115  private:
116  const char *const _val;
117  mutable size_type _sze;
118  };
119 
121  inline std::ostream & operator<<( std::ostream & str, const C_Str & obj )
122  { return str << obj.c_str(); }
123 
125 
129  namespace str
130  {
131 
133 
136  inline const std::string & asString( const std::string & t )
137  { return t; }
138 
139 #ifndef SWIG // Swig treats it as syntax error
140  inline std::string && asString( std::string && t )
141  { return std::move(t); }
142 #endif
143 
144  inline std::string asString( const char * t )
145  { return t; }
146 
147  inline std::string asString( char * t )
148  { return t; }
149 
150  template<class Tp>
151  inline std::string asString( const Tp &t )
152  { return t.asString(); }
153 
154  template<class Tp>
155  inline std::string asString( const intrusive_ptr<Tp> &p )
156  { return p->asString(); }
157 
158  template<class Tp>
159  inline std::string asString( const weak_ptr<Tp> &p )
160  { return p->asString(); }
161 
162  template<>
163  inline std::string asString( const bool &t )
164  { return t ? "true" : "false"; }
165 
167 
168  std::string form( const char * format, ... )
169  __attribute__ ((format (printf, 1, 2)));
170 
172 
176  std::string strerror( int errno_r );
177 
179 
189  struct SafeBuf
190  {
191  char * _buf;
192  SafeBuf() : _buf( 0 ) {}
193  ~SafeBuf() { if ( _buf ) free( _buf ); }
194  std::string asString() const
195  { return _buf ? std::string(_buf) : std::string(); }
196  };
197 
210  struct Str
211  {
212  template<class Tp>
213  Str & operator<<( Tp && val )
214  { _str << std::forward<Tp>(val); return *this; }
215 
216  Str & operator<<( std::ostream& (*iomanip)( std::ostream& ) )
217  { _str << iomanip; return *this; }
218 
219  operator std::string() const { return _str.str(); }
220  std::string str() const { return _str.str(); }
221 
222  const std::ostream & stream() const { return _str; }
223  std::ostream & stream() { return _str; }
224 
225  void clear() { _str.str( std::string() ); }
226 
227  private:
228  std::ostringstream _str;
229  };
230 
232  inline std::ostream & operator<<( std::ostream & str, const Str & obj )
233  { return str << obj.str(); }
234 
247  struct Format
248  {
249  Format() {}
250  Format( const std::string & format_r ) : _fmter( format_r ) {}
251 
252  template<class Tp>
253  Format & operator%( Tp && arg )
254  { _fmter % std::forward<Tp>(arg); return *this; }
255 
256  operator std::string() const { return _fmter.str(); }
257  std::string asString() const { return _fmter.str(); }
258  std::string str() const { return _fmter.str(); }
259 
260  const boost::format & fmter() const { return _fmter; }
261  boost::format & fmter() { return _fmter; }
262 
263  protected:
264  boost::format _fmter;
265  };
266 
268  inline std::ostream & operator<<( std::ostream & str, const Format & obj )
269  { return str << obj.fmter(); }
270 
278  struct FormatNAC : public Format
279  {
280  FormatNAC() { relax(); }
281  FormatNAC( const std::string & format_r ) : Format( format_r ) { relax(); }
282 
283  private:
284  void relax()
285  {
286  using namespace boost::io;
287  _fmter.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
288  }
289  };
291 
304  inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
305  inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
306  inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
307  inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
308  inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
309  inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
310  inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
311  inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
312  inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
313  inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
314 
315  template<> inline std::string asString( const char & t ) { return numstring( t ); }
316  template<> inline std::string asString( const unsigned char & t ) { return numstring( t ); }
317  template<> inline std::string asString( const short & t ) { return numstring( t ); }
318  template<> inline std::string asString( const unsigned short & t ) { return numstring( t ); }
319  template<> inline std::string asString( const int & t ) { return numstring( t ); }
320  template<> inline std::string asString( const unsigned & t ) { return numstring( t ); }
321  template<> inline std::string asString( const long & t ) { return numstring( t ); }
322  template<> inline std::string asString( const unsigned long & t ) { return numstring( t ); }
323  template<> inline std::string asString( const long long & t ) { return numstring( t ); }
324  template<> inline std::string asString( const unsigned long long & t ) { return numstring( t ); }
326 
328 
339  inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
340  inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
341  inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
342  inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
343  inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
344  inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
345  inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
346  inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
347  inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
348  inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
350 
352 
363  inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
364  inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
365  inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
366  inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
367  inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
368  inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
369  inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
370  inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
371  inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
372  inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
374 
375 
377 
378  template <typename TInt>
379  std::string binstring( TInt val_r )
380  {
381  constexpr unsigned bits = sizeof(TInt)*8;
382  std::string ret( bits, ' ' );
383  TInt bit = 1;
384  for ( unsigned pos = bits; pos > 0; )
385  { --pos; ret[pos] = ((val_r & bit)?'1':'0'); bit = bit<<1; }
386  return ret;
387  }
388 
390 
399  template<typename TInt>
400  TInt strtonum( const C_Str & str );
401 
402  template<>
403  inline short strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
404  template<>
405  inline int strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
406  template<>
407  inline long strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
408  template<>
409  inline long long strtonum( const C_Str & str ) { return ::strtoll ( str, NULL, 0 ); }
410 
411  template<>
412  inline unsigned short strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
413  template<>
414  inline unsigned strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
415  template<>
416  inline unsigned long strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
417  template<>
418  inline unsigned long long strtonum( const C_Str & str ) { return ::strtoull( str, NULL, 0 ); }
419 
425  template<typename TInt>
426  inline TInt strtonum( const C_Str & str, TInt & i )
427  { return i = strtonum<TInt>( str ); }
429 
431 
435  bool strToTrue( const C_Str & str );
436 
438  bool strToFalse( const C_Str & str );
439 
444  inline bool strToBool( const C_Str & str, bool default_r )
445  { return( default_r ? strToFalse( str ) : strToTrue( str ) ); }
446 
451  inline bool strToBoolNodefault( const C_Str & str, bool & return_r )
452  {
453  if ( strToTrue( str ) ) return (return_r = true);
454  if ( !strToFalse( str ) ) return (return_r = false);
455  return return_r;
456  }
457 
459  TriBool strToTriBool( const C_Str & str );
460 
462 
466  std::string gsub( const std::string & str_r, const std::string & from_r, const std::string & to_r );
467 
470  std::string gsubFun( const std::string & str_r, const std::string & from_r, function<std::string()> to_r );
471 
476  std::string & replaceAll( std::string & str_r, const std::string & from_r, const std::string & to_r );
477 
480  std::string & replaceAllFun( std::string & str_r, const std::string & from_r, function<std::string()> to_r );
481 
494  inline std::string gapify( std::string inp_r, std::string::size_type gap_r = 1, char gapchar = ' ' )
495  {
496  if ( gap_r && inp_r.size() > gap_r )
497  {
498  inp_r.reserve( inp_r.size() + (inp_r.size()-1)/gap_r );
499  for ( std::string::size_type pos = gap_r; pos < inp_r.size(); pos += gap_r+1 )
500  inp_r.insert( pos, 1, gapchar );
501  }
502  return inp_r;
503  }
504 
506 
517  template<class TOutputIterator>
518  unsigned split( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = " \t" )
519  {
520  const char * beg = line_r;
521  const char * cur = beg;
522  // skip leading sepchars
523  while ( *cur && ::strchr( sepchars_r, *cur ) )
524  ++cur;
525  unsigned ret = 0;
526  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
527  {
528  // skip non sepchars
529  while( *cur && !::strchr( sepchars_r, *cur ) )
530  ++cur;
531  // build string
532  *result_r = std::string( beg, cur-beg );
533  // skip sepchars
534  while ( *cur && ::strchr( sepchars_r, *cur ) )
535  ++cur;
536  }
537  return ret;
538  }
539 
576  template<class TOutputIterator>
577  unsigned splitEscaped( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = " \t", bool withEmpty = false)
578  {
579  const char * beg = line_r;
580  const char * cur = beg;
581  unsigned ret = 0;
582 
583  // skip leading sepchars
584  while ( *cur && ::strchr( sepchars_r, *cur ) )
585  {
586  ++cur;
587  if (withEmpty)
588  {
589  *result_r = "";
590  ++ret;
591  }
592  }
593 
594  // there were only sepchars in the string
595  if (!*cur && withEmpty)
596  {
597  *result_r = "";
598  return ++ret;
599  }
600 
601  // after the leading sepchars
602  enum class Quote { None, Slash, Single, Double, DoubleSlash };
603  std::vector<char> buf;
604  Quote quoting = Quote::None;
605  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
606  {
607  // read next value until unquoted sepchar
608  buf.clear();
609  quoting = Quote::None;
610  do {
611  switch ( quoting )
612  {
613  case Quote::None:
614  switch ( *cur )
615  {
616  case '\\': quoting = Quote::Slash; break;
617  case '\'': quoting = Quote::Single; break;
618  case '"': quoting = Quote::Double; break;
619  default: buf.push_back( *cur ); break;
620  }
621  break;
622 
623  case Quote::Slash:
624  buf.push_back( *cur );
625  quoting = Quote::None;
626  break;
627 
628  case Quote::Single:
629  switch ( *cur )
630  {
631  case '\'': quoting = Quote::None; break;
632  default: buf.push_back( *cur ); break;
633  }
634  break;
635 
636  case Quote::Double:
637  switch ( *cur )
638  {
639  case '\"': quoting = Quote::None; break;
640  case '\\': quoting = Quote::DoubleSlash; break;
641  default: buf.push_back( *cur ); break;
642  }
643  break;
644 
645  case Quote::DoubleSlash:
646  switch ( *cur )
647  {
648  case '\"': /*fallthrough*/
649  case '\\': buf.push_back( *cur ); break;
650  default:
651  buf.push_back( '\\' );
652  buf.push_back( *cur );
653  break;
654  }
655  quoting = Quote::Double;
656  break;
657  }
658  ++cur;
659  } while ( *cur && ( quoting != Quote::None || !::strchr( sepchars_r, *cur ) ) );
660  *result_r = std::string( buf.begin(), buf.end() );
661 
662 
663  // skip sepchars
664  if ( *cur && ::strchr( sepchars_r, *cur ) )
665  ++cur;
666  while ( *cur && ::strchr( sepchars_r, *cur ) )
667  {
668  ++cur;
669  if (withEmpty)
670  {
671  *result_r = "";
672  ++ret;
673  }
674  }
675  // the last was a separator => one more field
676  if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
677  {
678  *result_r = "";
679  ++ret;
680  }
681  }
682  return ret;
683  }
684 
706  template<class TOutputIterator>
707  unsigned splitFields( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = ":" )
708  {
709  const char * beg = line_r;
710  const char * cur = beg;
711  unsigned ret = 0;
712  for ( beg = cur; *beg; beg = cur, ++result_r )
713  {
714  // skip non sepchars
715  while( *cur && !::strchr( sepchars_r, *cur ) )
716  {
717  if ( *cur == '\\' && *(cur+1) )
718  ++cur;
719  ++cur;
720  }
721  // build string
722  *result_r = std::string( beg, cur-beg );
723  ++ret;
724  // skip sepchar
725  if ( *cur )
726  {
727  ++cur;
728  if ( ! *cur ) // ending with sepchar
729  {
730  *result_r = std::string(); // add final empty field
731  ++ret;
732  break;
733  }
734  }
735  }
736  return ret;
737  }
738 
745  template<class TOutputIterator>
746  unsigned splitFieldsEscaped( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = ":" )
747  {
748  return splitEscaped( line_r, result_r, sepchars_r, true /* withEmpty */ );
749  }
750 
752 
754 
757  template <class TIterator>
758  std::string join( TIterator begin, TIterator end, const C_Str & sep_r = " " )
759  {
760  std::string res;
761  for ( TIterator iter = begin; iter != end; ++ iter )
762  {
763  if ( iter != begin )
764  res += sep_r;
765  res += asString(*iter);
766  }
767  return res;
768  }
769 
771  template <class TContainer>
772  std::string join( const TContainer & cont_r, const C_Str & sep_r = " " )
773  { return join( cont_r.begin(), cont_r.end(), sep_r ); }
774 
779  template <class TIterator>
780  std::string joinEscaped( TIterator begin, TIterator end, const char sep_r = ' ' )
781  {
782  std::vector<char> buf;
783  for ( TIterator iter = begin; iter != end; ++ iter )
784  {
785  if ( iter != begin )
786  buf.push_back( sep_r );
787 
788  if ( iter->empty() )
789  {
790  // empty string goes ""
791  buf.push_back( '"' );
792  buf.push_back( '"' );
793  }
794  else
795  {
796  std::string toadd( asString(*iter) );
797  for_( ch, toadd.begin(), toadd.end() )
798  {
799  switch ( *ch )
800  {
801  case '"':
802  case '\'':
803  case '\\':
804  buf.push_back( '\\' );
805  buf.push_back( *ch );
806  break;
807  default:
808  if ( *ch == sep_r )
809  buf.push_back( '\\' );
810  buf.push_back( *ch );
811  }
812  }
813  }
814  }
815  return std::string( buf.begin(), buf.end() );
816  }
818 
819 
821 
827  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, const std::string & indent_r = " ", unsigned maxWitdh_r = 0 )
828  {
829  if ( maxWitdh_r )
830  {
831  if ( indent_r.size() >= maxWitdh_r )
832  maxWitdh_r = 0; // nonsense: indent larger than line witdh
833  else
834  maxWitdh_r -= indent_r.size();
835  }
836  unsigned width = 0;
837  for ( const char * e = text_r.c_str(), * s = e; *e; s = ++e )
838  {
839  for ( ; *e && *e != '\n'; ++e ) ;/*searching*/
840  width = e-s;
841  if ( maxWitdh_r && width > maxWitdh_r )
842  {
843  // must break line
844  width = maxWitdh_r;
845  for ( e = s+width; e > s && *e != ' '; --e ) ;/*searching*/
846  if ( e > s )
847  width = e-s; // on a ' ', replaced by '\n'
848  else
849  e = s+width-1; // cut line;
850  }
851  str << indent_r;
852  str.write( s, width );
853  str << "\n";
854  if ( !*e ) // on '\0'
855  break;
856  }
857  return str;
858  }
860  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, char indentch_r = ' ', unsigned maxWitdh_r = 0 )
861  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
863  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r = ' ' )
864  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
865 
869  inline std::ostream & autoPrefix( std::ostream & str, const std::string & text_r, function<std::string(const char*, const char*)> fnc_r )
870  {
871  for ( const char * e = text_r.c_str(); *e; ++e )
872  {
873  const char * s = e;
874  for ( ; *e && *e != '\n'; ++e ) /*searching*/;
875  str << fnc_r( s, e );
876  str.write( s, e-s );
877  str << "\n";
878  if ( !*e ) // on '\0'
879  break;
880  }
881  return str;
882  }
884  inline std::ostream & autoPrefix0( std::ostream & str, const std::string & text_r, function<std::string()> fnc_r )
885  {
886  auto wrap = [&fnc_r]( const char*, const char* )-> std::string {
887  return fnc_r();
888  };
889  return autoPrefix( str, text_r, wrap );
890  }
892 
901  std::string escape( const C_Str & str_r, const char c = ' ' );
902 
904  inline void appendEscaped( std::string & str_r, const C_Str & next_r, const char sep_r = ' ' )
905  {
906  if ( ! str_r.empty() )
907  str_r += sep_r;
908  if ( next_r.empty() )
909  str_r += "\"\"";
910  else
911  str_r += escape( next_r, sep_r );
912  }
913 
915  std::string bEscape( std::string str_r, const C_Str & special_r );
916 
918  std::string rxEscapeStr( std::string str_r );
919 
921  std::string rxEscapeGlob( std::string str_r );
922 
924 
926 
936  std::string hexencode( const C_Str & str_r );
938  std::string hexdecode( const C_Str & str_r );
940 
947  std::string toLower( const std::string & s );
948  std::string toLower( std::string && s );
950  inline std::string toLower( const char * s )
951  { return( s ? toLower( std::string(s) ) : std::string() ); }
952 
956  std::string toUpper( const std::string & s );
957  std::string toUpper( std::string && s );
959  inline std::string toUpper( const char * s )
960  { return( s ? toUpper( std::string(s) ) : std::string() ); }
962 
963 
966  inline int compareCI( const C_Str & lhs, const C_Str & rhs )
967  { return ::strcasecmp( lhs, rhs ); }
969 
973  inline bool contains( const C_Str & str_r, const C_Str & val_r )
974  { return ::strstr( str_r, val_r ); }
976  inline bool containsCI( const C_Str & str_r, const C_Str & val_r )
977  { return ::strcasestr( str_r, val_r ); }
979 
981 
986  enum Trim {
987  NO_TRIM = 0x00,
988  L_TRIM = 0x01,
989  R_TRIM = 0x02,
991  };
992 
993  std::string trim( const std::string & s, const Trim trim_r = TRIM );
994  std::string trim( std::string && s, const Trim trim_r = TRIM );
995 
996  inline std::string ltrim( const std::string & s )
997  { return trim( s, L_TRIM ); }
998  inline std::string ltrim( std::string && s )
999  { return trim( std::move(s), L_TRIM ); }
1000 
1001  inline std::string rtrim( const std::string & s )
1002  { return trim( s, R_TRIM ); }
1003  inline std::string rtrim( std::string && s )
1004  { return trim( std::move(s), R_TRIM ); }
1006 
1007  std::string stripFirstWord( std::string & line, const bool ltrim_first = true );
1008 
1009  std::string stripLastWord( std::string & line, const bool rtrim_first = true );
1010 
1014  std::string getline( std::istream & str, bool trim = false );
1015 
1019  std::string getline( std::istream & str, const Trim trim_r );
1020 
1028  std::string receiveUpTo( std::istream & str, const char delim_r, bool returnDelim_r = false );
1029 
1031 
1036  inline bool hasPrefix( const C_Str & str_r, const C_Str & prefix_r )
1037  { return( ::strncmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }
1038 
1040  inline std::string stripPrefix( const C_Str & str_r, const C_Str & prefix_r )
1041  { return( hasPrefix( str_r, prefix_r ) ? str_r + prefix_r.size() : str_r.c_str() ); }
1042 
1044  inline bool hasSuffix( const C_Str & str_r, const C_Str & suffix_r )
1045  { return( str_r.size() >= suffix_r.size() && ::strncmp( str_r + str_r.size() - suffix_r.size() , suffix_r, suffix_r.size() ) == 0 ); }
1046 
1048  inline std::string stripSuffix( const C_Str & str_r, const C_Str & suffix_r )
1049  {
1050  if ( hasSuffix( str_r, suffix_r ) )
1051  return std::string( str_r, str_r.size() - suffix_r.size() );
1052  return str_r.c_str();
1053  }
1055  inline std::string::size_type commonPrefix( const C_Str & lhs, const C_Str & rhs )
1056  {
1057  const char * lp = lhs.c_str();
1058  const char * rp = rhs.c_str();
1059  std::string::size_type ret = 0;
1060  while ( *lp == *rp && *lp != '\0' )
1061  { ++lp, ++rp, ++ret; }
1062  return ret;
1063  }
1064 
1066  inline bool startsWith( const C_Str & str_r, const C_Str & prefix_r )
1067  { return hasPrefix( str_r, prefix_r ); }
1069  inline bool endsWith( const C_Str & str_r, const C_Str & prefix_r )
1070  { return hasSuffix( str_r, prefix_r ); }
1072  } // namespace str
1074 
1075  // drag into zypp:: namespace
1076  using str::asString;
1077 
1078 } // namespace zypp
1080 #endif // ZYPP_BASE_STRING_H
std::string bEscape(std::string str_r, const C_Str &special_r)
Return str_r with '\'-escaped chars occurring in special_r (and '\').
Definition: String.cc:392
TriBool strToTriBool(const C_Str &str)
Parse str into a bool if it's a legal true or false string; else indterminate.
Definition: String.cc:91
unsigned splitEscaped(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", bool withEmpty=false)
Split line_r into words with respect to escape delimeters.
Definition: String.h:577
const std::ostream & stream() const
Definition: String.h:222
void appendEscaped(std::string &str_r, const C_Str &next_r, const char sep_r= ' ')
Escape next_r and append it to str_r using separator sep_r.
Definition: String.h:904
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
Definition: String.h:973
std::string octstring(char n, int w=4)
Definition: String.h:363
C_Str(const boost::string_ref &str_r)
Definition: String.h:100
bool strToBoolNodefault(const C_Str &str, bool &return_r)
Parse str into a bool if it's a legal true or false string.
Definition: String.h:451
std::string stripPrefix(const C_Str &str_r, const C_Str &prefix_r)
Strip a prefix_r from str_r and return the resulting string.
Definition: String.h:1040
std::string::size_type size_type
Definition: String.h:93
std::string rxEscapeStr(std::string str_r)
Escape plain STRING str_r for use in a regex (not anchored by "^" or "$").
Definition: String.cc:413
std::string join(TIterator begin, TIterator end, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
Definition: String.h:758
boost::format _fmter
Definition: String.h:264
std::string::size_type commonPrefix(const C_Str &lhs, const C_Str &rhs)
Return size of the common prefix of lhs and rhs.
Definition: String.h:1055
std::string escape(const C_Str &str_r, const char sep_r)
Escape desired character c using a backslash.
Definition: String.cc:369
std::string stripSuffix(const C_Str &str_r, const C_Str &suffix_r)
Strip a suffix_r from str_r and return the resulting string.
Definition: String.h:1048
std::string asString() const
Definition: String.h:194
std::ostream & autoPrefix0(std::ostream &str, const std::string &text_r, function< std::string()> fnc_r)
Definition: String.h:884
C_Str(const std::string &str_r)
Definition: String.h:99
const std::string & asString(const std::string &t)
Global asString() that works with std::string too.
Definition: String.h:136
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
FormatNAC(const std::string &format_r)
Definition: String.h:281
Convenient building of std::string with boost::format.
Definition: String.h:247
Format & operator%(Tp &&arg)
Definition: String.h:253
bool empty() const
Definition: String.h:104
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:36
Trim
To define how to trim.
Definition: String.h:986
std::string asString() const
Definition: String.h:257
std::string ltrim(const std::string &s)
Definition: String.h:996
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
Definition: String.h:30
std::string & replaceAll(std::string &str_r, const std::string &from_r, const std::string &to_r)
Replace all occurrences of from_r with to_r in str_r (inplace).
Definition: String.cc:328
std::string str() const
Definition: String.h:220
std::ostream & printIndented(std::ostream &str, const std::string &text_r, const std::string &indent_r=" ", unsigned maxWitdh_r=0)
Indent by string [" "] optionally wrap.
Definition: String.h:827
std::string str() const
Definition: String.h:258
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
Definition: String.h:518
std::string stripFirstWord(std::string &line, const bool ltrim_first)
Definition: String.cc:261
std::string rxEscapeGlob(std::string str_r)
Escape GLOB str_r for use in a regex (not anchored by "^" or "$").
Definition: String.cc:418
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition: String.h:210
std::string trim(const std::string &s, const Trim trim_r)
Definition: String.cc:221
boost::format & fmter()
Definition: String.h:261
Convenience char* constructible from std::string and char*, it maps (char*)0 to an empty string...
Definition: String.h:90
std::ostream & operator<<(std::ostream &str, const Format &obj)
Definition: String.h:268
bool startsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasPrefix
Definition: String.h:1066
const char *const _val
Definition: String.h:116
std::string gapify(std::string inp_r, std::string::size_type gap_r=1, char gapchar= ' ')
Enhance readability: insert gaps at regular distance.
Definition: String.h:494
std::string getline(std::istream &str, const Trim trim_r)
Return stream content up to (but not returning) the next newline.
Definition: String.cc:476
TInt strtonum(const C_Str &str)
Parsing numbers from string.
Definition: String.h:403
const boost::format & fmter() const
Definition: String.h:260
std::string stripLastWord(std::string &line, const bool rtrim_first)
Definition: String.cc:294
bool strToFalse(const C_Str &str)
Return false if str is 0, false, no, off.
Definition: String.cc:80
bool containsCI(const C_Str &str_r, const C_Str &val_r)
Locate substring case insensitive.
Definition: String.h:976
std::string gsubFun(const std::string &str_r, const std::string &from_r, function< std::string()> to_r)
Definition: String.cc:345
bool endsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasSuffix
Definition: String.h:1069
std::string numstring(char n, int w=0)
Definition: String.h:304
size_type _sze
Definition: String.h:117
std::string toLower(const std::string &s)
Return lowercase version of s.
Definition: String.cc:175
int compareCI(const C_Str &lhs, const C_Str &rhs)
Definition: String.h:966
SolvableIdType size_type
Definition: PoolMember.h:152
std::string rtrim(const std::string &s)
Definition: String.h:1001
Str & operator<<(Tp &&val)
Definition: String.h:213
std::string joinEscaped(TIterator begin, TIterator end, const char sep_r= ' ')
Join strings using separator sep_r, quoting or escaping the values.
Definition: String.h:780
std::ostream & operator<<(std::ostream &str, const Str &obj)
Definition: String.h:232
bool strToTrue(const C_Str &str)
Parsing boolean from string.
Definition: String.cc:63
unsigned splitFields(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=":")
Split line_r into fields.
Definition: String.h:707
std::string & replaceAllFun(std::string &str_r, const std::string &from_r, function< std::string()> to_r)
Definition: String.cc:351
bool hasSuffix(const C_Str &str_r, const C_Str &suffix_r)
Return whether str_r has suffix suffix_r.
Definition: String.h:1044
std::string gsub(const std::string &str_r, const std::string &from_r, const std::string &to_r)
Return a string with all occurrences of from_r replaced with to_r.
Definition: String.cc:322
bool isNull() const
Definition: String.h:103
C_Str(const char *c_str_r)
Definition: String.h:98
std::string receiveUpTo(std::istream &str, const char delim_r, bool returnDelim_r)
Return stream content up to the next ocurrence of delim_r or EOF delim_r, if found, is always read from the stream.
Definition: String.cc:486
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Definition: String.h:444
const char * c_str() const
Definition: String.h:113
std::string hexencode(const C_Str &str_r)
Encode all characters other than [a-zA-Z0-9] as XX.
Definition: String.cc:122
Str & operator<<(std::ostream &(*iomanip)(std::ostream &))
Definition: String.h:216
std::string binstring(TInt val_r)
String representation of number as bit-string with leading '0's.
Definition: String.h:379
std::ostream & operator<<(std::ostream &str, const C_Str &obj)
Definition: String.h:121
size_type size() const
Definition: String.h:105
std::ostream & autoPrefix(std::ostream &str, const std::string &text_r, function< std::string(const char *, const char *)> fnc_r)
Prefix lines by string computed by function taking line begin/end [std::string(const char*...
Definition: String.h:869
std::string strerror(int errno_r)
Return string describing the error_r code.
Definition: String.cc:53
C_Str(char *c_str_r)
Definition: String.h:97
std::string toUpper(const std::string &s)
Return uppercase version of s.
Definition: String.cc:198
std::string asUserString(const Tp &val_r)
Request a human readable (translated) string representation of Tp [Tp.asUserString()] Classes may imp...
Definition: String.h:40
std::ostream & stream()
Definition: String.h:223
Assert free called for allocated char *.
Definition: String.h:189
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition: String.h:1036
void clear()
Definition: String.h:225
Format with (N)o (A)rgument (C)heck.
Definition: String.h:278
std::string hexstring(char n, int w=4)
Definition: String.h:339
std::ostringstream _str
Definition: String.h:228
std::string hexdecode(const C_Str &str_r)
Decode hexencoded XX sequences.
Definition: String.cc:143
unsigned splitFieldsEscaped(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=":")
Split line_r into fields handling also escaped separators.
Definition: String.h:746
Format(const std::string &format_r)
Definition: String.h:250