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) );
180 std::string ret( s );
183 if ( isupper( ret[i] ) )
184 ret[i] = static_cast<char>(tolower( ret[i] ));
199 std::string ret( s );
202 if ( islower( ret[i] ) )
203 ret[i] = static_cast<char>(toupper( ret[i] ));
213 std::string
trim(
const std::string & s,
const Trim trim_r )
215 if ( s.empty() || trim_r ==
NO_TRIM )
218 std::string ret( s );
223 if ( p == std::string::npos )
224 return std::string();
226 ret = ret.substr( p );
232 if ( p == std::string::npos )
233 return std::string();
235 ret = ret.substr( 0, p+1 );
249 line =
ltrim( line );
257 if ( p == std::string::npos ) {
261 }
else if ( p == 0 ) {
264 line =
ltrim( line );
268 ret = line.substr( 0, p );
269 line =
ltrim( line.erase( 0, p ) );
282 line =
rtrim( line );
290 if ( p == std::string::npos ) {
294 }
else if ( p == line.size()-1 ) {
297 line =
rtrim( line );
301 ret = line.substr( p+1 );
302 line =
rtrim( line.erase( p ) );
307 std::string
gsub(
const std::string & str_r,
const std::string & from_r,
const std::string & to_r )
309 std::string ret( str_r );
313 std::string &
replaceAll( std::string & str_r,
const std::string & from_r,
const std::string & to_r )
315 if ( ! from_r.empty() )
318 while ( (pos = str_r.find( from_r, pos )) != std::string::npos )
320 str_r.replace( pos, from_r.size(), to_r );
323 if ( pos >= str_r.length() )
330 std::string
gsubFun(
const std::string & str_r,
const std::string & from_r,
function<std::string()> to_r )
332 std::string ret( str_r );
336 std::string &
replaceAllFun( std::string & str_r,
const std::string & from_r,
function<std::string()> to_r )
338 if ( ! from_r.empty() )
341 while ( (pos = str_r.find( from_r, pos )) != std::string::npos )
343 std::string to( to_r() );
344 str_r.replace( pos, from_r.size(), to );
347 if ( pos >= str_r.length() )
356 std::vector<char> buf;
364 buf.push_back(
'\\' );
369 buf.push_back(
'\\' );
373 return std::string( buf.begin(), buf.end() );
382 if ( str_r.find_first_of( special_r ) == std::string::npos
383 && ( ::strchr( special_r.
c_str(),
'\\' ) || !::strchr( str_r.c_str(),
'\\' ) ) )
387 for_( s, str_r.c_str(), s+str_r.size() )
389 if ( *s ==
'\\' || ::strchr( special_r.
c_str(), *s ) )
396 #define RXSPECIALCHARS "\\.*+?^$[()|{"
412 for_( s, str_r.c_str(), s+str_r.size() )
417 if ( *(s+1) ) { ++s; buf << *s; }
419 else if ( *s ==
'?' )
423 else if ( *s ==
'*' )
427 else if ( *s ==
'[' )
429 const char * e = s+1;
430 if ( *e ==
'^' || *e ==
'!' )
434 while ( *e && *e !=
']' )
438 ++s; buf <<
'[' << (*s ==
'!' ?
'^' : *s );
466 std::string
getline( std::istream & str,
bool trim_r )
471 std::string
receiveUpTo( std::istream & str,
const char delim_r,
bool returnDelim_r )
473 std::ostringstream datas;
492 if ( str.eof() && datas.tellp() )
493 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.