Skip to content

Commit

Permalink
Fix setlocale usage in utf8_decode function
Browse files Browse the repository at this point in the history
  • Loading branch information
sean2077 committed Nov 21, 2023
1 parent ef71abd commit 68d059d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 8 additions & 2 deletions include/indicators/display_width.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ static inline int mk_wcswidth_cjk(const wchar_t *pwcs, size_t n) {
// convert UTF-8 string to wstring
#ifdef _MSC_VER
static inline std::wstring utf8_decode(const std::string& s) {
std::string curLocale = setlocale(LC_ALL, "");
auto r = setlocale(LC_ALL, "");
std::string curLocale;
if (r)
curLocale = r;
const char* _Source = s.c_str();
size_t _Dsize = std::strlen(_Source) + 1;
wchar_t* _Dest = new wchar_t[_Dsize];
Expand All @@ -324,7 +327,10 @@ static inline std::wstring utf8_decode(const std::string& s) {
}
#else
static inline std::wstring utf8_decode(const std::string& s) {
std::string curLocale = setlocale(LC_ALL, "");
auto r = setlocale(LC_ALL, "");
std::string curLocale;
if (r)
curLocale = r;
const char* _Source = s.c_str();
size_t _Dsize = mbstowcs(NULL, _Source, 0) + 1;
wchar_t* _Dest = new wchar_t[_Dsize];
Expand Down
10 changes: 8 additions & 2 deletions single_include/indicators/indicators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,10 @@ static inline int mk_wcswidth_cjk(const wchar_t *pwcs, size_t n) {
// convert UTF-8 string to wstring
#ifdef _MSC_VER
static inline std::wstring utf8_decode(const std::string& s) {
std::string curLocale = setlocale(LC_ALL, "");
auto r = setlocale(LC_ALL, "");
std::string curLocale;
if (r)
curLocale = r;
const char* _Source = s.c_str();
size_t _Dsize = std::strlen(_Source) + 1;
wchar_t* _Dest = new wchar_t[_Dsize];
Expand All @@ -1665,7 +1668,10 @@ static inline std::wstring utf8_decode(const std::string& s) {
}
#else
static inline std::wstring utf8_decode(const std::string& s) {
std::string curLocale = setlocale(LC_ALL, "");
auto r = setlocale(LC_ALL, "");
std::string curLocale;
if (r)
curLocale = r;
const char* _Source = s.c_str();
size_t _Dsize = mbstowcs(NULL, _Source, 0) + 1;
wchar_t* _Dest = new wchar_t[_Dsize];
Expand Down

0 comments on commit 68d059d

Please sign in to comment.