From cfae2039c8f02b3a5890f4876236700e60fde02c Mon Sep 17 00:00:00 2001 From: Urs Fleisch Date: Sun, 28 Jan 2024 16:03:11 +0100 Subject: [PATCH] Format and optimize last commit --- taglib/toolkit/tfilestream.cpp | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/taglib/toolkit/tfilestream.cpp b/taglib/toolkit/tfilestream.cpp index 0e170197..9ace7c85 100644 --- a/taglib/toolkit/tfilestream.cpp +++ b/taglib/toolkit/tfilestream.cpp @@ -49,10 +49,6 @@ namespace const FileHandle InvalidFileHandle = INVALID_HANDLE_VALUE; - const std::wstring LongLocalPathPrefix = L"\\\\?\\"; - const std::wstring UNCPathPrefix = L"\\\\"; - const std::wstring LongUNCPathPrefix = L"\\\\?\\UNC\\"; - FileHandle openFile(const FileName &path, bool readOnly) { const DWORD access = readOnly ? GENERIC_READ : (GENERIC_READ | GENERIC_WRITE); @@ -60,25 +56,21 @@ namespace #if defined (PLATFORM_WINRT) return CreateFile2(path.wstr().c_str(), access, FILE_SHARE_READ, OPEN_EXISTING, nullptr); #else - std::wstring fixed_path; - if (path.wstr().length() > MAX_PATH && path.wstr().compare(0, LongLocalPathPrefix.length(), LongLocalPathPrefix) != 0 && path.wstr().compare(0, LongUNCPathPrefix.length(), LongUNCPathPrefix) != 0) - { - if (path.wstr().compare(0, UNCPathPrefix.length(), UNCPathPrefix) == 0) - { - fixed_path.append(LongUNCPathPrefix); - fixed_path.append(path.wstr().substr(2)); + constexpr wchar_t LongLocalPathPrefix[] = L"\\\\?\\"; + constexpr wchar_t UNCPathPrefix[] = L"\\\\"; + constexpr wchar_t LongUNCPathPrefix[] = L"\\\\?\\UNC\\"; + std::wstring pathWStr = path.wstr(); + if(pathWStr.length() > MAX_PATH && + pathWStr.compare(0, std::size(LongLocalPathPrefix) - 1, LongLocalPathPrefix) != 0 && + pathWStr.compare(0, std::size(LongUNCPathPrefix) - 1, LongUNCPathPrefix) != 0) { + if(pathWStr.compare(0, std::size(UNCPathPrefix) - 1, UNCPathPrefix) == 0) { + pathWStr = LongUNCPathPrefix + pathWStr.substr(2); } - else - { - fixed_path.append(LongLocalPathPrefix); - fixed_path.append(path.wstr()); + else { + pathWStr = LongLocalPathPrefix + pathWStr; } } - else - { - fixed_path = path.wstr(); - } - return CreateFileW(fixed_path.c_str(), access, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); + return CreateFileW(pathWStr.c_str(), access, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); #endif }