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