QFile: refactor {to,from}FilesystemPath to avoid temporary allocations

Avoiding #ifdef and reinterpret_cast too, to boot.

It was already the case on Windows and for fromFilesystemPath
everywhere, but now we can also benefit from the QString::toStdString()
that also avoids a temporary.

Change-Id: Ie4f0a129d0f2f3653046fffd2ec22713a5516ada
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
This commit is contained in:
Thiago Macieira 2025-01-27 12:18:45 -08:00
parent 2429c73cc9
commit 8379d1e90a

View File

@ -58,17 +58,19 @@ public:
namespace QtPrivate {
inline QString fromFilesystemPath(const std::filesystem::path &path)
{
#ifdef Q_OS_WIN
return QString::fromStdWString(path.native());
#else
return QString::fromStdString(path.native());
#endif
// we could use QAnyStringView, but this allows us to statically determine
// the correct toString() call
using View = std::conditional_t<sizeof(std::filesystem::path::value_type) == sizeof(char16_t),
QStringView, QUtf8StringView>;
return View(path.native()).toString();
}
inline std::filesystem::path toFilesystemPath(const QString &path)
{
return std::filesystem::path(reinterpret_cast<const char16_t *>(path.cbegin()),
reinterpret_cast<const char16_t *>(path.cend()));
if constexpr (sizeof(std::filesystem::path::value_type) == sizeof(char16_t))
return std::u16string_view(QStringView(path));
else
return path.toStdString();
}
// Both std::filesystem::path and QString (without QT_NO_CAST_FROM_ASCII) can be implicitly