libzypp  15.3.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 
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 boost
34 {
40  inline format formatNAC( const std::string & string_r ) {
41  using namespace boost::io;
42  format fmter( string_r );
43  fmter.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
44  return fmter;
45  }
46 } // namespace boost
47 namespace zypp { using boost::formatNAC; }
49 
51 namespace zypp
52 {
56  template <class _Tp>
57  std::string asUserString( const _Tp & val_r )
58  { return val_r.asUserString(); }
59 
60 }// namespace zypp
62 
64 namespace zypp
65 {
66 
67  struct MessageString : public std::string
68  {
70  MessageString( const char * str_r ) : std::string( str_r ? str_r : "" ) {}
71  MessageString( const std::string & str_r ) : std::string( str_r ) {}
72  // boost::format, std::ostringstream, str::Str ...
73  template<class _Str>
74  MessageString( const _Str & str_r ) : std::string( str_r.str() ) {}
75  };
76 
115  class C_Str
116  {
117  public:
119 
120  public:
121  C_Str() : _val( 0 ), _sze( 0 ) {}
122  C_Str( char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
123  C_Str( const char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
124  C_Str( const std::string & str_r ) : _val( str_r.c_str() ), _sze( str_r.size() ) {}
125 
126  public:
127  bool isNull() const { return !_val; }
128  bool empty() const { return !(_val && *_val); }
129  size_type size() const
130  {
131  if ( _sze == std::string::npos )
132  { _sze = _val ? ::strlen( _val ) : 0; }
133  return _sze;
134  };
135 
136  operator const char *() const { return c_str(); }
137  const char * c_str() const { return _val ? _val : ""; }
138 
139  private:
140  const char *const _val;
141  mutable size_type _sze;
142  };
143 
145  inline std::ostream & operator<<( std::ostream & str, const C_Str & obj )
146  { return str << obj.c_str(); }
147 
149 
153  namespace str
154  {
155 
157 
160  inline std::string asString( const std::string &t )
161  { return t; }
162 
163 #ifndef SWIG // Swig treats it as syntax error
164  inline std::string asString( std::string && t )
165  { return std::move(t); }
166 #endif
167 
168  inline std::string asString( const char * t )
169  { return t; }
170 
171  inline std::string asString( char * t )
172  { return t; }
173 
174  template<class _T>
175  inline std::string asString( const _T &t )
176  { return t.asString(); }
177 
178  template<class _T>
179  inline std::string asString( const intrusive_ptr<_T> &p )
180  { return p->asString(); }
181 
182  template<class _T>
183  inline std::string asString( const weak_ptr<_T> &p )
184  { return p->asString(); }
185 
186  template<>
187  inline std::string asString( const bool &t )
188  { return t ? "true" : "false"; }
189 
191 
192  std::string form( const char * format, ... )
193  __attribute__ ((format (printf, 1, 2)));
194 
196 
200  std::string strerror( int errno_r );
201 
203 
213  struct SafeBuf
214  {
215  char * _buf;
216  SafeBuf() : _buf( 0 ) {}
217  ~SafeBuf() { if ( _buf ) free( _buf ); }
218  std::string asString() const
219  { return _buf ? std::string(_buf) : std::string(); }
220  };
221 
223 
233  struct Str
234  {
235  template<class _Tp>
236  Str & operator<<( const _Tp & val )
237  { _str << val; return *this; }
238 
239  operator std::string() const
240  { return _str.str(); }
241 
242  std::string str() const
243  { return _str.str(); }
244 
245  std::ostream & stream()
246  { return _str; }
247 
248  void clear()
249  { _str.str( std::string() ); }
250 
251  std::ostringstream _str;
252  };
253 
254  inline std::ostream & operator<<( std::ostream & str, const Str & obj )
255  { return str << (std::string)obj; }
256 
258 
271  inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
272  inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
273  inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
274  inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
275  inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
276  inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
277  inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
278  inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
279  inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
280  inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
281 
282  template<> inline std::string asString( const char & t ) { return numstring( t ); }
283  template<> inline std::string asString( const unsigned char & t ) { return numstring( t ); }
284  template<> inline std::string asString( const short & t ) { return numstring( t ); }
285  template<> inline std::string asString( const unsigned short & t ) { return numstring( t ); }
286  template<> inline std::string asString( const int & t ) { return numstring( t ); }
287  template<> inline std::string asString( const unsigned & t ) { return numstring( t ); }
288  template<> inline std::string asString( const long & t ) { return numstring( t ); }
289  template<> inline std::string asString( const unsigned long & t ) { return numstring( t ); }
290  template<> inline std::string asString( const long long & t ) { return numstring( t ); }
291  template<> inline std::string asString( const unsigned long long & t ) { return numstring( t ); }
293 
295 
306  inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
307  inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
308  inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
309  inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
310  inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
311  inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
312  inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
313  inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
314  inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
315  inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
317 
319 
330  inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
331  inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
332  inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
333  inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
334  inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
335  inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
336  inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
337  inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
338  inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
339  inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
341 
343 
352  template<typename _It>
353  _It strtonum( const C_Str & str );
354 
355  template<>
356  inline short strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
357  template<>
358  inline int strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
359  template<>
360  inline long strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
361  template<>
362  inline long long strtonum( const C_Str & str ) { return ::strtoll ( str, NULL, 0 ); }
363 
364  template<>
365  inline unsigned short strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
366  template<>
367  inline unsigned strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
368  template<>
369  inline unsigned long strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
370  template<>
371  inline unsigned long long strtonum( const C_Str & str ) { return ::strtoull( str, NULL, 0 ); }
372 
378  template<typename _It>
379  inline _It strtonum( const C_Str & str, _It & i )
380  { return i = strtonum<_It>( str ); }
382 
384 
388  bool strToTrue( const C_Str & str );
389 
391  bool strToFalse( const C_Str & str );
392 
397  inline bool strToBool( const C_Str & str, bool default_r )
398  { return( default_r ? strToFalse( str ) : strToTrue( str ) ); }
399 
404  inline bool strToBoolNodefault( const C_Str & str, bool & return_r )
405  {
406  if ( strToTrue( str ) ) return (return_r = true);
407  if ( !strToFalse( str ) ) return (return_r = false);
408  return return_r;
409  }
410 
412  TriBool strToTriBool( const C_Str & str );
413 
415 
419  std::string gsub( const std::string & str_r, const std::string & from_r, const std::string & to_r );
420 
423  std::string gsubFun( const std::string & str_r, const std::string & from_r, function<std::string()> to_r );
424 
429  std::string & replaceAll( std::string & str_r, const std::string & from_r, const std::string & to_r );
430 
433  std::string & replaceAllFun( std::string & str_r, const std::string & from_r, function<std::string()> to_r );
434 
447  inline std::string gapify( std::string inp_r, std::string::size_type gap_r = 1, char gapchar = ' ' )
448  {
449  if ( gap_r && inp_r.size() > gap_r )
450  {
451  inp_r.reserve( inp_r.size() + (inp_r.size()-1)/gap_r );
452  for ( std::string::size_type pos = gap_r; pos < inp_r.size(); pos += gap_r+1 )
453  inp_r.insert( pos, 1, gapchar );
454  }
455  return inp_r;
456  }
457 
459 
470  template<class _OutputIterator>
471  unsigned split( const C_Str & line_r,
472  _OutputIterator result_r,
473  const C_Str & sepchars_r = " \t" )
474  {
475  const char * beg = line_r;
476  const char * cur = beg;
477  // skip leading sepchars
478  while ( *cur && ::strchr( sepchars_r, *cur ) )
479  ++cur;
480  unsigned ret = 0;
481  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
482  {
483  // skip non sepchars
484  while( *cur && !::strchr( sepchars_r, *cur ) )
485  ++cur;
486  // build string
487  *result_r = std::string( beg, cur-beg );
488  // skip sepchars
489  while ( *cur && ::strchr( sepchars_r, *cur ) )
490  ++cur;
491  }
492  return ret;
493  }
494 
531  template<class _OutputIterator>
532  unsigned splitEscaped( const C_Str & line_r,
533  _OutputIterator result_r,
534  const C_Str & sepchars_r = " \t",
535  bool withEmpty = false)
536  {
537  const char * beg = line_r;
538  const char * cur = beg;
539  unsigned ret = 0;
540 
541  // skip leading sepchars
542  while ( *cur && ::strchr( sepchars_r, *cur ) )
543  {
544  ++cur;
545  if (withEmpty)
546  {
547  *result_r = "";
548  ++ret;
549  }
550  }
551 
552  // there were only sepchars in the string
553  if (!*cur && withEmpty)
554  {
555  *result_r = "";
556  return ++ret;
557  }
558 
559  // after the leading sepchars
560  enum class Quote { None, Slash, Single, Double, DoubleSlash };
561  std::vector<char> buf;
562  Quote quoting = Quote::None;
563  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
564  {
565  // read next value until unquoted sepchar
566  buf.clear();
567  quoting = Quote::None;
568  do {
569  switch ( quoting )
570  {
571  case Quote::None:
572  switch ( *cur )
573  {
574  case '\\': quoting = Quote::Slash; break;
575  case '\'': quoting = Quote::Single; break;
576  case '"': quoting = Quote::Double; break;
577  default: buf.push_back( *cur ); break;
578  }
579  break;
580 
581  case Quote::Slash:
582  buf.push_back( *cur );
583  quoting = Quote::None;
584  break;
585 
586  case Quote::Single:
587  switch ( *cur )
588  {
589  case '\'': quoting = Quote::None; break;
590  default: buf.push_back( *cur ); break;
591  }
592  break;
593 
594  case Quote::Double:
595  switch ( *cur )
596  {
597  case '\"': quoting = Quote::None; break;
598  case '\\': quoting = Quote::DoubleSlash; break;
599  default: buf.push_back( *cur ); break;
600  }
601  break;
602 
603  case Quote::DoubleSlash:
604  switch ( *cur )
605  {
606  case '\"': /*fallthrough*/
607  case '\\': buf.push_back( *cur ); break;
608  default:
609  buf.push_back( '\\' );
610  buf.push_back( *cur );
611  break;
612  }
613  quoting = Quote::Double;
614  break;
615  }
616  ++cur;
617  } while ( *cur && ( quoting != Quote::None || !::strchr( sepchars_r, *cur ) ) );
618  *result_r = std::string( buf.begin(), buf.end() );
619 
620 
621  // skip sepchars
622  if ( *cur && ::strchr( sepchars_r, *cur ) )
623  ++cur;
624  while ( *cur && ::strchr( sepchars_r, *cur ) )
625  {
626  ++cur;
627  if (withEmpty)
628  {
629  *result_r = "";
630  ++ret;
631  }
632  }
633  // the last was a separator => one more field
634  if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
635  {
636  *result_r = "";
637  ++ret;
638  }
639  }
640  return ret;
641  }
642 
664  template<class _OutputIterator>
665  unsigned splitFields( const C_Str & line_r,
666  _OutputIterator result_r,
667  const C_Str & sepchars_r = ":" )
668  {
669  const char * beg = line_r;
670  const char * cur = beg;
671  unsigned ret = 0;
672  for ( beg = cur; *beg; beg = cur, ++result_r )
673  {
674  // skip non sepchars
675  while( *cur && !::strchr( sepchars_r, *cur ) )
676  {
677  if ( *cur == '\\' && *(cur+1) )
678  ++cur;
679  ++cur;
680  }
681  // build string
682  *result_r = std::string( beg, cur-beg );
683  ++ret;
684  // skip sepchar
685  if ( *cur )
686  {
687  ++cur;
688  if ( ! *cur ) // ending with sepchar
689  {
690  *result_r = std::string(); // add final empty field
691  ++ret;
692  break;
693  }
694  }
695  }
696  return ret;
697  }
698 
705  template<class _OutputIterator>
706  unsigned splitFieldsEscaped( const C_Str & line_r,
707  _OutputIterator result_r,
708  const C_Str & sepchars_r = ":" )
709  {
710  return
711  splitEscaped( line_r, result_r, sepchars_r, true /* withEmpty */ );
712  }
713 
715 
717 
720  template <class _Iterator>
721  std::string join( _Iterator begin, _Iterator end,
722  const C_Str & sep_r = " " )
723  {
724  std::string res;
725  for ( _Iterator iter = begin; iter != end; ++ iter )
726  {
727  if ( iter != begin )
728  res += sep_r;
729  res += asString(*iter);
730  }
731  return res;
732  }
733 
735  template <class _Container>
736  std::string join( const _Container & cont_r,
737  const C_Str & sep_r = " " )
738  { return join( cont_r.begin(), cont_r.end(), sep_r ); }
739 
744  template <class _Iterator>
745  std::string joinEscaped( _Iterator begin, _Iterator end,
746  const char sep_r = ' ' )
747  {
748  std::vector<char> buf;
749  for ( _Iterator iter = begin; iter != end; ++ iter )
750  {
751  if ( iter != begin )
752  buf.push_back( sep_r );
753 
754  if ( iter->empty() )
755  {
756  // empty string goes ""
757  buf.push_back( '"' );
758  buf.push_back( '"' );
759  }
760  else
761  {
762  std::string toadd( asString(*iter) );
763  for_( ch, toadd.begin(), toadd.end() )
764  {
765  switch ( *ch )
766  {
767  case '"':
768  case '\'':
769  case '\\':
770  buf.push_back( '\\' );
771  buf.push_back( *ch );
772  break;
773  default:
774  if ( *ch == sep_r )
775  buf.push_back( '\\' );
776  buf.push_back( *ch );
777  }
778  }
779  }
780  }
781  return std::string( buf.begin(), buf.end() );
782  }
784 
785 
787 
793  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, const std::string & indent_r = " ", unsigned maxWitdh_r = 0 )
794  {
795  if ( maxWitdh_r )
796  {
797  if ( indent_r.size() >= maxWitdh_r )
798  maxWitdh_r = 0; // nonsense: indent larger than line witdh
799  else
800  maxWitdh_r -= indent_r.size();
801  }
802  unsigned width = 0;
803  for ( const char * e = text_r.c_str(), * s = e; *e; s = ++e )
804  {
805  for ( ; *e && *e != '\n'; ++e ) ;/*searching*/
806  width = e-s;
807  if ( maxWitdh_r && width > maxWitdh_r )
808  {
809  // must break line
810  width = maxWitdh_r;
811  for ( e = s+width; e > s && *e != ' '; --e ) ;/*searching*/
812  if ( e > s )
813  width = e-s; // on a ' ', replaced by '\n'
814  else
815  e = s+width-1; // cut line;
816  }
817  str << indent_r;
818  str.write( s, width );
819  str << "\n";
820  if ( !*e ) // on '\0'
821  break;
822  }
823  return str;
824  }
826  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, char indentch_r = ' ', unsigned maxWitdh_r = 0 )
827  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
829  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r = ' ' )
830  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
831 
835  inline std::ostream & autoPrefix( std::ostream & str, const std::string & text_r, function<std::string(const char*, const char*)> fnc_r )
836  {
837  for ( const char * e = text_r.c_str(); *e; ++e )
838  {
839  const char * s = e;
840  for ( ; *e && *e != '\n'; ++e ) /*searching*/;
841  str << fnc_r( s, e );
842  str.write( s, e-s );
843  str << "\n";
844  if ( !*e ) // on '\0'
845  break;
846  }
847  return str;
848  }
850  inline std::ostream & autoPrefix0( std::ostream & str, const std::string & text_r, function<std::string()> fnc_r )
851  {
852  auto wrap = [&fnc_r]( const char*, const char* )-> std::string {
853  return fnc_r();
854  };
855  return autoPrefix( str, text_r, wrap );
856  }
858 
867  std::string escape( const C_Str & str_r, const char c = ' ' );
868 
870  inline void appendEscaped( std::string & str_r, const C_Str & next_r, const char sep_r = ' ' )
871  {
872  if ( ! str_r.empty() )
873  str_r += sep_r;
874  if ( next_r.empty() )
875  str_r += "\"\"";
876  else
877  str_r += escape( next_r, sep_r );
878  }
879 
881 
883 
893  std::string hexencode( const C_Str & str_r );
895  std::string hexdecode( const C_Str & str_r );
897 
904  std::string toLower( const std::string & s );
906  inline std::string toLower( const char * s )
907  { return( s ? toLower( std::string(s) ) : std::string() ); }
908 
912  std::string toUpper( const std::string & s );
914  inline std::string toUpper( const char * s )
915  { return( s ? toUpper( std::string(s) ) : std::string() ); }
917 
918 
921  inline int compareCI( const C_Str & lhs, const C_Str & rhs )
922  { return ::strcasecmp( lhs, rhs ); }
924 
928  inline bool contains( const C_Str & str_r, const C_Str & val_r )
929  { return ::strstr( str_r, val_r ); }
931  inline bool containsCI( const C_Str & str_r, const C_Str & val_r )
932  { return ::strcasestr( str_r, val_r ); }
934 
936 
941  enum Trim {
942  NO_TRIM = 0x00,
943  L_TRIM = 0x01,
944  R_TRIM = 0x02,
946  };
947 
948  std::string trim( const std::string & s, const Trim trim_r = TRIM );
949 
950  inline std::string ltrim( const std::string & s )
951  { return trim( s, L_TRIM ); }
952 
953  inline std::string rtrim( const std::string & s )
954  { return trim( s, R_TRIM ); }
956 
957  std::string stripFirstWord( std::string & line, const bool ltrim_first = true );
958 
959  std::string stripLastWord( std::string & line, const bool rtrim_first = true );
960 
964  std::string getline( std::istream & str, bool trim = false );
965 
969  std::string getline( std::istream & str, const Trim trim_r );
970 
978  std::string receiveUpTo( std::istream & str, const char delim_r, bool returnDelim_r = false );
979 
981 
986  inline bool hasPrefix( const C_Str & str_r, const C_Str & prefix_r )
987  { return( ::strncmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }
988 
990  inline std::string stripPrefix( const C_Str & str_r, const C_Str & prefix_r )
991  { return( hasPrefix( str_r, prefix_r ) ? str_r + prefix_r.size() : str_r.c_str() ); }
992 
994  inline bool hasSuffix( const C_Str & str_r, const C_Str & suffix_r )
995  { return( str_r.size() >= suffix_r.size() && ::strncmp( str_r + str_r.size() - suffix_r.size() , suffix_r, suffix_r.size() ) == 0 ); }
996 
998  inline std::string stripSuffix( const C_Str & str_r, const C_Str & suffix_r )
999  {
1000  if ( hasSuffix( str_r, suffix_r ) )
1001  return std::string( str_r, str_r.size() - suffix_r.size() );
1002  return str_r.c_str();
1003  }
1005  inline std::string::size_type commonPrefix( const C_Str & lhs, const C_Str & rhs )
1006  {
1007  const char * lp = lhs.c_str();
1008  const char * rp = rhs.c_str();
1009  std::string::size_type ret = 0;
1010  while ( *lp == *rp && *lp != '\0' )
1011  { ++lp, ++rp, ++ret; }
1012  return ret;
1013  }
1014 
1016  inline bool startsWith( const C_Str & str_r, const C_Str & prefix_r )
1017  { return hasPrefix( str_r, prefix_r ); }
1019  inline bool endsWith( const C_Str & str_r, const C_Str & prefix_r )
1020  { return hasSuffix( str_r, prefix_r ); }
1022  } // namespace str
1024 
1025  // drag into zypp:: namespace
1026  using str::asString;
1027 
1028 } // namespace zypp
1030 #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
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:870
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
Definition: String.h:928
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:404
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:990
unsigned split(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
Definition: String.h:471
std::string asUserString(const _Tp &val_r)
Request a human readable (translated) string representation of _Tp [_Tp.asUserString()] Classes may i...
Definition: String.h:57
std::string::size_type size_type
Definition: String.h:118
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:1005
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:998
std::string asString() const
Definition: String.h:218
std::ostream & autoPrefix0(std::ostream &str, const std::string &text_r, function< std::string()> fnc_r)
Definition: String.h:850
String related utilities and Regular expression matching.
C_Str(const std::string &str_r)
Definition: String.h:124
Definition: Arch.h:330
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
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:706
std::string join(const _Container &cont_r, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
Definition: String.h:736
MessageString(const _Str &str_r)
Definition: String.h:74
bool empty() const
Definition: String.h:128
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:941
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:745
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:532
std::string ltrim(const std::string &s)
Definition: String.h:950
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:242
std::string getline(std::istream &str)
Read one line from stream.
Definition: IOStream.cc:33
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:291
format formatNAC(const std::string &string_r)
A formater with (N)o (A)rgument (C)heck.
Definition: String.h:40
Convenient building of std::string via std::ostream::operator<<.
Definition: String.h:233
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:115
std::ostream & operator<<(std::ostream &str, const ::_Dataiterator *obj)
Definition: LookupAttr.cc:811
bool startsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasPrefix
Definition: String.h:1016
const char *const _val
Definition: String.h:140
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:447
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:70
bool containsCI(const C_Str &str_r, const C_Str &val_r)
Locate substring case insensitive.
Definition: String.h:931
std::ostream & printIndented(std::ostream &str, const std::string &text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r= ' ')
Definition: String.h:829
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:1019
size_type _sze
Definition: String.h:141
int compareCI(const C_Str &lhs, const C_Str &rhs)
Definition: String.h:921
SolvableIdType size_type
Definition: PoolMember.h:99
std::string rtrim(const std::string &s)
Definition: String.h:953
Str & operator<<(const _Tp &val)
Definition: String.h:236
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:994
std::string hexstring(unsigned long long n, int w=0)
Definition: String.h:315
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:127
_It strtonum(const C_Str &str, _It &i)
String to integer type detemined 2nd function arg i.
Definition: String.h:379
C_Str(const char *c_str_r)
Definition: String.h:123
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:397
const char * c_str() const
Definition: String.h:137
std::string hexencode(const C_Str &str_r)
Encode all characters other than [a-zA-Z0-9] as XX.
Definition: String.cc:122
MessageString(const std::string &str_r)
Definition: String.h:71
std::string octstring(unsigned long long n, int w=0)
Definition: String.h:339
std::ostream & operator<<(std::ostream &str, const C_Str &obj)
Definition: String.h:145
size_type size() const
Definition: String.h:129
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:835
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:122
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
std::ostream & stream()
Definition: String.h:245
std::string toUpper(const char *s)
Definition: String.h:914
Assert free called for allocated char *.
Definition: String.h:213
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition: String.h:986
std::string toLower(const char *s)
Definition: String.h:906
void clear()
Definition: String.h:248
unsigned splitFields(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=":")
Split line_r into fields.
Definition: String.h:665
std::ostringstream _str
Definition: String.h:251
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:280