1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2026-01-09 20:31:03 +00:00

Use std::string_view in collections

This commit is contained in:
Evil Eye 2025-09-01 17:08:33 +02:00
parent ea3240ce03
commit 2105e98d0a
4 changed files with 19 additions and 23 deletions

View file

@ -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<MultiDirCollectionContainer::iterator, bool> 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++)
{

View file

@ -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<std::string, MultiDirCollection> MultiDirCollectionContainer;
Files::PathContainer mDirectories;
mutable MultiDirCollectionContainer mCollections;
mutable std::map<std::string, MultiDirCollection, Misc::StringUtils::CiComp> mCollections;
};
}

View file

@ -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

View file

@ -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;