libzypp  15.19.7
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 boost
35 {
41  inline format formatNAC( const std::string & string_r ) {
42  using namespace boost::io;
43  format fmter( string_r );
44  fmter.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
45  return fmter;
46  }
47 } // namespace boost
48 namespace zypp { using boost::formatNAC; }
50 
52 namespace zypp
53 {
57  template <class Tp>
58  std::string asUserString( const Tp & val_r )
59  { return val_r.asUserString(); }
60 
61 }// namespace zypp
63 
65 namespace zypp
66 {
67 
68  struct MessageString : public std::string
69  {
71  MessageString( const char * str_r ) : std::string( str_r ? str_r : "" ) {}
72  MessageString( const std::string & str_r ) : std::string( str_r ) {}
73  // boost::format, std::ostringstream, str::Str ...
74  template<class TStr>
75  MessageString( const TStr & str_r ) : std::string( str_r.str() ) {}
76  };
77 
118  class C_Str
119  {
120  public:
122 
123  public:
124  C_Str() : _val( 0 ), _sze( 0 ) {}
125  C_Str( char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
126  C_Str( const char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
127  C_Str( const std::string & str_r ) : _val( str_r.c_str() ), _sze( str_r.size() ) {}
128  C_Str( const boost::string_ref & str_r ) : _val( str_r.data() ), _sze( str_r.size() ) {}
129 
130  public:
131  bool isNull() const { return !_val; }
132  bool empty() const { return !(_val && *_val); }
133  size_type size() const
134  {
135  if ( _sze == std::string::npos )
136  { _sze = _val ? ::strlen( _val ) : 0; }
137  return _sze;
138  };
139 
140  operator const char *() const { return c_str(); }
141  const char * c_str() const { return _val ? _val : ""; }
142 
143  private:
144  const char *const _val;
145  mutable size_type _sze;
146  };
147 
149  inline std::ostream & operator<<( std::ostream & str, const C_Str & obj )
150  { return str << obj.c_str(); }
151 
153 
157  namespace str
158  {
159 
161 
164  inline const std::string & asString( const std::string & t )
165  { return t; }
166 
167 #ifndef SWIG // Swig treats it as syntax error
168  inline std::string && asString( std::string && t )
169  { return std::move(t); }
170 #endif
171 
172  inline std::string asString( const char * t )
173  { return t; }
174 
175  inline std::string asString( char * t )
176  { return t; }
177 
178  template<class Tp>
179  inline std::string asString( const Tp &t )
180  { return t.asString(); }
181 
182  template<class Tp>
183  inline std::string asString( const intrusive_ptr<Tp> &p )
184  { return p->asString(); }
185 
186  template<class Tp>
187  inline std::string asString( const weak_ptr<Tp> &p )
188  { return p->asString(); }
189 
190  template<>
191  inline std::string asString( const bool &t )
192  { return t ? "true" : "false"; }
193 
195 
196  std::string form( const char * format, ... )
197  __attribute__ ((format (printf, 1, 2)));
198 
200 
204  std::string strerror( int errno_r );
205 
207 
217  struct SafeBuf
218  {
219  char * _buf;
220  SafeBuf() : _buf( 0 ) {}
221  ~SafeBuf() { if ( _buf ) free( _buf ); }
222  std::string asString() const
223  { return _buf ? std::string(_buf) : std::string(); }
224  };
225 
227 
237  struct Str
238  {
239  template<class Tp>
240  Str & operator<<( const Tp & val )
241  { _str << val; return *this; }
242 
243  Str & operator<<( std::ostream& (*iomanip)( std::ostream& ) )
244  { _str << iomanip; return *this; }
245 
246  operator std::string() const
247  { return _str.str(); }
248 
249  std::string str() const
250  { return _str.str(); }
251 
252  std::ostream & stream()
253  { return _str; }
254 
255  void clear()
256  { _str.str( std::string() ); }
257 
258  std::ostringstream _str;
259  };
260 
261  inline std::ostream & operator<<( std::ostream & str, const Str & obj )
262  { return str << (std::string)obj; }
263 
265 
278  inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
279  inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
280  inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
281  inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
282  inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
283  inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
284  inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
285  inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
286  inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
287  inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
288 
289  template<> inline std::string asString( const char & t ) { return numstring( t ); }
290  template<> inline std::string asString( const unsigned char & t ) { return numstring( t ); }
291  template<> inline std::string asString( const short & t ) { return numstring( t ); }
292  template<> inline std::string asString( const unsigned short & t ) { return numstring( t ); }
293  template<> inline std::string asString( const int & t ) { return numstring( t ); }
294  template<> inline std::string asString( const unsigned & t ) { return numstring( t ); }
295  template<> inline std::string asString( const long & t ) { return numstring( t ); }
296  template<> inline std::string asString( const unsigned long & t ) { return numstring( t ); }
297  template<> inline std::string asString( const long long & t ) { return numstring( t ); }
298  template<> inline std::string asString( const unsigned long long & t ) { return numstring( t ); }
300 
302 
313  inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
314  inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
315  inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
316  inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
317  inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
318  inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
319  inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
320  inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
321  inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
322  inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
324 
326 
337  inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
338  inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
339  inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
340  inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
341  inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
342  inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
343  inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
344  inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
345  inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
346  inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
348 
349 
351 
352  template <typename TInt>
353  std::string binstring( TInt val_r )
354  {
355  constexpr unsigned bits = sizeof(TInt)*8;
356  std::string ret( bits, ' ' );
357  TInt bit = 1;
358  for ( unsigned pos = bits; pos > 0; )
359  { --pos; ret[pos] = ((val_r & bit)?'1':'0'); bit = bit<<1; }
360  return ret;
361  }
362 
364 
373  template<typename TInt>
374  TInt strtonum( const C_Str & str );
375 
376  template<>
377  inline short strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
378  template<>
379  inline int strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
380  template<>
381  inline long strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
382  template<>
383  inline long long strtonum( const C_Str & str ) { return ::strtoll ( str, NULL, 0 ); }
384 
385  template<>
386  inline unsigned short strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
387  template<>
388  inline unsigned strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
389  template<>
390  inline unsigned long strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
391  template<>
392  inline unsigned long long strtonum( const C_Str & str ) { return ::strtoull( str, NULL, 0 ); }
393 
399  template<typename TInt>
400  inline TInt strtonum( const C_Str & str, TInt & i )
401  { return i = strtonum<TInt>( str ); }
403 
405 
409  bool strToTrue( const C_Str & str );
410 
412  bool strToFalse( const C_Str & str );
413 
418  inline bool strToBool( const C_Str & str, bool default_r )
419  { return( default_r ? strToFalse( str ) : strToTrue( str ) ); }
420 
425  inline bool strToBoolNodefault( const C_Str & str, bool & return_r )
426  {
427  if ( strToTrue( str ) ) return (return_r = true);
428  if ( !strToFalse( str ) ) return (return_r = false);
429  return return_r;
430  }
431 
433  TriBool strToTriBool( const C_Str & str );
434 
436 
440  std::string gsub( const std::string & str_r, const std::string & from_r, const std::string & to_r );
441 
444  std::string gsubFun( const std::string & str_r, const std::string & from_r, function<std::string()> to_r );
445 
450  std::string & replaceAll( std::string & str_r, const std::string & from_r, const std::string & to_r );
451 
454  std::string & replaceAllFun( std::string & str_r, const std::string & from_r, function<std::string()> to_r );
455 
468  inline std::string gapify( std::string inp_r, std::string::size_type gap_r = 1, char gapchar = ' ' )
469  {
470  if ( gap_r && inp_r.size() > gap_r )
471  {
472  inp_r.reserve( inp_r.size() + (inp_r.size()-1)/gap_r );
473  for ( std::string::size_type pos = gap_r; pos < inp_r.size(); pos += gap_r+1 )
474  inp_r.insert( pos, 1, gapchar );
475  }
476  return inp_r;
477  }
478 
480 
491  template<class TOutputIterator>
492  unsigned split( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = " \t" )
493  {
494  const char * beg = line_r;
495  const char * cur = beg;
496  // skip leading sepchars
497  while ( *cur && ::strchr( sepchars_r, *cur ) )
498  ++cur;
499  unsigned ret = 0;
500  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
501  {
502  // skip non sepchars
503  while( *cur && !::strchr( sepchars_r, *cur ) )
504  ++cur;
505  // build string
506  *result_r = std::string( beg, cur-beg );
507  // skip sepchars
508  while ( *cur && ::strchr( sepchars_r, *cur ) )
509  ++cur;
510  }
511  return ret;
512  }
513 
550  template<class TOutputIterator>
551  unsigned splitEscaped( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = " \t", bool withEmpty = false)
552  {
553  const char * beg = line_r;
554  const char * cur = beg;
555  unsigned ret = 0;
556 
557  // skip leading sepchars
558  while ( *cur && ::strchr( sepchars_r, *cur ) )
559  {
560  ++cur;
561  if (withEmpty)
562  {
563  *result_r = "";
564  ++ret;
565  }
566  }
567 
568  // there were only sepchars in the string
569  if (!*cur && withEmpty)
570  {
571  *result_r = "";
572  return ++ret;
573  }
574 
575  // after the leading sepchars
576  enum class Quote { None, Slash, Single, Double, DoubleSlash };
577  std::vector<char> buf;
578  Quote quoting = Quote::None;
579  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
580  {
581  // read next value until unquoted sepchar
582  buf.clear();
583  quoting = Quote::None;
584  do {
585  switch ( quoting )
586  {
587  case Quote::None:
588  switch ( *cur )
589  {
590  case '\\': quoting = Quote::Slash; break;
591  case '\'': quoting = Quote::Single; break;
592  case '"': quoting = Quote::Double; break;
593  default: buf.push_back( *cur ); break;
594  }
595  break;
596 
597  case Quote::Slash:
598  buf.push_back( *cur );
599  quoting = Quote::None;
600  break;
601 
602  case Quote::Single:
603  switch ( *cur )
604  {
605  case '\'': quoting = Quote::None; break;
606  default: buf.push_back( *cur ); break;
607  }
608  break;
609 
610  case Quote::Double:
611  switch ( *cur )
612  {
613  case '\"': quoting = Quote::None; break;
614  case '\\': quoting = Quote::DoubleSlash; break;
615  default: buf.push_back( *cur ); break;
616  }
617  break;
618 
619  case Quote::DoubleSlash:
620  switch ( *cur )
621  {
622  case '\"': /*fallthrough*/
623  case '\\': buf.push_back( *cur ); break;
624  default:
625  buf.push_back( '\\' );
626  buf.push_back( *cur );
627  break;
628  }
629  quoting = Quote::Double;
630  break;
631  }
632  ++cur;
633  } while ( *cur && ( quoting != Quote::None || !::strchr( sepchars_r, *cur ) ) );
634  *result_r = std::string( buf.begin(), buf.end() );
635 
636 
637  // skip sepchars
638  if ( *cur && ::strchr( sepchars_r, *cur ) )
639  ++cur;
640  while ( *cur && ::strchr( sepchars_r, *cur ) )
641  {
642  ++cur;
643  if (withEmpty)
644  {
645  *result_r = "";
646  ++ret;
647  }
648  }
649  // the last was a separator => one more field
650  if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
651  {
652  *result_r = "";
653  ++ret;
654  }
655  }
656  return ret;
657  }
658 
680  template<class TOutputIterator>
681  unsigned splitFields( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = ":" )
682  {
683  const char * beg = line_r;
684  const char * cur = beg;
685  unsigned ret = 0;
686  for ( beg = cur; *beg; beg = cur, ++result_r )
687  {
688  // skip non sepchars
689  while( *cur && !::strchr( sepchars_r, *cur ) )
690  {
691  if ( *cur == '\\' && *(cur+1) )
692  ++cur;
693  ++cur;
694  }
695  // build string
696  *result_r = std::string( beg, cur-beg );
697  ++ret;
698  // skip sepchar
699  if ( *cur )
700  {
701  ++cur;
702  if ( ! *cur ) // ending with sepchar
703  {
704  *result_r = std::string(); // add final empty field
705  ++ret;
706  break;
707  }
708  }
709  }
710  return ret;
711  }
712 
719  template<class TOutputIterator>
720  unsigned splitFieldsEscaped( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = ":" )
721  {
722  return splitEscaped( line_r, result_r, sepchars_r, true /* withEmpty */ );
723  }
724 
726 
728 
731  template <class TIterator>
732  std::string join( TIterator begin, TIterator end, const C_Str & sep_r = " " )
733  {
734  std::string res;
735  for ( TIterator iter = begin; iter != end; ++ iter )
736  {
737  if ( iter != begin )
738  res += sep_r;
739  res += asString(*iter);
740  }
741  return res;
742  }
743 
745  template <class TContainer>
746  std::string join( const TContainer & cont_r, const C_Str & sep_r = " " )
747  { return join( cont_r.begin(), cont_r.end(), sep_r ); }
748 
753  template <class TIterator>
754  std::string joinEscaped( TIterator begin, TIterator end, const char sep_r = ' ' )
755  {
756  std::vector<char> buf;
757  for ( TIterator iter = begin; iter != end; ++ iter )
758  {
759  if ( iter != begin )
760  buf.push_back( sep_r );
761 
762  if ( iter->empty() )
763  {
764  // empty string goes ""
765  buf.push_back( '"' );
766  buf.push_back( '"' );
767  }
768  else
769  {
770  std::string toadd( asString(*iter) );
771  for_( ch, toadd.begin(), toadd.end() )
772  {
773  switch ( *ch )
774  {
775  case '"':
776  case '\'':
777  case '\\':
778  buf.push_back( '\\' );
779  buf.push_back( *ch );
780  break;
781  default:
782  if ( *ch == sep_r )
783  buf.push_back( '\\' );
784  buf.push_back( *ch );
785  }
786  }
787  }
788  }
789  return std::string( buf.begin(), buf.end() );
790  }
792 
793 
795 
801  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, const std::string & indent_r = " ", unsigned maxWitdh_r = 0 )
802  {
803  if ( maxWitdh_r )
804  {
805  if ( indent_r.size() >= maxWitdh_r )
806  maxWitdh_r = 0; // nonsense: indent larger than line witdh
807  else
808  maxWitdh_r -= indent_r.size();
809  }
810  unsigned width = 0;
811  for ( const char * e = text_r.c_str(), * s = e; *e; s = ++e )
812  {
813  for ( ; *e && *e != '\n'; ++e ) ;/*searching*/
814  width = e-s;
815  if ( maxWitdh_r && width > maxWitdh_r )
816  {
817  // must break line
818  width = maxWitdh_r;
819  for ( e = s+width; e > s && *e != ' '; --e ) ;/*searching*/
820  if ( e > s )
821  width = e-s; // on a ' ', replaced by '\n'
822  else
823  e = s+width-1; // cut line;
824  }
825  str << indent_r;
826  str.write( s, width );
827  str << "\n";
828  if ( !*e ) // on '\0'
829  break;
830  }
831  return str;
832  }
834  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, char indentch_r = ' ', unsigned maxWitdh_r = 0 )
835  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
837  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r = ' ' )
838  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
839 
843  inline std::ostream & autoPrefix( std::ostream & str, const std::string & text_r, function<std::string(const char*, const char*)> fnc_r )
844  {
845  for ( const char * e = text_r.c_str(); *e; ++e )
846  {
847  const char * s = e;
848  for ( ; *e && *e != '\n'; ++e ) /*searching*/;
849  str << fnc_r( s, e );
850  str.write( s, e-s );
851  str << "\n";
852  if ( !*e ) // on '\0'
853  break;
854  }
855  return str;
856  }
858  inline std::ostream & autoPrefix0( std::ostream & str, const std::string & text_r, function<std::string()> fnc_r )
859  {
860  auto wrap = [&fnc_r]( const char*, const char* )-> std::string {
861  return fnc_r();
862  };
863  return autoPrefix( str, text_r, wrap );
864  }
866 
875  std::string escape( const C_Str & str_r, const char c = ' ' );
876 
878  inline void appendEscaped( std::string & str_r, const C_Str & next_r, const char sep_r = ' ' )
879  {
880  if ( ! str_r.empty() )
881  str_r += sep_r;
882  if ( next_r.empty() )
883  str_r += "\"\"";
884  else
885  str_r += escape( next_r, sep_r );
886  }
887 
889 
891 
901  std::string hexencode( const C_Str & str_r );
903  std::string hexdecode( const C_Str & str_r );
905 
912  std::string toLower( const std::string & s );
914  inline std::string toLower( const char * s )
915  { return( s ? toLower( std::string(s) ) : std::string() ); }
916 
920  std::string toUpper( const std::string & s );
922  inline std::string toUpper( const char * s )
923  { return( s ? toUpper( std::string(s) ) : std::string() ); }
925 
926 
929  inline int compareCI( const C_Str & lhs, const C_Str & rhs )
930  { return ::strcasecmp( lhs, rhs ); }
932 
936  inline bool contains( const C_Str & str_r, const C_Str & val_r )
937  { return ::strstr( str_r, val_r ); }
939  inline bool containsCI( const C_Str & str_r, const C_Str & val_r )
940  { return ::strcasestr( str_r, val_r ); }
942 
944 
949  enum Trim {
950  NO_TRIM = 0x00,
951  L_TRIM = 0x01,
952  R_TRIM = 0x02,
954  };
955 
956  std::string trim( const std::string & s, const Trim trim_r = TRIM );
957 
958  inline std::string ltrim( const std::string & s )
959  { return trim( s, L_TRIM ); }
960 
961  inline std::string rtrim( const std::string & s )
962  { return trim( s, R_TRIM ); }
964 
965  std::string stripFirstWord( std::string & line, const bool ltrim_first = true );
966 
967  std::string stripLastWord( std::string & line, const bool rtrim_first = true );
968 
972  std::string getline( std::istream & str, bool trim = false );
973 
977  std::string getline( std::istream & str, const Trim trim_r );
978 
986  std::string receiveUpTo( std::istream & str, const char delim_r, bool returnDelim_r = false );
987 
989 
994  inline bool hasPrefix( const C_Str & str_r, const C_Str & prefix_r )
995  { return( ::strncmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }
996 
998  inline std::string stripPrefix( const C_Str & str_r, const C_Str & prefix_r )
999  { return( hasPrefix( str_r, prefix_r ) ? str_r + prefix_r.size() : str_r.c_str() ); }
1000 
1002  inline bool hasSuffix( const C_Str & str_r, const C_Str & suffix_r )
1003  { return( str_r.size() >= suffix_r.size() && ::strncmp( str_r + str_r.size() - suffix_r.size() , suffix_r, suffix_r.size() ) == 0 ); }
1004 
1006  inline std::string stripSuffix( const C_Str & str_r, const C_Str & suffix_r )
1007  {
1008  if ( hasSuffix( str_r, suffix_r ) )
1009  return std::string( str_r, str_r.size() - suffix_r.size() );
1010  return str_r.c_str();
1011  }
1013  inline std::string::size_type commonPrefix( const C_Str & lhs, const C_Str & rhs )
1014  {
1015  const char * lp = lhs.c_str();
1016  const char * rp = rhs.c_str();
1017  std::string::size_type ret = 0;
1018  while ( *lp == *rp && *lp != '\0' )
1019  { ++lp, ++rp, ++ret; }
1020  return ret;
1021  }
1022 
1024  inline bool startsWith( const C_Str & str_r, const C_Str & prefix_r )
1025  { return hasPrefix( str_r, prefix_r ); }
1027  inline bool endsWith( const C_Str & str_r, const C_Str & prefix_r )
1028  { return hasSuffix( str_r, prefix_r ); }
1030  } // namespace str
1032 
1033  // drag into zypp:: namespace
1034  using str::asString;
1035 
1036 } // namespace zypp
1038 #endif // ZYPP_BASE_STRING_H
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:551
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:878
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
Definition: String.h:936
C_Str(const boost::string_ref &str_r)
Definition: String.h:128
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:425
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:998
std::string::size_type size_type
Definition: String.h:121
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:1013
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:1006
std::string asString() const
Definition: String.h:222
std::ostream & autoPrefix0(std::ostream &str, const std::string &text_r, function< std::string()> fnc_r)
Definition: String.h:858
String related utilities and Regular expression matching.
C_Str(const std::string &str_r)
Definition: String.h:127
Definition: Arch.h:339
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
bool empty() const
Definition: String.h:132
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:949
std::string ltrim(const std::string &s)
Definition: String.h:958
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:313
std::string str() const
Definition: String.h:249
std::string getline(std::istream &str)
Read one line from stream.
Definition: IOStream.cc:33
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
Definition: String.h:492
std::string stripFirstWord(std::string &line, const bool ltrim_first)
Definition: String.cc:246
std::string asString(const unsigned long long &t)
Definition: String.h:298
format formatNAC(const std::string &string_r)
A formater with (N)o (A)rgument (C)heck.
Definition: String.h:41
Convenient building of std::string via std::ostream::operator<<.
Definition: String.h:237
std::string trim(const std::string &s, const Trim trim_r)
Definition: String.cc:213
Convenience char* constructible from std::string and char*, it maps (char*)0 to an empty string...
Definition: String.h:118
bool startsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasPrefix
Definition: String.h:1024
const char *const _val
Definition: String.h:144
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:468
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:71
bool containsCI(const C_Str &str_r, const C_Str &val_r)
Locate substring case insensitive.
Definition: String.h:939
std::ostream & printIndented(std::ostream &str, const std::string &text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r= ' ')
Definition: String.h:837
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:1027
std::ostream & operator<<(std::ostream &str, const zypp::sat::detail::CDataiterator *obj)
Definition: LookupAttr.cc:811
size_type _sze
Definition: String.h:145
int compareCI(const C_Str &lhs, const C_Str &rhs)
Definition: String.h:929
SolvableIdType size_type
Definition: PoolMember.h:126
std::string rtrim(const std::string &s)
Definition: String.h:961
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:754
MessageString(const TStr &str_r)
Definition: String.h:75
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:681
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:1002
std::string hexstring(unsigned long long n, int w=0)
Definition: String.h:322
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:131
C_Str(const char *c_str_r)
Definition: String.h:126
TInt strtonum(const C_Str &str, TInt &i)
String to integer type detemined 2nd function arg i.
Definition: String.h:400
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:386
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Definition: String.h:418
const char * c_str() const
Definition: String.h:141
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:243
std::string binstring(TInt val_r)
String representation of number as bit-string with leading '0's.
Definition: String.h:353
MessageString(const std::string &str_r)
Definition: String.h:72
std::string octstring(unsigned long long n, int w=0)
Definition: String.h:346
std::ostream & operator<<(std::ostream &str, const C_Str &obj)
Definition: String.h:149
size_type size() const
Definition: String.h:133
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:843
Str & operator<<(const Tp &val)
Definition: String.h:240
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:125
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
std::string asUserString(const Tp &val_r)
Request a human readable (translated) string representation of Tp [Tp.asUserString()] Classes may imp...
Definition: String.h:58
std::ostream & stream()
Definition: String.h:252
std::string join(const TContainer &cont_r, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
Definition: String.h:746
std::string toUpper(const char *s)
Definition: String.h:922
Assert free called for allocated char *.
Definition: String.h:217
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition: String.h:994
std::string toLower(const char *s)
Definition: String.h:914
void clear()
Definition: String.h:255
std::ostringstream _str
Definition: String.h:258
std::string hexdecode(const C_Str &str_r)
Decode hexencoded XX sequences.
Definition: String.cc:143
std::string numstring(unsigned long long n, int w=0)
Definition: String.h:287
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:720