19 #ifndef CA_MGM_STRING_HPP 20 #define CA_MGM_STRING_HPP 33 #ifndef __GXX_EXPERIMENTAL_CXX0X__ 34 #define for_(IT,BEG,END) for ( typeof(BEG) IT = BEG, _for_end = END; IT != _for_end; ++IT ) 36 #define for_(IT,BEG,END) for ( auto IT = BEG, _for_end = END; IT != _for_end; ++IT ) 97 if (
_sze == std::string::npos )
102 operator const char *()
const {
return c_str(); }
112 {
return str << obj.
c_str(); }
125 inline std::string
toString(
bool b) {
return (b?
"true":
"false"); }
129 {
return t.asString(); }
141 inline std::string
asString(
const std::string &t )
146 std::string
form(
const char * format, ... )
147 __attribute__ ((format (printf, 1, 2)));
154 std::string
strerror(
int errno_r );
173 {
return _buf ? std::string(_buf) : std::string(); }
190 inline std::string
numstring(
char n,
int w = 0 ) {
return form(
"%*hhd", w, n ); }
191 inline std::string
numstring(
unsigned char n,
int w = 0 ) {
return form(
"%*hhu", w, n ); }
192 inline std::string
numstring(
short n,
int w = 0 ) {
return form(
"%*hd", w, n ); }
193 inline std::string
numstring(
unsigned short n,
int w = 0 ) {
return form(
"%*hu", w, n ); }
194 inline std::string
numstring(
int n,
int w = 0 ) {
return form(
"%*d", w, n ); }
195 inline std::string
numstring(
unsigned n,
int w = 0 ) {
return form(
"%*u", w, n ); }
196 inline std::string
numstring(
long n,
int w = 0 ) {
return form(
"%*ld", w, n ); }
197 inline std::string
numstring(
unsigned long n,
int w = 0 ) {
return form(
"%*lu", w, n ); }
198 inline std::string
numstring(
long long n,
int w = 0 ) {
return form(
"%*lld", w, n ); }
199 inline std::string
numstring(
unsigned long long n,
int w = 0 ) {
return form(
"%*llu", w, n ); }
214 inline std::string
hexstring(
char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
215 inline std::string
hexstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
216 inline std::string
hexstring(
short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
217 inline std::string
hexstring(
unsigned short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
218 inline std::string
hexstring(
int n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
219 inline std::string
hexstring(
unsigned n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
220 inline std::string
hexstring(
long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
221 inline std::string
hexstring(
unsigned long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
222 inline std::string
hexstring(
long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
223 inline std::string
hexstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
238 inline std::string
octstring(
char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
239 inline std::string
octstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
240 inline std::string
octstring(
short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
241 inline std::string
octstring(
unsigned short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
242 inline std::string
octstring(
int n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
243 inline std::string
octstring(
unsigned n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
244 inline std::string
octstring(
long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
245 inline std::string
octstring(
unsigned long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
246 inline std::string
octstring(
long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
247 inline std::string
octstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
260 template<
typename _It>
264 inline short strtonum(
const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
266 inline int strtonum(
const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
268 inline long strtonum(
const C_Str & str ) { return ::strtol ( str, NULL, 0 ); }
270 inline long long strtonum(
const C_Str & str ) { return ::strtoll ( str, NULL, 0 ); }
273 inline unsigned short strtonum(
const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
275 inline unsigned strtonum(
const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
277 inline unsigned long strtonum(
const C_Str & str ) { return ::strtoul ( str, NULL, 0 ); }
279 inline unsigned long long strtonum(
const C_Str & str ) { return ::strtoull( str, NULL, 0 ); }
286 template<
typename _It>
288 {
return i = strtonum<_It>( str ); }
314 if (
strToTrue( str ) )
return (return_r =
true);
315 if ( !
strToFalse( str ) )
return (return_r =
false);
326 std::string
gsub(
const std::string& sData,
const std::string& sFrom,
const std::string& sTo);
335 std::string&
replaceAll( std::string & str,
const std::string & from,
const std::string & to);
349 template<
class _OutputIterator>
351 _OutputIterator result_r,
352 const C_Str & sepchars_r =
" \t" )
354 const char * beg = line_r;
355 const char * cur = beg;
357 while ( *cur && ::strchr( sepchars_r, *cur ) )
360 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
363 while( *cur && !::strchr( sepchars_r, *cur ) )
366 *result_r = std::string( beg, cur-beg );
368 while ( *cur && ::strchr( sepchars_r, *cur ) )
401 template<
class _OutputIterator>
403 _OutputIterator result_r,
404 const C_Str & sepchars_r =
" \t",
405 bool withEmpty =
false)
407 const char * beg = line_r;
408 const char * cur = beg;
412 while ( *cur && ::strchr( sepchars_r, *cur ) )
423 if (!*cur && withEmpty)
430 for ( beg = cur; *beg; beg = cur, ++result_r, ++ret )
432 if ( *cur ==
'"' || *cur ==
'\'' )
434 char closeChar = *cur;
439 while ( *cur && *cur != closeChar)
446 const char * esc = cur-1;
447 while ( esc != beg && *esc ==
'\\' )
452 cont = (escCount % 2 == 1);
456 std::string s( beg+1, cur-beg-2 );
460 char tmpn[2] = { closeChar, 0 };
461 char tmpo[3] = {
'\\', closeChar, 0 };
469 while( *cur && !::strchr( sepchars_r, *cur ) )
479 std::string s( beg, cur-beg );
483 const char *delimeter = sepchars_r;
486 std::string ds(
"\\");
487 const char tmp[2] = { *delimeter,
'\0' };
488 std::string del(tmp);
497 if ( *cur && ::strchr( sepchars_r, *cur ) )
499 while ( *cur && ::strchr( sepchars_r, *cur ) )
509 if ( !*cur && withEmpty && ::strchr( sepchars_r, *(cur-1) ) )
538 template<
class _OutputIterator>
540 _OutputIterator result_r,
541 const C_Str & sepchars_r =
":" )
543 const char * beg = line_r;
544 const char * cur = beg;
546 for ( beg = cur; *beg; beg = cur, ++result_r )
549 while( *cur && !::strchr( sepchars_r, *cur ) )
552 *result_r = std::string( beg, cur-beg );
560 *result_r = std::string();
575 template<
class _OutputIterator>
577 _OutputIterator result_r,
578 const C_Str & sepchars_r =
":" )
590 template <
class _Iterator>
591 std::string
join( _Iterator begin, _Iterator end,
592 const C_Str & sep_r =
" " )
595 for ( _Iterator iter = begin; iter != end; ++ iter )
605 template <
class _Container>
606 std::string
join(
const _Container & cont_r,
607 const C_Str & sep_r =
" " )
608 {
return join( cont_r.begin(), cont_r.end(), sep_r ); }
614 template <
class _Iterator>
616 const char sep_r =
' ' )
618 std::vector<char> buf;
619 for ( _Iterator iter = begin; iter != end; ++ iter )
622 buf.push_back( sep_r );
627 buf.push_back(
'"' );
628 buf.push_back(
'"' );
632 std::string toadd(
asString(*iter) );
633 for_( ch, toadd.begin(), toadd.end() )
640 buf.push_back(
'\\' );
641 buf.push_back( *ch );
645 buf.push_back(
'\\' );
646 buf.push_back( *ch );
651 return std::string( buf.begin(), buf.end() );
666 std::string
escape(
const std::string & str_r,
const char c =
' ' );
692 std::string
toLower(
const std::string & s );
695 {
return( s ?
toLower( std::string(s) ) : std::string() ); }
700 std::string
toUpper(
const std::string & s );
703 {
return( s ?
toUpper( std::string(s) ) : std::string() ); }
713 return ::strcasecmp( lhs, rhs );
721 { return ::strstr( str_r, val_r ); }
724 { return ::strcasestr( str_r, val_r ); }
740 std::string
trim(
const std::string & s,
const Trim trim_r =
TRIM );
742 inline std::string
ltrim(
const std::string & s )
745 inline std::string
rtrim(
const std::string & s )
749 std::string
stripFirstWord( std::string & line,
const bool ltrim_first );
751 std::string
stripLastWord( std::string & line,
const bool rtrim_first );
753 std::string
getline( std::istream & str,
bool trim =
false );
755 std::string
getline( std::istream & str,
const Trim trim_r );
764 {
return( ::strncmp( str_r, prefix_r, prefix_r.
size() ) == 0 ); }
767 {
return( ::strncasecmp( str_r, prefix_r, prefix_r.
size() ) == 0 ); }
775 {
return( str_r.
size() >= suffix_r.
size() && ::strncmp( str_r + str_r.
size() - suffix_r.
size() , suffix_r, suffix_r.
size() ) == 0 ); }
778 {
return( str_r.
size() >= suffix_r.
size() && ::strncasecmp( str_r + str_r.
size() - suffix_r.
size() , suffix_r, suffix_r.
size() ) == 0 ); }
784 return std::string( str_r, str_r.
size() - suffix_r.
size() );
785 return str_r.
c_str();
809 #endif // CA_MGM_STRING_HPP C_Str(const char *c_str_r)
Definition: String.hpp:89
SafeBuf()
Definition: String.hpp:170
size_type size() const
Definition: String.hpp:95
std::string strerror(int errno_r)
std::string trim(const std::string &s, const Trim trim_r=TRIM)
Definition: String.hpp:167
std::string stripPrefix(const C_Str &str_r, const C_Str &prefix_r)
Definition: String.hpp:770
std::string numstring(unsigned long long n, int w=0)
Definition: String.hpp:199
const char * c_str() const
Definition: String.hpp:103
size_type _sze
Definition: String.hpp:107
std::string toString(bool b)
Definition: String.hpp:125
C_Str()
Definition: String.hpp:87
C_Str(char *c_str_r)
Definition: String.hpp:88
std::string escape(const std::string &str_r, const char c= ' ')
C_Str(const std::string &str_r)
Definition: String.hpp:90
bool strToBoolNodefault(const C_Str &str, bool &return_r)
Definition: String.hpp:312
std::string & replaceAll(std::string &str, const std::string &from, const std::string &to)
Looks for text in string and replaces it in place.
int compareCI(const C_Str &lhs, const C_Str &rhs)
Definition: String.hpp:709
std::string stripFirstWord(std::string &line, const bool ltrim_first)
std::string ltrim(const std::string &s)
Definition: String.hpp:742
Definition: PtrTypes.hpp:102
bool strToBool(const C_Str &str, bool default_r)
Definition: String.hpp:305
bool strToFalse(const C_Str &str)
bool isNull() const
Definition: String.hpp:93
bool endsWithCI(const C_Str &str_r, const C_Str &prefix_r)
Definition: String.hpp:799
std::string hexdecode(const C_Str &str_r)
unsigned split(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t")
Definition: String.hpp:350
Definition: String.hpp:735
std::string hexstring(unsigned long long n, int w=0)
Definition: String.hpp:223
Definition: String.hpp:737
std::ostream & operator<<(std::ostream &str, const C_Str &obj)
Definition: String.hpp:111
bool containsCI(const C_Str &str_r, const C_Str &val_r)
Definition: String.hpp:723
std::string getline(std::istream &str, const Trim trim_r)
Definition: String.hpp:736
std::string join(const _Container &cont_r, const C_Str &sep_r=" ")
Definition: String.hpp:606
std::string toLower(const char *s)
Definition: String.hpp:694
unsigned splitFieldsEscaped(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=":")
Definition: String.hpp:576
Trim
Definition: String.hpp:733
std::string stripLastWord(std::string &line, const bool rtrim_first)
bool startsWith(const C_Str &str_r, const C_Str &prefix_r)
Definition: String.hpp:789
const char *const _val
Definition: String.hpp:106
bool endsWith(const C_Str &str_r, const C_Str &prefix_r)
Definition: String.hpp:796
bool strToTrue(const C_Str &str)
std::string octstring(unsigned long long n, int w=0)
Definition: String.hpp:247
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Definition: String.hpp:763
std::string hexencode(const C_Str &str_r)
~SafeBuf()
Definition: String.hpp:171
std::string rtrim(const std::string &s)
Definition: String.hpp:745
std::string gsub(const std::string &sData, const std::string &sFrom, const std::string &sTo)
Looks for text in a string and replaces it.
#define for_(IT, BEG, END)
Definition: String.hpp:34
std::string asString(const std::string &t)
Definition: String.hpp:141
_It strtonum(const C_Str &str, _It &i)
Definition: String.hpp:287
std::string joinEscaped(_Iterator begin, _Iterator end, const char sep_r= ' ')
Definition: String.hpp:615
std::string::size_type size_type
Definition: String.hpp:84
bool hasSuffix(const C_Str &str_r, const C_Str &suffix_r)
Definition: String.hpp:774
bool startsWithCI(const C_Str &str_r, const C_Str &prefix_r)
Definition: String.hpp:792
bool contains(const C_Str &str_r, const C_Str &val_r)
Definition: String.hpp:720
bool empty() const
Definition: String.hpp:94
Definition: String.hpp:734
std::string asString() const
Definition: String.hpp:172
bool hasSuffixCI(const C_Str &str_r, const C_Str &suffix_r)
Definition: String.hpp:777
unsigned splitFields(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=":")
Definition: String.hpp:539
bool hasPrefixCI(const C_Str &str_r, const C_Str &prefix_r)
Definition: String.hpp:766
std::string stripSuffix(const C_Str &str_r, const C_Str &suffix_r)
Definition: String.hpp:781
char * _buf
Definition: String.hpp:169
std::string form(const char *format,...)
std::string toUpper(const char *s)
Definition: String.hpp:702
Definition: String.hpp:81
unsigned splitEscaped(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t", bool withEmpty=false)
Definition: String.hpp:402
Definition: ByteBuffer.hpp:37