From 484e27c109a1f8107730a897f0f23362b912cfb0 Mon Sep 17 00:00:00 2001 From: Chris Burel Date: Mon, 13 Dec 2021 09:02:03 -0800 Subject: [PATCH] Use AZStd::equal to implement path comparison Signed-off-by: Chris Burel --- .../native/FileWatcher/FileWatcher.cpp | 49 +++++++++---------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/Code/Tools/AssetProcessor/native/FileWatcher/FileWatcher.cpp b/Code/Tools/AssetProcessor/native/FileWatcher/FileWatcher.cpp index c1c1a29d74..f802bdbada 100644 --- a/Code/Tools/AssetProcessor/native/FileWatcher/FileWatcher.cpp +++ b/Code/Tools/AssetProcessor/native/FileWatcher/FileWatcher.cpp @@ -13,46 +13,43 @@ //! IsSubfolder(folderA, folderB) //! returns whether folderA is a subfolder of folderB -//! assumptions: absolute paths, case insensitive +//! assumptions: absolute paths static bool IsSubfolder(const QString& folderA, const QString& folderB) { // lets avoid allocating or messing with memory - this is a MAJOR hotspot as it is called for any file change even in the cache! - int sizeB = folderB.length(); - int sizeA = folderA.length(); - - if (sizeA <= sizeB) + if (folderA.length() <= folderB.length()) { return false; } - QChar slash1 = QChar('\\'); - QChar slash2 = QChar('/'); - int posA = 0; + using AZStd::begin; + using AZStd::end; - // A is going to be the longer one, so use B: - for (int idx = 0; idx < sizeB; ++idx) + constexpr auto isSlash = [](const QChar c) constexpr { - QChar charAtA = folderA.at(posA); - QChar charAtB = folderB.at(idx); + return c == AZ::IO::WindowsPathSeparator || c == AZ::IO::PosixPathSeparator; + }; - if ((charAtB == slash1) || (charAtB == slash2)) + const auto firstPathSeparator = AZStd::find_if(begin(folderB), end(folderB), [&isSlash](const QChar c) + { + return isSlash(c); + }); + + // Follow the convention used by AZ::IO::Path, and use a case-sensitive comparison on Posix paths + const bool useCaseSensitiveCompare = (firstPathSeparator == end(folderB)) ? true : (*firstPathSeparator == AZ::IO::PosixPathSeparator); + + return AZStd::equal(begin(folderB), end(folderB), begin(folderA), [isSlash, useCaseSensitiveCompare](const QChar charAtB, const QChar charAtA) + { + if (isSlash(charAtA)) { - if ((charAtA != slash1) && (charAtA != slash2)) - { - return false; - } - ++posA; + return isSlash(charAtB); } - else + if (useCaseSensitiveCompare) { - if (charAtA.toLower() != charAtB.toLower()) - { - return false; - } - ++posA; + return charAtA == charAtB; } - } - return true; + return charAtA.toLower() == charAtB.toLower(); + }); } //////////////////////////////////////////////////////////////////////////