diff --git a/src/lib/src/Path.cc b/src/lib/src/Path.cc index 279b69b..7a344b1 100644 --- a/src/lib/src/Path.cc +++ b/src/lib/src/Path.cc @@ -16,8 +16,26 @@ namespace jes Path Path::dirname() { - // TODO - return ""; + size_t i = m_path.size() - 1; + while (m_path[i] == '/') + { + if (i == 0) + return "/"; + i--; + } + while (m_path[i] != '/') + { + if (i == 0) + return "."; + i--; + } + while (m_path[i] == '/') + { + if (i == 0) + return "/"; + i--; + } + return std::string(m_path, 0, i + 1); } Path Path::join(const Path & other) diff --git a/test/src/test_Path.cc b/test/src/test_Path.cc index daa31ad..96aae3a 100644 --- a/test/src/test_Path.cc +++ b/test/src/test_Path.cc @@ -27,3 +27,14 @@ TEST(PathTest, joins_two_paths_together) EXPECT_EQ("C:/foo/../baz", Path("C:\\foo").join("../baz").to_s()); EXPECT_EQ("/xyz/f.txt", Path("/xyz/").join("f.txt").to_s()); } + +TEST(PathTest, dirname) +{ + EXPECT_EQ("/one/two", Path("/one/two/f.c").dirname().to_s()); + EXPECT_EQ("C:/a", Path("C:\\a\\b\\").dirname().to_s()); + EXPECT_EQ("/", Path("/").dirname().to_s()); + EXPECT_EQ("/", Path("///foo").dirname().to_s()); + EXPECT_EQ(".", Path("file.txt").dirname().to_s()); + EXPECT_EQ(".", Path("folder//").dirname().to_s()); + EXPECT_EQ("a", Path("a/b").dirname().to_s()); +}