use cm::string_view for language extension lookups

Once the list of extensions is build the set is just a copy of the vector and
not modified anymore. Use a string_view for the members of the set, which saves
a small amount of memory. It also makes possible to use string_views as lookup
keys, so the callers do not need to create copies for the extensions anymore.
This commit is contained in:
Rolf Eike Beer
2020-03-20 20:30:44 +01:00
parent 48adc29721
commit ada6a3226f
4 changed files with 18 additions and 12 deletions

View File

@@ -6,6 +6,8 @@
#include <cstddef>
#include <utility>
#include <cm/string_view>
#include "cmsys/Directory.hxx"
#include "cmExecutionStatus.h"
@@ -50,11 +52,10 @@ bool cmAuxSourceDirectoryCommand(std::vector<std::string> const& args,
// Split the filename into base and extension
std::string::size_type dotpos = file.rfind('.');
if (dotpos != std::string::npos) {
std::string ext = file.substr(dotpos + 1);
std::string base = file.substr(0, dotpos);
auto ext = cm::string_view(file).substr(dotpos + 1);
// Process only source files
auto cm = mf.GetCMakeInstance();
if (!base.empty() && cm->IsSourceExtension(ext)) {
if (dotpos > 0 && cm->IsSourceExtension(ext)) {
std::string fullname = cmStrCat(templateDirectory, '/', file);
// add the file as a class file so
// depends can be done

View File

@@ -4,6 +4,8 @@
#include <cassert>
#include <cm/string_view>
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmMessageType.h"
@@ -152,7 +154,7 @@ bool cmSourceFileLocation::MatchesAmbiguousExtension(
// Only a fixed set of extensions will be tried to match a file on
// disk. One of these must match if loc refers to this source file.
std::string const& ext = this->Name.substr(loc.Name.size() + 1);
auto ext = cm::string_view(this->Name).substr(loc.Name.size() + 1);
cmMakefile const* mf = this->Makefile;
auto cm = mf->GetCMakeInstance();
return cm->IsSourceExtension(ext) || cm->IsHeaderExtension(ext);

View File

@@ -1978,9 +1978,10 @@ std::string cmake::StripExtension(const std::string& file) const
{
auto dotpos = file.rfind('.');
if (dotpos != std::string::npos) {
auto ext = file.substr(dotpos + 1);
#if defined(_WIN32) || defined(__APPLE__)
ext = cmSystemTools::LowerCase(ext);
auto ext = cmSystemTools::LowerCase(file.substr(dotpos + 1));
#else
auto ext = cm::string_view(file).substr(dotpos + 1);
#endif
if (this->IsSourceExtension(ext) || this->IsHeaderExtension(ext)) {
return file.substr(0, dotpos);

View File

@@ -16,6 +16,8 @@
#include <utility>
#include <vector>
#include <cm/string_view>
#include "cmGeneratedFileStream.h"
#include "cmInstalledFile.h"
#include "cmListFileCache.h"
@@ -138,13 +140,13 @@ public:
struct FileExtensions
{
bool Test(std::string const& ext) const
bool Test(cm::string_view ext) const
{
return (this->unordered.find(ext) != this->unordered.end());
}
std::vector<std::string> ordered;
std::unordered_set<std::string> unordered;
std::unordered_set<cm::string_view> unordered;
};
using InstalledFilesMap = std::map<std::string, cmInstalledFile>;
@@ -266,7 +268,7 @@ public:
return this->SourceFileExtensions.ordered;
}
bool IsSourceExtension(const std::string& ext) const
bool IsSourceExtension(cm::string_view ext) const
{
return this->SourceFileExtensions.Test(ext);
}
@@ -276,7 +278,7 @@ public:
return this->HeaderFileExtensions.ordered;
}
bool IsHeaderExtension(const std::string& ext) const
bool IsHeaderExtension(cm::string_view ext) const
{
return this->HeaderFileExtensions.Test(ext);
}
@@ -286,7 +288,7 @@ public:
return this->CudaFileExtensions.ordered;
}
bool IsCudaExtension(const std::string& ext) const
bool IsCudaExtension(cm::string_view ext) const
{
return this->CudaFileExtensions.Test(ext);
}
@@ -296,7 +298,7 @@ public:
return this->FortranFileExtensions.ordered;
}
bool IsFortranExtension(const std::string& ext) const
bool IsFortranExtension(cm::string_view ext) const
{
return this->FortranFileExtensions.Test(ext);
}