12 #ifndef ZYPP_BASE_STRING_H
13 #define ZYPP_BASE_STRING_H
21 #include <boost/format.hpp>
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 ) );
54 {
return val_r.asUserString(); }
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() ) {}
124 bool empty()
const {
return !(_val && *_val); }
127 if ( _sze == std::string::npos )
128 { _sze = _val ? ::strlen( _val ) : 0; }
132 operator const char *()
const {
return c_str(); }
133 const char *
c_str()
const {
return _val ? _val :
""; }
142 {
return str << obj.
c_str(); }
155 inline std::string
asString(
const std::string &t )
158 #ifndef SWIG // Swig treats it as syntax error
160 {
return std::move(t); }
171 {
return t.asString(); }
174 inline std::string
asString(
const intrusive_ptr<_T> &p )
175 {
return p->asString(); }
178 inline std::string
asString(
const weak_ptr<_T> &p )
179 {
return p->asString(); }
183 {
return t ?
"true" :
"false"; }
187 std::string
form(
const char * format, ... )
188 __attribute__ ((format (printf, 1, 2)));
214 {
return _buf ? std::string(_buf) : std::string(); }
232 { _str << val;
return *
this; }
234 operator std::string()
const
235 {
return _str.
str(); }
238 {
return _str.str(); }
244 { _str.str( std::string() ); }
250 {
return str << (std::string)obj; }
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 ); }
286 template<>
inline std::string
asString(
const unsigned long long & t ) {
return numstring( t ); }
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 ); }
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 ); }
347 template<
typename _It>
366 inline unsigned long long strtonum(
const C_Str &
str ) { return ::strtoull( str, NULL, 0 ); }
373 template<
typename _It>
375 {
return i = strtonum<_It>( str ); }
401 if (
strToTrue( str ) )
return (return_r =
true);
402 if ( !
strToFalse( str ) )
return (return_r =
false);
411 std::string
gsub(
const std::string & str_r,
const std::string & from_r,
const std::string & to_r );
415 std::string
gsubFun(
const std::string & str_r,
const std::string & from_r,
function<std::string()> to_r );
421 std::string &
replaceAll( std::string & str_r,
const std::string & from_r,
const std::string & to_r );
425 std::string &
replaceAllFun( std::string & str_r,
const std::string & from_r,
function<std::string()> to_r );
441 if ( gap_r && inp_r.size() > gap_r )
443 inp_r.reserve( inp_r.size() + (inp_r.size()-1)/gap_r );
445 inp_r.insert( pos, 1, gapchar );
462 template<
class _OutputIterator>
464 _OutputIterator result_r,
465 const C_Str & sepchars_r =
" \t" )
467 const char * beg = line_r;
468 const char * cur = beg;
470 while ( *cur && ::strchr( sepchars_r, *cur ) )
473 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
476 while( *cur && !::strchr( sepchars_r, *cur ) )
479 *result_r = std::string( beg, cur-beg );
481 while ( *cur && ::strchr( sepchars_r, *cur ) )
514 template<
class _OutputIterator>
516 _OutputIterator result_r,
517 const C_Str & sepchars_r =
" \t",
518 bool withEmpty =
false)
520 const char * beg = line_r;
521 const char * cur = beg;
525 while ( *cur && ::strchr( sepchars_r, *cur ) )
536 if (!*cur && withEmpty)
543 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
545 if ( *cur ==
'"' || *cur ==
'\'' )
547 char closeChar = *cur;
552 while ( *cur && *cur != closeChar)
559 const char * esc = cur-1;
560 while ( esc != beg && *esc ==
'\\' )
565 cont = (escCount % 2 == 1);
569 std::string s( beg+1, cur-beg-2 );
573 char tmpn[2] = { closeChar, 0 };
574 char tmpo[3] = {
'\\', closeChar, 0 };
582 while( *cur && !::strchr( sepchars_r, *cur ) )
592 std::string s( beg, cur-beg );
596 const char *delimeter = sepchars_r;
599 std::string ds(
"\\");
600 const char tmp[2] = { *delimeter,
'\0' };
601 std::string del(tmp);
610 if ( *cur && ::strchr( sepchars_r, *cur ) )
612 while ( *cur && ::strchr( sepchars_r, *cur ) )
622 if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
652 template<
class _OutputIterator>
654 _OutputIterator result_r,
655 const C_Str & sepchars_r =
":" )
657 const char * beg = line_r;
658 const char * cur = beg;
660 for ( beg = cur; *beg; beg = cur, ++result_r )
663 while( *cur && !::strchr( sepchars_r, *cur ) )
665 if ( *cur ==
'\\' && *(cur+1) )
670 *result_r = std::string( beg, cur-beg );
678 *result_r = std::string();
693 template<
class _OutputIterator>
695 _OutputIterator result_r,
696 const C_Str & sepchars_r =
":" )
708 template <
class _Iterator>
709 std::string
join( _Iterator begin, _Iterator end,
710 const C_Str & sep_r =
" " )
713 for ( _Iterator iter = begin; iter != end; ++ iter )
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 ); }
732 template <
class _Iterator>
734 const char sep_r =
' ' )
736 std::vector<char> buf;
737 for ( _Iterator iter = begin; iter != end; ++ iter )
740 buf.push_back( sep_r );
745 buf.push_back(
'"' );
746 buf.push_back(
'"' );
750 std::string toadd(
asString(*iter) );
751 for_( ch, toadd.begin(), toadd.end() )
758 buf.push_back(
'\\' );
759 buf.push_back( *ch );
763 buf.push_back(
'\\' );
764 buf.push_back( *ch );
769 return std::string( buf.begin(), buf.end() );
781 inline std::ostream &
printIndented( std::ostream &
str,
const std::string & text_r,
const std::string & indent_r =
" ",
unsigned maxWitdh_r = 0 )
785 if ( indent_r.size() >= maxWitdh_r )
788 maxWitdh_r -= indent_r.size();
791 for (
const char * e = text_r.c_str(), * s = e; *e; s = ++e )
793 for ( ; *e && *e !=
'\n'; ++e ) ;
795 if ( maxWitdh_r && width > maxWitdh_r )
799 for ( e = s+width; e > s && *e !=
' '; --e ) ;
806 str.write( s, width );
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 ); }
823 inline std::ostream &
autoPrefix( std::ostream &
str,
const std::string & text_r,
function<std::string(
const char*,
const char*)> fnc_r )
825 for (
const char * e = text_r.c_str(); *e; ++e )
828 for ( ; *e && *e !=
'\n'; ++e ) ;
829 str << fnc_r( s, e );
838 inline std::ostream &
autoPrefix0( std::ostream & str,
const std::string & text_r,
function<std::string()> fnc_r )
840 auto wrap = [&fnc_r](
const char*,
const char* )-> std::string {
855 std::string
escape(
const C_Str & str_r,
const char c =
' ' );
860 if ( ! str_r.empty() )
862 if ( next_r.
empty() )
865 str_r +=
escape( next_r, sep_r );
892 std::string
toLower(
const std::string & s );
895 {
return( s ?
toLower( std::string(s) ) : std::string() ); }
900 std::string
toUpper(
const std::string & s );
903 {
return( s ?
toUpper( std::string(s) ) : std::string() ); }
910 { return ::strcasecmp( lhs, rhs ); }
917 { return ::strstr( str_r, val_r ); }
920 { return ::strcasestr( str_r, val_r ); }
936 std::string
trim(
const std::string & s,
const Trim trim_r =
TRIM );
938 inline std::string
ltrim(
const std::string & s )
941 inline std::string
rtrim(
const std::string & s )
945 std::string
stripFirstWord( std::string & line,
const bool ltrim_first =
true );
947 std::string
stripLastWord( std::string & line,
const bool rtrim_first =
true );
952 std::string
getline( std::istream & str,
bool trim =
false );
957 std::string
getline( std::istream & str,
const Trim trim_r );
966 std::string
receiveUpTo( std::istream & str,
const char delim_r,
bool returnDelim_r =
false );
975 {
return( ::strncmp( str_r, prefix_r, prefix_r.
size() ) == 0 ); }
983 {
return( str_r.
size() >= suffix_r.
size() && ::strncmp( str_r + str_r.
size() - suffix_r.
size() , suffix_r, suffix_r.
size() ) == 0 ); }
989 return std::string( str_r, str_r.
size() - suffix_r.
size() );
990 return str_r.
c_str();
995 const char * lp = lhs.
c_str();
996 const char * rp = rhs.
c_str();
998 while ( *lp == *rp && *lp !=
'\0' )
999 { ++lp, ++rp, ++ret; }
1005 {
return hasPrefix( str_r, prefix_r ); }
1008 {
return hasSuffix( str_r, prefix_r ); }
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.
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
bool strToBoolNodefault(const C_Str &str, bool &return_r)
Parse str into a bool if it's a legal true or false string.
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.
unsigned split(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
std::string asUserString(const _Tp &val_r)
Request a human readable (translated) string representation of _Tp [_Tp.asUserString()] Classes may i...
std::string::size_type size_type
std::string::size_type commonPrefix(const C_Str &lhs, const C_Str &rhs)
Return size of the common prefix of lhs and rhs.
std::string escape(const C_Str &str_r, const char sep_r)
Escape desired character c using a backslash.
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.
std::string asString() const
std::ostream & autoPrefix0(std::ostream &str, const std::string &text_r, function< std::string()> fnc_r)
String related utilities and Regular expression matching.
C_Str(const std::string &str_r)
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
unsigned splitFieldsEscaped(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=":")
Split line_r into fields handling also escaped separators.
std::string join(const _Container &cont_r, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
MessageString(const _Str &str_r)
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Trim
To define how to trim.
std::string joinEscaped(_Iterator begin, _Iterator end, const char sep_r= ' ')
Join strings using separator sep_r, quoting or escaping the values.
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.
std::string ltrim(const std::string &s)
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).
std::string getline(std::istream &str)
Read one line from stream.
std::string stripFirstWord(std::string &line, const bool ltrim_first)
std::string asString(const unsigned long long &t)
format formatNAC(const std::string &string_r)
A formater with (N)o (A)rgument (C)heck.
Convenient building of std::string via std::ostream::operator<<.
std::string trim(const std::string &s, const Trim trim_r)
Convenience char* constructible from std::string and char*, it maps (char*)0 to an empty string...
std::ostream & operator<<(std::ostream &str, const ::_Dataiterator *obj)
bool startsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasPrefix
std::string gapify(std::string inp_r, std::string::size_type gap_r=1, char gapchar= ' ')
Enhance readability: insert gaps at regular distance.
std::string stripLastWord(std::string &line, const bool rtrim_first)
bool strToFalse(const C_Str &str)
Return false if str is 0, false, no, off.
MessageString(const char *str_r)
bool containsCI(const C_Str &str_r, const C_Str &val_r)
Locate substring case insensitive.
std::ostream & printIndented(std::ostream &str, const std::string &text_r, unsigned indent_r, unsigned maxWitdh_r, char indentch_r= ' ')
std::string gsubFun(const std::string &str_r, const std::string &from_r, function< std::string()> to_r)
bool endsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasSuffix
int compareCI(const C_Str &lhs, const C_Str &rhs)
std::string rtrim(const std::string &s)
Str & operator<<(const _Tp &val)
bool strToTrue(const C_Str &str)
Parsing boolean from string.
std::string & replaceAllFun(std::string &str_r, const std::string &from_r, function< std::string()> to_r)
bool hasSuffix(const C_Str &str_r, const C_Str &suffix_r)
Return whether str_r has suffix suffix_r.
std::string hexstring(unsigned long long n, int w=0)
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.
_It strtonum(const C_Str &str, _It &i)
String to integer type detemined 2nd function arg i.
C_Str(const char *c_str_r)
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.
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
const char * c_str() const
std::string hexencode(const C_Str &str_r)
Encode all characters other than [a-zA-Z0-9] as XX.
MessageString(const std::string &str_r)
std::string octstring(unsigned long long n, int w=0)
std::ostream & operator<<(std::ostream &str, const C_Str &obj)
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*...
std::string strerror(int errno_r)
Return string describing the error_r code.
Easy-to use interface to the ZYPP dependency resolver.
std::string toUpper(const char *s)
Assert free called for allocated char *.
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
std::string toLower(const char *s)
unsigned splitFields(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=":")
Split line_r into fields.
std::string hexdecode(const C_Str &str_r)
Decode hexencoded XX sequences.
std::string numstring(unsigned long long n, int w=0)