Common/StringUtil: Add wide string conversion for non-windows

This commit is contained in:
Connor McLaughlin 2021-09-26 18:21:23 +10:00 committed by refractionpcsx2
parent e50feb74fd
commit 1a598ccf0d
2 changed files with 35 additions and 10 deletions

View File

@ -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<int>(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<int>(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

View File

@ -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