From 48f3361e2d5b59bbf5011a3fdf588b6843e263d5 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Thu, 26 Mar 2009 12:00:11 +0100 Subject: [PATCH] improve conversions std::string <=> std::wstring --- src/scrollpad.cpp | 11 ++------- src/scrollpad.h | 1 - src/window.cpp | 57 +++++++++++------------------------------------ src/window.h | 2 -- 4 files changed, 15 insertions(+), 56 deletions(-) diff --git a/src/scrollpad.cpp b/src/scrollpad.cpp index fe43d180..4472f6a8 100644 --- a/src/scrollpad.cpp +++ b/src/scrollpad.cpp @@ -194,17 +194,10 @@ Scrollpad &Scrollpad::operator<<(std::ostream &(*os)(std::ostream&)) } #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) { - return operator<<(s.c_str()); + itsBuffer << ToWString(s); + return *this; } #endif // _UTF8 diff --git a/src/scrollpad.h b/src/scrollpad.h index ab891376..f459a856 100644 --- a/src/scrollpad.h +++ b/src/scrollpad.h @@ -53,7 +53,6 @@ namespace NCurses # ifdef _UTF8 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); # endif // _UTF8 diff --git a/src/window.cpp b/src/window.cpp index a9b3ef08..226ed772 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -386,9 +386,6 @@ string Window::GetString(const string &base, size_t length, size_t width, bool e bool gotoend = 1; bool block_scrolling = 0; - mbstate_t state; - memset(&state, 0, sizeof(state)); - // disable scrolling if wide chars are used for (wstring::const_iterator it = tmp.begin(); it != tmp.end(); it++) if (wcwidth(*it) > 1) @@ -524,7 +521,7 @@ string Window::GetString(const string &base, size_t length, size_t width, bool e break; 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; if (wcwidth(wc_in) > 1) @@ -806,57 +803,29 @@ Window * Window::EmptyClone() const 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 result; - char *s = ToString(ws.c_str()); - if (s) + char *s = new char[MB_CUR_MAX]; + for (size_t i = 0; i < ws.length(); i++) { - result = s; - delete [] s; + int n = wcrtomb(s, ws[i], 0); + if (n > 0) + result.append(s, n); } + delete [] s; return result; } wstring ToWString(const string &s) { wstring result; - wchar_t *ws = ToWString(s.c_str()); - if (ws) - { - result = ws; - delete [] ws; - } + wchar_t *ws = new wchar_t[s.length()]; + const char *c_s = s.c_str(); + int n = mbsrtowcs(ws, &c_s, s.length(), 0); + if (n > 0) + result.append(ws, n); + delete [] ws; return result; } diff --git a/src/window.h b/src/window.h index d393f2a8..270bf797 100644 --- a/src/window.h +++ b/src/window.h @@ -45,8 +45,6 @@ # define TO_WSTRING(x) x #endif -char *ToString(const wchar_t *); -wchar_t *ToWString(const char *); std::string ToString(const std::wstring &); std::wstring ToWString(const std::string &);