12 #ifndef ZYPP_BASE_STRING_H
13 #define ZYPP_BASE_STRING_H
76 C_Str(
const char * c_str_r ) :
_val( c_str_r ),
_sze( std::string::npos ) {}
84 if (
_sze == std::string::npos )
89 operator const char *()
const {
return c_str(); }
99 {
return str << obj.
c_str(); }
112 inline std::string
asString(
const std::string &t )
123 {
return t.asString(); }
126 inline std::string
asString(
const intrusive_ptr<_T> &p )
127 {
return p->asString(); }
130 inline std::string
asString(
const weak_ptr<_T> &p )
131 {
return p->asString(); }
135 {
return t ?
"+" :
"-"; }
139 std::string
form(
const char * format, ... )
140 __attribute__ ((format (printf, 1, 2)));
147 std::
string strerror(
int errno_r );
166 {
return _buf ? std::string(_buf) : std::string(); }
184 {
_str << val;
return *
this; }
186 operator std::string()
const
187 {
return _str.str(); }
206 inline std::string
numstring(
char n,
int w = 0 ) {
return form(
"%*hhd", w, n ); }
207 inline std::string
numstring(
unsigned char n,
int w = 0 ) {
return form(
"%*hhu", w, n ); }
208 inline std::string
numstring(
short n,
int w = 0 ) {
return form(
"%*hd", w, n ); }
209 inline std::string
numstring(
unsigned short n,
int w = 0 ) {
return form(
"%*hu", w, n ); }
210 inline std::string
numstring(
int n,
int w = 0 ) {
return form(
"%*d", w, n ); }
211 inline std::string
numstring(
unsigned n,
int w = 0 ) {
return form(
"%*u", w, n ); }
212 inline std::string
numstring(
long n,
int w = 0 ) {
return form(
"%*ld", w, n ); }
213 inline std::string
numstring(
unsigned long n,
int w = 0 ) {
return form(
"%*lu", w, n ); }
214 inline std::string
numstring(
long long n,
int w = 0 ) {
return form(
"%*lld", w, n ); }
215 inline std::string
numstring(
unsigned long long n,
int w = 0 ) {
return form(
"%*llu", w, n ); }
226 template<>
inline std::string
asString(
const unsigned long long & t ) {
return numstring( t ); }
241 inline std::string
hexstring(
char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
242 inline std::string
hexstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
243 inline std::string
hexstring(
short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
244 inline std::string
hexstring(
unsigned short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
245 inline std::string
hexstring(
int n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
246 inline std::string
hexstring(
unsigned n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
247 inline std::string
hexstring(
long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
248 inline std::string
hexstring(
unsigned long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
249 inline std::string
hexstring(
long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
250 inline std::string
hexstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
265 inline std::string
octstring(
char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
266 inline std::string
octstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
267 inline std::string
octstring(
short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
268 inline std::string
octstring(
unsigned short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
269 inline std::string
octstring(
int n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
270 inline std::string
octstring(
unsigned n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
271 inline std::string
octstring(
long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
272 inline std::string
octstring(
unsigned long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
273 inline std::string
octstring(
long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
274 inline std::string
octstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
287 template<
typename _It>
291 inline short strtonum(
const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
293 inline int strtonum(
const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
295 inline long strtonum(
const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
297 inline long long strtonum(
const C_Str & str ) { return ::strtoll ( str, NULL, 0 ); }
300 inline unsigned short strtonum(
const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
302 inline unsigned strtonum(
const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
304 inline unsigned long strtonum(
const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
306 inline unsigned long long strtonum(
const C_Str & str ) { return ::strtoull( str, NULL, 0 ); }
313 template<
typename _It>
315 {
return i = strtonum<_It>( str ); }
341 if (
strToTrue( str ) )
return (return_r =
true);
342 if ( !
strToFalse( str ) )
return (return_r =
false);
351 std::string
gsub(
const std::string & str_r,
const std::string & from_r,
const std::string & to_r );
355 std::string
gsubFun(
const std::string & str_r,
const std::string & from_r,
function<std::string()> to_r );
361 std::string &
replaceAll( std::string & str_r,
const std::string & from_r,
const std::string & to_r );
365 std::string &
replaceAllFun( std::string & str_r,
const std::string & from_r,
function<std::string()> to_r );
380 template<
class _OutputIterator>
382 _OutputIterator result_r,
383 const C_Str & sepchars_r =
" \t" )
385 const char * beg = line_r;
386 const char * cur = beg;
388 while ( *cur && ::strchr( sepchars_r, *cur ) )
391 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
394 while( *cur && !::strchr( sepchars_r, *cur ) )
397 *result_r = std::string( beg, cur-beg );
399 while ( *cur && ::strchr( sepchars_r, *cur ) )
432 template<
class _OutputIterator>
434 _OutputIterator result_r,
435 const C_Str & sepchars_r =
" \t",
436 bool withEmpty =
false)
438 const char * beg = line_r;
439 const char * cur = beg;
443 while ( *cur && ::strchr( sepchars_r, *cur ) )
454 if (!*cur && withEmpty)
461 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
463 if ( *cur ==
'"' || *cur ==
'\'' )
465 char closeChar = *cur;
470 while ( *cur && *cur != closeChar)
477 const char * esc = cur-1;
478 while ( esc != beg && *esc ==
'\\' )
483 cont = (escCount % 2 == 1);
487 std::string s( beg+1, cur-beg-2 );
491 char tmpn[2] = { closeChar, 0 };
492 char tmpo[3] = {
'\\', closeChar, 0 };
500 while( *cur && !::strchr( sepchars_r, *cur ) )
510 std::string s( beg, cur-beg );
514 const char *delimeter = sepchars_r;
517 std::string ds(
"\\");
518 const char tmp[2] = { *delimeter,
'\0' };
519 std::string del(tmp);
528 if ( *cur && ::strchr( sepchars_r, *cur ) )
530 while ( *cur && ::strchr( sepchars_r, *cur ) )
540 if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
569 template<
class _OutputIterator>
571 _OutputIterator result_r,
572 const C_Str & sepchars_r =
":" )
574 const char * beg = line_r;
575 const char * cur = beg;
577 for ( beg = cur; *beg; beg = cur, ++result_r )
580 while( *cur && !::strchr( sepchars_r, *cur ) )
583 *result_r = std::string( beg, cur-beg );
591 *result_r = std::string();
606 template<
class _OutputIterator>
608 _OutputIterator result_r,
609 const C_Str & sepchars_r =
":" )
621 template <
class _Iterator>
622 std::string
join( _Iterator begin, _Iterator end,
623 const C_Str & sep_r =
" " )
626 for ( _Iterator iter = begin; iter != end; ++ iter )
636 template <
class _Container>
637 std::string
join(
const _Container & cont_r,
638 const C_Str & sep_r =
" " )
639 {
return join( cont_r.begin(), cont_r.end(), sep_r ); }
645 template <
class _Iterator>
647 const char sep_r =
' ' )
649 std::vector<char> buf;
650 for ( _Iterator iter = begin; iter != end; ++ iter )
653 buf.push_back( sep_r );
658 buf.push_back(
'"' );
659 buf.push_back(
'"' );
663 std::string toadd(
asString(*iter) );
664 for_( ch, toadd.begin(), toadd.end() )
671 buf.push_back(
'\\' );
672 buf.push_back( *ch );
676 buf.push_back(
'\\' );
677 buf.push_back( *ch );
682 return std::string( buf.begin(), buf.end() );
696 std::string
escape(
const C_Str & str_r,
const char c =
' ' );
701 if ( ! str_r.empty() )
703 if ( next_r.
empty() )
706 str_r +=
escape( next_r, sep_r );
733 std::string
toLower(
const std::string & s );
736 {
return( s ?
toLower( std::string(s) ) : std::string() ); }
741 std::string
toUpper(
const std::string & s );
744 {
return( s ?
toUpper( std::string(s) ) : std::string() ); }
751 { return ::strcasecmp( lhs, rhs ); }
758 { return ::strstr( str_r, val_r ); }
761 { return ::strcasestr( str_r, val_r ); }
777 std::string
trim(
const std::string & s,
const Trim trim_r =
TRIM );
779 inline std::string
ltrim(
const std::string & s )
782 inline std::string
rtrim(
const std::string & s )
786 std::string
stripFirstWord( std::string & line,
const bool ltrim_first );
788 std::string
stripLastWord( std::string & line,
const bool rtrim_first );
793 std::string
getline( std::istream & str,
bool trim =
false );
798 std::string
getline( std::istream & str,
const Trim trim_r );
807 std::string
receiveUpTo( std::istream & str,
const char delim_r,
bool returnDelim_r =
false );
816 {
return( ::strncmp( str_r, prefix_r, prefix_r.
size() ) == 0 ); }
824 {
return( str_r.
size() >= suffix_r.
size() && ::strncmp( str_r + str_r.
size() - suffix_r.
size() , suffix_r, suffix_r.
size() ) == 0 ); }
830 return std::string( str_r, str_r.
size() - suffix_r.
size() );
831 return str_r.
c_str();
836 const char * lp = lhs.
c_str();
837 const char * rp = rhs.
c_str();
839 while ( *lp == *rp && *lp !=
'\0' )
840 { ++lp, ++rp, ++ret; }
857 #endif // ZYPP_BASE_STRING_H