Reduce string copies by using View function alternatives

This commit is contained in:
John Franklin Rickard
2026-02-06 12:27:13 -05:00
committed by Brad King
parent 62a4099fd2
commit e531e7420d
15 changed files with 36 additions and 35 deletions
+3 -3
View File
@@ -769,7 +769,7 @@ bool cmDyndepCollation::WriteDyndepMetadata(
auto m = cb.ModuleFile(p.LogicalName);
if (m) {
install_bmi_path = cmStrCat(
bmi_destination, cmEscape(cmSystemTools::GetFilenameName(*m)));
bmi_destination, cmEscape(cmSystemTools::GetFilenameNameView(*m)));
build_bmi_path = cmEscape(*m);
}
@@ -780,7 +780,7 @@ bool cmDyndepCollation::WriteDyndepMetadata(
if (exp.Export->Install && file_set.Destination) {
auto rel =
cmStrCat('/', file_set.RelativeDirectory,
cmSystemTools::GetFilenameName(file_set.SourcePath));
cmSystemTools::GetFilenameNameView(file_set.SourcePath));
iface_source = cmStrCat(
install_destination(*file_set.Destination).second, cmEscape(rel));
mod.SourcePath = cmStrCat(*file_set.Destination, rel);
@@ -853,7 +853,7 @@ bool cmDyndepCollation::WriteDyndepMetadata(
*bmi_install_script
<< " list(APPEND CMAKE_ABSOLUTE_DESTINATION_FILES\n"
" \""
<< cmEscape(cmSystemTools::GetFilenameName(*m))
<< cmEscape(cmSystemTools::GetFilenameNameView(*m))
<< "\")\n"
" if (CMAKE_WARN_ON_ABSOLUTE_INSTALL_DESTINATION)\n"
" message(WARNING\n"
+3 -3
View File
@@ -131,7 +131,7 @@ static bool HandleTargetsMode(std::vector<std::string> const& args,
fname = arguments.ExportSetName + ".cmake";
} else {
// Make sure the file has a .cmake extension.
if (cmSystemTools::GetFilenameLastExtension(arguments.Filename) !=
if (cmSystemTools::GetFilenameLastExtensionView(arguments.Filename) !=
".cmake") {
std::ostringstream e;
e << "FILE option given filename \"" << arguments.Filename
@@ -296,7 +296,7 @@ static bool HandleExportMode(std::vector<std::string> const& args,
if (arguments.Filename.empty()) {
fname = arguments.ExportSetName + ".cmake";
} else {
if (cmSystemTools::GetFilenameLastExtension(arguments.Filename) !=
if (cmSystemTools::GetFilenameLastExtensionView(arguments.Filename) !=
".cmake") {
std::ostringstream e;
e << "FILE option given filename \"" << arguments.Filename
@@ -382,7 +382,7 @@ static bool HandleSpecialExportMode(std::vector<std::string> const& args,
if (gg->GetExportedTargetsFile(fname)) {
status.SetError(cmStrCat("command already specified for the file "_s,
cmSystemTools::GetFilenameName(fname), '.'));
cmSystemTools::GetFilenameNameView(fname), '.'));
return false;
}
@@ -382,7 +382,7 @@ std::string cmExportInstallCMakeConfigGenerator::GetFileSetFiles(
auto prefix = it.first.empty() ? "" : cmStrCat(it.first, '/');
for (auto const& filename : it.second) {
auto relFile =
cmStrCat(prefix, cmSystemTools::GetFilenameName(filename));
cmStrCat(prefix, cmSystemTools::GetFilenameNameView(filename));
auto escapedFile =
cmStrCat(dest,
cmOutputConverter::EscapeForCMake(
+1 -1
View File
@@ -1167,7 +1167,7 @@ Json::Value DirectoryObject::DumpInstaller(cmInstallGenerator* gen)
for (auto const& file : it.second) {
files.append(this->DumpInstallerPath(
this->TopSource, file,
cmStrCat(dir, cmSystemTools::GetFilenameName(file))));
cmStrCat(dir, cmSystemTools::GetFilenameNameView(file))));
}
}
installer["paths"] = std::move(files);
+1 -1
View File
@@ -881,7 +881,7 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
this->AddPathSuffix(args[i]);
} else if (doing == DoingConfigs) {
if (args[i].find_first_of(":/\\") != std::string::npos ||
cmSystemTools::GetFilenameLastExtension(args[i]) != ".cmake") {
cmSystemTools::GetFilenameLastExtensionView(args[i]) != ".cmake") {
this->SetError(cmStrCat(
"given CONFIGS option followed by invalid file name \"", args[i],
"\". The names given must be file names without "
+2 -2
View File
@@ -2826,7 +2826,7 @@ cmGlobalGenerator::SplitFrameworkPath(std::string const& path,
static cmsys::RegularExpression frameworkPath(
"((.+)/)?([^/]+)\\.framework(/Versions/([^/]+))?(/(.+))?$");
auto ext = cmSystemTools::GetFilenameLastExtension(path);
auto ext = cmSystemTools::GetFilenameLastExtensionView(path);
if ((ext.empty() || ext == ".tbd" || ext == ".framework") &&
frameworkPath.find(path)) {
auto name = frameworkPath.match(3);
@@ -2851,7 +2851,7 @@ cmGlobalGenerator::SplitFrameworkPath(std::string const& path,
if (format == FrameworkFormat::Extended) {
// path format can be more flexible: (/path/to/)?fwName(.framework)?
auto fwDir = cmSystemTools::GetParentDirectory(path);
auto name = cmSystemTools::GetFilenameLastExtension(path) == ".framework"
auto name = ext == ".framework"
? cmSystemTools::GetFilenameWithoutExtension(path)
: cmSystemTools::GetFilenameName(path);
+2 -2
View File
@@ -1263,8 +1263,8 @@ std::string cmGlobalVisualStudio10Generator::GenerateRuleFile(
std::string ruleDir = cmStrCat(
this->GetCMakeInstance()->GetHomeOutputDirectory(), "/CMakeFiles/",
hasher.HashString(cmSystemTools::GetFilenamePath(output)));
std::string ruleFile =
cmStrCat(ruleDir, '/', cmSystemTools::GetFilenameName(output), ".rule");
std::string ruleFile = cmStrCat(
ruleDir, '/', cmSystemTools::GetFilenameNameView(output), ".rule");
return ruleFile;
}
+2 -6
View File
@@ -1363,14 +1363,10 @@ cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeFileReferenceFromPath(
}
// Make a copy so that we can override it later
std::string path = cmSystemTools::CollapseFullPath(fullpath);
// Compute the extension without leading '.'.
std::string ext = cmSystemTools::GetFilenameLastExtension(path);
if (!ext.empty()) {
ext = ext.substr(1);
}
if (fileType.empty()) {
path = this->GetLibraryOrFrameworkPath(path);
ext = cmSystemTools::GetFilenameLastExtension(path);
// Compute the extension without leading '.'.
std::string ext = cmSystemTools::GetFilenameLastExtension(path);
if (!ext.empty()) {
ext = ext.substr(1);
}
+2 -2
View File
@@ -1991,7 +1991,7 @@ bool HandleExportAndroidMKMode(std::vector<std::string> const& args,
// Check the file extension.
if (!fname.empty() &&
cmSystemTools::GetFilenameLastExtension(fname) != ".mk") {
cmSystemTools::GetFilenameLastExtensionView(fname) != ".mk") {
status.SetError(cmStrCat(
args[0], " given invalid export file name \"", fname,
R"(". The FILE argument must specify a name ending in ".mk".)"));
@@ -2250,7 +2250,7 @@ bool HandleExportMode(std::vector<std::string> const& args,
// Check the file extension.
if (!fname.empty() &&
cmSystemTools::GetFilenameLastExtension(fname) != ".cmake") {
cmSystemTools::GetFilenameLastExtensionView(fname) != ".cmake") {
status.SetError(
cmStrCat(args[0], " given invalid export file name \"", fname,
"\". "
+5 -1
View File
@@ -5,6 +5,10 @@
#include <sstream>
#include <utility>
#if __cplusplus >= 201703L || defined(_MSVC_LANG) && _MSVC_LANG >= 201703L
# include <string_view> // To fix IWYU warning
#endif
#include "cmMakefile.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
@@ -84,7 +88,7 @@ void cmInstallGenerator::AddInstallRule(
if (rename && *rename) {
os << rename;
} else {
os << cmSystemTools::GetFilenameName(file);
os << cmSystemTools::GetFilenameNameView(file);
}
firstIteration = false;
}
+2 -4
View File
@@ -2321,7 +2321,7 @@ bool cmLocalGenerator::GetRealDependency(std::string const& inName,
if (name.empty()) {
return false;
}
if (cmSystemTools::GetFilenameLastExtension(name) == ".exe") {
if (cmSystemTools::GetFilenameLastExtensionView(name) == ".exe") {
name = cmSystemTools::GetFilenameWithoutLastExtension(name);
}
@@ -4693,8 +4693,6 @@ std::string cmLocalGenerator::GetShortObjectFileName(
cmSourceFile const& source) const
{
std::string objectName = this->GetRelativeSourceFileName(source);
std::string objectFileName =
cmSystemTools::GetFilenameName(source.GetFullPath());
cmCryptoHash objNameHasher(cmCryptoHash::AlgoSHA3_512);
std::string terseObjectName =
objNameHasher.HashString(objectName).substr(0, 8);
@@ -5200,7 +5198,7 @@ std::vector<std::string> ComputeISPCExtraObjects(
std::vector<std::string> computedObjects;
computedObjects.reserve(ispcSuffixes.size());
auto extension = cmSystemTools::GetFilenameLastExtension(objectName);
auto extension = cmSystemTools::GetFilenameLastExtensionView(objectName);
// We can't use cmSystemTools::GetFilenameWithoutLastExtension as it
// drops any directories in objectName
+3 -2
View File
@@ -340,14 +340,15 @@ void cmMakefile::PrintCommandTrace(cmListFileFunction const& lff,
std::vector<std::string> const& trace_only_this_files =
this->GetCMakeInstance()->GetTraceSources();
std::string const& full_path = bt.Top().FilePath;
std::string const& only_filename = cmSystemTools::GetFilenameName(full_path);
cm::string_view only_filename =
cmSystemTools::GetFilenameNameView(full_path);
bool trace = trace_only_this_files.empty();
if (!trace) {
for (std::string const& file : trace_only_this_files) {
std::string::size_type const pos = full_path.rfind(file);
trace = (pos != std::string::npos) &&
((pos + file.size()) == full_path.size()) &&
(only_filename == cmSystemTools::GetFilenameName(file));
(only_filename == cmSystemTools::GetFilenameNameView(file));
if (trace) {
break;
}
+3 -3
View File
@@ -263,11 +263,11 @@ bool cmSourceFile::FindFullPath(std::string* error,
void cmSourceFile::CheckExtension()
{
// Compute the extension.
std::string realExt =
cmSystemTools::GetFilenameLastExtension(this->FullPath);
cm::string_view realExt =
cmSystemTools::GetFilenameLastExtensionView(this->FullPath);
if (!realExt.empty()) {
// Store the extension without the leading '.'.
this->Extension = realExt.substr(1);
this->Extension = std::string(realExt.substr(1));
}
// Look for object files.
+1 -1
View File
@@ -103,7 +103,7 @@ void cmSourceFileLocation::UpdateExtension(std::string const& name)
{
assert(this->Makefile);
// Check the extension.
std::string ext = cmSystemTools::GetFilenameLastExtension(name);
cm::string_view ext = cmSystemTools::GetFilenameLastExtensionView(name);
if (!ext.empty()) {
ext = ext.substr(1);
}
+5 -3
View File
@@ -167,9 +167,11 @@ bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
// no output-name property which old code would not have set). In
// that case the target name will be the file basename of the
// dependency.
std::string util = cmSystemTools::GetFilenameName(dep);
if (cmSystemTools::GetFilenameLastExtension(util) == ".exe") {
util = cmSystemTools::GetFilenameWithoutLastExtension(util);
std::string util;
if (cmSystemTools::GetFilenameLastExtensionView(dep) == ".exe") {
util = cmSystemTools::GetFilenameWithoutLastExtension(dep);
} else {
util = cmSystemTools::GetFilenameName(dep);
}
// Check for a target with this name.