Skip to content

Commit

Permalink
npp: Update strhelper.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunjw committed May 30, 2024
1 parent 6bde4de commit 54a235f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 45 deletions.
59 changes: 23 additions & 36 deletions trunk/src/strhelper.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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];
Expand All @@ -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];
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -347,7 +347,7 @@ std::string sunjwbase::itostr(int num, int idx /* = 10 */)
break;
}
}

return ret;
}

Expand All @@ -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());
Expand All @@ -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();
Expand All @@ -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());
Expand Down
17 changes: 8 additions & 9 deletions trunk/src/strhelper.h
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,7 +23,7 @@ namespace sunjwbase
#else
typedef std::string tstring;
#endif

#if defined (WIN32)
typedef __int64 INT64;
#endif
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -89,7 +89,7 @@ namespace sunjwbase
return tstr;
#endif
}

inline std::wstring tstrtowstr(const tstring& tstr)
{
#if defined(_UNICODE_HELPER)
Expand Down Expand Up @@ -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);
Expand All @@ -200,7 +200,7 @@ namespace sunjwbase
return -1; // not found
}
}

// itostr
std::string itostr(int num, int idx = 10);

Expand All @@ -213,5 +213,4 @@ namespace sunjwbase

}


#endif

0 comments on commit 54a235f

Please sign in to comment.