From 9884018dfae766a3b7fd3e9bc0302e75feccf52d Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 24 Mar 2024 01:38:22 +0100 Subject: [PATCH] baseNameOf(): Remove all trailing slashes --- src/libutil/file-system.cc | 2 +- tests/unit/libutil/tests.cc | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index 9dd6a5133..fc0f216ea 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -128,7 +128,7 @@ std::string_view baseNameOf(std::string_view path) return ""; auto last = path.size() - 1; - if (path[last] == '/' && last > 0) + while (last > 0 && path[last] == '/') last -= 1; auto pos = path.rfind('/', last); diff --git a/tests/unit/libutil/tests.cc b/tests/unit/libutil/tests.cc index 4406fd184..d7e9edf0a 100644 --- a/tests/unit/libutil/tests.cc +++ b/tests/unit/libutil/tests.cc @@ -151,6 +151,16 @@ namespace nix { ASSERT_EQ(p1, "dir"); } + TEST(baseNameOf, trailingSlashes) { + auto p1 = baseNameOf("/dir//"); + ASSERT_EQ(p1, "dir"); + } + + TEST(baseNameOf, absoluteNothingSlashNothing) { + auto p1 = baseNameOf("//"); + ASSERT_EQ(p1, ""); + } + /* ---------------------------------------------------------------------------- * isInDir * --------------------------------------------------------------------------*/