Skip to content

Commit

Permalink
Updated single_include to support mbstowcs_s Windows impl
Browse files Browse the repository at this point in the history
  • Loading branch information
p-ranav committed Feb 15, 2023
1 parent 11c6067 commit bfe95fe
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions single_include/indicators/indicators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1649,18 +1649,33 @@ static inline int mk_wcswidth_cjk(const wchar_t *pwcs, size_t n) {
}

// convert UTF-8 string to wstring
static inline std::wstring utf8_decode(const std::string &s) {
std::string curLocale = setlocale(LC_ALL, "");
const char *_Source = s.c_str();
size_t _Dsize = mbstowcs(NULL, _Source, 0) + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest, _Source, _Dsize);
std::wstring result = _Dest;
delete[] _Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
#ifdef _MSC_VER
static inline std::wstring utf8_decode(const std::string& s) {
std::string curLocale = setlocale(LC_ALL, "");
const char* _Source = s.c_str();
size_t _Dsize = std::strlen(_Source) + 1;
wchar_t* _Dest = new wchar_t[_Dsize];
size_t _Osize;
mbstowcs_s(&_Osize, _Dest, _Dsize, _Source, _Dsize);
std::wstring result = _Dest;
delete[] _Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
#else
static inline std::wstring utf8_decode(const std::string& s) {
std::string curLocale = setlocale(LC_ALL, "");
const char* _Source = s.c_str();
size_t _Dsize = mbstowcs(NULL, _Source, 0) + 1;
wchar_t* _Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest, _Source, _Dsize);
std::wstring result = _Dest;
delete[] _Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
#endif

} // namespace details

Expand Down

0 comments on commit bfe95fe

Please sign in to comment.