diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 0b8749d59e..e878b70216 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -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(); + } +} diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index 4a78dc33a3..ad76abe4d1 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -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;