From 121bd0d0467c0ff43b732f321d54260017089632 Mon Sep 17 00:00:00 2001 From: wflohry Date: Sat, 19 Nov 2022 06:22:44 -0700 Subject: [PATCH] Ignoring wstring_convert deprecation warnings (#498) --- CHANGELOG.md | 1 + src/ftxui/screen/string.cpp | 45 +++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4a678f..b23b588 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ current (development) - Bugfix: Fix resetting `dim` clashing with resetting of `bold`. - Feature: Add emscripten screen resize support. - Bugfix: Add unicode 13 support for full width characters. +- Bugfix: Fix MSVC treating codecvt C++17 deprecated function as an error. 3.0.0 ----- diff --git a/src/ftxui/screen/string.cpp b/src/ftxui/screen/string.cpp index 438a999..175f02c 100644 --- a/src/ftxui/screen/string.cpp +++ b/src/ftxui/screen/string.cpp @@ -14,7 +14,15 @@ #include // for string, basic_string, wstring #include // 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 +#include +#else +#include // for codecvt_utf8_utf16 +#include // for wstring_convert +#endif namespace { @@ -1855,26 +1863,35 @@ std::vector Utf8ToWordBreakProperty( 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. std::string to_string(const std::wstring& s) { - std::wstring_convert> converter; - return converter.to_bytes(s); +#if defined(_WIN32) + 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>().to_bytes(s); +#endif } /// Convert a std::wstring into a UTF8 std::string. std::wstring to_wstring(const std::string& s) { - std::wstring_convert> converter; - return converter.from_bytes(s); -} - -#ifdef _MSC_VER -#pragma warning(pop) +#if defined(_WIN32) + if (s.empty()) + return std::wstring(); + int size = MultiByteToWideChar(CP_UTF8, 0, &s[0], (int)s.size(), nullptr, 0); + std::wstring out(size, 0); + MultiByteToWideChar(CP_UTF8, 0, &s[0], (int)s.size(), &out[0], size); + return out; +#else + return std::wstring_convert>().from_bytes(s); #endif +} } // namespace ftxui