34 std::string
form(
const char * format, ... )
39 va_start( ap, format );
40 vasprintf( &safe.
_buf, format, ap );
63 std::string t(
toLower( str ) );
69 || strtonum<long long>( str )
80 std::string t(
toLower( str ) );
94 inline bool heIsAlNum(
char ch )
96 return ( (
'a' <= ch && ch <=
'z' )
97 ||(
'A' <= ch && ch <=
'Z' )
98 ||(
'0' <= ch && ch <=
'9' ) );
101 inline int heDecodeCh(
char ch )
103 if (
'0' <= ch && ch <=
'9' )
105 if (
'A' <= ch && ch <=
'F' )
106 return( ch -
'A' + 10 );
107 if (
'a' <= ch && ch <=
'f' )
108 return( ch -
'a' + 10 );
115 static const char *
const hdig =
"0123456789ABCDEF";
117 res.reserve( str_r.
size() );
118 for (
const char * it = str_r.
c_str(); *it; ++it )
120 if ( heIsAlNum( *it ) )
127 res += hdig[(
unsigned char)(*it)/16];
128 res += hdig[(
unsigned char)(*it)%16];
137 res.reserve( str_r.
size() );
142 int d1 = heDecodeCh( *(it+1) );
145 int d2 = heDecodeCh( *(it+2) );
171 std::string ret( s );
174 if ( isupper( ret[i] ) )
175 ret[i] = static_cast<char>(tolower( ret[i] ));
190 std::string ret( s );
193 if ( islower( ret[i] ) )
194 ret[i] = static_cast<char>(toupper( ret[i] ));
204 std::string
trim(
const std::string & s,
const Trim trim_r )
206 if ( s.empty() || trim_r ==
NO_TRIM )
209 std::string ret( s );
214 if ( p == std::string::npos )
215 return std::string();
217 ret = ret.substr( p );
223 if ( p == std::string::npos )
224 return std::string();
226 ret = ret.substr( 0, p+1 );
240 line =
ltrim( line );
248 if ( p == std::string::npos ) {
252 }
else if ( p == 0 ) {
255 line =
ltrim( line );
259 ret = line.substr( 0, p );
260 line =
ltrim( line.erase( 0, p ) );
273 line =
rtrim( line );
281 if ( p == std::string::npos ) {
285 }
else if ( p == line.size()-1 ) {
288 line =
rtrim( line );
292 ret = line.substr( p+1 );
293 line =
rtrim( line.erase( p ) );
298 std::string
gsub(
const std::string & str_r,
const std::string & from_r,
const std::string & to_r )
300 std::string ret( str_r );
304 std::string &
replaceAll( std::string & str_r,
const std::string & from_r,
const std::string & to_r )
307 while ( (pos = str_r.find( from_r, pos )) != std::string::npos )
309 str_r.replace( pos, from_r.size(), to_r );
312 if ( pos >= str_r.length() )
318 std::string
gsubFun(
const std::string & str_r,
const std::string & from_r,
function<std::string()> to_r )
320 std::string ret( str_r );
324 std::string &
replaceAllFun( std::string & str_r,
const std::string & from_r,
function<std::string()> to_r )
327 while ( (pos = str_r.find( from_r, pos )) != std::string::npos )
329 std::string to( to_r() );
330 str_r.replace( pos, from_r.size(), to );
333 if ( pos >= str_r.length() )
341 std::vector<char> buf;
349 buf.push_back(
'\\' );
354 buf.push_back(
'\\' );
358 return std::string( buf.begin(), buf.end() );
366 std::string
getline( std::istream & str,
bool trim_r )
371 std::string
receiveUpTo( std::istream & str,
const char delim_r,
bool returnDelim_r )
373 std::ostringstream datas;
392 if ( str.eof() && datas.tellp() )
393 str.clear( std::ios::eofbit );
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.
Trim
To define how to trim.
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)
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 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)
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.