libzypp  14.48.5
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 
23 #include "zypp/base/Easy.h"
24 #include "zypp/base/PtrTypes.h"
25 #include "zypp/base/Function.h"
26 
28 namespace boost { namespace logic { class tribool; } }
29 namespace zypp { typedef boost::logic::tribool TriBool; }
31 
33 namespace zypp
34 {
38  template <class _Tp>
39  std::string asUserString( const _Tp & val_r )
40  { return val_r.asUserString(); }
41 
42 }// namespace zypp
44 
46 namespace zypp
47 {
48 
49  struct MessageString : public std::string
50  {
52  MessageString( const char * str_r ) : std::string( str_r ? str_r : "" ) {}
53  MessageString( const std::string & str_r ) : std::string( str_r ) {}
54  // boost::format, std::ostringstream, str::Str ...
55  template<class _Str>
56  MessageString( const _Str & str_r ) : std::string( str_r.str() ) {}
57  };
58 
97  class C_Str
98  {
99  public:
101 
102  public:
103  C_Str() : _val( 0 ), _sze( 0 ) {}
104  C_Str( char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
105  C_Str( const char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
106  C_Str( const std::string & str_r ) : _val( str_r.c_str() ), _sze( str_r.size() ) {}
107 
108  public:
109  bool isNull() const { return !_val; }
110  bool empty() const { return !(_val && *_val); }
111  size_type size() const
112  {
113  if ( _sze == std::string::npos )
114  { _sze = _val ? ::strlen( _val ) : 0; }
115  return _sze;
116  };
117 
118  operator const char *() const { return c_str(); }
119  const char * c_str() const { return _val ? _val : ""; }
120 
121  private:
122  const char *const _val;
123  mutable size_type _sze;
124  };
125 
127  inline std::ostream & operator<<( std::ostream & str, const C_Str & obj )
128  { return str << obj.c_str(); }
129 
131 
135  namespace str
136  {
137 
139 
142  inline std::string asString( const std::string &t )
143  { return t; }
144 
145 #ifndef SWIG // Swig treats it as syntax error
146  inline std::string asString( std::string && t )
147  { return std::move(t); }
148 #endif
149 
150  inline std::string asString( const char * t )
151  { return t; }
152 
153  inline std::string asString( char * t )
154  { return t; }
155 
156  template<class _T>
157  inline std::string asString( const _T &t )
158  { return t.asString(); }
159 
160  template<class _T>
161  inline std::string asString( const intrusive_ptr<_T> &p )
162  { return p->asString(); }
163 
164  template<class _T>
165  inline std::string asString( const weak_ptr<_T> &p )
166  { return p->asString(); }
167 
168  template<>
169  inline std::string asString( const bool &t )
170  { return t ? "true" : "false"; }
171 
173 
174  std::string form( const char * format, ... )
175  __attribute__ ((format (printf, 1, 2)));
176 
178 
182  std::string strerror( int errno_r );
183 
185 
195  struct SafeBuf
196  {
197  char * _buf;
198  SafeBuf() : _buf( 0 ) {}
199  ~SafeBuf() { if ( _buf ) free( _buf ); }
200  std::string asString() const
201  { return _buf ? std::string(_buf) : std::string(); }
202  };
203 
216  struct Str
217  {
218  template<class Tp>
219  Str & operator<<( const Tp & val )
220  { _str << val; return *this; }
221 
222  Str & operator<<( std::ostream& (*iomanip)( std::ostream& ) )
223  { _str << iomanip; return *this; }
224 
225  operator std::string() const { return _str.str(); }
226  std::string asString() const { return _str.str(); }
227  std::string str() const { return _str.str(); }
228 
229  const std::ostream & stream() const { return _str; }
230  std::ostream & stream() { return _str; }
231 
232  void clear() { _str.str( std::string() ); }
233 
234  private:
235  std::ostringstream _str;
236  };
237 
239  inline std::ostream & operator<<( std::ostream & str, const Str & obj )
240  { return str << obj.str(); }
241 
254  struct Format
255  {
256  Format() {}
257  Format( const std::string & format_r ) : _fmter( format_r ) {}
258 
259  template<class Tp>
260  Format & operator%( Tp && arg )
261  { _fmter % std::forward<Tp>(arg); return *this; }
262 
263  operator std::string() const { return _fmter.str(); }
264  std::string asString() const { return _fmter.str(); }
265  std::string str() const { return _fmter.str(); }
266 
267  const boost::format & fmter() const { return _fmter; }
268  boost::format & fmter() { return _fmter; }
269 
270  protected:
271  boost::format _fmter;
272  };
273 
275  inline std::ostream & operator<<( std::ostream & str, const Format & obj )
276  { return str << obj.fmter(); }
277 
285  struct FormatNAC : public Format
286  {
287  FormatNAC() { relax(); }
288  FormatNAC( const std::string & format_r ) : Format( format_r ) { relax(); }
289 
290  private:
291  void relax()
292  {
293  using namespace boost::io;
294  _fmter.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
295  }
296  };
298 
311  inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
312  inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
313  inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
314  inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
315  inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
316  inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
317  inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
318  inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
319  inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
320  inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
321 
322  template<> inline std::string asString( const char & t ) { return numstring( t ); }
323  template<> inline std::string asString( const unsigned char & t ) { return numstring( t ); }
324  template<> inline std::string asString( const short & t ) { return numstring( t ); }
325  template<> inline std::string asString( const unsigned short & t ) { return numstring( t ); }
326  template<> inline std::string asString( const int & t ) { return numstring( t ); }
327  template<> inline std::string asString( const unsigned & t ) { return numstring( t ); }
328  template<> inline std::string asString( const long & t ) { return numstring( t ); }
329  template<> inline std::string asString( const unsigned long & t ) { return numstring( t ); }
330  template<> inline std::string asString( const long long & t ) { return numstring( t ); }
331  template<> inline std::string asString( const unsigned long long & t ) { return numstring( t ); }
333 
335 
346  inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
347  inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
348  inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
349  inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
350  inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
351  inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
352  inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
353  inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
354  inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
355  inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
357 
359 
370  inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
371  inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
372  inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
373  inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
374  inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
375  inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
376  inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
377  inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
378  inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
379  inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
381 
383 
392  template<typename _It>
393  _It strtonum( const C_Str & str );
394 
395  template<>
396  inline short strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
397  template<>
398  inline int strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
399  template<>
400  inline long strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
401  template<>
402  inline long long strtonum( const C_Str & str ) { return ::strtoll ( str, NULL, 0 ); }
403 
404  template<>
405  inline unsigned short strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
406  template<>
407  inline unsigned strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
408  template<>
409  inline unsigned long strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
410  template<>
411  inline unsigned long long strtonum( const C_Str & str ) { return ::strtoull( str, NULL, 0 ); }
412 
418  template<typename _It>
419  inline _It strtonum( const C_Str & str, _It & i )
420  { return i = strtonum<_It>( str ); }
422 
424 
428  bool strToTrue( const C_Str & str );
429 
431  bool strToFalse( const C_Str & str );
432 
437  inline bool strToBool( const C_Str & str, bool default_r )
438  { return( default_r ? strToFalse( str ) : strToTrue( str ) ); }
439 
444  inline bool strToBoolNodefault( const C_Str & str, bool & return_r )
445  {
446  if ( strToTrue( str ) ) return (return_r = true);
447  if ( !strToFalse( str ) ) return (return_r = false);
448  return return_r;
449  }
450 
452  TriBool strToTriBool( const C_Str & str );
453 
455 
459  std::string gsub( const std::string & str_r, const std::string & from_r, const std::string & to_r );
460 
463  std::string gsubFun( const std::string & str_r, const std::string & from_r, function<std::string()> to_r );
464 
469  std::string & replaceAll( std::string & str_r, const std::string & from_r, const std::string & to_r );
470 
473  std::string & replaceAllFun( std::string & str_r, const std::string & from_r, function<std::string()> to_r );
474 
487  inline std::string gapify( std::string inp_r, std::string::size_type gap_r = 1, char gapchar = ' ' )
488  {
489  if ( gap_r && inp_r.size() > gap_r )
490  {
491  inp_r.reserve( inp_r.size() + (inp_r.size()-1)/gap_r );
492  for ( std::string::size_type pos = gap_r; pos < inp_r.size(); pos += gap_r+1 )
493  inp_r.insert( pos, 1, gapchar );
494  }
495  return inp_r;
496  }
497 
499 
510  template<class _OutputIterator>
511  unsigned split( const C_Str & line_r,
512  _OutputIterator result_r,
513  const C_Str & sepchars_r = " \t" )
514  {
515  const char * beg = line_r;
516  const char * cur = beg;
517  // skip leading sepchars
518  while ( *cur && ::strchr( sepchars_r, *cur ) )
519  ++cur;
520  unsigned ret = 0;
521  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
522  {
523  // skip non sepchars
524  while( *cur && !::strchr( sepchars_r, *cur ) )
525  ++cur;
526  // build string
527  *result_r = std::string( beg, cur-beg );
528  // skip sepchars
529  while ( *cur && ::strchr( sepchars_r, *cur ) )
530  ++cur;
531  }
532  return ret;
533  }
534 
571  template<class _OutputIterator>
572  unsigned splitEscaped( const C_Str & line_r,
573  _OutputIterator result_r,
574  const C_Str & sepchars_r = " \t",
575  bool withEmpty = false)
576  {
577  const char * beg = line_r;
578  const char * cur = beg;
579  unsigned ret = 0;
580 
581  // skip leading sepchars
582  while ( *cur && ::strchr( sepchars_r, *cur ) )
583  {
584  ++cur;
585  if (withEmpty)
586  {
587  *result_r = "";
588  ++ret;
589  }
590  }
591 
592  // there were only sepchars in the string
593  if (!*cur && withEmpty)
594  {
595  *result_r = "";
596  return ++ret;
597  }
598 
599  // after the leading sepchars
600  enum class Quote { None, Slash, Single, Double, DoubleSlash };
601  std::vector<char> buf;
602  Quote quoting = Quote::None;
603  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
604  {
605  // read next value until unquoted sepchar
606  buf.clear();
607  quoting = Quote::None;
608  do {
609  switch ( quoting )
610  {
611  case Quote::None:
612  switch ( *cur )
613  {
614  case '\\': quoting = Quote::Slash; break;
615  case '\'': quoting = Quote::Single; break;
616  case '"': quoting = Quote::Double; break;
617  default: buf.push_back( *cur ); break;
618  }
619  break;
620 
621  case Quote::Slash:
622  buf.push_back( *cur );
623  quoting = Quote::None;
624  break;
625 
626  case Quote::Single:
627  switch ( *cur )
628  {
629  case '\'': quoting = Quote::None; break;
630  default: buf.push_back( *cur ); break;
631  }
632  break;
633 
634  case Quote::Double:
635  switch ( *cur )
636  {
637  case '\"': quoting = Quote::None; break;
638  case '\\': quoting = Quote::DoubleSlash; break;
639  default: buf.push_back( *cur ); break;
640  }
641  break;
642 
643  case Quote::DoubleSlash:
644  switch ( *cur )
645  {
646  case '\"': /*fallthrough*/
647  case '\\': buf.push_back( *cur ); break;
648  default:
649  buf.push_back( '\\' );
650  buf.push_back( *cur );
651  break;
652  }
653  quoting = Quote::Double;
654  break;
655  }
656  ++cur;
657  } while ( *cur && ( quoting != Quote::None || !::strchr( sepchars_r, *cur ) ) );
658  *result_r = std::string( buf.begin(), buf.end() );
659 
660 
661  // skip sepchars
662  if ( *cur && ::strchr( sepchars_r, *cur ) )
663  ++cur;
664  while ( *cur && ::strchr( sepchars_r, *cur ) )
665  {
666  ++cur;
667  if (withEmpty)
668  {
669  *result_r = "";
670  ++ret;
671  }
672  }
673  // the last was a separator => one more field
674  if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
675  {
676  *result_r = "";
677  ++ret;
678  }
679  }
680  return ret;
681  }
682 
704  template<class _OutputIterator>
705  unsigned splitFields( const C_Str & line_r,
706  _OutputIterator result_r,
707  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 _OutputIterator>
746  unsigned splitFieldsEscaped( const C_Str & line_r,
747  _OutputIterator result_r,
748  const C_Str & sepchars_r = ":" )
749  {
750  return
751  splitEscaped( line_r, result_r, sepchars_r, true /* withEmpty */ );
752  }
753 
755 
757 
760  template <class _Iterator>
761  std::string join( _Iterator begin, _Iterator end,
762  const C_Str & sep_r = " " )
763  {
764  std::string res;
765  for ( _Iterator iter = begin; iter != end; ++ iter )
766  {
767  if ( iter != begin )
768  res += sep_r;
769  res += asString(*iter);
770  }
771  return res;
772  }
773 
775  template <class _Container>
776  std::string join( const _Container & cont_r,
777  const C_Str & sep_r = " " )
778  { return join( cont_r.begin(), cont_r.end(), sep_r ); }
779 
784  template <class _Iterator>
785  std::string joinEscaped( _Iterator begin, _Iterator end,
786  const char sep_r = ' ' )
787  {
788  std::vector<char> buf;
789  for ( _Iterator iter = begin; iter != end; ++ iter )
790  {
791  if ( iter != begin )
792  buf.push_back( sep_r );
793 
794  if ( iter->empty() )
795  {
796  // empty string goes ""
797  buf.push_back( '"' );
798  buf.push_back( '"' );
799  }
800  else
801  {
802  std::string toadd( asString(*iter) );
803  for_( ch, toadd.begin(), toadd.end() )
804  {
805  switch ( *ch )
806  {
807  case '"':
808  case '\'':
809  case '\\':
810  buf.push_back( '\\' );
811  buf.push_back( *ch );
812  break;
813  default:
814  if ( *ch == sep_r )
815  buf.push_back( '\\' );
816  buf.push_back( *ch );
817  }
818  }
819  }
820  }
821  return std::string( buf.begin(), buf.end() );
822  }
824 
825 
827 
833  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, const std::string & indent_r = " ", unsigned maxWitdh_r = 0 )
834  {
835  if ( maxWitdh_r )
836  {
837  if ( indent_r.size() >= maxWitdh_r )
838  maxWitdh_r = 0; // nonsense: indent larger than line witdh
839  else
840  maxWitdh_r -= indent_r.size();
841  }
842  unsigned width = 0;
843  for ( const char * e = text_r.c_str(), * s = e; *e; s = ++e )
844  {
845  for ( ; *e && *e != '\n'; ++e ) ;/*searching*/
846  width = e-s;
847  if ( maxWitdh_r && width > maxWitdh_r )
848  {
849  // must break line
850  width = maxWitdh_r;
851  for ( e = s+width; e > s && *e != ' '; --e ) ;/*searching*/
852  if ( e > s )
853  width = e-s; // on a ' ', replaced by '\n'
854  else
855  e = s+width-1; // cut line;
856  }
857  str << indent_r;
858  str.write( s, width );
859  str << "\n";
860  if ( !*e ) // on '\0'
861  break;
862  }
863  return str;
864  }
866  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, char indentch_r = ' ', unsigned maxWitdh_r = 0 )
867  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
869  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r = ' ' )
870  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
871 
875  inline std::ostream & autoPrefix( std::ostream & str, const std::string & text_r, function<std::string(const char*, const char*)> fnc_r )
876  {
877  for ( const char * e = text_r.c_str(); *e; ++e )
878  {
879  const char * s = e;
880  for ( ; *e && *e != '\n'; ++e ) /*searching*/;
881  str << fnc_r( s, e );
882  str.write( s, e-s );
883  str << "\n";
884  if ( !*e ) // on '\0'
885  break;
886  }
887  return str;
888  }
890  inline std::ostream & autoPrefix0( std::ostream & str, const std::string & text_r, function<std::string()> fnc_r )
891  {
892  auto wrap = [&fnc_r]( const char*, const char* )-> std::string {
893  return fnc_r();
894  };
895  return autoPrefix( str, text_r, wrap );
896  }
898 
907  std::string escape( const C_Str & str_r, const char c = ' ' );
908 
910  inline void appendEscaped( std::string & str_r, const C_Str & next_r, const char sep_r = ' ' )
911  {
912  if ( ! str_r.empty() )
913  str_r += sep_r;
914  if ( next_r.empty() )
915  str_r += "\"\"";
916  else
917  str_r += escape( next_r, sep_r );
918  }
919 
921  std::string bEscape( std::string str_r, const C_Str & special_r );
922 
924  std::string rxEscapeStr( std::string str_r );
925 
927  std::string rxEscapeGlob( std::string str_r );
928 
930 
932 
942  std::string hexencode( const C_Str & str_r );
944  std::string hexdecode( const C_Str & str_r );
946 
953  std::string toLower( const std::string & s );
955  inline std::string toLower( const char * s )
956  { return( s ? toLower( std::string(s) ) : std::string() ); }
957 
961  std::string toUpper( const std::string & s );
963  inline std::string toUpper( const char * s )
964  { return( s ? toUpper( std::string(s) ) : std::string() ); }
966 
967 
970  inline int compareCI( const C_Str & lhs, const C_Str & rhs )
971  { return ::strcasecmp( lhs, rhs ); }
973 
977  inline bool contains( const C_Str & str_r, const C_Str & val_r )
978  { return ::strstr( str_r, val_r ); }
980  inline bool containsCI( const C_Str & str_r, const C_Str & val_r )
981  { return ::strcasestr( str_r, val_r ); }
983 
985 
990  enum Trim {
991  NO_TRIM = 0x00,
992  L_TRIM = 0x01,
993  R_TRIM = 0x02,
995  };
996 
997  std::string trim( const std::string & s, const Trim trim_r = TRIM );
998 
999  inline std::string ltrim( const std::string & s )
1000  { return trim( s, L_TRIM ); }
1001 
1002  inline std::string rtrim( const std::string & s )
1003  { return trim( s, R_TRIM ); }
1005 
1006  std::string stripFirstWord( std::string & line, const bool ltrim_first = true );
1007 
1008  std::string stripLastWord( std::string & line, const bool rtrim_first = true );
1009 
1013  std::string getline( std::istream & str, bool trim = false );
1014 
1018  std::string getline( std::istream & str, const Trim trim_r );
1019 
1027  std::string receiveUpTo( std::istream & str, const char delim_r, bool returnDelim_r = false );
1028 
1030 
1035  inline bool hasPrefix( const C_Str & str_r, const C_Str & prefix_r )
1036  { return( ::strncmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }
1037 
1039  inline std::string stripPrefix( const C_Str & str_r, const C_Str & prefix_r )
1040  { return( hasPrefix( str_r, prefix_r ) ? str_r + prefix_r.size() : str_r.c_str() ); }
1041 
1043  inline bool hasSuffix( const C_Str & str_r, const C_Str & suffix_r )
1044  { return( str_r.size() >= suffix_r.size() && ::strncmp( str_r + str_r.size() - suffix_r.size() , suffix_r, suffix_r.size() ) == 0 ); }
1045 
1047  inline std::string stripSuffix( const C_Str & str_r, const C_Str & suffix_r )
1048  {
1049  if ( hasSuffix( str_r, suffix_r ) )
1050  return std::string( str_r, str_r.size() - suffix_r.size() );
1051  return str_r.c_str();
1052  }
1054  inline std::string::size_type commonPrefix( const C_Str & lhs, const C_Str & rhs )
1055  {
1056  const char * lp = lhs.c_str();
1057  const char * rp = rhs.c_str();
1058  std::string::size_type ret = 0;
1059  while ( *lp == *rp && *lp != '\0' )
1060  { ++lp, ++rp, ++ret; }
1061  return ret;
1062  }
1063 
1065  inline bool startsWith( const C_Str & str_r, const C_Str & prefix_r )
1066  { return hasPrefix( str_r, prefix_r ); }
1068  inline bool endsWith( const C_Str & str_r, const C_Str & prefix_r )
1069  { return hasSuffix( str_r, prefix_r ); }
1071  } // namespace str
1073 
1074  // drag into zypp:: namespace
1075  using str::asString;
1076 
1077 } // namespace zypp
1079 #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:377
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
const std::ostream & stream() const
Definition: String.h:229
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:910
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
Definition: String.h:977
std::string octstring(char n, int w=4)
Definition: String.h:370
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:444
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:1039
unsigned split(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
Definition: String.h:511
std::string asUserString(const _Tp &val_r)
Request a human readable (translated) string representation of _Tp [_Tp.asUserString()] Classes may i...
Definition: String.h:39
std::string::size_type size_type
Definition: String.h:100
std::string rxEscapeStr(std::string str_r)
Escape plain STRING str_r for use in a regex (not anchored by "^" or "$").
Definition: String.cc:398
boost::format _fmter
Definition: String.h:271
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:1054
std::string escape(const C_Str &str_r, const char sep_r)
Escape desired character c using a backslash.
Definition: String.cc:354
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:1047
std::string asString() const
Definition: String.h:200
std::ostream & autoPrefix0(std::ostream &str, const std::string &text_r, function< std::string()> fnc_r)
Definition: String.h:890
C_Str(const std::string &str_r)
Definition: String.h:106
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
FormatNAC(const std::string &format_r)
Definition: String.h:288
unsigned splitFieldsEscaped(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=":")
Split line_r into fields handling also escaped separators.
Definition: String.h:746
Convenient building of std::string with boost::format.
Definition: String.h:254
Format & operator%(Tp &&arg)
Definition: String.h:260
std::string asString() const
Definition: String.h:226
MessageString(const _Str &str_r)
Definition: String.h:56
bool empty() const
Definition: String.h:110
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:990
std::string joinEscaped(_Iterator begin, _Iterator end, const char sep_r= ' ')
Join strings using separator sep_r, quoting or escaping the values.
Definition: String.h:785
std::string asString() const
Definition: String.h:264
unsigned splitEscaped(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t", bool withEmpty=false)
Split line_r into words with respect to escape delimeters.
Definition: String.h:572
std::string ltrim(const std::string &s)
Definition: String.h:999
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
Definition: String.h:29
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:313
std::string str() const
Definition: String.h:227
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:833
std::string str() const
Definition: String.h:265
std::string stripFirstWord(std::string &line, const bool ltrim_first)
Definition: String.cc:246
std::string rxEscapeGlob(std::string str_r)
Escape GLOB str_r for use in a regex (not anchored by "^" or "$").
Definition: String.cc:403
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition: String.h:216
std::string trim(const std::string &s, const Trim trim_r)
Definition: String.cc:213
std::string join(_Iterator begin, _Iterator end, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
Definition: String.h:761
boost::format & fmter()
Definition: String.h:268
Convenience char* constructible from std::string and char*, it maps (char*)0 to an empty string...
Definition: String.h:97
std::ostream & operator<<(std::ostream &str, const Format &obj)
Definition: String.h:275
bool startsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasPrefix
Definition: String.h:1065
const char *const _val
Definition: String.h:122
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:487
std::string getline(std::istream &str, const Trim trim_r)
Return stream content up to (but not returning) the next newline.
Definition: String.cc:461
const boost::format & fmter() const
Definition: String.h:267
std::string stripLastWord(std::string &line, const bool rtrim_first)
Definition: String.cc:279
bool strToFalse(const C_Str &str)
Return false if str is 0, false, no, off.
Definition: String.cc:80
MessageString(const char *str_r)
Definition: String.h:52
bool containsCI(const C_Str &str_r, const C_Str &val_r)
Locate substring case insensitive.
Definition: String.h:980
_It strtonum(const C_Str &str)
Parsing numbers from string.
Definition: String.h:396
std::string gsubFun(const std::string &str_r, const std::string &from_r, function< std::string()> to_r)
Definition: String.cc:330
bool endsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasSuffix
Definition: String.h:1068
std::string numstring(char n, int w=0)
Definition: String.h:311
size_type _sze
Definition: String.h:123
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:970
SolvableIdType size_type
Definition: PoolMember.h:147
std::string rtrim(const std::string &s)
Definition: String.h:1002
std::ostream & operator<<(std::ostream &str, const Str &obj)
Definition: String.h:239
bool strToTrue(const C_Str &str)
Parsing boolean from string.
Definition: String.cc:63
std::string & replaceAllFun(std::string &str_r, const std::string &from_r, function< std::string()> to_r)
Definition: String.cc:336
bool hasSuffix(const C_Str &str_r, const C_Str &suffix_r)
Return whether str_r has suffix suffix_r.
Definition: String.h:1043
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:307
bool isNull() const
Definition: String.h:109
C_Str(const char *c_str_r)
Definition: String.h:105
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:471
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Definition: String.h:437
const char * c_str() const
Definition: String.h:119
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:222
MessageString(const std::string &str_r)
Definition: String.h:53
std::ostream & operator<<(std::ostream &str, const C_Str &obj)
Definition: String.h:127
size_type size() const
Definition: String.h:111
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:875
Str & operator<<(const Tp &val)
Definition: String.h:219
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:104
std::string asString(const std::string &t)
Global asString() that works with std::string too.
Definition: String.h:142
std::string toUpper(const std::string &s)
Return uppercase version of s.
Definition: String.cc:194
std::ostream & stream()
Definition: String.h:230
Assert free called for allocated char *.
Definition: String.h:195
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition: String.h:1035
void clear()
Definition: String.h:232
Format with (N)o (A)rgument (C)heck.
Definition: String.h:285
std::string hexstring(char n, int w=4)
Definition: String.h:346
unsigned splitFields(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=":")
Split line_r into fields.
Definition: String.h:705
std::ostringstream _str
Definition: String.h:235
std::string hexdecode(const C_Str &str_r)
Decode hexencoded XX sequences.
Definition: String.cc:143
Format(const std::string &format_r)
Definition: String.h:257