implement Path::dirname()

This commit is contained in:
Josh Holtrop 2014-06-19 00:01:10 -04:00
parent eb12420155
commit 49ccad5ed4
2 changed files with 31 additions and 2 deletions

View File

@ -16,8 +16,26 @@ namespace jes
Path Path::dirname() Path Path::dirname()
{ {
// TODO size_t i = m_path.size() - 1;
return ""; 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) Path Path::join(const Path & other)

View File

@ -27,3 +27,14 @@ TEST(PathTest, joins_two_paths_together)
EXPECT_EQ("C:/foo/../baz", Path("C:\\foo").join("../baz").to_s()); EXPECT_EQ("C:/foo/../baz", Path("C:\\foo").join("../baz").to_s());
EXPECT_EQ("/xyz/f.txt", Path("/xyz/").join("f.txt").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());
}