libzypp  14.32.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 
27 
29 namespace boost
30 {
36  inline format formatNAC( const std::string & string_r ) {
37  using namespace boost::io;
38  format fmter( string_r );
39  fmter.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
40  return fmter;
41  }
42 } // namespace boost
43 namespace zypp { using boost::formatNAC; }
45 
47 namespace zypp
48 {
52  template <class _Tp>
53  std::string asUserString( const _Tp & val_r )
54  { return val_r.asUserString(); }
55 
56 }// namespace zypp
58 
60 namespace zypp
61 {
62 
63  struct MessageString : public std::string
64  {
66  MessageString( const char * str_r ) : std::string( str_r ? str_r : "" ) {}
67  MessageString( const std::string & str_r ) : std::string( str_r ) {}
68  // boost::format, std::ostringstream, str::Str ...
69  template<class _Str>
70  MessageString( const _Str & str_r ) : std::string( str_r.str() ) {}
71  };
72 
111  class C_Str
112  {
113  public:
115 
116  public:
117  C_Str() : _val( 0 ), _sze( 0 ) {}
118  C_Str( char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
119  C_Str( const char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
120  C_Str( const std::string & str_r ) : _val( str_r.c_str() ), _sze( str_r.size() ) {}
121 
122  public:
123  bool isNull() const { return !_val; }
124  bool empty() const { return !(_val && *_val); }
125  size_type size() const
126  {
127  if ( _sze == std::string::npos )
128  { _sze = _val ? ::strlen( _val ) : 0; }
129  return _sze;
130  };
131 
132  operator const char *() const { return c_str(); }
133  const char * c_str() const { return _val ? _val : ""; }
134 
135  private:
136  const char *const _val;
137  mutable size_type _sze;
138  };
139 
141  inline std::ostream & operator<<( std::ostream & str, const C_Str & obj )
142  { return str << obj.c_str(); }
143 
145 
149  namespace str
150  {
151 
153 
156  inline std::string asString( const std::string &t )
157  { return t; }
158 
159 #ifndef SWIG // Swig treats it as syntax error
160  inline std::string asString( std::string && t )
161  { return std::move(t); }
162 #endif
163 
164  inline std::string asString( const char * t )
165  { return t; }
166 
167  inline std::string asString( char * t )
168  { return t; }
169 
170  template<class _T>
171  inline std::string asString( const _T &t )
172  { return t.asString(); }
173 
174  template<class _T>
175  inline std::string asString( const intrusive_ptr<_T> &p )
176  { return p->asString(); }
177 
178  template<class _T>
179  inline std::string asString( const weak_ptr<_T> &p )
180  { return p->asString(); }
181 
182  template<>
183  inline std::string asString( const bool &t )
184  { return t ? "true" : "false"; }
185 
187 
188  std::string form( const char * format, ... )
189  __attribute__ ((format (printf, 1, 2)));
190 
192 
196  std::string strerror( int errno_r );
197 
199 
209  struct SafeBuf
210  {
211  char * _buf;
212  SafeBuf() : _buf( 0 ) {}
213  ~SafeBuf() { if ( _buf ) free( _buf ); }
214  std::string asString() const
215  { return _buf ? std::string(_buf) : std::string(); }
216  };
217 
219 
229  struct Str
230  {
231  template<class _Tp>
232  Str & operator<<( const _Tp & val )
233  { _str << val; return *this; }
234 
235  operator std::string() const
236  { return _str.str(); }
237 
238  std::string str() const
239  { return _str.str(); }
240 
241  std::ostream & stream()
242  { return _str; }
243 
244  void clear()
245  { _str.str( std::string() ); }
246 
247  std::ostringstream _str;
248  };
249 
250  inline std::ostream & operator<<( std::ostream & str, const Str & obj )
251  { return str << (std::string)obj; }
252 
254 
267  inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
268  inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
269  inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
270  inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
271  inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
272  inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
273  inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
274  inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
275  inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
276  inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
277 
278  template<> inline std::string asString( const char & t ) { return numstring( t ); }
279  template<> inline std::string asString( const unsigned char & t ) { return numstring( t ); }
280  template<> inline std::string asString( const short & t ) { return numstring( t ); }
281  template<> inline std::string asString( const unsigned short & t ) { return numstring( t ); }
282  template<> inline std::string asString( const int & t ) { return numstring( t ); }
283  template<> inline std::string asString( const unsigned & t ) { return numstring( t ); }
284  template<> inline std::string asString( const long & t ) { return numstring( t ); }
285  template<> inline std::string asString( const unsigned long & t ) { return numstring( t ); }
286  template<> inline std::string asString( const long long & t ) { return numstring( t ); }
287  template<> inline std::string asString( const unsigned long long & t ) { return numstring( t ); }
289 
291 
302  inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
303  inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
304  inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
305  inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
306  inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
307  inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
308  inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
309  inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
310  inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
311  inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
313 
315 
326  inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
327  inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
328  inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
329  inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
330  inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
331  inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
332  inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
333  inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
334  inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
335  inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
337 
339 
348  template<typename _It>
349  _It strtonum( const C_Str & str );
350 
351  template<>
352  inline short strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
353  template<>
354  inline int strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
355  template<>
356  inline long strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
357  template<>
358  inline long long strtonum( const C_Str & str ) { return ::strtoll ( str, NULL, 0 ); }
359 
360  template<>
361  inline unsigned short strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
362  template<>
363  inline unsigned strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
364  template<>
365  inline unsigned long strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
366  template<>
367  inline unsigned long long strtonum( const C_Str & str ) { return ::strtoull( str, NULL, 0 ); }
368 
374  template<typename _It>
375  inline _It strtonum( const C_Str & str, _It & i )
376  { return i = strtonum<_It>( str ); }
378 
380 
384  bool strToTrue( const C_Str & str );
385 
387  bool strToFalse( const C_Str & str );
388 
393  inline bool strToBool( const C_Str & str, bool default_r )
394  { return( default_r ? strToFalse( str ) : strToTrue( str ) ); }
395 
400  inline bool strToBoolNodefault( const C_Str & str, bool & return_r )
401  {
402  if ( strToTrue( str ) ) return (return_r = true);
403  if ( !strToFalse( str ) ) return (return_r = false);
404  return return_r;
405  }
406 
408 
412  std::string gsub( const std::string & str_r, const std::string & from_r, const std::string & to_r );
413 
416  std::string gsubFun( const std::string & str_r, const std::string & from_r, function<std::string()> to_r );
417 
422  std::string & replaceAll( std::string & str_r, const std::string & from_r, const std::string & to_r );
423 
426  std::string & replaceAllFun( std::string & str_r, const std::string & from_r, function<std::string()> to_r );
427 
440  inline std::string gapify( std::string inp_r, std::string::size_type gap_r = 1, char gapchar = ' ' )
441  {
442  if ( gap_r && inp_r.size() > gap_r )
443  {
444  inp_r.reserve( inp_r.size() + (inp_r.size()-1)/gap_r );
445  for ( std::string::size_type pos = gap_r; pos < inp_r.size(); pos += gap_r+1 )
446  inp_r.insert( pos, 1, gapchar );
447  }
448  return inp_r;
449  }
450 
452 
463  template<class _OutputIterator>
464  unsigned split( const C_Str & line_r,
465  _OutputIterator result_r,
466  const C_Str & sepchars_r = " \t" )
467  {
468  const char * beg = line_r;
469  const char * cur = beg;
470  // skip leading sepchars
471  while ( *cur && ::strchr( sepchars_r, *cur ) )
472  ++cur;
473  unsigned ret = 0;
474  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
475  {
476  // skip non sepchars
477  while( *cur && !::strchr( sepchars_r, *cur ) )
478  ++cur;
479  // build string
480  *result_r = std::string( beg, cur-beg );
481  // skip sepchars
482  while ( *cur && ::strchr( sepchars_r, *cur ) )
483  ++cur;
484  }
485  return ret;
486  }
487 
515  template<class _OutputIterator>
516  unsigned splitEscaped( const C_Str & line_r,
517  _OutputIterator result_r,
518  const C_Str & sepchars_r = " \t",
519  bool withEmpty = false)
520  {
521  const char * beg = line_r;
522  const char * cur = beg;
523  unsigned ret = 0;
524 
525  // skip leading sepchars
526  while ( *cur && ::strchr( sepchars_r, *cur ) )
527  {
528  ++cur;
529  if (withEmpty)
530  {
531  *result_r = "";
532  ++ret;
533  }
534  }
535 
536  // there were only sepchars in the string
537  if (!*cur && withEmpty)
538  {
539  *result_r = "";
540  return ++ret;
541  }
542 
543  // after the leading sepchars
544  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
545  {
546  if ( *cur == '"' || *cur == '\'' )
547  {
548  char closeChar = *cur;
549  ++cur;
550  bool cont = true;
551  while (cont)
552  {
553  while ( *cur && *cur != closeChar)
554  ++cur;
555  if ( *cur == '\0' )
556  {
557  return ret; //TODO parsing exception no closing quote
558  }
559  int escCount = 0;
560  const char * esc = cur-1;
561  while ( esc != beg && *esc == '\\' )
562  {
563  escCount++;
564  --esc;
565  }
566  cont = (escCount % 2 == 1); // find some non escaped escape char
567  cur++; //skip quote
568  }
569 
570  std::string s( beg+1, cur-beg-2 ); //without quotes
571  //transform escaped escape
572  replaceAll( s, "\\\\", "\\" );
573  //transform escaped quotes (only same as open
574  char tmpn[2] = { closeChar, 0 };
575  char tmpo[3] = { '\\', closeChar, 0 };
576  replaceAll( s, tmpo, tmpn );
577 
578  *result_r = s;
579  }
580  else
581  {
582  // skip non sepchars
583  while( *cur && !::strchr( sepchars_r, *cur ) )
584  {
585  //ignore char after backslash
586  if ( *cur == '\\' )
587  {
588  ++cur;
589  }
590  ++cur;
591  }
592  // build string
593  std::string s( beg, cur-beg );
594  //transform escaped escape
595  replaceAll( s, "\\\\", "\\" );
596 
597  const char *delimeter = sepchars_r;
598  while ( *delimeter )
599  {
600  std::string ds("\\");
601  const char tmp[2] = { *delimeter, '\0' };
602  std::string del(tmp);
603  ds+= del;
604  replaceAll( s, ds, del );
605  ++delimeter;
606  }
607 
608  *result_r = s;
609  }
610  // skip sepchars
611  if ( *cur && ::strchr( sepchars_r, *cur ) )
612  ++cur;
613  while ( *cur && ::strchr( sepchars_r, *cur ) )
614  {
615  ++cur;
616  if (withEmpty)
617  {
618  *result_r = "";
619  ++ret;
620  }
621  }
622  // the last was a separator => one more field
623  if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
624  {
625  *result_r = "";
626  ++ret;
627  }
628  }
629  return ret;
630  }
631 
653  template<class _OutputIterator>
654  unsigned splitFields( const C_Str & line_r,
655  _OutputIterator result_r,
656  const C_Str & sepchars_r = ":" )
657  {
658  const char * beg = line_r;
659  const char * cur = beg;
660  unsigned ret = 0;
661  for ( beg = cur; *beg; beg = cur, ++result_r )
662  {
663  // skip non sepchars
664  while( *cur && !::strchr( sepchars_r, *cur ) )
665  {
666  if ( *cur == '\\' && *(cur+1) )
667  ++cur;
668  ++cur;
669  }
670  // build string
671  *result_r = std::string( beg, cur-beg );
672  ++ret;
673  // skip sepchar
674  if ( *cur )
675  {
676  ++cur;
677  if ( ! *cur ) // ending with sepchar
678  {
679  *result_r = std::string(); // add final empty field
680  ++ret;
681  break;
682  }
683  }
684  }
685  return ret;
686  }
687 
694  template<class _OutputIterator>
695  unsigned splitFieldsEscaped( const C_Str & line_r,
696  _OutputIterator result_r,
697  const C_Str & sepchars_r = ":" )
698  {
699  return
700  splitEscaped( line_r, result_r, sepchars_r, true /* withEmpty */ );
701  }
702 
704 
706 
709  template <class _Iterator>
710  std::string join( _Iterator begin, _Iterator end,
711  const C_Str & sep_r = " " )
712  {
713  std::string res;
714  for ( _Iterator iter = begin; iter != end; ++ iter )
715  {
716  if ( iter != begin )
717  res += sep_r;
718  res += asString(*iter);
719  }
720  return res;
721  }
722 
724  template <class _Container>
725  std::string join( const _Container & cont_r,
726  const C_Str & sep_r = " " )
727  { return join( cont_r.begin(), cont_r.end(), sep_r ); }
728 
733  template <class _Iterator>
734  std::string joinEscaped( _Iterator begin, _Iterator end,
735  const char sep_r = ' ' )
736  {
737  std::vector<char> buf;
738  for ( _Iterator iter = begin; iter != end; ++ iter )
739  {
740  if ( iter != begin )
741  buf.push_back( sep_r );
742 
743  if ( iter->empty() )
744  {
745  // empty string goes ""
746  buf.push_back( '"' );
747  buf.push_back( '"' );
748  }
749  else
750  {
751  std::string toadd( asString(*iter) );
752  for_( ch, toadd.begin(), toadd.end() )
753  {
754  switch ( *ch )
755  {
756  case '"':
757  case '\'':
758  case '\\':
759  buf.push_back( '\\' );
760  buf.push_back( *ch );
761  break;
762  default:
763  if ( *ch == sep_r )
764  buf.push_back( '\\' );
765  buf.push_back( *ch );
766  }
767  }
768  }
769  }
770  return std::string( buf.begin(), buf.end() );
771  }
773 
774 
776 
782  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, const std::string & indent_r = " ", unsigned maxWitdh_r = 0 )
783  {
784  if ( maxWitdh_r )
785  {
786  if ( indent_r.size() >= maxWitdh_r )
787  maxWitdh_r = 0; // nonsense: indent larger than line witdh
788  else
789  maxWitdh_r -= indent_r.size();
790  }
791  unsigned width = 0;
792  for ( const char * e = text_r.c_str(), * s = e; *e; s = ++e )
793  {
794  for ( ; *e && *e != '\n'; ++e ) ;/*searching*/
795  width = e-s;
796  if ( maxWitdh_r && width > maxWitdh_r )
797  {
798  // must break line
799  width = maxWitdh_r;
800  for ( e = s+width; e > s && *e != ' '; --e ) ;/*searching*/
801  if ( e > s )
802  width = e-s; // on a ' ', replaced by '\n'
803  else
804  e = s+width-1; // cut line;
805  }
806  str << indent_r;
807  str.write( s, width );
808  str << "\n";
809  if ( !*e ) // on '\0'
810  break;
811  }
812  return str;
813  }
815  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, char indentch_r = ' ', unsigned maxWitdh_r = 0 )
816  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
818  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r = ' ' )
819  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
820 
824  inline std::ostream & autoPrefix( std::ostream & str, const std::string & text_r, function<std::string(const char*, const char*)> fnc_r )
825  {
826  for ( const char * e = text_r.c_str(); *e; ++e )
827  {
828  const char * s = e;
829  for ( ; *e && *e != '\n'; ++e ) /*searching*/;
830  str << fnc_r( s, e );
831  str.write( s, e-s );
832  str << "\n";
833  if ( !*e ) // on '\0'
834  break;
835  }
836  return str;
837  }
839  inline std::ostream & autoPrefix0( std::ostream & str, const std::string & text_r, function<std::string()> fnc_r )
840  {
841  auto wrap = [&fnc_r]( const char*, const char* )-> std::string {
842  return fnc_r();
843  };
844  return autoPrefix( str, text_r, wrap );
845  }
847 
856  std::string escape( const C_Str & str_r, const char c = ' ' );
857 
859  inline void appendEscaped( std::string & str_r, const C_Str & next_r, const char sep_r = ' ' )
860  {
861  if ( ! str_r.empty() )
862  str_r += sep_r;
863  if ( next_r.empty() )
864  str_r += "\"\"";
865  else
866  str_r += escape( next_r, sep_r );
867  }
868 
870 
872 
882  std::string hexencode( const C_Str & str_r );
884  std::string hexdecode( const C_Str & str_r );
886 
893  std::string toLower( const std::string & s );
895  inline std::string toLower( const char * s )
896  { return( s ? toLower( std::string(s) ) : std::string() ); }
897 
901  std::string toUpper( const std::string & s );
903  inline std::string toUpper( const char * s )
904  { return( s ? toUpper( std::string(s) ) : std::string() ); }
906 
907 
910  inline int compareCI( const C_Str & lhs, const C_Str & rhs )
911  { return ::strcasecmp( lhs, rhs ); }
913 
917  inline bool contains( const C_Str & str_r, const C_Str & val_r )
918  { return ::strstr( str_r, val_r ); }
920  inline bool containsCI( const C_Str & str_r, const C_Str & val_r )
921  { return ::strcasestr( str_r, val_r ); }
923 
925 
930  enum Trim {
931  NO_TRIM = 0x00,
932  L_TRIM = 0x01,
933  R_TRIM = 0x02,
935  };
936 
937  std::string trim( const std::string & s, const Trim trim_r = TRIM );
938 
939  inline std::string ltrim( const std::string & s )
940  { return trim( s, L_TRIM ); }
941 
942  inline std::string rtrim( const std::string & s )
943  { return trim( s, R_TRIM ); }
945 
946  std::string stripFirstWord( std::string & line, const bool ltrim_first = true );
947 
948  std::string stripLastWord( std::string & line, const bool rtrim_first = true );
949 
953  std::string getline( std::istream & str, bool trim = false );
954 
958  std::string getline( std::istream & str, const Trim trim_r );
959 
967  std::string receiveUpTo( std::istream & str, const char delim_r, bool returnDelim_r = false );
968 
970 
975  inline bool hasPrefix( const C_Str & str_r, const C_Str & prefix_r )
976  { return( ::strncmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }
977 
979  inline std::string stripPrefix( const C_Str & str_r, const C_Str & prefix_r )
980  { return( hasPrefix( str_r, prefix_r ) ? str_r + prefix_r.size() : str_r.c_str() ); }
981 
983  inline bool hasSuffix( const C_Str & str_r, const C_Str & suffix_r )
984  { return( str_r.size() >= suffix_r.size() && ::strncmp( str_r + str_r.size() - suffix_r.size() , suffix_r, suffix_r.size() ) == 0 ); }
985 
987  inline std::string stripSuffix( const C_Str & str_r, const C_Str & suffix_r )
988  {
989  if ( hasSuffix( str_r, suffix_r ) )
990  return std::string( str_r, str_r.size() - suffix_r.size() );
991  return str_r.c_str();
992  }
994  inline std::string::size_type commonPrefix( const C_Str & lhs, const C_Str & rhs )
995  {
996  const char * lp = lhs.c_str();
997  const char * rp = rhs.c_str();
998  std::string::size_type ret = 0;
999  while ( *lp == *rp && *lp != '\0' )
1000  { ++lp, ++rp, ++ret; }
1001  return ret;
1002  }
1003 
1005  inline bool startsWith( const C_Str & str_r, const C_Str & prefix_r )
1006  { return hasPrefix( str_r, prefix_r ); }
1008  inline bool endsWith( const C_Str & str_r, const C_Str & prefix_r )
1009  { return hasSuffix( str_r, prefix_r ); }
1011  } // namespace str
1013 
1014  // drag into zypp:: namespace
1015  using str::asString;
1016 
1017 } // namespace zypp
1019 #endif // ZYPP_BASE_STRING_H
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:859
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
Definition: String.h:917
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:400
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:979
unsigned split(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
Definition: String.h:464
std::string asUserString(const _Tp &val_r)
Request a human readable (translated) string representation of _Tp [_Tp.asUserString()] Classes may i...
Definition: String.h:53
std::string::size_type size_type
Definition: String.h:114
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:994
std::string escape(const C_Str &str_r, const char sep_r)
Escape desired character c using a backslash.
Definition: String.cc:345
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:987
std::string asString() const
Definition: String.h:214
std::ostream & autoPrefix0(std::ostream &str, const std::string &text_r, function< std::string()> fnc_r)
Definition: String.h:839
String related utilities and Regular expression matching.
C_Str(const std::string &str_r)
Definition: String.h:120
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:695
std::string join(const _Container &cont_r, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
Definition: String.h:725
MessageString(const _Str &str_r)
Definition: String.h:70
bool empty() const
Definition: String.h:124
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:34
Trim
To define how to trim.
Definition: String.h:930
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:734
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:516
std::string ltrim(const std::string &s)
Definition: String.h:939
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:304
std::string str() const
Definition: String.h:238
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:237
std::string asString(const unsigned long long &t)
Definition: String.h:287
format formatNAC(const std::string &string_r)
A formater with (N)o (A)rgument (C)heck.
Definition: String.h:36
Convenient building of std::string via std::ostream::operator<<.
Definition: String.h:229
std::string trim(const std::string &s, const Trim trim_r)
Definition: String.cc:204
Convenience char* constructible from std::string and char*, it maps (char*)0 to an empty string...
Definition: String.h:111
std::ostream & operator<<(std::ostream &str, const ::_Dataiterator *obj)
Definition: LookupAttr.cc:799
bool startsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasPrefix
Definition: String.h:1005
const char *const _val
Definition: String.h:136
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:440
std::string stripLastWord(std::string &line, const bool rtrim_first)
Definition: String.cc:270
bool strToFalse(const C_Str &str)
Return false if str is 0, false, no, off.
Definition: String.cc:78
MessageString(const char *str_r)
Definition: String.h:66
bool containsCI(const C_Str &str_r, const C_Str &val_r)
Locate substring case insensitive.
Definition: String.h:920
std::ostream & printIndented(std::ostream &str, const std::string &text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r= ' ')
Definition: String.h:818
std::string gsubFun(const std::string &str_r, const std::string &from_r, function< std::string()> to_r)
Definition: String.cc:321
bool endsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasSuffix
Definition: String.h:1008
size_type _sze
Definition: String.h:137
int compareCI(const C_Str &lhs, const C_Str &rhs)
Definition: String.h:910
SolvableIdType size_type
Definition: PoolMember.h:99
std::string rtrim(const std::string &s)
Definition: String.h:942
Str & operator<<(const _Tp &val)
Definition: String.h:232
bool strToTrue(const C_Str &str)
Parsing boolean from string.
Definition: String.cc:61
std::string & replaceAllFun(std::string &str_r, const std::string &from_r, function< std::string()> to_r)
Definition: String.cc:327
bool hasSuffix(const C_Str &str_r, const C_Str &suffix_r)
Return whether str_r has suffix suffix_r.
Definition: String.h:983
std::string hexstring(unsigned long long n, int w=0)
Definition: String.h:311
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:298
bool isNull() const
Definition: String.h:123
_It strtonum(const C_Str &str, _It &i)
String to integer type detemined 2nd function arg i.
Definition: String.h:375
C_Str(const char *c_str_r)
Definition: String.h:119
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:377
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Definition: String.h:393
const char * c_str() const
Definition: String.h:133
std::string hexencode(const C_Str &str_r)
Encode all characters other than [a-zA-Z0-9] as XX.
Definition: String.cc:113
MessageString(const std::string &str_r)
Definition: String.h:67
std::string octstring(unsigned long long n, int w=0)
Definition: String.h:335
std::ostream & operator<<(std::ostream &str, const C_Str &obj)
Definition: String.h:141
size_type size() const
Definition: String.h:125
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:824
std::string strerror(int errno_r)
Return string describing the error_r code.
Definition: String.cc:51
C_Str(char *c_str_r)
Definition: String.h:118
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
std::ostream & stream()
Definition: String.h:241
std::string toUpper(const char *s)
Definition: String.h:903
Assert free called for allocated char *.
Definition: String.h:209
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition: String.h:975
std::string toLower(const char *s)
Definition: String.h:895
void clear()
Definition: String.h:244
unsigned splitFields(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=":")
Split line_r into fields.
Definition: String.h:654
std::ostringstream _str
Definition: String.h:247
std::string hexdecode(const C_Str &str_r)
Decode hexencoded XX sequences.
Definition: String.cc:134
std::string numstring(unsigned long long n, int w=0)
Definition: String.h:276