diff --git a/common/StringUtil.cpp b/common/StringUtil.cpp index 0fa7409578..3fc15b1661 100644 --- a/common/StringUtil.cpp +++ b/common/StringUtil.cpp @@ -236,8 +236,6 @@ namespace StringUtil return lines; } -#ifdef _WIN32 - std::wstring UTF8StringToWideString(const std::string_view& str) { std::wstring ret; @@ -249,6 +247,7 @@ namespace StringUtil bool UTF8StringToWideString(std::wstring& dest, const std::string_view& str) { +#ifdef _WIN32 int wlen = MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast(str.length()), nullptr, 0); if (wlen < 0) return false; @@ -258,19 +257,36 @@ namespace StringUtil return false; return true; +#else + // This depends on wxString, which isn't great. But hopefully we won't need any wide strings outside + // of windows once wx is gone anyway. + if (str.empty()) + { + dest.clear(); + return true; + } + + const wxString wxstr(wxString::FromUTF8(str.data(), str.length())); + if (wxstr.IsEmpty()) + return false; + + dest = wxstr.ToStdWstring(); + return true; +#endif } std::string WideStringToUTF8String(const std::wstring_view& str) { std::string ret; if (!WideStringToUTF8String(ret, str)) - return {}; + ret.clear(); return ret; } bool WideStringToUTF8String(std::string& dest, const std::wstring_view& str) { +#ifdef _WIN32 int mblen = WideCharToMultiByte(CP_UTF8, 0, str.data(), static_cast(str.length()), nullptr, 0, nullptr, nullptr); if (mblen < 0) return false; @@ -283,8 +299,22 @@ namespace StringUtil } return true; - } +#else + // This depends on wxString, which isn't great. But hopefully we won't need any wide strings outside + // of windows once wx is gone anyway. + if (str.empty()) + { + dest.clear(); + return true; + } + const wxString wxstr(str.data(), str.data() + str.length()); + if (wxstr.IsEmpty()) + return false; + + const auto buf = wxstr.ToUTF8(); + dest.assign(buf.data(), buf.length()); + return true; #endif - + } } // namespace StringUtil diff --git a/common/StringUtil.h b/common/StringUtil.h index d7a31056a2..808d6493c5 100644 --- a/common/StringUtil.h +++ b/common/StringUtil.h @@ -207,8 +207,6 @@ namespace StringUtil return wxString::FromUTF8(str.data(), str.length()); } -#ifdef _WIN32 - /// Converts the specified UTF-8 string to a wide string. std::wstring UTF8StringToWideString(const std::string_view& str); bool UTF8StringToWideString(std::wstring& dest, const std::string_view& str); @@ -216,7 +214,4 @@ namespace StringUtil /// Converts the specified wide string to a UTF-8 string. std::string WideStringToUTF8String(const std::wstring_view& str); bool WideStringToUTF8String(std::string& dest, const std::wstring_view& str); - -#endif - } // namespace StringUtil