Go to the documentation of this file.
12 #ifndef ZYPP_BASE_STRING_H
13 #define ZYPP_BASE_STRING_H
21 #include <boost/format.hpp>
22 #include <boost/utility/string_ref.hpp>
29 namespace boost {
namespace logic {
class tribool; } }
41 {
return val_r.asUserString(); }
107 if (
_sze == std::string::npos )
112 operator const char *()
const {
return c_str(); }
136 inline const std::string &
asString(
const std::string & t )
140 {
return std::move(t); }
143 {
return t ==
nullptr ? std::string() : t; }
146 {
return t ==
nullptr ? std::string() : t; }
150 {
return t.asString(); }
153 inline std::string
asString(
const intrusive_ptr<Tp> &p )
154 {
return p->asString(); }
157 inline std::string
asString(
const weak_ptr<Tp> &p )
158 {
return p->asString(); }
162 {
return t ?
"true" :
"false"; }
166 std::string
form(
const char * format, ... )
167 __attribute__ ((format (printf, 1, 2)));
193 {
return _buf ? std::string(_buf) : std::string(); }
212 { _str << std::forward<Tp>(val);
return *
this; }
215 {
_str << iomanip;
return *
this; }
217 operator std::string()
const {
return _str.str(); }
219 std::string
str()
const {
return _str.str(); }
232 {
return str << obj.
str(); }
256 {
_fmter % std::forward<Tp>(arg);
return *
this; }
258 operator std::string()
const {
return _fmter.str(); }
286 inline std::string
numstring(
char n,
int w = 0 ) {
return form(
"%*hhd", w, n ); }
287 inline std::string
numstring(
unsigned char n,
int w = 0 ) {
return form(
"%*hhu", w, n ); }
288 inline std::string
numstring(
short n,
int w = 0 ) {
return form(
"%*hd", w, n ); }
289 inline std::string
numstring(
unsigned short n,
int w = 0 ) {
return form(
"%*hu", w, n ); }
290 inline std::string
numstring(
int n,
int w = 0 ) {
return form(
"%*d", w, n ); }
291 inline std::string
numstring(
unsigned n,
int w = 0 ) {
return form(
"%*u", w, n ); }
292 inline std::string
numstring(
long n,
int w = 0 ) {
return form(
"%*ld", w, n ); }
293 inline std::string
numstring(
unsigned long n,
int w = 0 ) {
return form(
"%*lu", w, n ); }
294 inline std::string
numstring(
long long n,
int w = 0 ) {
return form(
"%*lld", w, n ); }
295 inline std::string
numstring(
unsigned long long n,
int w = 0 ) {
return form(
"%*llu", w, n ); }
306 template<>
inline std::string
asString(
const unsigned long long & t ) {
return numstring( t ); }
321 inline std::string
hexstring(
char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
322 inline std::string
hexstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
323 inline std::string
hexstring(
short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
324 inline std::string
hexstring(
unsigned short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
325 inline std::string
hexstring(
int n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
326 inline std::string
hexstring(
unsigned n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
327 inline std::string
hexstring(
long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
328 inline std::string
hexstring(
unsigned long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
329 inline std::string
hexstring(
long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
330 inline std::string
hexstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
345 inline std::string
octstring(
char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
346 inline std::string
octstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
347 inline std::string
octstring(
short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
348 inline std::string
octstring(
unsigned short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
349 inline std::string
octstring(
int n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
350 inline std::string
octstring(
unsigned n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
351 inline std::string
octstring(
long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
352 inline std::string
octstring(
unsigned long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
353 inline std::string
octstring(
long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
354 inline std::string
octstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
360 template <
typename TInt>
363 constexpr
unsigned bits =
sizeof(TInt)*8;
364 std::string ret( bits,
' ' );
366 for (
unsigned pos = bits; pos > 0; )
367 { --pos; ret[pos] = ((val_r & bit)?
'1':
'0'); bit = bit<<1; }
381 template<
typename TInt>
389 inline long strtonum(
const C_Str &
str ) { return ::strtol (
str, NULL, 0 ); }
391 inline long long strtonum(
const C_Str &
str ) { return ::strtoll (
str, NULL, 0 ); }
394 inline unsigned short strtonum(
const C_Str &
str ) { return ::strtoul (
str, NULL, 0 ); }
396 inline unsigned strtonum(
const C_Str &
str ) { return ::strtoul (
str, NULL, 0 ); }
398 inline unsigned long strtonum(
const C_Str &
str ) { return ::strtoul (
str, NULL, 0 ); }
400 inline unsigned long long strtonum(
const C_Str &
str ) { return ::strtoull(
str, NULL, 0 ); }
407 template<
typename TInt>
409 {
return i = strtonum<TInt>(
str ); }
448 std::string
gsub(
const std::string & str_r,
const std::string & from_r,
const std::string & to_r );
452 std::string
gsubFun(
const std::string & str_r,
const std::string & from_r,
function<std::string()> to_r );
458 std::string &
replaceAll( std::string & str_r,
const std::string & from_r,
const std::string & to_r );
462 std::string &
replaceAllFun( std::string & str_r,
const std::string & from_r,
function<std::string()> to_r );
478 if ( gap_r && inp_r.size() > gap_r )
480 inp_r.reserve( inp_r.size() + (inp_r.size()-1)/gap_r );
482 inp_r.insert( pos, 1, gapchar );
500 std::string
trim(
const std::string & s,
const Trim trim_r =
TRIM );
501 std::string
trim( std::string && s,
const Trim trim_r =
TRIM );
503 inline std::string
ltrim(
const std::string & s )
505 inline std::string
ltrim( std::string && s )
508 inline std::string
rtrim(
const std::string & s )
510 inline std::string
rtrim( std::string && s )
526 template<
class TOutputIterator>
529 const char * beg = line_r;
530 const char * cur = beg;
532 while ( *cur && ::strchr( sepchars_r, *cur ) )
535 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
538 while( *cur && !::strchr( sepchars_r, *cur ) )
541 *result_r =
trim( std::string( beg, cur-beg ), trim_r );
543 while ( *cur && ::strchr( sepchars_r, *cur ) )
549 template<
class TOutputIterator>
550 unsigned split(
const C_Str & line_r, TOutputIterator result_r,
const Trim trim_r )
551 {
return split( line_r, result_r,
" \t", trim_r ); }
590 template<
class TOutputIterator>
591 unsigned splitEscaped(
const C_Str & line_r, TOutputIterator result_r,
const C_Str & sepchars_r =
" \t",
bool withEmpty =
false)
593 const char * beg = line_r;
594 const char * cur = beg;
598 while ( *cur && ::strchr( sepchars_r, *cur ) )
609 if (!*cur && withEmpty)
616 enum class Quote { None, Slash, Single, Double, DoubleSlash };
617 std::vector<char> buf;
618 Quote quoting = Quote::None;
619 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
623 quoting = Quote::None;
630 case '\\': quoting = Quote::Slash;
break;
631 case '\'': quoting = Quote::Single;
break;
632 case '"': quoting = Quote::Double;
break;
633 default: buf.push_back( *cur );
break;
638 buf.push_back( *cur );
639 quoting = Quote::None;
645 case '\'': quoting = Quote::None;
break;
646 default: buf.push_back( *cur );
break;
653 case '\"': quoting = Quote::None;
break;
654 case '\\': quoting = Quote::DoubleSlash;
break;
655 default: buf.push_back( *cur );
break;
659 case Quote::DoubleSlash:
663 case '\\': buf.push_back( *cur );
break;
665 buf.push_back(
'\\' );
666 buf.push_back( *cur );
669 quoting = Quote::Double;
673 }
while ( *cur && ( quoting != Quote::None || !::strchr( sepchars_r, *cur ) ) );
674 *result_r = std::string( buf.begin(), buf.end() );
678 if ( *cur && ::strchr( sepchars_r, *cur ) )
680 while ( *cur && ::strchr( sepchars_r, *cur ) )
690 if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
720 template<
class TOutputIterator>
723 const char * beg = line_r;
724 const char * cur = beg;
726 for ( beg = cur; *beg; beg = cur, ++result_r )
729 while( *cur && !::strchr( sepchars_r, *cur ) )
731 if ( *cur ==
'\\' && *(cur+1) )
736 *result_r = std::string( beg, cur-beg );
744 *result_r = std::string();
759 template<
class TOutputIterator>
762 return splitEscaped( line_r, result_r, sepchars_r,
true );
771 template <
class TIterator>
772 std::string
join( TIterator begin, TIterator end,
const C_Str & sep_r =
" " )
775 for ( TIterator iter = begin; iter != end; ++ iter )
785 template <
class TContainer>
786 std::string
join(
const TContainer & cont_r,
const C_Str & sep_r =
" " )
787 {
return join( cont_r.begin(), cont_r.end(), sep_r ); }
793 template <
class TIterator>
794 std::string
joinEscaped( TIterator begin, TIterator end,
const char sep_r =
' ' )
796 std::vector<char> buf;
797 for ( TIterator iter = begin; iter != end; ++ iter )
800 buf.push_back( sep_r );
805 buf.push_back(
'"' );
806 buf.push_back(
'"' );
810 std::string toadd(
asString(*iter) );
811 for_( ch, toadd.begin(), toadd.end() )
818 buf.push_back(
'\\' );
819 buf.push_back( *ch );
823 buf.push_back(
'\\' );
824 buf.push_back( *ch );
829 return std::string( buf.begin(), buf.end() );
841 inline std::ostream &
printIndented( std::ostream &
str,
const std::string & text_r,
const std::string & indent_r =
" ",
unsigned maxWitdh_r = 0 )
845 if ( indent_r.size() >= maxWitdh_r )
848 maxWitdh_r -= indent_r.size();
851 for (
const char * e = text_r.c_str(), * s = e; *e; s = ++e )
853 for ( ; *e && *e !=
'\n'; ++e ) ;
855 if ( maxWitdh_r && width > maxWitdh_r )
859 for ( e = s+width; e > s && *e !=
' '; --e ) ;
866 str.write( s, width );
874 inline std::ostream &
printIndented( std::ostream &
str,
const std::string & text_r,
unsigned indent_r,
char indentch_r =
' ',
unsigned maxWitdh_r = 0 )
875 {
return printIndented(
str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
877 inline std::ostream &
printIndented( std::ostream &
str,
const std::string & text_r,
unsigned indent_r,
unsigned maxWitdh_r,
char indentch_r =
' ' )
878 {
return printIndented(
str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
883 inline std::ostream &
autoPrefix( std::ostream &
str,
const std::string & text_r,
function<std::string(
const char*,
const char*)> fnc_r )
885 for (
const char * e = text_r.c_str(); *e; ++e )
888 for ( ; *e && *e !=
'\n'; ++e ) ;
889 str << fnc_r( s, e );
898 inline std::ostream &
autoPrefix0( std::ostream &
str,
const std::string & text_r,
function<std::string()> fnc_r )
900 auto wrap = [&fnc_r](
const char*,
const char* )-> std::string {
915 std::string
escape(
const C_Str & str_r,
const char c =
' ' );
920 if ( ! str_r.empty() )
922 if ( next_r.
empty() )
925 str_r +=
escape( next_r, sep_r );
929 std::string
bEscape( std::string str_r,
const C_Str & special_r );
961 std::string
toLower(
const std::string & s );
962 std::string
toLower( std::string && s );
965 {
return( s ?
toLower( std::string(s) ) : std::string() ); }
970 std::string
toUpper(
const std::string & s );
971 std::string
toUpper( std::string && s );
974 {
return( s ?
toUpper( std::string(s) ) : std::string() ); }
981 { return ::strcasecmp( lhs, rhs ); }
988 { return ::strstr( str_r, val_r ); }
991 { return ::strcasestr( str_r, val_r ); }
994 std::string
stripFirstWord( std::string & line,
const bool ltrim_first =
true );
996 std::string
stripLastWord( std::string & line,
const bool rtrim_first =
true );
1015 std::string
receiveUpTo( std::istream &
str,
const char delim_r,
bool returnDelim_r =
false );
1024 {
return( ::strncmp( str_r, prefix_r, prefix_r.
size() ) == 0 ); }
1027 {
return( ::strncasecmp( str_r, prefix_r, prefix_r.
size() ) == 0 ); }
1031 {
return(
hasPrefix( str_r, prefix_r ) ? str_r + prefix_r.
size() : str_r.
c_str() ); }
1038 {
return( str_r.
size() >= suffix_r.
size() && ::strncmp( str_r + str_r.
size() - suffix_r.
size() , suffix_r, suffix_r.
size() ) == 0 ); }
1041 {
return( str_r.
size() >= suffix_r.
size() && ::strncasecmp( str_r + str_r.
size() - suffix_r.
size() , suffix_r, suffix_r.
size() ) == 0 ); }
1047 return std::string( str_r, str_r.
size() - suffix_r.
size() );
1048 return str_r.
c_str();
1054 return std::string( str_r, str_r.
size() - suffix_r.
size() );
1055 return str_r.
c_str();
1061 const char * lp = lhs.
c_str();
1062 const char * rp = rhs.
c_str();
1064 while ( *lp == *rp && *lp !=
'\0' )
1065 { ++lp, ++rp, ++ret; }
1071 const char * lp = lhs.
c_str();
1072 const char * rp = rhs.
c_str();
1074 while ( tolower(*lp) == tolower(*rp) && *lp !=
'\0' )
1075 { ++lp, ++rp, ++ret; }
1082 {
return hasPrefix( str_r, prefix_r ); }
1089 {
return hasSuffix( str_r, prefix_r ); }
1102 #endif // ZYPP_BASE_STRING_H
bool startsWithCI(const C_Str &str_r, const C_Str &prefix_r)
std::string octstring(char n, int w=4)
std::string::size_type commonPrefix(const C_Str &lhs, const C_Str &rhs)
Return size of the common prefix of lhs and rhs.
bool startsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasPrefix
TriBool strToTriBool(const C_Str &str)
Parse str into a bool if it's a legal true or false string; else indterminate.
bool containsCI(const C_Str &str_r, const C_Str &val_r)
Locate substring case insensitive.
std::string hexstring(char n, int w=4)
std::string gsubFun(const std::string &str_r, const std::string &from_r, function< std::string()> to_r)
unsigned splitFieldsEscaped(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=":")
Split line_r into fields handling also escaped separators.
std::string asString() const
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.
std::ostream & operator<<(std::ostream &str, const C_Str &obj)
Stream output.
std::string asUserString(const Tp &val_r)
Request a human readable (translated) string representation of Tp [Tp.asUserString()] Classes may imp...
std::string::size_type size_type
std::string joinEscaped(TIterator begin, TIterator end, const char sep_r=' ')
Join strings using separator sep_r, quoting or escaping the values.
std::string stripSuffixCI(const C_Str &str_r, const C_Str &suffix_r)
std::string binstring(TInt val_r)
String representation of number as bit-string with leading '0's.
bool strToTrue(const C_Str &str)
Parsing boolean from string.
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,...
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Trim
To define how to trim.
std::ostream & autoPrefix0(std::ostream &str, const std::string &text_r, function< std::string()> fnc_r)
std::string strerror(int errno_r)
Return string describing the error_r code.
std::string hexencode(const C_Str &str_r)
Encode all characters other than [a-zA-Z0-9] as XX.
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", const Trim trim_r=NO_TRIM)
Split line_r into words.
const std::ostream & stream() const
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 bEscape(std::string str_r, const C_Str &special_r)
Return str_r with '\'-escaped chars occurring in special_r (and '\').
std::ostream & operator<<(std::ostream &str, const Str &obj)
Stream output.
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
std::string rtrim(const std::string &s)
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).
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.
C_Str(const char *c_str_r)
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
unsigned splitEscaped(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", bool withEmpty=false)
Split line_r into words with respect to escape delimeters.
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::size_type commonPrefixCI(const C_Str &lhs, const C_Str &rhs)
std::string & replaceAllFun(std::string &str_r, const std::string &from_r, function< std::string()> to_r)
Easy-to use interface to the ZYPP dependency resolver.
std::string numstring(char n, int w=0)
std::string join(TIterator begin, TIterator end, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
std::string stripFirstWord(std::string &line, const bool ltrim_first)
std::string escape(const C_Str &str_r, const char sep_r)
Escape desired character c using a backslash.
std::string toLower(const std::string &s)
Return lowercase version of s.
std::string rxEscapeGlob(std::string str_r)
Escape GLOB str_r for use in a regex (not anchored by "^" or "$").
int compareCI(const C_Str &lhs, const C_Str &rhs)
Convenience char* constructible from std::string and char*, it maps (char*)0 to an empty string.
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
std::string rxEscapeStr(std::string str_r)
Escape plain STRING str_r for use in a regex (not anchored by "^" or "$").
std::string asString() const
bool hasPrefixCI(const C_Str &str_r, const C_Str &prefix_r)
bool hasSuffix(const C_Str &str_r, const C_Str &suffix_r)
Return whether str_r has suffix suffix_r.
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)
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.
bool hasSuffixCI(const C_Str &str_r, const C_Str &suffix_r)
bool strToFalse(const C_Str &str)
Return false if str is 0, false, no, off, never.
unsigned splitFields(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=":")
Split line_r into fields.
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.
bool endsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasSuffix
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Str & operator<<(std::ostream &(*iomanip)(std::ostream &))
C_Str(const boost::string_ref &str_r)
const std::string & asString(const std::string &t)
Global asString() that works with std::string too.
bool endsWithCI(const C_Str &str_r, const C_Str &prefix_r)
std::string stripPrefixCI(const C_Str &str_r, const C_Str &prefix_r)
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 hexdecode(const C_Str &str_r)
Decode hexencoded XX sequences.
String related utilities and Regular expression matching.
std::string toUpper(const std::string &s)
Return uppercase version of s.
Str & operator<<(Tp &&val)
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
C_Str(const std::string &str_r)
std::string trim(const std::string &s, const Trim trim_r)
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.
Assert free called for allocated char *.
TInt strtonum(const C_Str &str)
Parsing numbers from string.
const char * c_str() const