cmSystemTools: Add string_view FileName and Extension Getter

This commit is contained in:
John Franklin Rickard
2026-01-22 19:47:23 +01:00
committed by Brad King
parent f53296147f
commit c99ea2f78e
2 changed files with 38 additions and 0 deletions

View File

@@ -4392,3 +4392,31 @@ char cmSystemTools::GetSystemPathlistSeparator()
return ':';
#endif
}
cm::string_view cmSystemTools::GetFilenameNameView(std::string const& filename)
{
// implementation mostly taken from cmsys::SystemTools
#if defined(_WIN32) || defined(KWSYS_SYSTEMTOOLS_SUPPORT_WINDOWS_SLASHES)
cm::static_string_view separators = "/\\"_s;
#else
char separators = '/';
#endif
std::string::size_type slash_pos = filename.find_last_of(separators);
if (slash_pos != std::string::npos) {
return cm::string_view(filename).substr(slash_pos + 1);
} else {
return cm::string_view(filename);
}
}
cm::string_view cmSystemTools::GetFilenameLastExtensionView(
std::string const& filename)
{
cm::string_view name = cmSystemTools::GetFilenameNameView(filename);
cm::string_view::size_type dot_pos = name.rfind('.');
if (dot_pos != std::string::npos) {
return name.substr(dot_pos);
} else {
return cm::string_view();
}
}

View File

@@ -705,6 +705,16 @@ public:
/** Get the system path separator character */
static char GetSystemPathlistSeparator();
/** Return subview of the full filename (i.e. file name without path) */
static cm::string_view GetFilenameNameView(std::string const& filename);
/**
* Return subview of file extension of a full filename (dot included).
* Warning: this is the shortest extension (for example: .gz of .tar.gz)
*/
static cm::string_view GetFilenameLastExtensionView(
std::string const& filename);
private:
static bool s_ForceUnixPaths;
static bool s_RunCommandHideConsole;