diff --git a/trunk/src/strhelper.cpp b/trunk/src/strhelper.cpp index 2643312..ab7379a 100644 --- a/trunk/src/strhelper.cpp +++ b/trunk/src/strhelper.cpp @@ -1,7 +1,7 @@ /* * strhelper implementation file * Author: Sun Junwen - * Version: 2.2.1 + * Version: 2.2.2 * Provides converting from tstring, string and wstring to each other * And provides string's utf8 converting. * Provides triming function to string and wstring. @@ -35,7 +35,7 @@ namespace sunjwbase // Windows convert static std::string _wstrtostr(const std::wstring& wstr, UINT codePage) { - // Convert a wstring to a specified code page encoded string + // Convert a wstring to a specified code page encoded string size_t strLen = WideCharToMultiByte(codePage, 0, wstr.c_str(), (int)wstr.length(), NULL, 0, NULL, NULL); std::string strTo; char *szTo = new char[strLen + 1]; @@ -45,10 +45,10 @@ namespace sunjwbase delete[] szTo; return strTo; } - + static std::wstring _strtowstr(const std::string& str, UINT codePage) { - // Convert a specified code page encoded string to a wstring + // Convert a specified code page encoded string to a wstring size_t wstrLen = MultiByteToWideChar(codePage, 0, str.c_str(), -1, NULL, 0); std::wstring wstrTo; wchar_t *wszTo = new wchar_t[wstrLen + 1]; @@ -71,27 +71,27 @@ std::string sunjwbase::striconv(const std::string& input, size_t outleft = inleft * 4 + 1; // should be large enough char* outptr = new char[outleft]; bzero(outptr, outleft); - + strcpy(inptr, input.c_str()); - + iconv_t cd; // conversion descriptor if ((cd = iconv_open(to_code.c_str(), from_code.c_str())) == (iconv_t) (-1)) { iconv_close(cd); // failed clean return input; } - + char* in = inptr; char* out = outptr; outleft = iconv(cd, &in, &inleft, &out, &outleft); - + iconv_close(cd); - + std::string strRet(outptr); - + delete[] inptr; delete[] outptr; - + return strRet; } #endif @@ -141,7 +141,7 @@ std::string sunjwbase::wstrtostr(const std::wstring& wstr) std::string str(char_buf); delete[] char_buf; setlocale(LC_ALL, "C"); - + return str; #endif } @@ -161,7 +161,7 @@ std::wstring sunjwbase::strtowstr(const std::string& str) std::wstring wstr(wct_buf, num_chars); delete[] wct_buf; setlocale(LC_ALL, "C"); - + return wstr; #endif } @@ -181,7 +181,7 @@ std::string sunjwbase::asciiconvjson(std::string& strJsonUtf16) n[1] = strJsonUtf16[i + 3]; n[2] = strJsonUtf16[i + 4]; n[3] = strJsonUtf16[i + 5]; - + i += 5; int utf16 = 0; @@ -260,7 +260,7 @@ std::string sunjwbase::strreplace(const std::string& base, const std::string& sr ret.replace(pos, srcLen, des); pos = ret.find(src, pos + desLen); } - + return ret; } @@ -276,7 +276,7 @@ std::wstring sunjwbase::strreplace(const std::wstring& base, const std::wstring& ret.replace(pos, srcLen, des); pos = ret.find(src, pos + desLen); } - + return ret; } @@ -347,7 +347,7 @@ std::string sunjwbase::itostr(int num, int idx /* = 10 */) break; } } - + return ret; } @@ -361,45 +361,36 @@ std::string sunjwbase::strappendformat(std::string& str, const char *format, ... temp.resize(size); va_start(vl, format); #if defined (WIN32) - int n = vsnprintf_s((char *)temp.c_str(), size, size, format, vl); + int n = vsnprintf_s((char *)temp.data(), size, _TRUNCATE, format, vl); #else - int n = vsnprintf((char *)temp.c_str(), size, format, vl); + int n = vsnprintf((char *)temp.data(), size, format, vl); #endif va_end(vl); - if (n > -1 && n < size) - { - temp.resize(n); + if (n > -1 && n < size) { + // temp.resize(n); break; } if (n > -1) - { size = n + 1; // not large enough - } else - { size *= 2; - } } - str.append(temp); - + str.append(temp.c_str()); + return str; } bool sunjwbase::str_startwith(const std::string& str, const std::string& target) { if (str.length() < target.length()) - { return false; - } // Length is enough, let's check content. size_t i = 0; for (; i < target.length(); ++i) { if (str[i] != target[i]) - { return false; - } } return (i == target.length()); @@ -408,9 +399,7 @@ bool sunjwbase::str_startwith(const std::string& str, const std::string& target) bool sunjwbase::str_endwith(const std::string& str, const std::string& target) { if (str.length() < target.length()) - { return false; - } // Length is enough, let's check content. size_t str_len = str.length(); @@ -419,9 +408,7 @@ bool sunjwbase::str_endwith(const std::string& str, const std::string& target) for (; i < target.length(); ++i) { if (str[str_len - target_len + i] != target[i]) - { return false; - } } return (i == target.length()); diff --git a/trunk/src/strhelper.h b/trunk/src/strhelper.h index 17cd624..046eb20 100644 --- a/trunk/src/strhelper.h +++ b/trunk/src/strhelper.h @@ -1,7 +1,7 @@ /* * strhelper header file * Author: Sun Junwen - * Version: 2.2.1 + * Version: 2.2.2 * Provides converting from tstring, string and wstring to each other * And provides string's utf8 converting. * Provides triming function to string and wstring. @@ -23,7 +23,7 @@ namespace sunjwbase #else typedef std::string tstring; #endif - + #if defined (WIN32) typedef __int64 INT64; #endif @@ -44,10 +44,10 @@ namespace sunjwbase * 2nd, call strtowstr */ std::wstring strtowstrutf8(const std::string& str); - + std::string wstrtostr(const std::wstring& wstr); std::wstring strtowstr(const std::string& str); - + #if defined (__APPLE__) || defined (__unix) std::string striconv(const std::string& input, const std::string& to_code, @@ -80,7 +80,7 @@ namespace sunjwbase // convert JSON utf16 encoded string to native encoded string std::string asciiconvjson(std::string& strJsonUtf16); - + inline std::string tstrtostr(const tstring& tstr) { #if defined(_UNICODE_HELPER) @@ -89,7 +89,7 @@ namespace sunjwbase return tstr; #endif } - + inline std::wstring tstrtowstr(const tstring& tstr) { #if defined(_UNICODE_HELPER) @@ -174,7 +174,7 @@ namespace sunjwbase { strequal_ci( const std::locale& loc ):m_loc(loc) {} - + bool operator()(charT ch1, charT ch2) { return std::toupper(ch1, m_loc) == std::toupper(ch2, m_loc); @@ -200,7 +200,7 @@ namespace sunjwbase return -1; // not found } } - + // itostr std::string itostr(int num, int idx = 10); @@ -213,5 +213,4 @@ namespace sunjwbase } - #endif