39 std::string
form(
const char * format, ... )
44 va_start( ap, format );
45 vasprintf( &safe.
_buf, format, ap );
100 return indeterminate;
108 inline bool heIsAlNum(
char ch )
110 return ( (
'a' <= ch && ch <=
'z' )
111 ||(
'A' <= ch && ch <=
'Z' )
112 ||(
'0' <= ch && ch <=
'9' ) );
115 inline int heDecodeCh(
char ch )
117 if (
'0' <= ch && ch <=
'9' )
119 if (
'A' <= ch && ch <=
'F' )
120 return( ch -
'A' + 10 );
121 if (
'a' <= ch && ch <=
'f' )
122 return( ch -
'a' + 10 );
129 static const char *
const hdig =
"0123456789ABCDEF";
131 res.reserve( str_r.
size() );
132 for (
const char * it = str_r.
c_str(); *it; ++it )
134 if ( heIsAlNum( *it ) )
141 res += hdig[(
unsigned char)(*it)/16];
142 res += hdig[(
unsigned char)(*it)%16];
151 res.reserve( str_r.
size() );
156 int d1 = heDecodeCh( *(it+1) );
159 int d2 = heDecodeCh( *(it+2) );
181 {
return toLower( std::string(s) ); }
185 std::string ret( std::move(s) );
190 for ( std::string::size_type i = 0; i < ret.length(); ++i )
192 if ( isupper( ret[i] ) )
193 ret[i] =
static_cast<char>(tolower( ret[i] ));
204 {
return toUpper( std::string(s) ); }
208 std::string ret( std::move(s) );
213 for ( std::string::size_type i = 0; i < ret.length(); ++i )
215 if ( islower( ret[i] ) )
216 ret[i] =
static_cast<char>(toupper( ret[i] ));
226 std::string
trim(
const std::string & s,
const Trim trim_r )
227 {
return trim( std::string(s), trim_r ); }
229 std::string
trim( std::string && s,
const Trim trim_r )
231 std::string ret( std::move(s) );
233 if ( ret.empty() || trim_r ==
NO_TRIM )
238 std::string::size_type p = ret.find_first_not_of(
" \t\n" );
239 if ( p == std::string::npos )
249 std::string::size_type p = ret.find_last_not_of(
" \t\n" );
250 if ( p == std::string::npos )
255 ret = ret.erase( p+1 );
269 line =
ltrim( line );
275 std::string::size_type p = line.find_first_of(
" \t" );
277 if ( p == std::string::npos ) {
281 }
else if ( p == 0 ) {
284 line =
ltrim( line );
288 ret = line.substr( 0, p );
289 line =
ltrim( line.erase( 0, p ) );
302 line =
rtrim( line );
308 std::string::size_type p = line.find_last_of(
" \t" );
310 if ( p == std::string::npos ) {
314 }
else if ( p == line.size()-1 ) {
317 line =
rtrim( line );
321 ret = line.substr( p+1 );
322 line =
rtrim( line.erase( p ) );
327 std::string
gsub(
const std::string & str_r,
const std::string & from_r,
const std::string & to_r )
329 std::string ret( str_r );
333 std::string &
replaceAll( std::string & str_r,
const std::string & from_r,
const std::string & to_r )
335 if ( ! from_r.empty() )
337 std::string::size_type pos = 0;
338 while ( (pos = str_r.find( from_r, pos )) != std::string::npos )
340 str_r.replace( pos, from_r.size(), to_r );
343 if ( pos >= str_r.length() )
350 std::string
gsubFun(
const std::string & str_r,
const std::string & from_r, function<std::string()> to_r )
352 std::string ret( str_r );
356 std::string &
replaceAllFun( std::string & str_r,
const std::string & from_r,
const function<std::string()>& to_r )
358 if ( ! from_r.empty() )
360 std::string::size_type pos = 0;
361 while ( (pos = str_r.find( from_r, pos )) != std::string::npos )
363 std::string to( to_r() );
364 str_r.replace( pos, from_r.size(), to );
367 if ( pos >= str_r.length() )
376 std::vector<char> buf;
384 buf.push_back(
'\\' );
389 buf.push_back(
'\\' );
393 return std::string( buf.begin(), buf.end() );
402 if ( str_r.find_first_of( special_r ) == std::string::npos
403 && ( ::strchr( special_r.
c_str(),
'\\' ) || !::strchr( str_r.c_str(),
'\\' ) ) )
407 for_( s, str_r.c_str(), s+str_r.size() )
409 if ( *s ==
'\\' || ::strchr( special_r.
c_str(), *s ) )
416 #define RXSPECIALCHARS "\\.*+?^$[()|{"
432 for_( s, str_r.c_str(), s+str_r.size() )
437 if ( *(s+1) ) { ++s; buf << *s; }
439 else if ( *s ==
'?' )
443 else if ( *s ==
'*' )
447 else if ( *s ==
'[' )
449 const char * e = s+1;
450 if ( *e ==
'^' || *e ==
'!' )
454 while ( *e && *e !=
']' )
458 ++s; buf <<
'[' << (*s ==
'!' ?
'^' : *s );
491 std::string
receiveUpTo( std::istream &
str,
const char delim_r,
bool returnDelim_r )
493 std::ostringstream datas;
512 if (
str.eof() && datas.tellp() )
513 str.clear( std::ios::eofbit );
522 return g_utf8_validate (
str.data(),
str.length (),
nullptr );
527 char outbuf[6] = {
'\0'};
528 auto bytes = g_unichar_to_utf8( unichar, outbuf );
532 return std::string( outbuf, bytes );
537 auto codepoint = hexCharToValue<uint32_t>(hexChar);
Convenience char* constructible from std::string and char*, it maps (char*)0 to an empty string.
const char * c_str() const
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
String related utilities and Regular expression matching.
String related utilities and Regular expression matching.
std::string stripLastWord(std::string &line, const bool rtrim_first)
std::string hexdecode(const C_Str &str_r)
Decode hexencoded XX sequences.
Trim
To define how to trim.
std::string hexencode(const C_Str &str_r)
Encode all characters other than [a-zA-Z0-9] as XX.
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 hexCodepointToUtf8String(std::string_view hexChar)
std::string gsubFun(const std::string &str_r, const std::string &from_r, function< std::string()> to_r)
bool strToTrue(const C_Str &str)
Parsing boolean from string.
TriBool strToTriBool(const C_Str &str)
Parse str into a bool if it's a legal true or false string; else indeterminate.
std::string codepointToUtf8String(uint32_t unichar)
std::string rxEscapeGlob(std::string str_r)
Escape GLOB str_r for use in a regex (not anchored by "^" or "$").
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 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,...
std::string rtrim(const std::string &s)
std::string strerror(int errno_r)
Return string describing the error_r code.
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::string toUpper(const std::string &s)
Return uppercase version of s.
std::string ltrim(const std::string &s)
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
std::string bEscape(std::string str_r, const C_Str &special_r)
Return str_r with '\'-escaped chars occurring in special_r (and '\').
std::string & replaceAllFun(std::string &str_r, const std::string &from_r, const function< std::string()> &to_r)
std::string rxEscapeStr(std::string str_r)
Escape plain STRING str_r for use in a regex (not anchored by "^" or "$").
std::string getline(std::istream &str, const Trim trim_r)
Return stream content up to (but not returning) the next newline.
bool validateUtf8(std::string_view str)
TInt strtonum(const C_Str &str)
Parsing numbers from string.
bool strToFalse(const C_Str &str)
Return false if str is 0, false, no, off, never.
std::string trim(const std::string &s, const Trim trim_r)
Easy-to use interface to the ZYPP dependency resolver.
Assert free called for allocated char *.
std::string asString() const
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
#define for_(IT, BEG, END)
Convenient for-loops using iterator.