Skip to content

Commit

Permalink
Merge pull request #129 from zhangxianbing/master
Browse files Browse the repository at this point in the history
Fix setlocale usage in utf8_decode function
  • Loading branch information
p-ranav authored Nov 21, 2023
2 parents ef71abd + 68d059d commit 222382c
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 222382c

Please sign in to comment.