commit 8f115d3d37db7c64d4980ce2fda2d41a9ea631fe
parent d4c94dc311dac254af88da2fa9a4b590bbafab7c
Author: cfillion <cfillion@users.noreply.github.com>
Date: Sun, 10 Sep 2017 18:47:00 -0700
path: implement absolute path detection on Windows
Diffstat:
2 files changed, 42 insertions(+), 10 deletions(-)
diff --git a/src/path.cpp b/src/path.cpp
@@ -53,15 +53,22 @@ static vector<string> Split(const string &input, bool *absolute)
break;
}
else if(last + pos == 0) {
+#ifndef _WIN32
*absolute = true;
+#endif
last++;
continue;
}
const string &part = input.substr(last, pos - last);
- if(!part.empty() && part != DOT)
+ if(!part.empty() && part != DOT) {
+#ifdef _WIN32
+ if(list.empty() && part.size() == 2 && isalpha(part[0]) && part[1] == ':')
+ *absolute = true;
+#endif
list.push_back(part);
+ }
last = pos + 1;
}
@@ -153,10 +160,16 @@ Path Path::dirname() const
string Path::join(const char sep) const
{
+#ifdef _WIN32
+ constexpr bool absoluteSlash = false;
+#else
+ const bool absoluteSlash = m_absolute;
+#endif
+
string path;
for(const string &part : m_parts) {
- if(!path.empty() || m_absolute)
+ if(!path.empty() || absoluteSlash)
path += sep ? sep : SEPARATOR;
path += part;
diff --git a/test/path.cpp b/test/path.cpp
@@ -146,25 +146,42 @@ TEST_CASE("split input", M) {
}
}
-#ifndef _WIN32
-TEST_CASE("absolute path (unix)", M) {
+TEST_CASE("absolute path", M) {
CHECK_FALSE(Path("a/b").absolute());
+#ifdef _WIN32
+ const Path a("C:\\Windows\\System32");
+#else
const Path a("/usr/bin/zsh");
- REQUIRE(a.size() == 3);
+#endif
+
REQUIRE(a.absolute());
- REQUIRE(a[0] == "usr");
- REQUIRE(a.join() == "/usr/bin/zsh");
+ CHECK(a.size() == 3);
+
+#ifdef _WIN32
+ CHECK(a[0] == "C:");
+ CHECK(a.join() == "C:\\Windows\\System32");
+#else
+ CHECK(a[0] == "usr");
+ CHECK(a.join() == "/usr/bin/zsh");
+#endif
}
-TEST_CASE("append absolute path (unix)", M) {
+TEST_CASE("append absolute path to empty path", M) {
+#ifdef _WIN32
+ const Path abs("C:\\Windows\\");
+#else
+ const Path abs("/usr/bin");
+#endif
+
Path path;
- path += Path("/a/b");
+ path += abs;
- CHECK(path == Path("/a/b"));
+ CHECK(path == abs);
REQUIRE(path.absolute());
}
+#ifndef _WIN32
TEST_CASE("compare absolute to relative path (unix)", M) {
REQUIRE(Path("/a/b") != Path("a/b"));
}
@@ -272,7 +289,9 @@ TEST_CASE("path starts with", M) {
REQUIRE(ref.startsWith(ref));
REQUIRE(Path("a/b/c").startsWith(ref));
+#ifndef _WIN32
REQUIRE_FALSE(Path("/a/b/c").startsWith(ref));
+#endif
REQUIRE_FALSE(Path("0/a/b/c").startsWith(ref));
REQUIRE_FALSE(Path("a").startsWith(ref));
}