libzypp  15.19.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 #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 
350 
359  template<typename TInt>
360  TInt strtonum( const C_Str & str );
361 
362  template<>
363  inline short strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
364  template<>
365  inline int strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
366  template<>
367  inline long strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
368  template<>
369  inline long long strtonum( const C_Str & str ) { return ::strtoll ( str, NULL, 0 ); }
370 
371  template<>
372  inline unsigned short strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
373  template<>
374  inline unsigned strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
375  template<>
376  inline unsigned long strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
377  template<>
378  inline unsigned long long strtonum( const C_Str & str ) { return ::strtoull( str, NULL, 0 ); }
379 
385  template<typename TInt>
386  inline TInt strtonum( const C_Str & str, TInt & i )
387  { return i = strtonum<TInt>( str ); }
389 
391 
395  bool strToTrue( const C_Str & str );
396 
398  bool strToFalse( const C_Str & str );
399 
404  inline bool strToBool( const C_Str & str, bool default_r )
405  { return( default_r ? strToFalse( str ) : strToTrue( str ) ); }
406 
411  inline bool strToBoolNodefault( const C_Str & str, bool & return_r )
412  {
413  if ( strToTrue( str ) ) return (return_r = true);
414  if ( !strToFalse( str ) ) return (return_r = false);
415  return return_r;
416  }
417 
419  TriBool strToTriBool( const C_Str & str );
420 
422 
426  std::string gsub( const std::string & str_r, const std::string & from_r, const std::string & to_r );
427 
430  std::string gsubFun( const std::string & str_r, const std::string & from_r, function<std::string()> to_r );
431 
436  std::string & replaceAll( std::string & str_r, const std::string & from_r, const std::string & to_r );
437 
440  std::string & replaceAllFun( std::string & str_r, const std::string & from_r, function<std::string()> to_r );
441 
454  inline std::string gapify( std::string inp_r, std::string::size_type gap_r = 1, char gapchar = ' ' )
455  {
456  if ( gap_r && inp_r.size() > gap_r )
457  {
458  inp_r.reserve( inp_r.size() + (inp_r.size()-1)/gap_r );
459  for ( std::string::size_type pos = gap_r; pos < inp_r.size(); pos += gap_r+1 )
460  inp_r.insert( pos, 1, gapchar );
461  }
462  return inp_r;
463  }
464 
466 
477  template<class TOutputIterator>
478  unsigned split( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = " \t" )
479  {
480  const char * beg = line_r;
481  const char * cur = beg;
482  // skip leading sepchars
483  while ( *cur && ::strchr( sepchars_r, *cur ) )
484  ++cur;
485  unsigned ret = 0;
486  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
487  {
488  // skip non sepchars
489  while( *cur && !::strchr( sepchars_r, *cur ) )
490  ++cur;
491  // build string
492  *result_r = std::string( beg, cur-beg );
493  // skip sepchars
494  while ( *cur && ::strchr( sepchars_r, *cur ) )
495  ++cur;
496  }
497  return ret;
498  }
499 
536  template<class TOutputIterator>
537  unsigned splitEscaped( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = " \t", bool withEmpty = false)
538  {
539  const char * beg = line_r;
540  const char * cur = beg;
541  unsigned ret = 0;
542 
543  // skip leading sepchars
544  while ( *cur && ::strchr( sepchars_r, *cur ) )
545  {
546  ++cur;
547  if (withEmpty)
548  {
549  *result_r = "";
550  ++ret;
551  }
552  }
553 
554  // there were only sepchars in the string
555  if (!*cur && withEmpty)
556  {
557  *result_r = "";
558  return ++ret;
559  }
560 
561  // after the leading sepchars
562  enum class Quote { None, Slash, Single, Double, DoubleSlash };
563  std::vector<char> buf;
564  Quote quoting = Quote::None;
565  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
566  {
567  // read next value until unquoted sepchar
568  buf.clear();
569  quoting = Quote::None;
570  do {
571  switch ( quoting )
572  {
573  case Quote::None:
574  switch ( *cur )
575  {
576  case '\\': quoting = Quote::Slash; break;
577  case '\'': quoting = Quote::Single; break;
578  case '"': quoting = Quote::Double; break;
579  default: buf.push_back( *cur ); break;
580  }
581  break;
582 
583  case Quote::Slash:
584  buf.push_back( *cur );
585  quoting = Quote::None;
586  break;
587 
588  case Quote::Single:
589  switch ( *cur )
590  {
591  case '\'': quoting = Quote::None; break;
592  default: buf.push_back( *cur ); break;
593  }
594  break;
595 
596  case Quote::Double:
597  switch ( *cur )
598  {
599  case '\"': quoting = Quote::None; break;
600  case '\\': quoting = Quote::DoubleSlash; break;
601  default: buf.push_back( *cur ); break;
602  }
603  break;
604 
605  case Quote::DoubleSlash:
606  switch ( *cur )
607  {
608  case '\"': /*fallthrough*/
609  case '\\': buf.push_back( *cur ); break;
610  default:
611  buf.push_back( '\\' );
612  buf.push_back( *cur );
613  break;
614  }
615  quoting = Quote::Double;
616  break;
617  }
618  ++cur;
619  } while ( *cur && ( quoting != Quote::None || !::strchr( sepchars_r, *cur ) ) );
620  *result_r = std::string( buf.begin(), buf.end() );
621 
622 
623  // skip sepchars
624  if ( *cur && ::strchr( sepchars_r, *cur ) )
625  ++cur;
626  while ( *cur && ::strchr( sepchars_r, *cur ) )
627  {
628  ++cur;
629  if (withEmpty)
630  {
631  *result_r = "";
632  ++ret;
633  }
634  }
635  // the last was a separator => one more field
636  if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
637  {
638  *result_r = "";
639  ++ret;
640  }
641  }
642  return ret;
643  }
644 
666  template<class TOutputIterator>
667  unsigned splitFields( const C_Str & line_r, TOutputIterator result_r, 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 TOutputIterator>
706  unsigned splitFieldsEscaped( const C_Str & line_r, TOutputIterator result_r, const C_Str & sepchars_r = ":" )
707  {
708  return splitEscaped( line_r, result_r, sepchars_r, true /* withEmpty */ );
709  }
710 
712 
714 
717  template <class TIterator>
718  std::string join( TIterator begin, TIterator end, const C_Str & sep_r = " " )
719  {
720  std::string res;
721  for ( TIterator iter = begin; iter != end; ++ iter )
722  {
723  if ( iter != begin )
724  res += sep_r;
725  res += asString(*iter);
726  }
727  return res;
728  }
729 
731  template <class TContainer>
732  std::string join( const TContainer & cont_r, const C_Str & sep_r = " " )
733  { return join( cont_r.begin(), cont_r.end(), sep_r ); }
734 
739  template <class TIterator>
740  std::string joinEscaped( TIterator begin, TIterator end, const char sep_r = ' ' )
741  {
742  std::vector<char> buf;
743  for ( TIterator iter = begin; iter != end; ++ iter )
744  {
745  if ( iter != begin )
746  buf.push_back( sep_r );
747 
748  if ( iter->empty() )
749  {
750  // empty string goes ""
751  buf.push_back( '"' );
752  buf.push_back( '"' );
753  }
754  else
755  {
756  std::string toadd( asString(*iter) );
757  for_( ch, toadd.begin(), toadd.end() )
758  {
759  switch ( *ch )
760  {
761  case '"':
762  case '\'':
763  case '\\':
764  buf.push_back( '\\' );
765  buf.push_back( *ch );
766  break;
767  default:
768  if ( *ch == sep_r )
769  buf.push_back( '\\' );
770  buf.push_back( *ch );
771  }
772  }
773  }
774  }
775  return std::string( buf.begin(), buf.end() );
776  }
778 
779 
781 
787  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, const std::string & indent_r = " ", unsigned maxWitdh_r = 0 )
788  {
789  if ( maxWitdh_r )
790  {
791  if ( indent_r.size() >= maxWitdh_r )
792  maxWitdh_r = 0; // nonsense: indent larger than line witdh
793  else
794  maxWitdh_r -= indent_r.size();
795  }
796  unsigned width = 0;
797  for ( const char * e = text_r.c_str(), * s = e; *e; s = ++e )
798  {
799  for ( ; *e && *e != '\n'; ++e ) ;/*searching*/
800  width = e-s;
801  if ( maxWitdh_r && width > maxWitdh_r )
802  {
803  // must break line
804  width = maxWitdh_r;
805  for ( e = s+width; e > s && *e != ' '; --e ) ;/*searching*/
806  if ( e > s )
807  width = e-s; // on a ' ', replaced by '\n'
808  else
809  e = s+width-1; // cut line;
810  }
811  str << indent_r;
812  str.write( s, width );
813  str << "\n";
814  if ( !*e ) // on '\0'
815  break;
816  }
817  return str;
818  }
820  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, char indentch_r = ' ', unsigned maxWitdh_r = 0 )
821  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
823  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r = ' ' )
824  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
825 
829  inline std::ostream & autoPrefix( std::ostream & str, const std::string & text_r, function<std::string(const char*, const char*)> fnc_r )
830  {
831  for ( const char * e = text_r.c_str(); *e; ++e )
832  {
833  const char * s = e;
834  for ( ; *e && *e != '\n'; ++e ) /*searching*/;
835  str << fnc_r( s, e );
836  str.write( s, e-s );
837  str << "\n";
838  if ( !*e ) // on '\0'
839  break;
840  }
841  return str;
842  }
844  inline std::ostream & autoPrefix0( std::ostream & str, const std::string & text_r, function<std::string()> fnc_r )
845  {
846  auto wrap = [&fnc_r]( const char*, const char* )-> std::string {
847  return fnc_r();
848  };
849  return autoPrefix( str, text_r, wrap );
850  }
852 
861  std::string escape( const C_Str & str_r, const char c = ' ' );
862 
864  inline void appendEscaped( std::string & str_r, const C_Str & next_r, const char sep_r = ' ' )
865  {
866  if ( ! str_r.empty() )
867  str_r += sep_r;
868  if ( next_r.empty() )
869  str_r += "\"\"";
870  else
871  str_r += escape( next_r, sep_r );
872  }
873 
875 
877 
887  std::string hexencode( const C_Str & str_r );
889  std::string hexdecode( const C_Str & str_r );
891 
898  std::string toLower( const std::string & s );
900  inline std::string toLower( const char * s )
901  { return( s ? toLower( std::string(s) ) : std::string() ); }
902 
906  std::string toUpper( const std::string & s );
908  inline std::string toUpper( const char * s )
909  { return( s ? toUpper( std::string(s) ) : std::string() ); }
911 
912 
915  inline int compareCI( const C_Str & lhs, const C_Str & rhs )
916  { return ::strcasecmp( lhs, rhs ); }
918 
922  inline bool contains( const C_Str & str_r, const C_Str & val_r )
923  { return ::strstr( str_r, val_r ); }
925  inline bool containsCI( const C_Str & str_r, const C_Str & val_r )
926  { return ::strcasestr( str_r, val_r ); }
928 
930 
935  enum Trim {
936  NO_TRIM = 0x00,
937  L_TRIM = 0x01,
938  R_TRIM = 0x02,
940  };
941 
942  std::string trim( const std::string & s, const Trim trim_r = TRIM );
943 
944  inline std::string ltrim( const std::string & s )
945  { return trim( s, L_TRIM ); }
946 
947  inline std::string rtrim( const std::string & s )
948  { return trim( s, R_TRIM ); }
950 
951  std::string stripFirstWord( std::string & line, const bool ltrim_first = true );
952 
953  std::string stripLastWord( std::string & line, const bool rtrim_first = true );
954 
958  std::string getline( std::istream & str, bool trim = false );
959 
963  std::string getline( std::istream & str, const Trim trim_r );
964 
972  std::string receiveUpTo( std::istream & str, const char delim_r, bool returnDelim_r = false );
973 
975 
980  inline bool hasPrefix( const C_Str & str_r, const C_Str & prefix_r )
981  { return( ::strncmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }
982 
984  inline std::string stripPrefix( const C_Str & str_r, const C_Str & prefix_r )
985  { return( hasPrefix( str_r, prefix_r ) ? str_r + prefix_r.size() : str_r.c_str() ); }
986 
988  inline bool hasSuffix( const C_Str & str_r, const C_Str & suffix_r )
989  { return( str_r.size() >= suffix_r.size() && ::strncmp( str_r + str_r.size() - suffix_r.size() , suffix_r, suffix_r.size() ) == 0 ); }
990 
992  inline std::string stripSuffix( const C_Str & str_r, const C_Str & suffix_r )
993  {
994  if ( hasSuffix( str_r, suffix_r ) )
995  return std::string( str_r, str_r.size() - suffix_r.size() );
996  return str_r.c_str();
997  }
999  inline std::string::size_type commonPrefix( const C_Str & lhs, const C_Str & rhs )
1000  {
1001  const char * lp = lhs.c_str();
1002  const char * rp = rhs.c_str();
1003  std::string::size_type ret = 0;
1004  while ( *lp == *rp && *lp != '\0' )
1005  { ++lp, ++rp, ++ret; }
1006  return ret;
1007  }
1008 
1010  inline bool startsWith( const C_Str & str_r, const C_Str & prefix_r )
1011  { return hasPrefix( str_r, prefix_r ); }
1013  inline bool endsWith( const C_Str & str_r, const C_Str & prefix_r )
1014  { return hasSuffix( str_r, prefix_r ); }
1016  } // namespace str
1018 
1019  // drag into zypp:: namespace
1020  using str::asString;
1021 
1022 } // namespace zypp
1024 #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:537
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:864
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
Definition: String.h:922
C_Str(const boost::string_ref &str_r)
Definition: String.h:128
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:411
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:984
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:999
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:992
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:844
C_Str(const std::string &str_r)
Definition: String.h:127
#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:935
std::string ltrim(const std::string &s)
Definition: String.h:944
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:478
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:1010
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:454
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:925
std::ostream & printIndented(std::ostream &str, const std::string &text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r= ' ')
Definition: String.h:823
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:1013
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:915
SolvableIdType size_type
Definition: PoolMember.h:126
std::string rtrim(const std::string &s)
Definition: String.h:947
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:740
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:667
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:988
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:386
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:404
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
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:829
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
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:732
std::string toUpper(const char *s)
Definition: String.h:908
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:980
std::string toLower(const char *s)
Definition: String.h:900
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:706