12 #ifndef ZYPP_BASE_STRING_H
13 #define ZYPP_BASE_STRING_H
21 #include <boost/format.hpp>
28 namespace boost {
namespace logic {
class tribool; } }
40 inline format
formatNAC(
const std::string & string_r ) {
41 using namespace boost::io;
42 format fmter( string_r );
43 fmter.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
58 {
return val_r.asUserString(); }
122 C_Str(
char * c_str_r ) : _val( c_str_r ), _sze(
std::string::npos ) {}
123 C_Str(
const char * c_str_r ) : _val( c_str_r ), _sze(
std::string::npos ) {}
124 C_Str(
const std::string & str_r ) : _val( str_r.c_str() ), _sze( str_r.size() ) {}
128 bool empty()
const {
return !(_val && *_val); }
131 if ( _sze == std::string::npos )
132 { _sze = _val ? ::strlen( _val ) : 0; }
136 operator const char *()
const {
return c_str(); }
137 const char *
c_str()
const {
return _val ? _val :
""; }
146 {
return str << obj.
c_str(); }
160 inline std::string
asString(
const std::string &t )
163 #ifndef SWIG // Swig treats it as syntax error
165 {
return std::move(t); }
176 {
return t.asString(); }
179 inline std::string
asString(
const intrusive_ptr<_T> &p )
180 {
return p->asString(); }
183 inline std::string
asString(
const weak_ptr<_T> &p )
184 {
return p->asString(); }
188 {
return t ?
"true" :
"false"; }
192 std::string
form(
const char * format, ... )
193 __attribute__ ((format (printf, 1, 2)));
219 {
return _buf ? std::string(_buf) : std::string(); }
237 { _str << val;
return *
this; }
239 operator std::string()
const
240 {
return _str.
str(); }
243 {
return _str.str(); }
249 { _str.str( std::string() ); }
255 {
return str << (std::string)obj; }
271 inline std::string
numstring(
char n,
int w = 0 ) {
return form(
"%*hhd", w, n ); }
272 inline std::string
numstring(
unsigned char n,
int w = 0 ) {
return form(
"%*hhu", w, n ); }
273 inline std::string
numstring(
short n,
int w = 0 ) {
return form(
"%*hd", w, n ); }
274 inline std::string
numstring(
unsigned short n,
int w = 0 ) {
return form(
"%*hu", w, n ); }
275 inline std::string
numstring(
int n,
int w = 0 ) {
return form(
"%*d", w, n ); }
276 inline std::string
numstring(
unsigned n,
int w = 0 ) {
return form(
"%*u", w, n ); }
277 inline std::string
numstring(
long n,
int w = 0 ) {
return form(
"%*ld", w, n ); }
278 inline std::string
numstring(
unsigned long n,
int w = 0 ) {
return form(
"%*lu", w, n ); }
279 inline std::string
numstring(
long long n,
int w = 0 ) {
return form(
"%*lld", w, n ); }
280 inline std::string
numstring(
unsigned long long n,
int w = 0 ) {
return form(
"%*llu", w, n ); }
291 template<>
inline std::string
asString(
const unsigned long long & t ) {
return numstring( t ); }
306 inline std::string
hexstring(
char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
307 inline std::string
hexstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
308 inline std::string
hexstring(
short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
309 inline std::string
hexstring(
unsigned short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
310 inline std::string
hexstring(
int n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
311 inline std::string
hexstring(
unsigned n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
312 inline std::string
hexstring(
long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
313 inline std::string
hexstring(
unsigned long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
314 inline std::string
hexstring(
long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
315 inline std::string
hexstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
330 inline std::string
octstring(
char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
331 inline std::string
octstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
332 inline std::string
octstring(
short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
333 inline std::string
octstring(
unsigned short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
334 inline std::string
octstring(
int n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
335 inline std::string
octstring(
unsigned n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
336 inline std::string
octstring(
long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
337 inline std::string
octstring(
unsigned long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
338 inline std::string
octstring(
long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
339 inline std::string
octstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
352 template<
typename _It>
371 inline unsigned long long strtonum(
const C_Str &
str ) { return ::strtoull( str, NULL, 0 ); }
378 template<
typename _It>
380 {
return i = strtonum<_It>( str ); }
406 if (
strToTrue( str ) )
return (return_r =
true);
407 if ( !
strToFalse( str ) )
return (return_r =
false);
419 std::string
gsub(
const std::string & str_r,
const std::string & from_r,
const std::string & to_r );
423 std::string
gsubFun(
const std::string & str_r,
const std::string & from_r,
function<std::string()> to_r );
429 std::string &
replaceAll( std::string & str_r,
const std::string & from_r,
const std::string & to_r );
433 std::string &
replaceAllFun( std::string & str_r,
const std::string & from_r,
function<std::string()> to_r );
449 if ( gap_r && inp_r.size() > gap_r )
451 inp_r.reserve( inp_r.size() + (inp_r.size()-1)/gap_r );
453 inp_r.insert( pos, 1, gapchar );
470 template<
class _OutputIterator>
472 _OutputIterator result_r,
473 const C_Str & sepchars_r =
" \t" )
475 const char * beg = line_r;
476 const char * cur = beg;
478 while ( *cur && ::strchr( sepchars_r, *cur ) )
481 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
484 while( *cur && !::strchr( sepchars_r, *cur ) )
487 *result_r = std::string( beg, cur-beg );
489 while ( *cur && ::strchr( sepchars_r, *cur ) )
531 template<
class _OutputIterator>
533 _OutputIterator result_r,
534 const C_Str & sepchars_r =
" \t",
535 bool withEmpty =
false)
537 const char * beg = line_r;
538 const char * cur = beg;
542 while ( *cur && ::strchr( sepchars_r, *cur ) )
553 if (!*cur && withEmpty)
560 enum class Quote { None, Slash, Single, Double, DoubleSlash };
561 std::vector<char> buf;
562 Quote quoting = Quote::None;
563 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
567 quoting = Quote::None;
574 case '\\': quoting = Quote::Slash;
break;
575 case '\'': quoting = Quote::Single;
break;
576 case '"': quoting = Quote::Double;
break;
577 default: buf.push_back( *cur );
break;
582 buf.push_back( *cur );
583 quoting = Quote::None;
589 case '\'': quoting = Quote::None;
break;
590 default: buf.push_back( *cur );
break;
597 case '\"': quoting = Quote::None;
break;
598 case '\\': quoting = Quote::DoubleSlash;
break;
599 default: buf.push_back( *cur );
break;
603 case Quote::DoubleSlash:
607 case '\\': buf.push_back( *cur );
break;
609 buf.push_back(
'\\' );
610 buf.push_back( *cur );
613 quoting = Quote::Double;
617 }
while ( *cur && ( quoting != Quote::None || !::strchr( sepchars_r, *cur ) ) );
618 *result_r = std::string( buf.begin(), buf.end() );
622 if ( *cur && ::strchr( sepchars_r, *cur ) )
624 while ( *cur && ::strchr( sepchars_r, *cur ) )
634 if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
664 template<
class _OutputIterator>
666 _OutputIterator result_r,
667 const C_Str & sepchars_r =
":" )
669 const char * beg = line_r;
670 const char * cur = beg;
672 for ( beg = cur; *beg; beg = cur, ++result_r )
675 while( *cur && !::strchr( sepchars_r, *cur ) )
677 if ( *cur ==
'\\' && *(cur+1) )
682 *result_r = std::string( beg, cur-beg );
690 *result_r = std::string();
705 template<
class _OutputIterator>
707 _OutputIterator result_r,
708 const C_Str & sepchars_r =
":" )
720 template <
class _Iterator>
721 std::string
join( _Iterator begin, _Iterator end,
722 const C_Str & sep_r =
" " )
725 for ( _Iterator iter = begin; iter != end; ++ iter )
735 template <
class _Container>
736 std::string
join(
const _Container & cont_r,
737 const C_Str & sep_r =
" " )
738 {
return join( cont_r.begin(), cont_r.end(), sep_r ); }
744 template <
class _Iterator>
746 const char sep_r =
' ' )
748 std::vector<char> buf;
749 for ( _Iterator iter = begin; iter != end; ++ iter )
752 buf.push_back( sep_r );
757 buf.push_back(
'"' );
758 buf.push_back(
'"' );
762 std::string toadd(
asString(*iter) );
763 for_( ch, toadd.begin(), toadd.end() )
770 buf.push_back(
'\\' );
771 buf.push_back( *ch );
775 buf.push_back(
'\\' );
776 buf.push_back( *ch );
781 return std::string( buf.begin(), buf.end() );
793 inline std::ostream &
printIndented( std::ostream &
str,
const std::string & text_r,
const std::string & indent_r =
" ",
unsigned maxWitdh_r = 0 )
797 if ( indent_r.size() >= maxWitdh_r )
800 maxWitdh_r -= indent_r.size();
803 for (
const char * e = text_r.c_str(), * s = e; *e; s = ++e )
805 for ( ; *e && *e !=
'\n'; ++e ) ;
807 if ( maxWitdh_r && width > maxWitdh_r )
811 for ( e = s+width; e > s && *e !=
' '; --e ) ;
818 str.write( s, width );
826 inline std::ostream &
printIndented( std::ostream &
str,
const std::string & text_r,
unsigned indent_r,
char indentch_r =
' ',
unsigned maxWitdh_r = 0 )
827 {
return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
829 inline std::ostream &
printIndented( std::ostream &
str,
const std::string & text_r,
unsigned indent_r,
unsigned maxWitdh_r,
char indentch_r =
' ' )
830 {
return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
835 inline std::ostream &
autoPrefix( std::ostream &
str,
const std::string & text_r,
function<std::string(
const char*,
const char*)> fnc_r )
837 for (
const char * e = text_r.c_str(); *e; ++e )
840 for ( ; *e && *e !=
'\n'; ++e ) ;
841 str << fnc_r( s, e );
850 inline std::ostream &
autoPrefix0( std::ostream & str,
const std::string & text_r,
function<std::string()> fnc_r )
852 auto wrap = [&fnc_r](
const char*,
const char* )-> std::string {
867 std::string
escape(
const C_Str & str_r,
const char c =
' ' );
872 if ( ! str_r.empty() )
874 if ( next_r.
empty() )
877 str_r +=
escape( next_r, sep_r );
904 std::string
toLower(
const std::string & s );
907 {
return( s ?
toLower( std::string(s) ) : std::string() ); }
912 std::string
toUpper(
const std::string & s );
915 {
return( s ?
toUpper( std::string(s) ) : std::string() ); }
922 { return ::strcasecmp( lhs, rhs ); }
929 { return ::strstr( str_r, val_r ); }
932 { return ::strcasestr( str_r, val_r ); }
948 std::string
trim(
const std::string & s,
const Trim trim_r =
TRIM );
950 inline std::string
ltrim(
const std::string & s )
953 inline std::string
rtrim(
const std::string & s )
957 std::string
stripFirstWord( std::string & line,
const bool ltrim_first =
true );
959 std::string
stripLastWord( std::string & line,
const bool rtrim_first =
true );
964 std::string
getline( std::istream & str,
bool trim =
false );
969 std::string
getline( std::istream & str,
const Trim trim_r );
978 std::string
receiveUpTo( std::istream & str,
const char delim_r,
bool returnDelim_r =
false );
987 {
return( ::strncmp( str_r, prefix_r, prefix_r.
size() ) == 0 ); }
995 {
return( str_r.
size() >= suffix_r.
size() && ::strncmp( str_r + str_r.
size() - suffix_r.
size() , suffix_r, suffix_r.
size() ) == 0 ); }
1001 return std::string( str_r, str_r.
size() - suffix_r.
size() );
1002 return str_r.
c_str();
1007 const char * lp = lhs.
c_str();
1008 const char * rp = rhs.
c_str();
1010 while ( *lp == *rp && *lp !=
'\0' )
1011 { ++lp, ++rp, ++ret; }
1017 {
return hasPrefix( str_r, prefix_r ); }
1020 {
return hasSuffix( str_r, prefix_r ); }
1030 #endif // ZYPP_BASE_STRING_H
TriBool strToTriBool(const C_Str &str)
Parse str into a bool if it's a legal true or false string; else indterminate.
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)
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
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)