libzypp  14.29.1
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 
148  namespace str
149  {
150 
152 
155  inline std::string asString( const std::string &t )
156  { return t; }
157 
158 #ifndef SWIG // Swig treats it as syntax error
159  inline std::string asString( std::string && t )
160  { return std::move(t); }
161 #endif
162 
163  inline std::string asString( const char * t )
164  { return t; }
165 
166  inline std::string asString( char * t )
167  { return t; }
168 
169  template<class _T>
170  inline std::string asString( const _T &t )
171  { return t.asString(); }
172 
173  template<class _T>
174  inline std::string asString( const intrusive_ptr<_T> &p )
175  { return p->asString(); }
176 
177  template<class _T>
178  inline std::string asString( const weak_ptr<_T> &p )
179  { return p->asString(); }
180 
181  template<>
182  inline std::string asString( const bool &t )
183  { return t ? "true" : "false"; }
184 
186 
187  std::string form( const char * format, ... )
188  __attribute__ ((format (printf, 1, 2)));
189 
191 
195  std::string strerror( int errno_r );
196 
198 
208  struct SafeBuf
209  {
210  char * _buf;
211  SafeBuf() : _buf( 0 ) {}
212  ~SafeBuf() { if ( _buf ) free( _buf ); }
213  std::string asString() const
214  { return _buf ? std::string(_buf) : std::string(); }
215  };
216 
218 
228  struct Str
229  {
230  template<class _Tp>
231  Str & operator<<( const _Tp & val )
232  { _str << val; return *this; }
233 
234  operator std::string() const
235  { return _str.str(); }
236 
237  std::string str() const
238  { return _str.str(); }
239 
240  std::ostream & stream()
241  { return _str; }
242 
243  void clear()
244  { _str.str( std::string() ); }
245 
246  std::ostringstream _str;
247  };
248 
249  inline std::ostream & operator<<( std::ostream & str, const Str & obj )
250  { return str << (std::string)obj; }
251 
253 
266  inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
267  inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
268  inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
269  inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
270  inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
271  inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
272  inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
273  inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
274  inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
275  inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
276 
277  template<> inline std::string asString( const char & t ) { return numstring( t ); }
278  template<> inline std::string asString( const unsigned char & t ) { return numstring( t ); }
279  template<> inline std::string asString( const short & t ) { return numstring( t ); }
280  template<> inline std::string asString( const unsigned short & t ) { return numstring( t ); }
281  template<> inline std::string asString( const int & t ) { return numstring( t ); }
282  template<> inline std::string asString( const unsigned & t ) { return numstring( t ); }
283  template<> inline std::string asString( const long & t ) { return numstring( t ); }
284  template<> inline std::string asString( const unsigned long & t ) { return numstring( t ); }
285  template<> inline std::string asString( const long long & t ) { return numstring( t ); }
286  template<> inline std::string asString( const unsigned long long & t ) { return numstring( t ); }
288 
290 
301  inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
302  inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
303  inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
304  inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
305  inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
306  inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
307  inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
308  inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
309  inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
310  inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
312 
314 
325  inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
326  inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
327  inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
328  inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
329  inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
330  inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
331  inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
332  inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
333  inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
334  inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
336 
338 
347  template<typename _It>
348  _It strtonum( const C_Str & str );
349 
350  template<>
351  inline short strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
352  template<>
353  inline int strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
354  template<>
355  inline long strtonum( const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
356  template<>
357  inline long long strtonum( const C_Str & str ) { return ::strtoll ( str, NULL, 0 ); }
358 
359  template<>
360  inline unsigned short strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
361  template<>
362  inline unsigned strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
363  template<>
364  inline unsigned long strtonum( const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
365  template<>
366  inline unsigned long long strtonum( const C_Str & str ) { return ::strtoull( str, NULL, 0 ); }
367 
373  template<typename _It>
374  inline _It strtonum( const C_Str & str, _It & i )
375  { return i = strtonum<_It>( str ); }
377 
379 
383  bool strToTrue( const C_Str & str );
384 
386  bool strToFalse( const C_Str & str );
387 
392  inline bool strToBool( const C_Str & str, bool default_r )
393  { return( default_r ? strToFalse( str ) : strToTrue( str ) ); }
394 
399  inline bool strToBoolNodefault( const C_Str & str, bool & return_r )
400  {
401  if ( strToTrue( str ) ) return (return_r = true);
402  if ( !strToFalse( str ) ) return (return_r = false);
403  return return_r;
404  }
405 
407 
411  std::string gsub( const std::string & str_r, const std::string & from_r, const std::string & to_r );
412 
415  std::string gsubFun( const std::string & str_r, const std::string & from_r, function<std::string()> to_r );
416 
421  std::string & replaceAll( std::string & str_r, const std::string & from_r, const std::string & to_r );
422 
425  std::string & replaceAllFun( std::string & str_r, const std::string & from_r, function<std::string()> to_r );
426 
439  inline std::string gapify( std::string inp_r, std::string::size_type gap_r = 1, char gapchar = ' ' )
440  {
441  if ( gap_r && inp_r.size() > gap_r )
442  {
443  inp_r.reserve( inp_r.size() + (inp_r.size()-1)/gap_r );
444  for ( std::string::size_type pos = gap_r; pos < inp_r.size(); pos += gap_r+1 )
445  inp_r.insert( pos, 1, gapchar );
446  }
447  return inp_r;
448  }
449 
451 
462  template<class _OutputIterator>
463  unsigned split( const C_Str & line_r,
464  _OutputIterator result_r,
465  const C_Str & sepchars_r = " \t" )
466  {
467  const char * beg = line_r;
468  const char * cur = beg;
469  // skip leading sepchars
470  while ( *cur && ::strchr( sepchars_r, *cur ) )
471  ++cur;
472  unsigned ret = 0;
473  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
474  {
475  // skip non sepchars
476  while( *cur && !::strchr( sepchars_r, *cur ) )
477  ++cur;
478  // build string
479  *result_r = std::string( beg, cur-beg );
480  // skip sepchars
481  while ( *cur && ::strchr( sepchars_r, *cur ) )
482  ++cur;
483  }
484  return ret;
485  }
486 
514  template<class _OutputIterator>
515  unsigned splitEscaped( const C_Str & line_r,
516  _OutputIterator result_r,
517  const C_Str & sepchars_r = " \t",
518  bool withEmpty = false)
519  {
520  const char * beg = line_r;
521  const char * cur = beg;
522  unsigned ret = 0;
523 
524  // skip leading sepchars
525  while ( *cur && ::strchr( sepchars_r, *cur ) )
526  {
527  ++cur;
528  if (withEmpty)
529  {
530  *result_r = "";
531  ++ret;
532  }
533  }
534 
535  // there were only sepchars in the string
536  if (!*cur && withEmpty)
537  {
538  *result_r = "";
539  return ++ret;
540  }
541 
542  // after the leading sepchars
543  for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
544  {
545  if ( *cur == '"' || *cur == '\'' )
546  {
547  char closeChar = *cur;
548  ++cur;
549  bool cont = true;
550  while (cont)
551  {
552  while ( *cur && *cur != closeChar)
553  ++cur;
554  if ( *cur == '\0' )
555  {
556  return ret; //TODO parsing exception no closing quote
557  }
558  int escCount = 0;
559  const char * esc = cur-1;
560  while ( esc != beg && *esc == '\\' )
561  {
562  escCount++;
563  --esc;
564  }
565  cont = (escCount % 2 == 1); // find some non escaped escape char
566  cur++; //skip quote
567  }
568 
569  std::string s( beg+1, cur-beg-2 ); //without quotes
570  //transform escaped escape
571  replaceAll( s, "\\\\", "\\" );
572  //transform escaped quotes (only same as open
573  char tmpn[2] = { closeChar, 0 };
574  char tmpo[3] = { '\\', closeChar, 0 };
575  replaceAll( s, tmpo, tmpn );
576 
577  *result_r = s;
578  }
579  else
580  {
581  // skip non sepchars
582  while( *cur && !::strchr( sepchars_r, *cur ) )
583  {
584  //ignore char after backslash
585  if ( *cur == '\\' )
586  {
587  ++cur;
588  }
589  ++cur;
590  }
591  // build string
592  std::string s( beg, cur-beg );
593  //transform escaped escape
594  replaceAll( s, "\\\\", "\\" );
595 
596  const char *delimeter = sepchars_r;
597  while ( *delimeter )
598  {
599  std::string ds("\\");
600  const char tmp[2] = { *delimeter, '\0' };
601  std::string del(tmp);
602  ds+= del;
603  replaceAll( s, ds, del );
604  ++delimeter;
605  }
606 
607  *result_r = s;
608  }
609  // skip sepchars
610  if ( *cur && ::strchr( sepchars_r, *cur ) )
611  ++cur;
612  while ( *cur && ::strchr( sepchars_r, *cur ) )
613  {
614  ++cur;
615  if (withEmpty)
616  {
617  *result_r = "";
618  ++ret;
619  }
620  }
621  // the last was a separator => one more field
622  if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
623  {
624  *result_r = "";
625  ++ret;
626  }
627  }
628  return ret;
629  }
630 
652  template<class _OutputIterator>
653  unsigned splitFields( const C_Str & line_r,
654  _OutputIterator result_r,
655  const C_Str & sepchars_r = ":" )
656  {
657  const char * beg = line_r;
658  const char * cur = beg;
659  unsigned ret = 0;
660  for ( beg = cur; *beg; beg = cur, ++result_r )
661  {
662  // skip non sepchars
663  while( *cur && !::strchr( sepchars_r, *cur ) )
664  {
665  if ( *cur == '\\' && *(cur+1) )
666  ++cur;
667  ++cur;
668  }
669  // build string
670  *result_r = std::string( beg, cur-beg );
671  ++ret;
672  // skip sepchar
673  if ( *cur )
674  {
675  ++cur;
676  if ( ! *cur ) // ending with sepchar
677  {
678  *result_r = std::string(); // add final empty field
679  ++ret;
680  break;
681  }
682  }
683  }
684  return ret;
685  }
686 
693  template<class _OutputIterator>
694  unsigned splitFieldsEscaped( const C_Str & line_r,
695  _OutputIterator result_r,
696  const C_Str & sepchars_r = ":" )
697  {
698  return
699  splitEscaped( line_r, result_r, sepchars_r, true /* withEmpty */ );
700  }
701 
703 
705 
708  template <class _Iterator>
709  std::string join( _Iterator begin, _Iterator end,
710  const C_Str & sep_r = " " )
711  {
712  std::string res;
713  for ( _Iterator iter = begin; iter != end; ++ iter )
714  {
715  if ( iter != begin )
716  res += sep_r;
717  res += asString(*iter);
718  }
719  return res;
720  }
721 
723  template <class _Container>
724  std::string join( const _Container & cont_r,
725  const C_Str & sep_r = " " )
726  { return join( cont_r.begin(), cont_r.end(), sep_r ); }
727 
732  template <class _Iterator>
733  std::string joinEscaped( _Iterator begin, _Iterator end,
734  const char sep_r = ' ' )
735  {
736  std::vector<char> buf;
737  for ( _Iterator iter = begin; iter != end; ++ iter )
738  {
739  if ( iter != begin )
740  buf.push_back( sep_r );
741 
742  if ( iter->empty() )
743  {
744  // empty string goes ""
745  buf.push_back( '"' );
746  buf.push_back( '"' );
747  }
748  else
749  {
750  std::string toadd( asString(*iter) );
751  for_( ch, toadd.begin(), toadd.end() )
752  {
753  switch ( *ch )
754  {
755  case '"':
756  case '\'':
757  case '\\':
758  buf.push_back( '\\' );
759  buf.push_back( *ch );
760  break;
761  default:
762  if ( *ch == sep_r )
763  buf.push_back( '\\' );
764  buf.push_back( *ch );
765  }
766  }
767  }
768  }
769  return std::string( buf.begin(), buf.end() );
770  }
772 
773 
775 
781  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, const std::string & indent_r = " ", unsigned maxWitdh_r = 0 )
782  {
783  if ( maxWitdh_r )
784  {
785  if ( indent_r.size() >= maxWitdh_r )
786  maxWitdh_r = 0; // nonsense: indent larger than line witdh
787  else
788  maxWitdh_r -= indent_r.size();
789  }
790  unsigned width = 0;
791  for ( const char * e = text_r.c_str(), * s = e; *e; s = ++e )
792  {
793  for ( ; *e && *e != '\n'; ++e ) ;/*searching*/
794  width = e-s;
795  if ( maxWitdh_r && width > maxWitdh_r )
796  {
797  // must break line
798  width = maxWitdh_r;
799  for ( e = s+width; e > s && *e != ' '; --e ) ;/*searching*/
800  if ( e > s )
801  width = e-s; // on a ' ', replaced by '\n'
802  else
803  e = s+width-1; // cut line;
804  }
805  str << indent_r;
806  str.write( s, width );
807  str << "\n";
808  if ( !*e ) // on '\0'
809  break;
810  }
811  return str;
812  }
814  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, char indentch_r = ' ', unsigned maxWitdh_r = 0 )
815  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
817  inline std::ostream & printIndented( std::ostream & str, const std::string & text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r = ' ' )
818  { return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
819 
823  inline std::ostream & autoPrefix( std::ostream & str, const std::string & text_r, function<std::string(const char*, const char*)> fnc_r )
824  {
825  for ( const char * e = text_r.c_str(); *e; ++e )
826  {
827  const char * s = e;
828  for ( ; *e && *e != '\n'; ++e ) /*searching*/;
829  str << fnc_r( s, e );
830  str.write( s, e-s );
831  str << "\n";
832  if ( !*e ) // on '\0'
833  break;
834  }
835  return str;
836  }
838  inline std::ostream & autoPrefix0( std::ostream & str, const std::string & text_r, function<std::string()> fnc_r )
839  {
840  auto wrap = [&fnc_r]( const char*, const char* )-> std::string {
841  return fnc_r();
842  };
843  return autoPrefix( str, text_r, wrap );
844  }
846 
855  std::string escape( const C_Str & str_r, const char c = ' ' );
856 
858  inline void appendEscaped( std::string & str_r, const C_Str & next_r, const char sep_r = ' ' )
859  {
860  if ( ! str_r.empty() )
861  str_r += sep_r;
862  if ( next_r.empty() )
863  str_r += "\"\"";
864  else
865  str_r += escape( next_r, sep_r );
866  }
867 
869 
871 
881  std::string hexencode( const C_Str & str_r );
883  std::string hexdecode( const C_Str & str_r );
885 
892  std::string toLower( const std::string & s );
894  inline std::string toLower( const char * s )
895  { return( s ? toLower( std::string(s) ) : std::string() ); }
896 
900  std::string toUpper( const std::string & s );
902  inline std::string toUpper( const char * s )
903  { return( s ? toUpper( std::string(s) ) : std::string() ); }
905 
906 
909  inline int compareCI( const C_Str & lhs, const C_Str & rhs )
910  { return ::strcasecmp( lhs, rhs ); }
912 
916  inline bool contains( const C_Str & str_r, const C_Str & val_r )
917  { return ::strstr( str_r, val_r ); }
919  inline bool containsCI( const C_Str & str_r, const C_Str & val_r )
920  { return ::strcasestr( str_r, val_r ); }
922 
924 
929  enum Trim {
930  NO_TRIM = 0x00,
931  L_TRIM = 0x01,
932  R_TRIM = 0x02,
934  };
935 
936  std::string trim( const std::string & s, const Trim trim_r = TRIM );
937 
938  inline std::string ltrim( const std::string & s )
939  { return trim( s, L_TRIM ); }
940 
941  inline std::string rtrim( const std::string & s )
942  { return trim( s, R_TRIM ); }
944 
945  std::string stripFirstWord( std::string & line, const bool ltrim_first = true );
946 
947  std::string stripLastWord( std::string & line, const bool rtrim_first = true );
948 
952  std::string getline( std::istream & str, bool trim = false );
953 
957  std::string getline( std::istream & str, const Trim trim_r );
958 
966  std::string receiveUpTo( std::istream & str, const char delim_r, bool returnDelim_r = false );
967 
969 
974  inline bool hasPrefix( const C_Str & str_r, const C_Str & prefix_r )
975  { return( ::strncmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }
976 
978  inline std::string stripPrefix( const C_Str & str_r, const C_Str & prefix_r )
979  { return( hasPrefix( str_r, prefix_r ) ? str_r + prefix_r.size() : str_r.c_str() ); }
980 
982  inline bool hasSuffix( const C_Str & str_r, const C_Str & suffix_r )
983  { return( str_r.size() >= suffix_r.size() && ::strncmp( str_r + str_r.size() - suffix_r.size() , suffix_r, suffix_r.size() ) == 0 ); }
984 
986  inline std::string stripSuffix( const C_Str & str_r, const C_Str & suffix_r )
987  {
988  if ( hasSuffix( str_r, suffix_r ) )
989  return std::string( str_r, str_r.size() - suffix_r.size() );
990  return str_r.c_str();
991  }
993  inline std::string::size_type commonPrefix( const C_Str & lhs, const C_Str & rhs )
994  {
995  const char * lp = lhs.c_str();
996  const char * rp = rhs.c_str();
997  std::string::size_type ret = 0;
998  while ( *lp == *rp && *lp != '\0' )
999  { ++lp, ++rp, ++ret; }
1000  return ret;
1001  }
1002 
1004  inline bool startsWith( const C_Str & str_r, const C_Str & prefix_r )
1005  { return hasPrefix( str_r, prefix_r ); }
1007  inline bool endsWith( const C_Str & str_r, const C_Str & prefix_r )
1008  { return hasSuffix( str_r, prefix_r ); }
1010  } // namespace str
1014 } // namespace zypp
1016 #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:858
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
Definition: String.h:916
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:399
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:978
unsigned split(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
Definition: String.h:463
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:993
std::string escape(const C_Str &str_r, const char sep_r)
Escape desired character c using a backslash.
Definition: String.cc:339
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:986
std::string asString() const
Definition: String.h:213
std::ostream & autoPrefix0(std::ostream &str, const std::string &text_r, function< std::string()> fnc_r)
Definition: String.h:838
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:694
std::string join(const _Container &cont_r, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
Definition: String.h:724
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:929
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:733
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:515
std::string ltrim(const std::string &s)
Definition: String.h:938
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:237
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:286
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:228
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:1004
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:439
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:919
std::ostream & printIndented(std::ostream &str, const std::string &text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r= ' ')
Definition: String.h:817
std::string gsubFun(const std::string &str_r, const std::string &from_r, function< std::string()> to_r)
Definition: String.cc:318
bool endsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasSuffix
Definition: String.h:1007
size_type _sze
Definition: String.h:137
int compareCI(const C_Str &lhs, const C_Str &rhs)
Definition: String.h:909
SolvableIdType size_type
Definition: PoolMember.h:99
std::string rtrim(const std::string &s)
Definition: String.h:941
Str & operator<<(const _Tp &val)
Definition: String.h:231
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:324
bool hasSuffix(const C_Str &str_r, const C_Str &suffix_r)
Return whether str_r has suffix suffix_r.
Definition: String.h:982
std::string hexstring(unsigned long long n, int w=0)
Definition: String.h:310
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:374
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:371
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Definition: String.h:392
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:334
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:823
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:240
std::string toUpper(const char *s)
Definition: String.h:902
Assert free called for allocated char *.
Definition: String.h:208
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition: String.h:974
std::string toLower(const char *s)
Definition: String.h:894
void clear()
Definition: String.h:243
unsigned splitFields(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=":")
Split line_r into fields.
Definition: String.h:653
std::ostringstream _str
Definition: String.h:246
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:275