diff options
author | Rolf Eike Beer <eike@sf-mail.de> | 2020-03-20 19:30:44 (GMT) |
---|---|---|
committer | Rolf Eike Beer <eike@sf-mail.de> | 2020-03-23 21:41:43 (GMT) |
commit | ada6a3226f678df8cc83b752c06c404336718f43 (patch) | |
tree | abe5a5b8ceb6cc3f565751c02bed04b418f79780 | |
parent | 48adc297211181a97fab75ef0260fda5147c1195 (diff) | |
download | CMake-ada6a3226f678df8cc83b752c06c404336718f43.zip CMake-ada6a3226f678df8cc83b752c06c404336718f43.tar.gz CMake-ada6a3226f678df8cc83b752c06c404336718f43.tar.bz2 |
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.
-rw-r--r-- | Source/cmAuxSourceDirectoryCommand.cxx | 7 | ||||
-rw-r--r-- | Source/cmSourceFileLocation.cxx | 4 | ||||
-rw-r--r-- | Source/cmake.cxx | 5 | ||||
-rw-r--r-- | Source/cmake.h | 14 |
4 files changed, 18 insertions, 12 deletions
diff --git a/Source/cmAuxSourceDirectoryCommand.cxx b/Source/cmAuxSourceDirectoryCommand.cxx index 289bb72..d6f7500e 100644 --- a/Source/cmAuxSourceDirectoryCommand.cxx +++ b/Source/cmAuxSourceDirectoryCommand.cxx @@ -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 diff --git a/Source/cmSourceFileLocation.cxx b/Source/cmSourceFileLocation.cxx index 5f807b8..e852c05 100644 --- a/Source/cmSourceFileLocation.cxx +++ b/Source/cmSourceFileLocation.cxx @@ -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); diff --git a/Source/cmake.cxx b/Source/cmake.cxx index d0a0593..4be4820 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -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); diff --git a/Source/cmake.h b/Source/cmake.h index 58769fd..0f6440f 100644 --- a/Source/cmake.h +++ b/Source/cmake.h @@ -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); } |