]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Add util::with_extension
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 2 Jun 2024 06:58:02 +0000 (08:58 +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 e15846c8c246b03312aeea3ca267b39e9b82891b..3ce20d5357206915e3242565b06fbc3dc5b84cd5 100644 (file)
@@ -82,6 +82,11 @@ bool path_starts_with(const std::filesystem::path& path,
 // std::filesystem::path::value_type is char (that is, not wchar_t).
 using pstr = PathString;
 
+// Return a new path with `extension` added to `path` (removing any existing
+// extension).
+std::filesystem::path with_extension(const std::filesystem::path& path,
+                                     std::string_view extension);
+
 // --- Inline implementations ---
 
 inline std::filesystem::path
@@ -113,4 +118,12 @@ is_full_path(const std::string_view path)
   return path.find('/') != std::string_view::npos;
 }
 
+inline std::filesystem::path
+with_extension(const std::filesystem::path& path, std::string_view extension)
+{
+  std::filesystem::path result(path);
+  result.replace_extension(extension);
+  return result;
+}
+
 } // namespace util
index 23dcedf42e322ca21799fc438bca2f22b6d9f032..b29199182b2a9aeed4b7230ff30f76a23bbc024d 100644 (file)
@@ -147,3 +147,9 @@ TEST_CASE("util::path_starts_with")
   CHECK(!util::path_starts_with("C:\\beh\\foo", "C:/foo"));
 #endif
 }
+
+TEST_CASE("util::with_extension")
+{
+  CHECK(util::with_extension("foo.x", "") == "foo");
+  CHECK(util::with_extension("foo.x", ".y") == "foo.y");
+}