Skip to content

Commit 222382c

Browse files
authored
Merge pull request #129 from zhangxianbing/master
Fix setlocale usage in utf8_decode function
2 parents ef71abd + 68d059d commit 222382c

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

include/indicators/display_width.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,10 @@ static inline int mk_wcswidth_cjk(const wchar_t *pwcs, size_t n) {
311311
// convert UTF-8 string to wstring
312312
#ifdef _MSC_VER
313313
static inline std::wstring utf8_decode(const std::string& s) {
314-
std::string curLocale = setlocale(LC_ALL, "");
314+
auto r = setlocale(LC_ALL, "");
315+
std::string curLocale;
316+
if (r)
317+
curLocale = r;
315318
const char* _Source = s.c_str();
316319
size_t _Dsize = std::strlen(_Source) + 1;
317320
wchar_t* _Dest = new wchar_t[_Dsize];
@@ -324,7 +327,10 @@ static inline std::wstring utf8_decode(const std::string& s) {
324327
}
325328
#else
326329
static inline std::wstring utf8_decode(const std::string& s) {
327-
std::string curLocale = setlocale(LC_ALL, "");
330+
auto r = setlocale(LC_ALL, "");
331+
std::string curLocale;
332+
if (r)
333+
curLocale = r;
328334
const char* _Source = s.c_str();
329335
size_t _Dsize = mbstowcs(NULL, _Source, 0) + 1;
330336
wchar_t* _Dest = new wchar_t[_Dsize];

single_include/indicators/indicators.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,10 @@ static inline int mk_wcswidth_cjk(const wchar_t *pwcs, size_t n) {
16521652
// convert UTF-8 string to wstring
16531653
#ifdef _MSC_VER
16541654
static inline std::wstring utf8_decode(const std::string& s) {
1655-
std::string curLocale = setlocale(LC_ALL, "");
1655+
auto r = setlocale(LC_ALL, "");
1656+
std::string curLocale;
1657+
if (r)
1658+
curLocale = r;
16561659
const char* _Source = s.c_str();
16571660
size_t _Dsize = std::strlen(_Source) + 1;
16581661
wchar_t* _Dest = new wchar_t[_Dsize];
@@ -1665,7 +1668,10 @@ static inline std::wstring utf8_decode(const std::string& s) {
16651668
}
16661669
#else
16671670
static inline std::wstring utf8_decode(const std::string& s) {
1668-
std::string curLocale = setlocale(LC_ALL, "");
1671+
auto r = setlocale(LC_ALL, "");
1672+
std::string curLocale;
1673+
if (r)
1674+
curLocale = r;
16691675
const char* _Source = s.c_str();
16701676
size_t _Dsize = mbstowcs(NULL, _Source, 0) + 1;
16711677
wchar_t* _Dest = new wchar_t[_Dsize];

0 commit comments

Comments
 (0)