12 #ifndef ZYPP_BASE_STRING_H
13 #define ZYPP_BASE_STRING_H
34 {
return val_r.asUserString(); }
89 C_Str(
const char * c_str_r ) :
_val( c_str_r ),
_sze( std::string::npos ) {}
97 if (
_sze == std::string::npos )
102 operator const char *()
const {
return c_str(); }
112 {
return str << obj.
c_str(); }
125 inline std::string
asString(
const std::string &t )
136 {
return t.asString(); }
139 inline std::string
asString(
const intrusive_ptr<_T> &p )
140 {
return p->asString(); }
143 inline std::string
asString(
const weak_ptr<_T> &p )
144 {
return p->asString(); }
148 {
return t ?
"+" :
"-"; }
152 std::string
form(
const char * format, ... )
153 __attribute__ ((format (printf, 1, 2)));
160 std::
string strerror(
int errno_r );
179 {
return _buf ? std::string(_buf) : std::string(); }
197 {
_str << val;
return *
this; }
199 operator std::string()
const
200 {
return _str.str(); }
219 inline std::string
numstring(
char n,
int w = 0 ) {
return form(
"%*hhd", w, n ); }
220 inline std::string
numstring(
unsigned char n,
int w = 0 ) {
return form(
"%*hhu", w, n ); }
221 inline std::string
numstring(
short n,
int w = 0 ) {
return form(
"%*hd", w, n ); }
222 inline std::string
numstring(
unsigned short n,
int w = 0 ) {
return form(
"%*hu", w, n ); }
223 inline std::string
numstring(
int n,
int w = 0 ) {
return form(
"%*d", w, n ); }
224 inline std::string
numstring(
unsigned n,
int w = 0 ) {
return form(
"%*u", w, n ); }
225 inline std::string
numstring(
long n,
int w = 0 ) {
return form(
"%*ld", w, n ); }
226 inline std::string
numstring(
unsigned long n,
int w = 0 ) {
return form(
"%*lu", w, n ); }
227 inline std::string
numstring(
long long n,
int w = 0 ) {
return form(
"%*lld", w, n ); }
228 inline std::string
numstring(
unsigned long long n,
int w = 0 ) {
return form(
"%*llu", w, n ); }
239 template<>
inline std::string
asString(
const unsigned long long & t ) {
return numstring( t ); }
254 inline std::string
hexstring(
char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
255 inline std::string
hexstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
256 inline std::string
hexstring(
short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
257 inline std::string
hexstring(
unsigned short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
258 inline std::string
hexstring(
int n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
259 inline std::string
hexstring(
unsigned n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
260 inline std::string
hexstring(
long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
261 inline std::string
hexstring(
unsigned long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
262 inline std::string
hexstring(
long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
263 inline std::string
hexstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
278 inline std::string
octstring(
char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
279 inline std::string
octstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
280 inline std::string
octstring(
short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
281 inline std::string
octstring(
unsigned short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
282 inline std::string
octstring(
int n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
283 inline std::string
octstring(
unsigned n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
284 inline std::string
octstring(
long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
285 inline std::string
octstring(
unsigned long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
286 inline std::string
octstring(
long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
287 inline std::string
octstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
300 template<
typename _It>
304 inline short strtonum(
const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
306 inline int strtonum(
const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
308 inline long strtonum(
const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
310 inline long long strtonum(
const C_Str & str ) { return ::strtoll ( str, NULL, 0 ); }
313 inline unsigned short strtonum(
const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
315 inline unsigned strtonum(
const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
317 inline unsigned long strtonum(
const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
319 inline unsigned long long strtonum(
const C_Str & str ) { return ::strtoull( str, NULL, 0 ); }
326 template<
typename _It>
328 {
return i = strtonum<_It>( str ); }
354 if (
strToTrue( str ) )
return (return_r =
true);
355 if ( !
strToFalse( str ) )
return (return_r =
false);
364 std::string
gsub(
const std::string & str_r,
const std::string & from_r,
const std::string & to_r );
368 std::string
gsubFun(
const std::string & str_r,
const std::string & from_r,
function<std::string()> to_r );
374 std::string &
replaceAll( std::string & str_r,
const std::string & from_r,
const std::string & to_r );
378 std::string &
replaceAllFun( std::string & str_r,
const std::string & from_r,
function<std::string()> to_r );
393 template<
class _OutputIterator>
395 _OutputIterator result_r,
396 const C_Str & sepchars_r =
" \t" )
398 const char * beg = line_r;
399 const char * cur = beg;
401 while ( *cur && ::strchr( sepchars_r, *cur ) )
404 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
407 while( *cur && !::strchr( sepchars_r, *cur ) )
410 *result_r = std::string( beg, cur-beg );
412 while ( *cur && ::strchr( sepchars_r, *cur ) )
445 template<
class _OutputIterator>
447 _OutputIterator result_r,
448 const C_Str & sepchars_r =
" \t",
449 bool withEmpty =
false)
451 const char * beg = line_r;
452 const char * cur = beg;
456 while ( *cur && ::strchr( sepchars_r, *cur ) )
467 if (!*cur && withEmpty)
474 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
476 if ( *cur ==
'"' || *cur ==
'\'' )
478 char closeChar = *cur;
483 while ( *cur && *cur != closeChar)
490 const char * esc = cur-1;
491 while ( esc != beg && *esc ==
'\\' )
496 cont = (escCount % 2 == 1);
500 std::string s( beg+1, cur-beg-2 );
504 char tmpn[2] = { closeChar, 0 };
505 char tmpo[3] = {
'\\', closeChar, 0 };
513 while( *cur && !::strchr( sepchars_r, *cur ) )
523 std::string s( beg, cur-beg );
527 const char *delimeter = sepchars_r;
530 std::string ds(
"\\");
531 const char tmp[2] = { *delimeter,
'\0' };
532 std::string del(tmp);
541 if ( *cur && ::strchr( sepchars_r, *cur ) )
543 while ( *cur && ::strchr( sepchars_r, *cur ) )
553 if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
582 template<
class _OutputIterator>
584 _OutputIterator result_r,
585 const C_Str & sepchars_r =
":" )
587 const char * beg = line_r;
588 const char * cur = beg;
590 for ( beg = cur; *beg; beg = cur, ++result_r )
593 while( *cur && !::strchr( sepchars_r, *cur ) )
596 *result_r = std::string( beg, cur-beg );
604 *result_r = std::string();
619 template<
class _OutputIterator>
621 _OutputIterator result_r,
622 const C_Str & sepchars_r =
":" )
634 template <
class _Iterator>
635 std::string
join( _Iterator begin, _Iterator end,
636 const C_Str & sep_r =
" " )
639 for ( _Iterator iter = begin; iter != end; ++ iter )
649 template <
class _Container>
650 std::string
join(
const _Container & cont_r,
651 const C_Str & sep_r =
" " )
652 {
return join( cont_r.begin(), cont_r.end(), sep_r ); }
658 template <
class _Iterator>
660 const char sep_r =
' ' )
662 std::vector<char> buf;
663 for ( _Iterator iter = begin; iter != end; ++ iter )
666 buf.push_back( sep_r );
671 buf.push_back(
'"' );
672 buf.push_back(
'"' );
676 std::string toadd(
asString(*iter) );
677 for_( ch, toadd.begin(), toadd.end() )
684 buf.push_back(
'\\' );
685 buf.push_back( *ch );
689 buf.push_back(
'\\' );
690 buf.push_back( *ch );
695 return std::string( buf.begin(), buf.end() );
709 std::string
escape(
const C_Str & str_r,
const char c =
' ' );
714 if ( ! str_r.empty() )
716 if ( next_r.
empty() )
719 str_r +=
escape( next_r, sep_r );
746 std::string
toLower(
const std::string & s );
749 {
return( s ?
toLower( std::string(s) ) : std::string() ); }
754 std::string
toUpper(
const std::string & s );
757 {
return( s ?
toUpper( std::string(s) ) : std::string() ); }
764 { return ::strcasecmp( lhs, rhs ); }
771 { return ::strstr( str_r, val_r ); }
774 { return ::strcasestr( str_r, val_r ); }
790 std::string
trim(
const std::string & s,
const Trim trim_r =
TRIM );
792 inline std::string
ltrim(
const std::string & s )
795 inline std::string
rtrim(
const std::string & s )
799 std::string
stripFirstWord( std::string & line,
const bool ltrim_first );
801 std::string
stripLastWord( std::string & line,
const bool rtrim_first );
806 std::string
getline( std::istream & str,
bool trim =
false );
811 std::string
getline( std::istream & str,
const Trim trim_r );
820 std::string
receiveUpTo( std::istream & str,
const char delim_r,
bool returnDelim_r =
false );
829 {
return( ::strncmp( str_r, prefix_r, prefix_r.
size() ) == 0 ); }
837 {
return( str_r.
size() >= suffix_r.
size() && ::strncmp( str_r + str_r.
size() - suffix_r.
size() , suffix_r, suffix_r.
size() ) == 0 ); }
843 return std::string( str_r, str_r.
size() - suffix_r.
size() );
844 return str_r.
c_str();
849 const char * lp = lhs.
c_str();
850 const char * rp = rhs.
c_str();
852 while ( *lp == *rp && *lp !=
'\0' )
853 { ++lp, ++rp, ++ret; }
870 #endif // ZYPP_BASE_STRING_H
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::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
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.
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)
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 stripFirstWord(std::string &line, const bool ltrim_first)
Convenient building of std::string via std::ostream::operator<<.
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 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.
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)
Str & operator<<(const _Tp &val)
bool strToTrue(const C_Str &str)
Parsing boolean from string.
std::string form(const char *format,...)
Printf style construction of std::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.
std::ostream & operator<<(std::ostream &str, const C_Str &obj)
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.