Ignoring wstring_convert deprecation warnings (#498)

This commit is contained in:
wflohry 2022-11-19 06:22:44 -07:00 committed by GitHub
parent 2c5681ee20
commit 121bd0d046
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 14 deletions

View File

@ -43,6 +43,7 @@ current (development)
- Bugfix: Fix resetting `dim` clashing with resetting of `bold`. - Bugfix: Fix resetting `dim` clashing with resetting of `bold`.
- Feature: Add emscripten screen resize support. - Feature: Add emscripten screen resize support.
- Bugfix: Add unicode 13 support for full width characters. - Bugfix: Add unicode 13 support for full width characters.
- Bugfix: Fix MSVC treating codecvt C++17 deprecated function as an error.
3.0.0 3.0.0
----- -----

View File

@ -14,7 +14,15 @@
#include <string> // for string, basic_string, wstring #include <string> // for string, basic_string, wstring
#include <tuple> // for std::ignore #include <tuple> // for std::ignore
#include "ftxui/screen/deprecated.hpp" // for wchar_width, wstring_width // `codecvt_utf8_utf16 is deprecated in C++17. However there are no replacement.
// Microsoft provides one, but that's not standardized. Hence the two code path.
#if defined(_WIN32)
#include <windows.h>
#include <stringapiset.h>
#else
#include <codecvt> // for codecvt_utf8_utf16
#include <locale> // for wstring_convert
#endif
namespace { namespace {
@ -1855,26 +1863,35 @@ std::vector<WordBreakProperty> Utf8ToWordBreakProperty(
return out; return out;
} }
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996) // codecvt_utf8_utf16 is deprecated
#endif
/// Convert a UTF8 std::string into a std::wstring. /// Convert a UTF8 std::string into a std::wstring.
std::string to_string(const std::wstring& s) { std::string to_string(const std::wstring& s) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; #if defined(_WIN32)
return converter.to_bytes(s); if (s.empty())
return std::string();
int size = WideCharToMultiByte(CP_UTF8, 0, &s[0], (int)s.size(), nullptr, 0,
nullptr, nullptr);
std::string out(size, 0);
WideCharToMultiByte(CP_UTF8, 0, &s[0], (int)s.size(), &out[0], size, nullptr,
nullptr);
return out;
#else
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(s);
#endif
} }
/// Convert a std::wstring into a UTF8 std::string. /// Convert a std::wstring into a UTF8 std::string.
std::wstring to_wstring(const std::string& s) { std::wstring to_wstring(const std::string& s) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; #if defined(_WIN32)
return converter.from_bytes(s); if (s.empty())
} return std::wstring();
int size = MultiByteToWideChar(CP_UTF8, 0, &s[0], (int)s.size(), nullptr, 0);
#ifdef _MSC_VER std::wstring out(size, 0);
#pragma warning(pop) MultiByteToWideChar(CP_UTF8, 0, &s[0], (int)s.size(), &out[0], size);
return out;
#else
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().from_bytes(s);
#endif #endif
}
} // namespace ftxui } // namespace ftxui