From: Joel Rosdahl Date: Fri, 8 Nov 2024 15:35:20 +0000 (+0100) Subject: enhance: Add util::lexically_normal X-Git-Tag: v4.11~50 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=279dc86b72979b46a0135e895a99409c52a3eaff;p=thirdparty%2Fccache.git enhance: Add util::lexically_normal --- diff --git a/src/ccache/util/path.cpp b/src/ccache/util/path.cpp index 83db436b..fb1b8e7f 100644 --- a/src/ccache/util/path.cpp +++ b/src/ccache/util/path.cpp @@ -67,6 +67,13 @@ get_dev_null_path() return k_dev_null_path; } +fs::path +lexically_normal(const fs::path& path) +{ + auto result = path.lexically_normal(); + return result.has_filename() ? result : result.parent_path(); +} + fs::path make_relative_path(const fs::path& actual_cwd, const fs::path& apparent_cwd, diff --git a/src/ccache/util/path.hpp b/src/ccache/util/path.hpp index aa09172c..3ede6224 100644 --- a/src/ccache/util/path.hpp +++ b/src/ccache/util/path.hpp @@ -48,6 +48,9 @@ std::filesystem::path apparent_cwd(const std::filesystem::path& actual_cwd); const char* get_dev_null_path(); +// Return lexically normal `path` without trailing slash. +std::filesystem::path lexically_normal(const std::filesystem::path& path); + // Return whether `path` is /dev/null or (on Windows) NUL. 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 0f2d8876..838571ea 100644 --- a/unittest/test_util_path.cpp +++ b/unittest/test_util_path.cpp @@ -69,6 +69,16 @@ TEST_CASE("util::is_dev_null_path") #endif } +TEST_CASE("util::lexically_normal") +{ + CHECK(util::lexically_normal("") == ""); + CHECK(util::lexically_normal("/") == "/"); + CHECK(util::lexically_normal("x") == "x"); + CHECK(util::lexically_normal("x/../y") == "y"); + CHECK(util::lexically_normal("x/") == "x"); + CHECK(util::lexically_normal("x/.") == "x"); +} + TEST_CASE("util::make_relative_path") { using util::make_relative_path;