]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Add util::lexically_normal
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 8 Nov 2024 15:35:20 +0000 (16:35 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 8 Nov 2024 15:36:17 +0000 (16:36 +0100)
src/ccache/util/path.cpp
src/ccache/util/path.hpp
unittest/test_util_path.cpp

index 83db436be7fc941e77765018bf69e4db301bf36b..fb1b8e7f28e82e519a99468ddb2eaa53bddd1787 100644 (file)
@@ -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,
index aa09172cfbf62d9d623717b00d9170f5396d1cfb..3ede6224a9078f66b9d41ff439c6a24df35e188c 100644 (file)
@@ -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);
 
index 0f2d88764e0f505f4670b2b5ecf868f83a1970aa..838571ea88d7dd6c2ec8a400e951e5f7d3979847 100644 (file)
@@ -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;