From: Joel Rosdahl Date: Sun, 2 Jun 2024 06:58:02 +0000 (+0200) Subject: enhance: Add util::with_extension X-Git-Tag: v4.11~132 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=454b59fcd213589306c457cf44410f1ce3f31382;p=thirdparty%2Fccache.git enhance: Add util::with_extension --- diff --git a/src/ccache/util/path.hpp b/src/ccache/util/path.hpp index e15846c8..3ce20d53 100644 --- a/src/ccache/util/path.hpp +++ b/src/ccache/util/path.hpp @@ -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 diff --git a/unittest/test_util_path.cpp b/unittest/test_util_path.cpp index 23dcedf4..b2919918 100644 --- a/unittest/test_util_path.cpp +++ b/unittest/test_util_path.cpp @@ -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"); +}