improve conversions std::string <=> std::wstring
This commit is contained in:
@@ -194,17 +194,10 @@ Scrollpad &Scrollpad::operator<<(std::ostream &(*os)(std::ostream&))
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _UTF8
|
#ifdef _UTF8
|
||||||
Scrollpad &Scrollpad::operator<<(const char *s)
|
|
||||||
{
|
|
||||||
wchar_t *ws = ToWString(s);
|
|
||||||
itsBuffer << ws;
|
|
||||||
delete [] ws;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Scrollpad &Scrollpad::operator<<(const std::string &s)
|
Scrollpad &Scrollpad::operator<<(const std::string &s)
|
||||||
{
|
{
|
||||||
return operator<<(s.c_str());
|
itsBuffer << ToWString(s);
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
#endif // _UTF8
|
#endif // _UTF8
|
||||||
|
|
||||||
|
|||||||
@@ -53,7 +53,6 @@ namespace NCurses
|
|||||||
|
|
||||||
# ifdef _UTF8
|
# ifdef _UTF8
|
||||||
void SetFormatting(short vb, const std::string &s, short ve, bool for_each = 1) { SetFormatting(vb, ToWString(s), ve, for_each); }
|
void SetFormatting(short vb, const std::string &s, short ve, bool for_each = 1) { SetFormatting(vb, ToWString(s), ve, for_each); }
|
||||||
Scrollpad &operator<<(const char *s);
|
|
||||||
Scrollpad &operator<<(const std::string &s);
|
Scrollpad &operator<<(const std::string &s);
|
||||||
# endif // _UTF8
|
# endif // _UTF8
|
||||||
|
|
||||||
|
|||||||
@@ -386,9 +386,6 @@ string Window::GetString(const string &base, size_t length, size_t width, bool e
|
|||||||
bool gotoend = 1;
|
bool gotoend = 1;
|
||||||
bool block_scrolling = 0;
|
bool block_scrolling = 0;
|
||||||
|
|
||||||
mbstate_t state;
|
|
||||||
memset(&state, 0, sizeof(state));
|
|
||||||
|
|
||||||
// disable scrolling if wide chars are used
|
// disable scrolling if wide chars are used
|
||||||
for (wstring::const_iterator it = tmp.begin(); it != tmp.end(); it++)
|
for (wstring::const_iterator it = tmp.begin(); it != tmp.end(); it++)
|
||||||
if (wcwidth(*it) > 1)
|
if (wcwidth(*it) > 1)
|
||||||
@@ -524,7 +521,7 @@ string Window::GetString(const string &base, size_t length, size_t width, bool e
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
tmp_in += input;
|
tmp_in += input;
|
||||||
if ((int)mbrtowc(&wc_in, tmp_in.c_str(), MB_CUR_MAX, &state) < 0)
|
if ((int)mbrtowc(&wc_in, tmp_in.c_str(), MB_CUR_MAX, 0) < 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (wcwidth(wc_in) > 1)
|
if (wcwidth(wc_in) > 1)
|
||||||
@@ -806,57 +803,29 @@ Window * Window::EmptyClone() const
|
|||||||
return new Window(GetStartX(), GetStartY(), GetWidth(), GetHeight(), itsTitle, itsBaseColor, itsBorder);
|
return new Window(GetStartX(), GetStartY(), GetWidth(), GetHeight(), itsTitle, itsBaseColor, itsBorder);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ToString(const wchar_t *ws)
|
|
||||||
{
|
|
||||||
mbstate_t mbs;
|
|
||||||
memset(&mbs, 0, sizeof(mbs));
|
|
||||||
size_t len = wcsrtombs(NULL, &ws, 0, &mbs);
|
|
||||||
|
|
||||||
if (len == size_t(-1))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
char *s = new char[len+1]();
|
|
||||||
wcsrtombs(s, &ws, len, &mbs);
|
|
||||||
s[len] = 0;
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
wchar_t *ToWString(const char *s)
|
|
||||||
{
|
|
||||||
mbstate_t mbs;
|
|
||||||
memset(&mbs, 0, sizeof(mbs));
|
|
||||||
size_t len = mbsrtowcs(NULL, &s, 0, &mbs);
|
|
||||||
|
|
||||||
if (len == size_t(-1))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
wchar_t *ws = new wchar_t[len+1]();
|
|
||||||
mbsrtowcs(ws, &s, len, &mbs);
|
|
||||||
ws[len] = 0;
|
|
||||||
return ws;
|
|
||||||
}
|
|
||||||
|
|
||||||
string ToString(const wstring &ws)
|
string ToString(const wstring &ws)
|
||||||
{
|
{
|
||||||
string result;
|
string result;
|
||||||
char *s = ToString(ws.c_str());
|
char *s = new char[MB_CUR_MAX];
|
||||||
if (s)
|
for (size_t i = 0; i < ws.length(); i++)
|
||||||
{
|
{
|
||||||
result = s;
|
int n = wcrtomb(s, ws[i], 0);
|
||||||
delete [] s;
|
if (n > 0)
|
||||||
|
result.append(s, n);
|
||||||
}
|
}
|
||||||
|
delete [] s;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
wstring ToWString(const string &s)
|
wstring ToWString(const string &s)
|
||||||
{
|
{
|
||||||
wstring result;
|
wstring result;
|
||||||
wchar_t *ws = ToWString(s.c_str());
|
wchar_t *ws = new wchar_t[s.length()];
|
||||||
if (ws)
|
const char *c_s = s.c_str();
|
||||||
{
|
int n = mbsrtowcs(ws, &c_s, s.length(), 0);
|
||||||
result = ws;
|
if (n > 0)
|
||||||
|
result.append(ws, n);
|
||||||
delete [] ws;
|
delete [] ws;
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,8 +45,6 @@
|
|||||||
# define TO_WSTRING(x) x
|
# define TO_WSTRING(x) x
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
char *ToString(const wchar_t *);
|
|
||||||
wchar_t *ToWString(const char *);
|
|
||||||
std::string ToString(const std::wstring &);
|
std::string ToString(const std::wstring &);
|
||||||
std::wstring ToWString(const std::string &);
|
std::wstring ToWString(const std::string &);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user