From: Joel Rosdahl Date: Sat, 1 Jun 2024 12:04:28 +0000 (+0200) Subject: enhance: Add util::add_extension X-Git-Tag: v4.11~133 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ee77132a588b6bd617c74f9f6bed20745adc7a1a;p=thirdparty%2Fccache.git enhance: Add util::add_extension --- diff --git a/src/ccache/util/path.hpp b/src/ccache/util/path.hpp index 9a675db4..e15846c8 100644 --- a/src/ccache/util/path.hpp +++ b/src/ccache/util/path.hpp @@ -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) { diff --git a/unittest/test_util_path.cpp b/unittest/test_util_path.cpp index 4e4fbf16..23dcedf4 100644 --- a/unittest/test_util_path.cpp +++ b/unittest/test_util_path.cpp @@ -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(""));