commit 9776e1226ffe0892b5ef86e6bcf0332c2c04d85f
parent 6728524708d732092b362c44cdba54e29aa24bb4
Author: cfillion <cfillion@users.noreply.github.com>
Date: Wed, 21 Sep 2016 23:03:17 -0400
path: avoid converting from Path to string to Path again when using dirname
Diffstat:
3 files changed, 8 insertions(+), 10 deletions(-)
diff --git a/src/path.cpp b/src/path.cpp
@@ -137,15 +137,14 @@ string Path::basename() const
return m_parts.back();
}
-string Path::dirname() const
+Path Path::dirname() const
{
if(empty())
return {};
Path dir(*this);
dir.removeLast();
-
- return dir.join();
+ return dir;
}
string Path::join(const char sep) const
diff --git a/src/path.hpp b/src/path.hpp
@@ -46,7 +46,7 @@ public:
size_t size() const { return m_parts.size(); }
std::string basename() const;
- std::string dirname() const;
+ Path dirname() const;
std::string join(const char sep = 0) const;
std::string first() const;
std::string last() const;
diff --git a/test/path.cpp b/test/path.cpp
@@ -24,14 +24,14 @@ TEST_CASE("prepend and append path components", M) {
REQUIRE(path.empty());
REQUIRE(path.size() == 0);
REQUIRE(path.join() == string());
- REQUIRE(path.dirname() == string());
- REQUIRE(path.basename() == string());
+ REQUIRE(path.dirname().empty());
+ REQUIRE(path.basename().empty());
path.prepend("world");
REQUIRE_FALSE(path.empty());
REQUIRE(path.size() == 1);
REQUIRE(path.join() == "world");
- REQUIRE(path.dirname() == string());
+ REQUIRE(path.dirname().empty());
REQUIRE(path.basename() == "world");
path.prepend("hello");
@@ -41,18 +41,17 @@ TEST_CASE("prepend and append path components", M) {
#else
REQUIRE(path.join() == "hello\\world");
#endif
- REQUIRE(path.dirname() == "hello");
+ REQUIRE(path.dirname().join() == "hello");
REQUIRE(path.basename() == "world");
path.append("test");
REQUIRE(path.size() == 3);
#ifndef _WIN32
REQUIRE(path.join() == "hello/world/test");
- REQUIRE(path.dirname() == "hello/world");
#else
REQUIRE(path.join() == "hello\\world\\test");
- REQUIRE(path.dirname() == "hello\\world");
#endif
+ REQUIRE(path.dirname() == Path("hello/world"));
REQUIRE(path.basename() == "test");
}