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; } }
30 namespace zypp {
typedef boost::logic::tribool
TriBool; }
41 inline format
formatNAC(
const std::string & string_r ) {
42 using namespace boost::io;
43 format fmter( string_r );
44 fmter.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
59 {
return val_r.asUserString(); }
71 MessageString(
const char * str_r ) : std::string( str_r ? str_r :
"" ) {}
125 C_Str(
char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
126 C_Str(
const char * c_str_r ) : _val( c_str_r ), _sze( std::string::npos ) {}
127 C_Str(
const std::string & str_r ) : _val( str_r.c_str() ), _sze( str_r.size() ) {}
128 C_Str(
const boost::string_ref & str_r ) : _val( str_r.data() ), _sze( str_r.size() ) {}
132 bool empty()
const {
return !(_val && *_val); }
135 if ( _sze == std::string::npos )
136 { _sze = _val ? ::strlen( _val ) : 0; }
140 operator const char *()
const {
return c_str(); }
141 const char *
c_str()
const {
return _val ? _val :
""; }
150 {
return str << obj.
c_str(); }
164 inline const std::string &
asString(
const std::string & t )
167 #ifndef SWIG // Swig treats it as syntax error
169 {
return std::move(t); }
180 {
return t.asString(); }
183 inline std::string
asString(
const intrusive_ptr<Tp> &p )
184 {
return p->asString(); }
187 inline std::string
asString(
const weak_ptr<Tp> &p )
188 {
return p->asString(); }
192 {
return t ?
"true" :
"false"; }
196 std::string
form(
const char * format, ... )
197 __attribute__ ((format (printf, 1, 2)));
204 std::
string strerror(
int errno_r );
223 {
return _buf ? std::string(_buf) : std::string(); }
241 { _str << val;
return *
this; }
244 { _str << iomanip;
return *
this; }
246 operator std::string()
const
247 {
return _str.
str(); }
250 {
return _str.str(); }
256 { _str.str( std::string() ); }
262 {
return str << (std::string)obj; }
278 inline std::string
numstring(
char n,
int w = 0 ) {
return form(
"%*hhd", w, n ); }
279 inline std::string
numstring(
unsigned char n,
int w = 0 ) {
return form(
"%*hhu", w, n ); }
280 inline std::string
numstring(
short n,
int w = 0 ) {
return form(
"%*hd", w, n ); }
281 inline std::string
numstring(
unsigned short n,
int w = 0 ) {
return form(
"%*hu", w, n ); }
282 inline std::string
numstring(
int n,
int w = 0 ) {
return form(
"%*d", w, n ); }
283 inline std::string
numstring(
unsigned n,
int w = 0 ) {
return form(
"%*u", w, n ); }
284 inline std::string
numstring(
long n,
int w = 0 ) {
return form(
"%*ld", w, n ); }
285 inline std::string
numstring(
unsigned long n,
int w = 0 ) {
return form(
"%*lu", w, n ); }
286 inline std::string
numstring(
long long n,
int w = 0 ) {
return form(
"%*lld", w, n ); }
287 inline std::string
numstring(
unsigned long long n,
int w = 0 ) {
return form(
"%*llu", w, n ); }
298 template<>
inline std::string
asString(
const unsigned long long & t ) {
return numstring( t ); }
313 inline std::string
hexstring(
char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
314 inline std::string
hexstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
315 inline std::string
hexstring(
short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
316 inline std::string
hexstring(
unsigned short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
317 inline std::string
hexstring(
int n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
318 inline std::string
hexstring(
unsigned n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
319 inline std::string
hexstring(
long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
320 inline std::string
hexstring(
unsigned long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
321 inline std::string
hexstring(
long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
322 inline std::string
hexstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
337 inline std::string
octstring(
char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
338 inline std::string
octstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
339 inline std::string
octstring(
short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
340 inline std::string
octstring(
unsigned short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
341 inline std::string
octstring(
int n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
342 inline std::string
octstring(
unsigned n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
343 inline std::string
octstring(
long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
344 inline std::string
octstring(
unsigned long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
345 inline std::string
octstring(
long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
346 inline std::string
octstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
359 template<
typename TInt>
363 inline short strtonum(
const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
365 inline int strtonum(
const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
367 inline long strtonum(
const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
369 inline long long strtonum(
const C_Str & str ) { return ::strtoll ( str, NULL, 0 ); }
372 inline unsigned short strtonum(
const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
374 inline unsigned strtonum(
const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
376 inline unsigned long strtonum(
const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
378 inline unsigned long long strtonum(
const C_Str & str ) { return ::strtoull( str, NULL, 0 ); }
385 template<
typename TInt>
387 {
return i = strtonum<TInt>( str ); }
413 if (
strToTrue( str ) )
return (return_r =
true);
414 if ( !
strToFalse( str ) )
return (return_r =
false);
426 std::string
gsub(
const std::string & str_r,
const std::string & from_r,
const std::string & to_r );
430 std::string
gsubFun(
const std::string & str_r,
const std::string & from_r,
function<std::string()> to_r );
436 std::string &
replaceAll( std::string & str_r,
const std::string & from_r,
const std::string & to_r );
440 std::string &
replaceAllFun( std::string & str_r,
const std::string & from_r,
function<std::string()> to_r );
456 if ( gap_r && inp_r.size() > gap_r )
458 inp_r.reserve( inp_r.size() + (inp_r.size()-1)/gap_r );
460 inp_r.insert( pos, 1, gapchar );
477 template<
class TOutputIterator>
478 unsigned split(
const C_Str & line_r, TOutputIterator result_r,
const C_Str & sepchars_r =
" \t" )
480 const char * beg = line_r;
481 const char * cur = beg;
483 while ( *cur && ::strchr( sepchars_r, *cur ) )
486 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
489 while( *cur && !::strchr( sepchars_r, *cur ) )
492 *result_r = std::string( beg, cur-beg );
494 while ( *cur && ::strchr( sepchars_r, *cur ) )
536 template<
class TOutputIterator>
537 unsigned splitEscaped(
const C_Str & line_r, TOutputIterator result_r,
const C_Str & sepchars_r =
" \t",
bool withEmpty =
false)
539 const char * beg = line_r;
540 const char * cur = beg;
544 while ( *cur && ::strchr( sepchars_r, *cur ) )
555 if (!*cur && withEmpty)
562 enum class Quote { None, Slash, Single, Double, DoubleSlash };
563 std::vector<char> buf;
564 Quote quoting = Quote::None;
565 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
569 quoting = Quote::None;
576 case '\\': quoting = Quote::Slash;
break;
577 case '\'': quoting = Quote::Single;
break;
578 case '"': quoting = Quote::Double;
break;
579 default: buf.push_back( *cur );
break;
584 buf.push_back( *cur );
585 quoting = Quote::None;
591 case '\'': quoting = Quote::None;
break;
592 default: buf.push_back( *cur );
break;
599 case '\"': quoting = Quote::None;
break;
600 case '\\': quoting = Quote::DoubleSlash;
break;
601 default: buf.push_back( *cur );
break;
605 case Quote::DoubleSlash:
609 case '\\': buf.push_back( *cur );
break;
611 buf.push_back(
'\\' );
612 buf.push_back( *cur );
615 quoting = Quote::Double;
619 }
while ( *cur && ( quoting != Quote::None || !::strchr( sepchars_r, *cur ) ) );
620 *result_r = std::string( buf.begin(), buf.end() );
624 if ( *cur && ::strchr( sepchars_r, *cur ) )
626 while ( *cur && ::strchr( sepchars_r, *cur ) )
636 if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
666 template<
class TOutputIterator>
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 TOutputIterator>
708 return splitEscaped( line_r, result_r, sepchars_r,
true );
717 template <
class TIterator>
718 std::string
join( TIterator begin, TIterator end,
const C_Str & sep_r =
" " )
721 for ( TIterator iter = begin; iter != end; ++ iter )
731 template <
class TContainer>
732 std::string
join(
const TContainer & cont_r,
const C_Str & sep_r =
" " )
733 {
return join( cont_r.begin(), cont_r.end(), sep_r ); }
739 template <
class TIterator>
740 std::string
joinEscaped( TIterator begin, TIterator end,
const char sep_r =
' ' )
742 std::vector<char> buf;
743 for ( TIterator iter = begin; iter != end; ++ iter )
746 buf.push_back( sep_r );
751 buf.push_back(
'"' );
752 buf.push_back(
'"' );
756 std::string toadd(
asString(*iter) );
757 for_( ch, toadd.begin(), toadd.end() )
764 buf.push_back(
'\\' );
765 buf.push_back( *ch );
769 buf.push_back(
'\\' );
770 buf.push_back( *ch );
775 return std::string( buf.begin(), buf.end() );
787 inline std::ostream &
printIndented( std::ostream & str,
const std::string & text_r,
const std::string & indent_r =
" ",
unsigned maxWitdh_r = 0 )
791 if ( indent_r.size() >= maxWitdh_r )
794 maxWitdh_r -= indent_r.size();
797 for (
const char * e = text_r.c_str(), * s = e; *e; s = ++e )
799 for ( ; *e && *e !=
'\n'; ++e ) ;
801 if ( maxWitdh_r && width > maxWitdh_r )
805 for ( e = s+width; e > s && *e !=
' '; --e ) ;
812 str.write( s, width );
820 inline std::ostream &
printIndented( std::ostream & str,
const std::string & text_r,
unsigned indent_r,
char indentch_r =
' ',
unsigned maxWitdh_r = 0 )
821 {
return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
823 inline std::ostream &
printIndented( std::ostream & str,
const std::string & text_r,
unsigned indent_r,
unsigned maxWitdh_r,
char indentch_r =
' ' )
824 {
return printIndented( str, text_r, std::string( indent_r, indentch_r ), maxWitdh_r ); }
829 inline std::ostream &
autoPrefix( std::ostream & str,
const std::string & text_r,
function<std::string(
const char*,
const char*)> fnc_r )
831 for (
const char * e = text_r.c_str(); *e; ++e )
834 for ( ; *e && *e !=
'\n'; ++e ) ;
835 str << fnc_r( s, e );
844 inline std::ostream &
autoPrefix0( std::ostream & str,
const std::string & text_r,
function<std::string()> fnc_r )
846 auto wrap = [&fnc_r](
const char*,
const char* )-> std::string {
861 std::string
escape(
const C_Str & str_r,
const char c =
' ' );
866 if ( ! str_r.empty() )
868 if ( next_r.
empty() )
871 str_r +=
escape( next_r, sep_r );
898 std::string
toLower(
const std::string & s );
901 {
return( s ?
toLower( std::string(s) ) : std::string() ); }
906 std::string
toUpper(
const std::string & s );
909 {
return( s ?
toUpper( std::string(s) ) : std::string() ); }
916 { return ::strcasecmp( lhs, rhs ); }
923 { return ::strstr( str_r, val_r ); }
926 { return ::strcasestr( str_r, val_r ); }
942 std::string
trim(
const std::string & s,
const Trim trim_r =
TRIM );
944 inline std::string
ltrim(
const std::string & s )
947 inline std::string
rtrim(
const std::string & s )
951 std::string
stripFirstWord( std::string & line,
const bool ltrim_first =
true );
953 std::string
stripLastWord( std::string & line,
const bool rtrim_first =
true );
958 std::string
getline( std::istream & str,
bool trim =
false );
963 std::string
getline( std::istream & str,
const Trim trim_r );
972 std::string
receiveUpTo( std::istream & str,
const char delim_r,
bool returnDelim_r =
false );
981 {
return( ::strncmp( str_r, prefix_r, prefix_r.
size() ) == 0 ); }
989 {
return( str_r.
size() >= suffix_r.
size() && ::strncmp( str_r + str_r.
size() - suffix_r.
size() , suffix_r, suffix_r.
size() ) == 0 ); }
995 return std::string( str_r, str_r.
size() - suffix_r.
size() );
996 return str_r.
c_str();
1001 const char * lp = lhs.
c_str();
1002 const char * rp = rhs.
c_str();
1004 while ( *lp == *rp && *lp !=
'\0' )
1005 { ++lp, ++rp, ++ret; }
1011 {
return hasPrefix( str_r, prefix_r ); }
1014 {
return hasSuffix( str_r, prefix_r ); }
1024 #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.
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.
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.
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.
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)
C_Str(const std::string &str_r)
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Trim
To define how to trim.
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.
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
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...
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
std::ostream & operator<<(std::ostream &str, const zypp::sat::detail::CDataiterator *obj)
int compareCI(const C_Str &lhs, const C_Str &rhs)
std::string rtrim(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.
MessageString(const TStr &str_r)
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 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.
C_Str(const char *c_str_r)
TInt strtonum(const C_Str &str, TInt &i)
String to integer type detemined 2nd function arg i.
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::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*...
Str & operator<<(const Tp &val)
std::string strerror(int errno_r)
Return string describing the error_r code.
std::string asUserString(const Tp &val_r)
Request a human readable (translated) string representation of Tp [Tp.asUserString()] Classes may imp...
std::string join(const TContainer &cont_r, const C_Str &sep_r=" ")
Join strings using separator sep_r (defaults to BLANK).
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)
std::string hexdecode(const C_Str &str_r)
Decode hexencoded XX sequences.
std::string numstring(unsigned long long n, int w=0)
unsigned splitFieldsEscaped(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=":")
Split line_r into fields handling also escaped separators.