12 #ifndef ZYPP_BASE_STRING_H
13 #define ZYPP_BASE_STRING_H
21 #include <boost/format.hpp>
28 namespace boost {
namespace logic {
class tribool; } }
29 namespace zypp {
typedef boost::logic::tribool
TriBool; }
40 {
return val_r.asUserString(); }
52 MessageString(
const char * str_r ) : std::string( str_r ? str_r :
"" ) {}
105 C_Str(
const char * c_str_r ) :
_val( c_str_r ),
_sze( std::string::npos ) {}
113 if (
_sze == std::string::npos )
118 operator const char *()
const {
return c_str(); }
128 {
return str << obj.
c_str(); }
142 inline std::string
asString(
const std::string &t )
145 #ifndef SWIG // Swig treats it as syntax error
147 {
return std::move(t); }
158 {
return t.asString(); }
161 inline std::string
asString(
const intrusive_ptr<_T> &p )
162 {
return p->asString(); }
165 inline std::string
asString(
const weak_ptr<_T> &p )
166 {
return p->asString(); }
170 {
return t ?
"true" :
"false"; }
174 std::string
form(
const char * format, ... )
175 __attribute__ ((format (printf, 1, 2)));
182 std::
string strerror(
int errno_r );
201 {
return _buf ? std::string(_buf) : std::string(); }
220 {
_str << val;
return *
this; }
223 {
_str << iomanip;
return *
this; }
225 operator std::string()
const {
return _str.str(); }
227 std::string
str()
const {
return _str.str(); }
240 {
return str << obj.
str(); }
261 {
_fmter % std::forward<Tp>(arg);
return *
this; }
263 operator std::string()
const {
return _fmter.str(); }
276 {
return str << obj.
fmter(); }
293 using namespace boost::io;
294 _fmter.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
311 inline std::string
numstring(
char n,
int w = 0 ) {
return form(
"%*hhd", w, n ); }
312 inline std::string
numstring(
unsigned char n,
int w = 0 ) {
return form(
"%*hhu", w, n ); }
313 inline std::string
numstring(
short n,
int w = 0 ) {
return form(
"%*hd", w, n ); }
314 inline std::string
numstring(
unsigned short n,
int w = 0 ) {
return form(
"%*hu", w, n ); }
315 inline std::string
numstring(
int n,
int w = 0 ) {
return form(
"%*d", w, n ); }
316 inline std::string
numstring(
unsigned n,
int w = 0 ) {
return form(
"%*u", w, n ); }
317 inline std::string
numstring(
long n,
int w = 0 ) {
return form(
"%*ld", w, n ); }
318 inline std::string
numstring(
unsigned long n,
int w = 0 ) {
return form(
"%*lu", w, n ); }
319 inline std::string
numstring(
long long n,
int w = 0 ) {
return form(
"%*lld", w, n ); }
320 inline std::string
numstring(
unsigned long long n,
int w = 0 ) {
return form(
"%*llu", w, n ); }
331 template<>
inline std::string
asString(
const unsigned long long & t ) {
return numstring( t ); }
346 inline std::string
hexstring(
char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
347 inline std::string
hexstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
348 inline std::string
hexstring(
short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
349 inline std::string
hexstring(
unsigned short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
350 inline std::string
hexstring(
int n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
351 inline std::string
hexstring(
unsigned n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
352 inline std::string
hexstring(
long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
353 inline std::string
hexstring(
unsigned long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
354 inline std::string
hexstring(
long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
355 inline std::string
hexstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
370 inline std::string
octstring(
char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
371 inline std::string
octstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
372 inline std::string
octstring(
short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
373 inline std::string
octstring(
unsigned short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
374 inline std::string
octstring(
int n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
375 inline std::string
octstring(
unsigned n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
376 inline std::string
octstring(
long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
377 inline std::string
octstring(
unsigned long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
378 inline std::string
octstring(
long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
379 inline std::string
octstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
392 template<
typename _It>
411 inline unsigned long long strtonum(
const C_Str &
str ) { return ::strtoull( str, NULL, 0 ); }
418 template<
typename _It>
420 {
return i = strtonum<_It>(
str ); }
446 if (
strToTrue( str ) )
return (return_r =
true);
447 if ( !
strToFalse( str ) )
return (return_r =
false);
459 std::string
gsub(
const std::string & str_r,
const std::string & from_r,
const std::string & to_r );
463 std::string
gsubFun(
const std::string & str_r,
const std::string & from_r,
function<std::string()> to_r );
469 std::string &
replaceAll( std::string & str_r,
const std::string & from_r,
const std::string & to_r );
473 std::string &
replaceAllFun( std::string & str_r,
const std::string & from_r,
function<std::string()> to_r );
489 if ( gap_r && inp_r.size() > gap_r )
491 inp_r.reserve( inp_r.size() + (inp_r.size()-1)/gap_r );
493 inp_r.insert( pos, 1, gapchar );
510 template<
class _OutputIterator>
512 _OutputIterator result_r,
513 const C_Str & sepchars_r =
" \t" )
515 const char * beg = line_r;
516 const char * cur = beg;
518 while ( *cur && ::strchr( sepchars_r, *cur ) )
521 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
524 while( *cur && !::strchr( sepchars_r, *cur ) )
527 *result_r = std::string( beg, cur-beg );
529 while ( *cur && ::strchr( sepchars_r, *cur ) )
571 template<
class _OutputIterator>
573 _OutputIterator result_r,
574 const C_Str & sepchars_r =
" \t",
575 bool withEmpty =
false)
577 const char * beg = line_r;
578 const char * cur = beg;
582 while ( *cur && ::strchr( sepchars_r, *cur ) )
593 if (!*cur && withEmpty)
600 enum class Quote { None, Slash, Single, Double, DoubleSlash };
601 std::vector<char> buf;
602 Quote quoting = Quote::None;
603 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
607 quoting = Quote::None;
614 case '\\': quoting = Quote::Slash;
break;
615 case '\'': quoting = Quote::Single;
break;
616 case '"': quoting = Quote::Double;
break;
617 default: buf.push_back( *cur );
break;
622 buf.push_back( *cur );
623 quoting = Quote::None;
629 case '\'': quoting = Quote::None;
break;
630 default: buf.push_back( *cur );
break;
637 case '\"': quoting = Quote::None;
break;
638 case '\\': quoting = Quote::DoubleSlash;
break;
639 default: buf.push_back( *cur );
break;
643 case Quote::DoubleSlash:
647 case '\\': buf.push_back( *cur );
break;
649 buf.push_back(
'\\' );
650 buf.push_back( *cur );
653 quoting = Quote::Double;
657 }
while ( *cur && ( quoting != Quote::None || !::strchr( sepchars_r, *cur ) ) );
658 *result_r = std::string( buf.begin(), buf.end() );
662 if ( *cur && ::strchr( sepchars_r, *cur ) )
664 while ( *cur && ::strchr( sepchars_r, *cur ) )
674 if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
704 template<
class _OutputIterator>
706 _OutputIterator result_r,
707 const C_Str & sepchars_r =
":" )
709 const char * beg = line_r;
710 const char * cur = beg;
712 for ( beg = cur; *beg; beg = cur, ++result_r )
715 while( *cur && !::strchr( sepchars_r, *cur ) )
717 if ( *cur ==
'\\' && *(cur+1) )
722 *result_r = std::string( beg, cur-beg );
730 *result_r = std::string();
745 template<
class _OutputIterator>
747 _OutputIterator result_r,
748 const C_Str & sepchars_r =
":" )
760 template <
class _Iterator>
761 std::string
join( _Iterator begin, _Iterator end,
762 const C_Str & sep_r =
" " )
765 for ( _Iterator iter = begin; iter != end; ++ iter )
775 template <
class _Container>
776 std::string
join(
const _Container & cont_r,
777 const C_Str & sep_r =
" " )
778 {
return join( cont_r.begin(), cont_r.end(), sep_r ); }
784 template <
class _Iterator>
786 const char sep_r =
' ' )
788 std::vector<char> buf;
789 for ( _Iterator iter = begin; iter != end; ++ iter )
792 buf.push_back( sep_r );
797 buf.push_back(
'"' );
798 buf.push_back(
'"' );
802 std::string toadd(
asString(*iter) );
803 for_( ch, toadd.begin(), toadd.end() )
810 buf.push_back(
'\\' );
811 buf.push_back( *ch );
815 buf.push_back(
'\\' );
816 buf.push_back( *ch );
821 return std::string( buf.begin(), buf.end() );
833 inline std::ostream &
printIndented( std::ostream &
str,
const std::string & text_r,
const std::string & indent_r =
" ",
unsigned maxWitdh_r = 0 )
837 if ( indent_r.size() >= maxWitdh_r )
840 maxWitdh_r -= indent_r.size();
843 for (
const char * e = text_r.c_str(), * s = e; *e; s = ++e )
845 for ( ; *e && *e !=
'\n'; ++e ) ;
847 if ( maxWitdh_r && width > maxWitdh_r )
851 for ( e = s+width; e > s && *e !=
' '; --e ) ;
858 str.write( s, width );
866 inline std::ostream &
printIndented( std::ostream &
str,
const std::string & text_r,
unsigned indent_r,
char indentch_r =
' ',
unsigned maxWitdh_r = 0 )
867 {
return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
869 inline std::ostream &
printIndented( std::ostream &
str,
const std::string & text_r,
unsigned indent_r,
unsigned maxWitdh_r,
char indentch_r =
' ' )
870 {
return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
875 inline std::ostream &
autoPrefix( std::ostream &
str,
const std::string & text_r,
function<std::string(
const char*,
const char*)> fnc_r )
877 for (
const char * e = text_r.c_str(); *e; ++e )
880 for ( ; *e && *e !=
'\n'; ++e ) ;
881 str << fnc_r( s, e );
890 inline std::ostream &
autoPrefix0( std::ostream & str,
const std::string & text_r,
function<std::string()> fnc_r )
892 auto wrap = [&fnc_r](
const char*,
const char* )-> std::string {
907 std::string
escape(
const C_Str & str_r,
const char c =
' ' );
912 if ( ! str_r.empty() )
914 if ( next_r.
empty() )
917 str_r +=
escape( next_r, sep_r );
921 std::string
bEscape( std::string str_r,
const C_Str & special_r );
953 std::string
toLower(
const std::string & s );
956 {
return( s ?
toLower( std::string(s) ) : std::string() ); }
961 std::string
toUpper(
const std::string & s );
964 {
return( s ?
toUpper( std::string(s) ) : std::string() ); }
971 { return ::strcasecmp( lhs, rhs ); }
978 { return ::strstr( str_r, val_r ); }
981 { return ::strcasestr( str_r, val_r ); }
997 std::string
trim(
const std::string & s,
const Trim trim_r =
TRIM );
999 inline std::string
ltrim(
const std::string & s )
1002 inline std::string
rtrim(
const std::string & s )
1006 std::string
stripFirstWord( std::string & line,
const bool ltrim_first =
true );
1008 std::string
stripLastWord( std::string & line,
const bool rtrim_first =
true );
1013 std::string
getline( std::istream & str,
bool trim =
false );
1018 std::string
getline( std::istream & str,
const Trim trim_r );
1027 std::string
receiveUpTo( std::istream & str,
const char delim_r,
bool returnDelim_r =
false );
1036 {
return( ::strncmp( str_r, prefix_r, prefix_r.
size() ) == 0 ); }
1040 {
return(
hasPrefix( str_r, prefix_r ) ? str_r + prefix_r.
size() : str_r.
c_str() ); }
1044 {
return( str_r.
size() >= suffix_r.
size() && ::strncmp( str_r + str_r.
size() - suffix_r.
size() , suffix_r, suffix_r.
size() ) == 0 ); }
1050 return std::string( str_r, str_r.
size() - suffix_r.
size() );
1051 return str_r.
c_str();
1056 const char * lp = lhs.
c_str();
1057 const char * rp = rhs.
c_str();
1059 while ( *lp == *rp && *lp !=
'\0' )
1060 { ++lp, ++rp, ++ret; }
1066 {
return hasPrefix( str_r, prefix_r ); }
1069 {
return hasSuffix( str_r, prefix_r ); }
1079 #endif // ZYPP_BASE_STRING_H
std::string bEscape(std::string str_r, const C_Str &special_r)
Return str_r with '\'-escaped chars occurring in special_r (and '\').
TriBool strToTriBool(const C_Str &str)
Parse str into a bool if it's a legal true or false string; else indterminate.
const std::ostream & stream() const
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.
std::string octstring(char n, int w=4)
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 rxEscapeStr(std::string str_r)
Escape plain STRING str_r for use in a regex (not anchored by "^" or "$").
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)
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 asString() const
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::ostream & printIndented(std::ostream &str, const std::string &text_r, const std::string &indent_r=" ", unsigned maxWitdh_r=0)
Indent by string [" "] optionally wrap.
std::string stripFirstWord(std::string &line, const bool ltrim_first)
std::string rxEscapeGlob(std::string str_r)
Escape GLOB str_r for use in a regex (not anchored by "^" or "$").
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
std::string trim(const std::string &s, const Trim trim_r)
std::string join(_Iterator begin, _Iterator end, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
Convenience char* constructible from std::string and char*, it maps (char*)0 to an empty string...
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 getline(std::istream &str, const Trim trim_r)
Return stream content up to (but not returning) the next newline.
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.
_It strtonum(const C_Str &str)
Parsing numbers from string.
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
std::string numstring(char n, int w=0)
std::string toLower(const std::string &s)
Return lowercase version of s.
int compareCI(const C_Str &lhs, const C_Str &rhs)
std::string rtrim(const std::string &s)
std::ostream & operator<<(std::ostream &str, const Str &obj)
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 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.
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.
Str & operator<<(std::ostream &(*iomanip)(std::ostream &))
MessageString(const std::string &str_r)
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*...
Str & operator<<(const Tp &val)
std::string strerror(int errno_r)
Return string describing the error_r code.
std::string asString(const std::string &t)
Global asString() that works with std::string too.
std::string toUpper(const std::string &s)
Return uppercase version of 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 hexstring(char n, int w=4)
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.