]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Add util::add_extension
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 1 Jun 2024 12:04:28 +0000 (14:04 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 30 Jun 2024 15:18:49 +0000 (17:18 +0200)
src/ccache/util/path.hpp
unittest/test_util_path.cpp

index 9a675db4badde3af69c47df6c2168c5c3fccae23..e15846c8c246b03312aeea3ca267b39e9b82891b 100644 (file)
@@ -35,6 +35,11 @@ namespace util {
 // or ".sh".
 std::string add_exe_suffix(const std::string& program);
 
+// Return a new path with `extension` added to `path` (keeping any existing
+// extension).
+std::filesystem::path add_extension(const std::filesystem::path& path,
+                                    std::string_view extension);
+
 // Return current working directory (CWD) by reading the environment variable
 // PWD (thus keeping any symlink parts in the path and potentially ".." or "//"
 // parts). If PWD does not resolve to the same inode as `actual_cwd` then
@@ -79,6 +84,14 @@ using pstr = PathString;
 
 // --- Inline implementations ---
 
+inline std::filesystem::path
+add_extension(const std::filesystem::path& path, std::string_view extension)
+{
+  std::filesystem::path result(path);
+  result += std::filesystem::path(extension);
+  return result;
+}
+
 inline bool
 is_dev_null_path(const std::filesystem::path& path)
 {
index 4e4fbf160e9b67c088f299c168e75195372bc8e5..23dcedf42e322ca21799fc438bca2f22b6d9f032 100644 (file)
@@ -39,6 +39,12 @@ TEST_CASE("util::add_exe_suffix")
   CHECK(util::add_exe_suffix("foo.sh") == "foo.sh");
 }
 
+TEST_CASE("util::add_extension")
+{
+  CHECK(util::add_extension("foo.x", "") == "foo.x");
+  CHECK(util::add_extension("foo.x", ".y") == "foo.x.y");
+}
+
 TEST_CASE("util::is_full_path")
 {
   CHECK(!util::is_full_path(""));