From 2105e98d0ab283b7fd8cc621a75cc7ba01dca4c2 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Mon, 1 Sep 2025 17:08:33 +0200 Subject: [PATCH] Use std::string_view in collections --- components/files/collections.cpp | 14 ++++++-------- components/files/collections.hpp | 11 +++++------ components/files/multidircollection.cpp | 10 +++++----- components/files/multidircollection.hpp | 7 +++---- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/components/files/collections.cpp b/components/files/collections.cpp index a1b444a746..6c70c0d3b9 100644 --- a/components/files/collections.cpp +++ b/components/files/collections.cpp @@ -18,14 +18,12 @@ namespace Files { } - const MultiDirCollection& Collections::getCollection(const std::string& extension) const + const MultiDirCollection& Collections::getCollection(std::string_view extension) const { - std::string ext = Misc::StringUtils::lowerCase(extension); - auto iter = mCollections.find(ext); + auto iter = mCollections.find(extension); if (iter == mCollections.end()) { - std::pair result - = mCollections.emplace(ext, MultiDirCollection(mDirectories, ext)); + auto result = mCollections.emplace(extension, MultiDirCollection(mDirectories, extension)); iter = result.first; } @@ -33,7 +31,7 @@ namespace Files return iter->second; } - std::filesystem::path Collections::getPath(const std::string& file) const + std::filesystem::path Collections::getPath(std::string_view file) const { for (auto iter = mDirectories.rbegin(); iter != mDirectories.rend(); iter++) { @@ -47,10 +45,10 @@ namespace Files } } - throw std::runtime_error("file " + file + " not found"); + throw std::runtime_error("file " + std::string(file) + " not found"); } - bool Collections::doesExist(const std::string& file) const + bool Collections::doesExist(std::string_view file) const { for (auto iter = mDirectories.rbegin(); iter != mDirectories.rend(); iter++) { diff --git a/components/files/collections.hpp b/components/files/collections.hpp index 88e047aa45..e87eab3e44 100644 --- a/components/files/collections.hpp +++ b/components/files/collections.hpp @@ -16,26 +16,25 @@ namespace Files Collections(const Files::PathContainer& directories); ///< Return a file collection for the given extension. Extension must contain the - /// leading dot and must be all lower-case. - const MultiDirCollection& getCollection(const std::string& extension) const; + /// leading dot + const MultiDirCollection& getCollection(std::string_view extension) const; - std::filesystem::path getPath(const std::string& file) const; + std::filesystem::path getPath(std::string_view file) const; ///< Return full path (including filename) of \a file. /// /// If the file does not exist in any of the collection's /// directories, an exception is thrown. \a file must include the /// extension. - bool doesExist(const std::string& file) const; + bool doesExist(std::string_view file) const; ///< \return Does a file with the given name exist? const Files::PathContainer& getPaths() const; private: - typedef std::map MultiDirCollectionContainer; Files::PathContainer mDirectories; - mutable MultiDirCollectionContainer mCollections; + mutable std::map mCollections; }; } diff --git a/components/files/multidircollection.cpp b/components/files/multidircollection.cpp index 16c9251f24..dda5f17d1c 100644 --- a/components/files/multidircollection.cpp +++ b/components/files/multidircollection.cpp @@ -8,7 +8,7 @@ namespace Files { - MultiDirCollection::MultiDirCollection(const Files::PathContainer& directories, const std::string& extension) + MultiDirCollection::MultiDirCollection(const Files::PathContainer& directories, std::string_view extension) { for (const auto& directory : directories) { @@ -47,19 +47,19 @@ namespace Files } } - std::filesystem::path MultiDirCollection::getPath(const std::string& file) const + std::filesystem::path MultiDirCollection::getPath(std::string_view file) const { TIter iter = mFiles.find(file); if (iter == mFiles.end()) - throw std::runtime_error("file " + file + " not found"); + throw std::runtime_error("file " + std::string(file) + " not found"); return iter->second; } - bool MultiDirCollection::doesExist(const std::string& file) const + bool MultiDirCollection::doesExist(std::string_view file) const { - return mFiles.find(file) != mFiles.end(); + return mFiles.contains(file); } MultiDirCollection::TIter MultiDirCollection::begin() const diff --git a/components/files/multidircollection.hpp b/components/files/multidircollection.hpp index f6549f9ddb..aae681a743 100644 --- a/components/files/multidircollection.hpp +++ b/components/files/multidircollection.hpp @@ -28,19 +28,18 @@ namespace Files TContainer mFiles; public: - MultiDirCollection(const Files::PathContainer& directories, const std::string& extension); + MultiDirCollection(const Files::PathContainer& directories, std::string_view extension); ///< Directories are listed with increasing priority. /// \param extension The extension that should be listed in this collection. Must /// contain the leading dot. - /// \param foldCase Ignore filename case - std::filesystem::path getPath(const std::string& file) const; + std::filesystem::path getPath(std::string_view file) const; ///< Return full path (including filename) of \a file. /// /// If the file does not exist, an exception is thrown. \a file must include /// the extension. - bool doesExist(const std::string& file) const; + bool doesExist(std::string_view file) const; ///< \return Does a file with the given name exist? TIter begin() const;