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>
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 )
527 template<
class TOutputIterator>
530 const char * beg = line_r;
531 const char * cur = beg;
533 while ( *cur && ::strchr( sepchars_r, *cur ) )
536 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
539 while( *cur && !::strchr( sepchars_r, *cur ) )
542 *result_r =
trim( std::string( beg, cur-beg ), trim_r );
544 while ( *cur && ::strchr( sepchars_r, *cur ) )
550 template<
class TOutputIterator>
551 unsigned split(
const C_Str & line_r, TOutputIterator result_r,
const Trim trim_r )
552 {
return split( line_r, result_r,
" \t", trim_r ); }
591 template<
class TOutputIterator>
592 unsigned splitEscaped(
const C_Str & line_r, TOutputIterator result_r,
const C_Str & sepchars_r =
" \t",
bool withEmpty =
false)
594 const char * beg = line_r;
595 const char * cur = beg;
599 while ( *cur && ::strchr( sepchars_r, *cur ) )
610 if (!*cur && withEmpty)
617 enum class Quote { None, Slash, Single, Double, DoubleSlash };
618 std::vector<char> buf;
619 Quote quoting = Quote::None;
620 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
624 quoting = Quote::None;
631 case '\\': quoting = Quote::Slash;
break;
632 case '\'': quoting = Quote::Single;
break;
633 case '"': quoting = Quote::Double;
break;
634 default: buf.push_back( *cur );
break;
639 buf.push_back( *cur );
640 quoting = Quote::None;
646 case '\'': quoting = Quote::None;
break;
647 default: buf.push_back( *cur );
break;
654 case '\"': quoting = Quote::None;
break;
655 case '\\': quoting = Quote::DoubleSlash;
break;
656 default: buf.push_back( *cur );
break;
660 case Quote::DoubleSlash:
664 case '\\': buf.push_back( *cur );
break;
666 buf.push_back(
'\\' );
667 buf.push_back( *cur );
670 quoting = Quote::Double;
674 }
while ( *cur && ( quoting != Quote::None || !::strchr( sepchars_r, *cur ) ) );
675 *result_r = std::string( buf.begin(), buf.end() );
679 if ( *cur && ::strchr( sepchars_r, *cur ) )
681 while ( *cur && ::strchr( sepchars_r, *cur ) )
691 if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
721 template<
class TOutputIterator>
724 const char * beg = line_r;
725 const char * cur = beg;
727 for ( beg = cur; *beg; beg = cur, ++result_r )
730 while( *cur && !::strchr( sepchars_r, *cur ) )
732 if ( *cur ==
'\\' && *(cur+1) )
737 *result_r = std::string( beg, cur-beg );
745 *result_r = std::string();
760 template<
class TOutputIterator>
763 return splitEscaped( line_r, result_r, sepchars_r,
true );
772 template <
class TIterator>
773 std::string
join( TIterator begin, TIterator end,
const C_Str & sep_r =
" " )
776 for ( TIterator iter = begin; iter != end; ++ iter )
786 template <
class TContainer>
787 std::string
join(
const TContainer & cont_r,
const C_Str & sep_r =
" " )
788 {
return join( cont_r.begin(), cont_r.end(), sep_r ); }
794 template <
class TIterator>
795 std::string
joinEscaped( TIterator begin, TIterator end,
const char sep_r =
' ' )
797 std::vector<char> buf;
798 for ( TIterator iter = begin; iter != end; ++ iter )
801 buf.push_back( sep_r );
806 buf.push_back(
'"' );
807 buf.push_back(
'"' );
811 std::string toadd(
asString(*iter) );
812 for_( ch, toadd.begin(), toadd.end() )
819 buf.push_back(
'\\' );
820 buf.push_back( *ch );
824 buf.push_back(
'\\' );
825 buf.push_back( *ch );
830 return std::string( buf.begin(), buf.end() );
842 inline std::ostream &
printIndented( std::ostream &
str,
const std::string & text_r,
const std::string & indent_r =
" ",
unsigned maxWitdh_r = 0 )
846 if ( indent_r.size() >= maxWitdh_r )
849 maxWitdh_r -= indent_r.size();
852 for (
const char * e = text_r.c_str(), * s = e; *e; s = ++e )
854 for ( ; *e && *e !=
'\n'; ++e ) ;
856 if ( maxWitdh_r && width > maxWitdh_r )
860 for ( e = s+width; e > s && *e !=
' '; --e ) ;
867 str.write( s, width );
875 inline std::ostream &
printIndented( std::ostream &
str,
const std::string & text_r,
unsigned indent_r,
char indentch_r =
' ',
unsigned maxWitdh_r = 0 )
876 {
return printIndented(
str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
878 inline std::ostream &
printIndented( std::ostream &
str,
const std::string & text_r,
unsigned indent_r,
unsigned maxWitdh_r,
char indentch_r =
' ' )
879 {
return printIndented(
str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
884 inline std::ostream &
autoPrefix( std::ostream &
str,
const std::string & text_r,
function<std::string(
const char*,
const char*)> fnc_r )
886 for (
const char * e = text_r.c_str(); *e; ++e )
889 for ( ; *e && *e !=
'\n'; ++e ) ;
890 str << fnc_r( s, e );
899 inline std::ostream &
autoPrefix0( std::ostream &
str,
const std::string & text_r,
function<std::string()> fnc_r )
901 auto wrap = [&fnc_r](
const char*,
const char* )-> std::string {
916 std::string
escape(
const C_Str & str_r,
const char c =
' ' );
921 if ( ! str_r.empty() )
923 if ( next_r.
empty() )
926 str_r +=
escape( next_r, sep_r );
930 std::string
bEscape( std::string str_r,
const C_Str & special_r );
962 std::string
toLower(
const std::string & s );
963 std::string
toLower( std::string && s );
966 {
return( s ?
toLower( std::string(s) ) : std::string() ); }
971 std::string
toUpper(
const std::string & s );
972 std::string
toUpper( std::string && s );
975 {
return( s ?
toUpper( std::string(s) ) : std::string() ); }
982 { return ::strcasecmp( lhs, rhs ); }
989 { return ::strstr( str_r, val_r ); }
992 { return ::strcasestr( str_r, val_r ); }
995 std::string
stripFirstWord( std::string & line,
const bool ltrim_first =
true );
997 std::string
stripLastWord( std::string & line,
const bool rtrim_first =
true );
1016 std::string
receiveUpTo( std::istream &
str,
const char delim_r,
bool returnDelim_r =
false );
1025 {
return( ::strncmp( str_r, prefix_r, prefix_r.
size() ) == 0 ); }
1028 {
return( ::strncasecmp( str_r, prefix_r, prefix_r.
size() ) == 0 ); }
1032 {
return(
hasPrefix( str_r, prefix_r ) ? str_r + prefix_r.
size() : str_r.
c_str() ); }
1039 {
return( str_r.
size() >= suffix_r.
size() && ::strncmp( str_r + str_r.
size() - suffix_r.
size() , suffix_r, suffix_r.
size() ) == 0 ); }
1042 {
return( str_r.
size() >= suffix_r.
size() && ::strncasecmp( str_r + str_r.
size() - suffix_r.
size() , suffix_r, suffix_r.
size() ) == 0 ); }
1048 return std::string( str_r, str_r.
size() - suffix_r.
size() );
1049 return str_r.
c_str();
1055 return std::string( str_r, str_r.
size() - suffix_r.
size() );
1056 return str_r.
c_str();
1062 const char * lp = lhs.
c_str();
1063 const char * rp = rhs.
c_str();
1065 while ( *lp == *rp && *lp !=
'\0' )
1066 { ++lp, ++rp, ++ret; }
1072 const char * lp = lhs.
c_str();
1073 const char * rp = rhs.
c_str();
1075 while ( tolower(*lp) == tolower(*rp) && *lp !=
'\0' )
1076 { ++lp, ++rp, ++ret; }
1083 {
return hasPrefix( str_r, prefix_r ); }
1090 {
return hasSuffix( str_r, prefix_r ); }
1103 #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.
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
std::string asString() const
unsigned splitEscaped(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \, bool withEmpty=false)
Split line_r into words with respect to escape delimeters.
std::string octstring(char n, int w=4)
C_Str(const boost::string_ref &str_r)
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.
bool hasPrefixCI(const C_Str &str_r, const C_Str &prefix_r)
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 join(TIterator begin, TIterator end, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
bool hasSuffixCI(const C_Str &str_r, const C_Str &suffix_r)
bool startsWithCI(const C_Str &str_r, const C_Str &prefix_r)
std::string stripSuffixCI(const C_Str &str_r, const C_Str &suffix_r)
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 stripSuffix(const C_Str &str_r, const C_Str &suffix_r)
Strip a suffix_r from str_r and return the resulting string.
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)
const std::string & asString(const std::string &t)
Global asString() that works with std::string too.
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
const char * c_str() const
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Trim
To define how to trim.
const std::ostream & stream() const
std::string ltrim(const std::string &s)
std::string joinEscaped(TIterator begin, TIterator end, const char sep_r=' ')
Join strings using separator sep_r, quoting or escaping the values.
bool endsWithCI(const C_Str &str_r, const C_Str &prefix_r)
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 asString() const
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.
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \, const Trim trim_r=NO_TRIM)
Split line_r into words.
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 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)
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 getline(std::istream &str, const Trim trim_r)
Return stream content up to (but not returning) the next newline.
TInt strtonum(const C_Str &str)
Parsing numbers from string.
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, never.
bool containsCI(const C_Str &str_r, const C_Str &val_r)
Locate substring case insensitive.
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)
Str & operator<<(Tp &&val)
std::ostream & operator<<(std::ostream &str, const Str &obj)
bool strToTrue(const C_Str &str)
Parsing boolean from string.
unsigned splitFields(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=":")
Split line_r into fields.
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.
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 &))
std::string binstring(TInt val_r)
String representation of number as bit-string with leading '0's.
std::ostream & operator<<(std::ostream &str, const C_Str &obj)
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 strerror(int errno_r)
Return string describing the error_r code.
Easy-to use interface to the ZYPP dependency resolver.
std::string toUpper(const std::string &s)
Return uppercase version of s.
std::string asUserString(const Tp &val_r)
Request a human readable (translated) string representation of Tp [Tp.asUserString()] Classes may imp...
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)
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.
std::string hexdecode(const C_Str &str_r)
Decode hexencoded XX sequences.
std::string::size_type commonPrefixCI(const C_Str &lhs, const C_Str &rhs)
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 gapify(std::string inp_r, std::string::size_type gap_r=1, char gapchar=' ')
Enhance readability: insert gaps at regular distance.