From 768b5cb11b39c54ae0cb97a4c9d1ba25dc1cdfd9 Mon Sep 17 00:00:00 2001 From: vgeorgiev Date: Wed, 21 Feb 2024 11:07:19 -0600 Subject: [PATCH 1/2] Fix lexically_relative return when base path evaluates to *this --- include/ghc/filesystem.hpp | 3 +++ test/filesystem_test.cpp | 3 +++ 2 files changed, 6 insertions(+) diff --git a/include/ghc/filesystem.hpp b/include/ghc/filesystem.hpp index 8aa4167..5384e7b 100644 --- a/include/ghc/filesystem.hpp +++ b/include/ghc/filesystem.hpp @@ -3306,6 +3306,9 @@ GHC_INLINE path path::lexically_relative(const path& base) const --count; } } + if (count == 0 && (a == end() || empty())) { + return path("."); + } if (count < 0) { return path(); } diff --git a/test/filesystem_test.cpp b/test/filesystem_test.cpp index 4a06c23..d8cc024 100644 --- a/test/filesystem_test.cpp +++ b/test/filesystem_test.cpp @@ -965,6 +965,9 @@ TEST_CASE("fs.path.gen - path generation", "[filesystem][path][fs.path.gen]") // lexically_relative() CHECK(fs::path("/a/d").lexically_relative("/a/b/c") == "../../d"); CHECK(fs::path("/a/b/c").lexically_relative("/a/d") == "../b/c"); + CHECK(fs::path("/a/b/c").lexically_relative("/a/b/c/d/..") == "."); + CHECK(fs::path("").lexically_relative("/a/..") == ""); + CHECK(fs::path("").lexically_relative("a/..") == "."); CHECK(fs::path("a/b/c").lexically_relative("a") == "b/c"); CHECK(fs::path("a/b/c").lexically_relative("a/b/c/x/y") == "../.."); CHECK(fs::path("a/b/c").lexically_relative("a/b/c") == "."); From eeed3142371745467d3ace3599db0744a86c53ad Mon Sep 17 00:00:00 2001 From: vgeorgiev Date: Wed, 21 Feb 2024 17:55:00 -0600 Subject: [PATCH 2/2] Fix handling of trailing slash --- include/ghc/filesystem.hpp | 2 +- test/filesystem_test.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/include/ghc/filesystem.hpp b/include/ghc/filesystem.hpp index 5384e7b..1770d73 100644 --- a/include/ghc/filesystem.hpp +++ b/include/ghc/filesystem.hpp @@ -3306,7 +3306,7 @@ GHC_INLINE path path::lexically_relative(const path& base) const --count; } } - if (count == 0 && (a == end() || empty())) { + if (count == 0 && (a == end() || a->empty())) { return path("."); } if (count < 0) { diff --git a/test/filesystem_test.cpp b/test/filesystem_test.cpp index d8cc024..0be179f 100644 --- a/test/filesystem_test.cpp +++ b/test/filesystem_test.cpp @@ -966,6 +966,7 @@ TEST_CASE("fs.path.gen - path generation", "[filesystem][path][fs.path.gen]") CHECK(fs::path("/a/d").lexically_relative("/a/b/c") == "../../d"); CHECK(fs::path("/a/b/c").lexically_relative("/a/d") == "../b/c"); CHECK(fs::path("/a/b/c").lexically_relative("/a/b/c/d/..") == "."); + CHECK(fs::path("/a/b/c/").lexically_relative("/a/b/c/d/..") == "."); CHECK(fs::path("").lexically_relative("/a/..") == ""); CHECK(fs::path("").lexically_relative("a/..") == "."); CHECK(fs::path("a/b/c").lexically_relative("a") == "b/c");