36 std::string
form(
const char * format, ... )
41 va_start( ap, format );
42 vasprintf( &safe.
_buf, format, ap );
65 std::string t(
toLower( str ) );
71 || strtonum<long long>( str )
82 std::string t(
toLower( str ) );
103 inline bool heIsAlNum(
char ch )
105 return ( (
'a' <= ch && ch <=
'z' )
106 ||(
'A' <= ch && ch <=
'Z' )
107 ||(
'0' <= ch && ch <=
'9' ) );
110 inline int heDecodeCh(
char ch )
112 if (
'0' <= ch && ch <=
'9' )
114 if (
'A' <= ch && ch <=
'F' )
115 return( ch -
'A' + 10 );
116 if (
'a' <= ch && ch <=
'f' )
117 return( ch -
'a' + 10 );
124 static const char *
const hdig =
"0123456789ABCDEF";
126 res.reserve( str_r.
size() );
127 for (
const char * it = str_r.
c_str(); *it; ++it )
129 if ( heIsAlNum( *it ) )
136 res += hdig[(
unsigned char)(*it)/16];
137 res += hdig[(
unsigned char)(*it)%16];
146 res.reserve( str_r.
size() );
151 int d1 = heDecodeCh( *(it+1) );
154 int d2 = heDecodeCh( *(it+2) );
176 {
return toLower( std::string(s) ); }
180 std::string ret( std::move(s) );
187 if ( isupper( ret[i] ) )
188 ret[i] = static_cast<char>(tolower( ret[i] ));
199 {
return toUpper( std::string(s) ); }
203 std::string ret( std::move(s) );
210 if ( islower( ret[i] ) )
211 ret[i] = static_cast<char>(toupper( ret[i] ));
221 std::string
trim(
const std::string & s,
const Trim trim_r )
222 {
return trim( std::string(s), trim_r ); }
224 std::string
trim( std::string && s,
const Trim trim_r )
226 std::string ret( std::move(s) );
228 if ( ret.empty() || trim_r ==
NO_TRIM )
234 if ( p == std::string::npos )
245 if ( p == std::string::npos )
250 ret = ret.erase( p+1 );
264 line =
ltrim( line );
272 if ( p == std::string::npos ) {
276 }
else if ( p == 0 ) {
279 line =
ltrim( line );
283 ret = line.substr( 0, p );
284 line =
ltrim( line.erase( 0, p ) );
297 line =
rtrim( line );
305 if ( p == std::string::npos ) {
309 }
else if ( p == line.size()-1 ) {
312 line =
rtrim( line );
316 ret = line.substr( p+1 );
317 line =
rtrim( line.erase( p ) );
322 std::string
gsub(
const std::string & str_r,
const std::string & from_r,
const std::string & to_r )
324 std::string ret( str_r );
328 std::string &
replaceAll( std::string & str_r,
const std::string & from_r,
const std::string & to_r )
330 if ( ! from_r.empty() )
333 while ( (pos = str_r.find( from_r, pos )) != std::string::npos )
335 str_r.replace( pos, from_r.size(), to_r );
338 if ( pos >= str_r.length() )
345 std::string
gsubFun(
const std::string & str_r,
const std::string & from_r,
function<std::string()> to_r )
347 std::string ret( str_r );
351 std::string &
replaceAllFun( std::string & str_r,
const std::string & from_r,
function<std::string()> to_r )
353 if ( ! from_r.empty() )
356 while ( (pos = str_r.find( from_r, pos )) != std::string::npos )
358 std::string to( to_r() );
359 str_r.replace( pos, from_r.size(), to );
362 if ( pos >= str_r.length() )
371 std::vector<char> buf;
379 buf.push_back(
'\\' );
384 buf.push_back(
'\\' );
388 return std::string( buf.begin(), buf.end() );
397 if ( str_r.find_first_of( special_r ) == std::string::npos
398 && ( ::strchr( special_r.
c_str(),
'\\' ) || !::strchr( str_r.c_str(),
'\\' ) ) )
402 for_( s, str_r.c_str(), s+str_r.size() )
404 if ( *s ==
'\\' || ::strchr( special_r.
c_str(), *s ) )
411 #define RXSPECIALCHARS "\\.*+?^$[()|{"
427 for_( s, str_r.c_str(), s+str_r.size() )
432 if ( *(s+1) ) { ++s; buf << *s; }
434 else if ( *s ==
'?' )
438 else if ( *s ==
'*' )
442 else if ( *s ==
'[' )
444 const char * e = s+1;
445 if ( *e ==
'^' || *e ==
'!' )
449 while ( *e && *e !=
']' )
453 ++s; buf <<
'[' << (*s ==
'!' ?
'^' : *s );
481 std::string
getline( std::istream & str,
bool trim_r )
486 std::string
receiveUpTo( std::istream & str,
const char delim_r,
bool returnDelim_r )
488 std::ostringstream datas;
507 if ( str.eof() && datas.tellp() )
508 str.clear( std::ios::eofbit );
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.
std::string rxEscapeStr(std::string str_r)
Escape plain STRING str_r for use in a regex (not anchored by "^" or "$").
std::string escape(const C_Str &str_r, const char sep_r)
Escape desired character c using a backslash.
std::string asString() const
#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 stripFirstWord(std::string &line, const bool ltrim_first)
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...
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.
std::string gsubFun(const std::string &str_r, const std::string &from_r, function< std::string()> to_r)
std::string toLower(const std::string &s)
Return lowercase version of s.
std::string rtrim(const std::string &s)
bool strToTrue(const C_Str &str)
Parsing boolean from string.
std::string & replaceAllFun(std::string &str_r, const std::string &from_r, function< std::string()> to_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.
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.
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::string strerror(int errno_r)
Return string describing the error_r code.
std::string toUpper(const std::string &s)
Return uppercase version of s.
Assert free called for allocated char *.
std::string hexdecode(const C_Str &str_r)
Decode hexencoded XX sequences.