libzypp  17.18.0
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 == nullptr ? std::string() : t; }
146 
147  inline std::string asString( char * t )
148  { return t == nullptr ? std::string() : 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 asString() const { return _str.str(); }
221  std::string str() const { return _str.str(); }
222 
223  const std::ostream & stream() const { return _str; }
224  std::ostream & stream() { return _str; }
225 
226  void clear() { _str.str( std::string() ); }
227 
228  private:
229  std::ostringstream _str;
230  };
231 
233  inline std::ostream & operator<<( std::ostream & str, const Str & obj )
234  { return str << obj.str(); }
235 
251  struct Format
252  {
253  Format() { _fmter.exceptions( boost::io::no_error_bits ); }
254  Format( const std::string & format_r ) : Format() { _fmter.parse( format_r ); }
255 
256  template<class Tp>
257  Format & operator%( Tp && arg )
258  { _fmter % std::forward<Tp>(arg); return *this; }
259 
260  operator std::string() const { return _fmter.str(); }
261  std::string asString() const { return _fmter.str(); }
262  std::string str() const { return _fmter.str(); }
263 
264  const boost::format & fmter() const { return _fmter; }
265  boost::format & fmter() { return _fmter; }
266 
267  protected:
268  boost::format _fmter;
269  };
270 
272  inline std::ostream & operator<<( std::ostream & str, const Format & obj )
273  { return str << obj.fmter(); }
274 
288  inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
289  inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
290  inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
291  inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
292  inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
293  inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
294  inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
295  inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
296  inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
297  inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
298 
299  template<> inline std::string asString( const char & t ) { return numstring( t ); }
300  template<> inline std::string asString( const unsigned char & t ) { return numstring( t ); }
301  template<> inline std::string asString( const short & t ) { return numstring( t ); }
302  template<> inline std::string asString( const unsigned short & t ) { return numstring( t ); }
303  template<> inline std::string asString( const int & t ) { return numstring( t ); }
304  template<> inline std::string asString( const unsigned & t ) { return numstring( t ); }
305  template<> inline std::string asString( const long & t ) { return numstring( t ); }
306  template<> inline std::string asString( const unsigned long & t ) { return numstring( t ); }
307  template<> inline std::string asString( const long long & t ) { return numstring( t ); }
308  template<> inline std::string asString( const unsigned long long & t ) { return numstring( t ); }
310 
312 
323  inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
324  inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
325  inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
326  inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
327  inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
328  inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
329  inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
330  inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
331  inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
332  inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
334 
336 
347  inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
348  inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
349  inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
350  inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
351  inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
352  inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
353  inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
354  inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
355  inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
356  inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
358 
359 
361 
362  template <typename TInt>
363  std::string binstring( TInt val_r )
364  {
365  constexpr unsigned bits = sizeof(TInt)*8;
366  std::string ret( bits, ' ' );
367  TInt bit = 1;
368  for ( unsigned pos = bits; pos > 0; )
369  { --pos; ret[pos] = ((val_r & bit)?'1':'0'); bit = bit<<1; }
370  return ret;
371  }
372 
374 
383  template<typename TInt>
384  TInt strtonum( const C_Str & str );
385 
386  template<>
387  inline short strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
388  template<>
389  inline int strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
390  template<>
391  inline long strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
392  template<>
393  inline long long strtonum( const C_Str & str ) { return ::strtoll ( str, NULL, 0 ); }
394 
395  template<>
396  inline unsigned short strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
397  template<>
398  inline unsigned strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
399  template<>
400  inline unsigned long strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
401  template<>
402  inline unsigned long long strtonum( const C_Str & str ) { return ::strtoull( str, NULL, 0 ); }
403 
409  template<typename TInt>
410  inline TInt strtonum( const C_Str & str, TInt & i )
411  { return i = strtonum<TInt>( str ); }
413 
415 
419  bool strToTrue( const C_Str & str );
420 
422  bool strToFalse( const C_Str & str );
423 
428  inline bool strToBool( const C_Str & str, bool default_r )
429  { return( default_r ? strToFalse( str ) : strToTrue( str ) ); }
430 
435  inline bool strToBoolNodefault( const C_Str & str, bool & return_r )
436  {
437  if ( strToTrue( str ) ) return (return_r = true);
438  if ( !strToFalse( str ) ) return (return_r = false);
439  return return_r;
440  }
441 
443  TriBool strToTriBool( const C_Str & str );
444 
446 
450  std::string gsub( const std::string & str_r, const std::string & from_r, const std::string & to_r );
451 
454  std::string gsubFun( const std::string & str_r, const std::string & from_r, function<std::string()> to_r );
455 
460  std::string & replaceAll( std::string & str_r, const std::string & from_r, const std::string & to_r );
461 
464  std::string & replaceAllFun( std::string & str_r, const std::string & from_r, function<std::string()> to_r );
465 
478  inline std::string gapify( std::string inp_r, std::string::size_type gap_r = 1, char gapchar = ' ' )
479  {
480  if ( gap_r && inp_r.size() > gap_r )
481  {
482  inp_r.reserve( inp_r.size() + (inp_r.size()-1)/gap_r );
483  for ( std::string::size_type pos = gap_r; pos < inp_r.size(); pos += gap_r+1 )
484  inp_r.insert( pos, 1, gapchar );
485  }
486  return inp_r;
487  }
488 
490 
495  enum Trim {
496  NO_TRIM = 0x00,
497  L_TRIM = 0x01,
498  R_TRIM = 0x02,
500  };
501 
502  std::string trim( const std::string & s, const Trim trim_r = TRIM );
503  std::string trim( std::string && s, const Trim trim_r = TRIM );
504 
505  inline std::string ltrim( const std::string & s )
506  { return trim( s, L_TRIM ); }
507  inline std::string ltrim( std::string && s )
508  { return trim( std::move(s), L_TRIM ); }
509 
510  inline std::string rtrim( const std::string & s )
511  { return trim( s, R_TRIM ); }
512  inline std::string rtrim( std::string && s )
513  { return trim( std::move(s), R_TRIM ); }
515 
517 
528  template<class TOutputIterator>
529  unsigned split( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = " \t", const Trim trim_r = NO_TRIM )
530  {
531  const char * beg = line_r;
532  const char * cur = beg;
533  // skip leading sepchars
534  while ( *cur && ::strchr( sepchars_r, *cur ) )
535  ++cur;
536  unsigned ret = 0;
537  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
538  {
539  // skip non sepchars
540  while( *cur && !::strchr( sepchars_r, *cur ) )
541  ++cur;
542  // build string
543  *result_r = trim( std::string( beg, cur-beg ), trim_r );
544  // skip sepchars
545  while ( *cur && ::strchr( sepchars_r, *cur ) )
546  ++cur;
547  }
548  return ret;
549  }
550 
551  template<class TOutputIterator>
552  unsigned split( const C_Str & line_r, TOutputIterator result_r, const Trim trim_r )
553  { return split( line_r, result_r, " \t", trim_r ); }
554 
555 
592  template<class TOutputIterator>
593  unsigned splitEscaped( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = " \t", bool withEmpty = false)
594  {
595  const char * beg = line_r;
596  const char * cur = beg;
597  unsigned ret = 0;
598 
599  // skip leading sepchars
600  while ( *cur && ::strchr( sepchars_r, *cur ) )
601  {
602  ++cur;
603  if (withEmpty)
604  {
605  *result_r = "";
606  ++ret;
607  }
608  }
609 
610  // there were only sepchars in the string
611  if (!*cur && withEmpty)
612  {
613  *result_r = "";
614  return ++ret;
615  }
616 
617  // after the leading sepchars
618  enum class Quote { None, Slash, Single, Double, DoubleSlash };
619  std::vector<char> buf;
620  Quote quoting = Quote::None;
621  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
622  {
623  // read next value until unquoted sepchar
624  buf.clear();
625  quoting = Quote::None;
626  do {
627  switch ( quoting )
628  {
629  case Quote::None:
630  switch ( *cur )
631  {
632  case '\\': quoting = Quote::Slash; break;
633  case '\'': quoting = Quote::Single; break;
634  case '"': quoting = Quote::Double; break;
635  default: buf.push_back( *cur ); break;
636  }
637  break;
638 
639  case Quote::Slash:
640  buf.push_back( *cur );
641  quoting = Quote::None;
642  break;
643 
644  case Quote::Single:
645  switch ( *cur )
646  {
647  case '\'': quoting = Quote::None; break;
648  default: buf.push_back( *cur ); break;
649  }
650  break;
651 
652  case Quote::Double:
653  switch ( *cur )
654  {
655  case '\"': quoting = Quote::None; break;
656  case '\\': quoting = Quote::DoubleSlash; break;
657  default: buf.push_back( *cur ); break;
658  }
659  break;
660 
661  case Quote::DoubleSlash:
662  switch ( *cur )
663  {
664  case '\"': /*fallthrough*/
665  case '\\': buf.push_back( *cur ); break;
666  default:
667  buf.push_back( '\\' );
668  buf.push_back( *cur );
669  break;
670  }
671  quoting = Quote::Double;
672  break;
673  }
674  ++cur;
675  } while ( *cur && ( quoting != Quote::None || !::strchr( sepchars_r, *cur ) ) );
676  *result_r = std::string( buf.begin(), buf.end() );
677 
678 
679  // skip sepchars
680  if ( *cur && ::strchr( sepchars_r, *cur ) )
681  ++cur;
682  while ( *cur && ::strchr( sepchars_r, *cur ) )
683  {
684  ++cur;
685  if (withEmpty)
686  {
687  *result_r = "";
688  ++ret;
689  }
690  }
691  // the last was a separator => one more field
692  if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
693  {
694  *result_r = "";
695  ++ret;
696  }
697  }
698  return ret;
699  }
700 
722  template<class TOutputIterator>
723  unsigned splitFields( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = ":" )
724  {
725  const char * beg = line_r;
726  const char * cur = beg;
727  unsigned ret = 0;
728  for ( beg = cur; *beg; beg = cur, ++result_r )
729  {
730  // skip non sepchars
731  while( *cur && !::strchr( sepchars_r, *cur ) )
732  {
733  if ( *cur == '\\' && *(cur+1) )
734  ++cur;
735  ++cur;
736  }
737  // build string
738  *result_r = std::string( beg, cur-beg );
739  ++ret;
740  // skip sepchar
741  if ( *cur )
742  {
743  ++cur;
744  if ( ! *cur ) // ending with sepchar
745  {
746  *result_r = std::string(); // add final empty field
747  ++ret;
748  break;
749  }
750  }
751  }
752  return ret;
753  }
754 
761  template<class TOutputIterator>
762  unsigned splitFieldsEscaped( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = ":" )
763  {
764  return splitEscaped( line_r, result_r, sepchars_r, true /* withEmpty */ );
765  }
766 
768 
770 
773  template <class TIterator>
774  std::string join( TIterator begin, TIterator end, const C_Str & sep_r = " " )
775  {
776  std::string res;
777  for ( TIterator iter = begin; iter != end; ++ iter )
778  {
779  if ( iter != begin )
780  res += sep_r;
781  res += asString(*iter);
782  }
783  return res;
784  }
785 
787  template <class TContainer>
788  std::string join( const TContainer & cont_r, const C_Str & sep_r = " " )
789  { return join( cont_r.begin(), cont_r.end(), sep_r ); }
790 
795  template <class TIterator>
796  std::string joinEscaped( TIterator begin, TIterator end, const char sep_r = ' ' )
797  {
798  std::vector<char> buf;
799  for ( TIterator iter = begin; iter != end; ++ iter )
800  {
801  if ( iter != begin )
802  buf.push_back( sep_r );
803 
804  if ( iter->empty() )
805  {
806  // empty string goes ""
807  buf.push_back( '"' );
808  buf.push_back( '"' );
809  }
810  else
811  {
812  std::string toadd( asString(*iter) );
813  for_( ch, toadd.begin(), toadd.end() )
814  {
815  switch ( *ch )
816  {
817  case '"':
818  case '\'':
819  case '\\':
820  buf.push_back( '\\' );
821  buf.push_back( *ch );
822  break;
823  default:
824  if ( *ch == sep_r )
825  buf.push_back( '\\' );
826  buf.push_back( *ch );
827  }
828  }
829  }
830  }
831  return std::string( buf.begin(), buf.end() );
832  }
834 
835 
837 
843  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, const std::string & indent_r = " ", unsigned maxWitdh_r = 0 )
844  {
845  if ( maxWitdh_r )
846  {
847  if ( indent_r.size() >= maxWitdh_r )
848  maxWitdh_r = 0; // nonsense: indent larger than line witdh
849  else
850  maxWitdh_r -= indent_r.size();
851  }
852  unsigned width = 0;
853  for ( const char * e = text_r.c_str(), * s = e; *e; s = ++e )
854  {
855  for ( ; *e && *e != '\n'; ++e ) ;/*searching*/
856  width = e-s;
857  if ( maxWitdh_r && width > maxWitdh_r )
858  {
859  // must break line
860  width = maxWitdh_r;
861  for ( e = s+width; e > s && *e != ' '; --e ) ;/*searching*/
862  if ( e > s )
863  width = e-s; // on a ' ', replaced by '\n'
864  else
865  e = s+width-1; // cut line;
866  }
867  str << indent_r;
868  str.write( s, width );
869  str << "\n";
870  if ( !*e ) // on '\0'
871  break;
872  }
873  return str;
874  }
876  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, char indentch_r = ' ', unsigned maxWitdh_r = 0 )
877  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
879  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r = ' ' )
880  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
881 
885  inline std::ostream & autoPrefix( std::ostream & str, const std::string & text_r, function<std::string(const char*, const char*)> fnc_r )
886  {
887  for ( const char * e = text_r.c_str(); *e; ++e )
888  {
889  const char * s = e;
890  for ( ; *e && *e != '\n'; ++e ) /*searching*/;
891  str << fnc_r( s, e );
892  str.write( s, e-s );
893  str << "\n";
894  if ( !*e ) // on '\0'
895  break;
896  }
897  return str;
898  }
900  inline std::ostream & autoPrefix0( std::ostream & str, const std::string & text_r, function<std::string()> fnc_r )
901  {
902  auto wrap = [&fnc_r]( const char*, const char* )-> std::string {
903  return fnc_r();
904  };
905  return autoPrefix( str, text_r, wrap );
906  }
908 
917  std::string escape( const C_Str & str_r, const char c = ' ' );
918 
920  inline void appendEscaped( std::string & str_r, const C_Str & next_r, const char sep_r = ' ' )
921  {
922  if ( ! str_r.empty() )
923  str_r += sep_r;
924  if ( next_r.empty() )
925  str_r += "\"\"";
926  else
927  str_r += escape( next_r, sep_r );
928  }
929 
931  std::string bEscape( std::string str_r, const C_Str & special_r );
932 
934  std::string rxEscapeStr( std::string str_r );
935 
937  std::string rxEscapeGlob( std::string str_r );
938 
940 
942 
952  std::string hexencode( const C_Str & str_r );
954  std::string hexdecode( const C_Str & str_r );
956 
963  std::string toLower( const std::string & s );
964  std::string toLower( std::string && s );
966  inline std::string toLower( const char * s )
967  { return( s ? toLower( std::string(s) ) : std::string() ); }
968 
972  std::string toUpper( const std::string & s );
973  std::string toUpper( std::string && s );
975  inline std::string toUpper( const char * s )
976  { return( s ? toUpper( std::string(s) ) : std::string() ); }
978 
979 
982  inline int compareCI( const C_Str & lhs, const C_Str & rhs )
983  { return ::strcasecmp( lhs, rhs ); }
985 
989  inline bool contains( const C_Str & str_r, const C_Str & val_r )
990  { return ::strstr( str_r, val_r ); }
992  inline bool containsCI( const C_Str & str_r, const C_Str & val_r )
993  { return ::strcasestr( str_r, val_r ); }
995 
996  std::string stripFirstWord( std::string & line, const bool ltrim_first = true );
997 
998  std::string stripLastWord( std::string & line, const bool rtrim_first = true );
999 
1003  std::string getline( std::istream & str, bool trim = false );
1004 
1008  std::string getline( std::istream & str, const Trim trim_r );
1009 
1017  std::string receiveUpTo( std::istream & str, const char delim_r, bool returnDelim_r = false );
1018 
1020 
1025  inline bool hasPrefix( const C_Str & str_r, const C_Str & prefix_r )
1026  { return( ::strncmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }
1028  inline bool hasPrefixCI( const C_Str & str_r, const C_Str & prefix_r )
1029  { return( ::strncasecmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }
1030 
1032  inline std::string stripPrefix( const C_Str & str_r, const C_Str & prefix_r )
1033  { return( hasPrefix( str_r, prefix_r ) ? str_r + prefix_r.size() : str_r.c_str() ); }
1035  inline std::string stripPrefixCI( const C_Str & str_r, const C_Str & prefix_r )
1036  { return( hasPrefixCI( str_r, prefix_r ) ? str_r + prefix_r.size() : str_r.c_str() ); }
1037 
1039  inline bool hasSuffix( const C_Str & str_r, const C_Str & suffix_r )
1040  { return( str_r.size() >= suffix_r.size() && ::strncmp( str_r + str_r.size() - suffix_r.size() , suffix_r, suffix_r.size() ) == 0 ); }
1042  inline bool hasSuffixCI( const C_Str & str_r, const C_Str & suffix_r )
1043  { return( str_r.size() >= suffix_r.size() && ::strncasecmp( str_r + str_r.size() - suffix_r.size() , suffix_r, suffix_r.size() ) == 0 ); }
1044 
1046  inline std::string stripSuffix( const C_Str & str_r, const C_Str & suffix_r )
1047  {
1048  if ( hasSuffix( str_r, suffix_r ) )
1049  return std::string( str_r, str_r.size() - suffix_r.size() );
1050  return str_r.c_str();
1051  }
1053  inline std::string stripSuffixCI( const C_Str & str_r, const C_Str & suffix_r )
1054  {
1055  if ( hasSuffixCI( str_r, suffix_r ) )
1056  return std::string( str_r, str_r.size() - suffix_r.size() );
1057  return str_r.c_str();
1058  }
1059 
1061  inline std::string::size_type commonPrefix( const C_Str & lhs, const C_Str & rhs )
1062  {
1063  const char * lp = lhs.c_str();
1064  const char * rp = rhs.c_str();
1065  std::string::size_type ret = 0;
1066  while ( *lp == *rp && *lp != '\0' )
1067  { ++lp, ++rp, ++ret; }
1068  return ret;
1069  }
1071  inline std::string::size_type commonPrefixCI( const C_Str & lhs, const C_Str & rhs )
1072  {
1073  const char * lp = lhs.c_str();
1074  const char * rp = rhs.c_str();
1075  std::string::size_type ret = 0;
1076  while ( tolower(*lp) == tolower(*rp) && *lp != '\0' )
1077  { ++lp, ++rp, ++ret; }
1078  return ret;
1079  }
1080 
1081 
1083  inline bool startsWith( const C_Str & str_r, const C_Str & prefix_r )
1084  { return hasPrefix( str_r, prefix_r ); }
1086  inline bool startsWithCI( const C_Str & str_r, const C_Str & prefix_r )
1087  { return hasPrefixCI( str_r, prefix_r ); }
1088 
1090  inline bool endsWith( const C_Str & str_r, const C_Str & prefix_r )
1091  { return hasSuffix( str_r, prefix_r ); }
1093  inline bool endsWithCI( const C_Str & str_r, const C_Str & prefix_r )
1094  { return hasSuffixCI( str_r, prefix_r ); }
1096  } // namespace str
1098 
1099  // drag into zypp:: namespace
1100  using str::asString;
1101 
1102 } // namespace zypp
1104 #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:394
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:93
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:593
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
Definition: String.h:989
std::string str() const
Definition: String.h:221
std::string asString() const
Definition: String.h:220
std::string octstring(char n, int w=4)
Definition: String.h:347
C_Str(const boost::string_ref &str_r)
Definition: String.h:100
Boost libraries.
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:435
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:1032
bool hasPrefixCI(const C_Str &str_r, const C_Str &prefix_r)
Definition: String.h:1028
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:415
std::string join(TIterator begin, TIterator end, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
Definition: String.h:774
boost::format _fmter
Definition: String.h:268
bool hasSuffixCI(const C_Str &str_r, const C_Str &suffix_r)
Definition: String.h:1042
bool startsWithCI(const C_Str &str_r, const C_Str &prefix_r)
Definition: String.h:1086
std::string stripSuffixCI(const C_Str &str_r, const C_Str &suffix_r)
Definition: String.h:1053
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:1061
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:1046
std::ostream & autoPrefix0(std::ostream &str, const std::string &text_r, function< std::string()> fnc_r)
Definition: String.h:900
String related utilities and Regular expression matching.
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
Definition: Arch.h:347
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:28
Convenient building of std::string with boost::format.
Definition: String.h:251
Format & operator%(Tp &&arg)
Definition: String.h:257
const char * c_str() const
Definition: String.h:113
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:495
const boost::format & fmter() const
Definition: String.h:264
std::string asString() const
Definition: String.h:261
const std::ostream & stream() const
Definition: String.h:223
std::string ltrim(const std::string &s)
Definition: String.h:505
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:796
bool endsWithCI(const C_Str &str_r, const C_Str &prefix_r)
Definition: String.h:1093
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:330
size_type size() const
Definition: String.h:105
std::string asString() const
Definition: String.h:194
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:843
std::string stripFirstWord(std::string &line, const bool ltrim_first)
Definition: String.cc:263
std::string escape(const C_Str &str_r, const char sep_r)
Escape desired character c using a backslash.
Definition: String.cc:371
std::string rxEscapeGlob(std::string str_r)
Escape GLOB str_r for use in a regex (not anchored by "^" or "$").
Definition: String.cc:420
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:223
boost::format & fmter()
Definition: String.h:265
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:272
bool startsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasPrefix
Definition: String.h:1083
const char *const _val
Definition: String.h:116
std::string getline(std::istream &str, const Trim trim_r)
Return stream content up to (but not returning) the next newline.
Definition: String.cc:478
TInt strtonum(const C_Str &str)
Parsing numbers from string.
Definition: String.h:387
std::string stripLastWord(std::string &line, const bool rtrim_first)
Definition: String.cc:296
bool strToFalse(const C_Str &str)
Return false if str is 0, false, no, off, never.
Definition: String.cc:81
bool containsCI(const C_Str &str_r, const C_Str &val_r)
Locate substring case insensitive.
Definition: String.h:992
std::string gsubFun(const std::string &str_r, const std::string &from_r, function< std::string()> to_r)
Definition: String.cc:347
bool endsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasSuffix
Definition: String.h:1090
std::string numstring(char n, int w=0)
Definition: String.h:288
size_type _sze
Definition: String.h:117
std::string toLower(const std::string &s)
Return lowercase version of s.
Definition: String.cc:177
int compareCI(const C_Str &lhs, const C_Str &rhs)
Definition: String.h:982
SolvableIdType size_type
Definition: PoolMember.h:126
std::string rtrim(const std::string &s)
Definition: String.h:510
Str & operator<<(Tp &&val)
Definition: String.h:213
std::ostream & operator<<(std::ostream &str, const Str &obj)
Definition: String.h:233
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:723
std::string & replaceAllFun(std::string &str_r, const std::string &from_r, function< std::string()> to_r)
Definition: String.cc:353
bool hasSuffix(const C_Str &str_r, const C_Str &suffix_r)
Return whether str_r has suffix suffix_r.
Definition: String.h:1039
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:324
std::string str() const
Definition: String.h:262
C_Str(const char *c_str_r)
Definition: String.h:98
bool isNull() const
Definition: String.h:103
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,...
Definition: String.cc:488
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Definition: String.h:428
std::string hexencode(const C_Str &str_r)
Encode all characters other than [a-zA-Z0-9] as XX.
Definition: String.cc:124
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:363
bool empty() const
Definition: String.h:104
std::ostream & operator<<(std::ostream &str, const C_Str &obj)
Definition: String.h:121
std::string stripPrefixCI(const C_Str &str_r, const C_Str &prefix_r)
Definition: String.h:1035
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:885
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
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
std::string toUpper(const std::string &s)
Return uppercase version of s.
Definition: String.cc:200
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:224
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:1025
void clear()
Definition: String.h:226
std::string hexstring(char n, int w=4)
Definition: String.h:323
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", const Trim trim_r=NO_TRIM)
Split line_r into words.
Definition: String.h:529
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:920
std::ostringstream _str
Definition: String.h:229
std::string hexdecode(const C_Str &str_r)
Decode hexencoded XX sequences.
Definition: String.cc:145
std::string::size_type commonPrefixCI(const C_Str &lhs, const C_Str &rhs)
Definition: String.h:1071
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:762
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:478
Format(const std::string &format_r)
Definition: String.h:254