22 #include <boost/regex.hpp>
25 #define TMP_STRING "MaSs2pLus0PlUs1tMp1sTrInG"
30 #define STRING_BUFFER_SIZE 0x1000
42 std::string
strfmt(
const char* fmt, ... ) {
52 s = (
char*)malloc( STRING_BUFFER_SIZE );
53 vsprintf_s( s, STRING_BUFFER_SIZE, fmt, ap );
57 vasprintf( &s, fmt, ap );
64 std::string s2 = std::string( s );
75 size_t bufferSize = ( ws.length() + 1 ) * MB_CUR_MAX;
78 char* buffer =
new char[ bufferSize ];
84 wcstombs_s( &size, buffer, bufferSize, ws.c_str(), ws.size() );
86 size = wcstombs( buffer, ws.c_str(), size );
89 std::string s = std::string( buffer );
99 size_t bufferSize = s.length() + 1;
102 wchar_t* buffer =
new wchar_t[ bufferSize ];
108 mbstowcs_s( &size, buffer, bufferSize, s.c_str(), s.size() );
110 size = mbstowcs( buffer, s.c_str(), bufferSize );
113 std::wstring ws = std::wstring( buffer );
129 unsigned int len = (
unsigned int)strlen( s );
132 for(
unsigned int i = 0; i < len; i++ ) {
133 str.push_back( tolower( s[ i ] ) );
148 unsigned int len = (
unsigned int)strlen( s );
151 for(
unsigned int i = 0; i < len; i++ ) {
152 str.push_back( toupper( s[ i ] ) );
167 std::vector< std::string >& tokens
170 std::string::size_type delimLen = strlen( delim );
171 if( delimLen == 0 ) {
172 tokens.push_back( s );
177 std::string str =
NVL( s,
"" );
178 std::list< std::string > strList;
181 std::string::size_type start = std::string::npos;
182 std::string::size_type pos = 0;
183 while( ( pos = str.find(
"\"", ( start == std::string::npos ? 0 : start + 1 ) ) ) != str.npos ) {
184 if( start == std::string::npos ) {
188 strList.push_back( str.substr( start + 1, pos - start - 1 ) );
190 std::string tmp = str.substr( 0, start );
191 tmp.append( TMP_STRING );
192 tmp.append( str.substr( pos + 1 ) );
195 start = std::string::npos;
198 str = FMT(
"%s%c", str.c_str(), delim[ 0 ] );
201 std::string::size_type tmpLen = strlen( TMP_STRING );
202 std::string::size_type cnt = 0;
205 for(
unsigned int i = 0; i < str.length(); i++ ) {
209 for(
unsigned int j = 0; j < delimLen && !found; j++ ) {
210 if( c == delim[ j ] ) {
217 std::string val = str.substr( start, i - start );
219 std::string::size_type rPos = 0;
220 while( ( rPos = val.find( TMP_STRING ) ) != val.npos ) {
221 std::string tmp = val.substr( 0, rPos );
222 tmp.append( strList.front() );
224 tmp.append( val.substr( rPos + tmpLen ) );
230 tokens.push_back( val );
238 return (
unsigned int)cnt;
244 const char* separator,
245 std::vector< std::string >& tokens
248 std::string::size_type sepLen = strlen( separator );
250 tokens.push_back( s );
255 std::string str =
NVL( s,
"" );
256 std::list< std::string > strList;
259 std::string::size_type start = std::string::npos;
260 std::string::size_type pos = 0;
261 while( ( pos = str.find(
"\"", ( start == std::string::npos ? 0 : start + 1 ) ) ) != str.npos ) {
262 if( start == std::string::npos ) {
266 strList.push_back( str.substr( start + 1, (
int)pos - start - 1 ) );
268 std::string tmp = str.substr( 0, start );
269 tmp.append( TMP_STRING );
270 tmp.append( str.substr( pos + 1 ) );
273 start = std::string::npos;
276 str.append( separator );
279 std::string::size_type tmpLen = strlen( TMP_STRING );
280 std::string::size_type cnt = 0;
284 while( ( pos = str.find( separator, start ) ) != str.npos ) {
286 std::string val = str.substr( start, (
int)pos - start );
288 std::string::size_type rPos = 0;
289 while( ( rPos = val.find( TMP_STRING ) ) != val.npos ) {
290 std::string tmp = val.substr( 0, rPos );
291 tmp.append( strList.front() );
293 tmp.append( val.substr( rPos + tmpLen ) );
298 tokens.push_back( val );
301 start = (int)( pos + sepLen );
304 return (
unsigned int)cnt;
308 bool isint(
const char* s,
const int radix,
int* val ) {
316 int v = strtol( s, &endptr, radix );
323 if( 0 < strlen( endptr ) ){
336 int toint(
const char* s,
const int radix,
const int dfval ) {
344 int val = strtol( s, &endptr, radix );
354 bool isint64(
const char* s,
const int radix,
long long* val ) {
365 v = _strtoi64( s, &endptr, radix );
367 v = strtoll( s, &endptr, radix );
382 long long toint64(
const char* s,
const int radix,
const long long dfval ) {
390 long long val = dfval;
393 val = _strtoi64( s, &endptr, radix );
395 val = strtoll( s, &endptr, radix );
414 double v = strtod( s, &endptr );
420 if( 0 < strlen( endptr ) ){
433 double todouble(
const char* s,
const double dfval ) {
441 double val = strtod( s, &endptr );
450 bool tobool(
const char* s,
bool dfVal ) {
455 if( value.compare(
"1" ) == 0
456 || value.compare(
"TRUE" ) == 0 || value.compare(
"ON" ) == 0 ) {
459 if( value.compare(
"0" ) == 0
460 || value.compare(
"FALSE" ) == 0 || value.compare(
"OFF" ) == 0 ) {
479 for(
unsigned int i = 0; i < num; i++ ) {
485 size_t length = strlen( s );
486 size_t size = length + 1;
487 char* buffer =
new char[ size ];
490 memcpy( buffer, s, size );
493 char* firstChar = buffer;
495 && *firstChar !=
'\0' ) {
500 char* lastChar = buffer + length;
501 if( firstChar != lastChar ) {
504 while( lastChar != firstChar
511 std::string trimmed( firstChar );
514 if( trimmed.length() >= 2 && trimmed.front() ==
'\"' && trimmed.back() ==
'\"' ) {
515 trimmed = trimmed.substr( 1, trimmed.length() - 2 );
526 std::string str(
NVL( s,
"" ) );
527 std::string prfx(
NVL( prefix,
"" ) );
530 std::string::size_type pos = str.find( prfx );
536 bool endswith(
const char* s,
const char* suffix ) {
538 std::string str(
NVL( s,
"" ) );
539 std::string sffx(
NVL( suffix,
"" ) );
542 size_t pos = str.rfind( sffx );
544 return ( pos == ( str.length() - sffx.length() ) );
548 std::string
replacestring(
const char* s,
const char* oldStr,
const char* newStr ) {
557 if( oldStr ==
NULL || newStr ==
NULL ) {
560 if( strcmp( oldStr, newStr ) == 0 ) {
565 std::string::size_type pos = str.find( oldStr );
566 if( pos == str.npos ) {
571 size_t length = strlen( oldStr );
572 str = str.substr( 0, pos ) + std::string( newStr ) +
replacestring( s + ( pos + length), oldStr, newStr );
578 std::string
shortenstring(
const char* s,
const unsigned int len,
const bool cutTail ) {
580 std::string tmp =
NVL( s,
"" );
583 if( tmp.length() <= len ) {
594 tmp = tmp.substr( 0, len - 3 );
598 tmp = tmp.substr( 3 );
599 tmp = FMT(
"...%s", tmp.c_str() );
610 boost::regex r( regex );
611 boost::match_results< const char* > results;
612 if( boost::regex_search( s, results, r ) ) {
613 ret = results.str( 1 );
std::string strfmt(const char *fmt,...)
get formated string
std::string shortenstring(const char *s, const unsigned int len, const bool cutTail)
shorten the specified string
const char * g_whiteSpaces
white spaces
bool isdouble(const char *s, double *val)
judge whether sutisfied character string shows real number.
unsigned int stringseparate(const char *s, const char *separator, std::vector< std::string > &tokens)
separates string
bool tobool(const char *s, bool dfVal)
get true or false from character string.
int toint(const char *s, const int radix, const int dfval)
convert string into integer
int compareignorecase(const char *s1, const char *s2)
compare two strings ignoring case considerations
std::string trimstring(const char *s)
remove white spaces from both ends of specified string
unsigned int stringtoken(const char *s, const char *delim, std::vector< std::string > &tokens)
get tokens from string
bool isint(const char *s, const int radix, int *val)
judge whether sutisfied character string shows integer number.
std::set< char > g_whiteSpacesSet
white spaces set
#define NVL(checkVal, replaceVal)
std::string tolowercase(const char *s)
convert all of the characters in the string given to lower case
bool isint64(const char *s, const int radix, long long *val)
judge whether sutisfied character string shows 64 bit integer number.
std::wstring str2wstr(std::string &s)
convert string to wstring
interfaces of string function
std::string replacestring(const char *s, const char *oldStr, const char *newStr)
replaces specified substring with another one
double todouble(const char *s, const double dfval)
convert string into double
std::string regexsearch(const char *s, const char *regex)
search string using regular expression.
std::string wstr2str(std::wstring &ws)
convert wstring to string
bool endswith(const char *s, const char *suffix)
judge whether there is a sub-string at the end of string
long long toint64(const char *s, const int radix, const long long dfval)
convert string into 64 bit integer
bool startswith(const char *s, const char *prefix)
judges whether there is a sub-string at the start of string
std::string touppercase(const char *s)
convert all of the characters int the string given to upper case