mirror of
https://github.com/PCSX2/pcsx2.git
synced 2026-12-03 19:33:33 +01:00
Common/StringUtil: Add wide string conversion for non-windows
This commit is contained in:
1 parent
e50feb74fd
commit
1a598ccf0d
2 files changed
+35
-10
No files matched your search
+35
-5
@@ -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
|
||||
Reference in new issue
Block a user