From 7961bba0dace8d779ef9cb749bb80f2f35246593 Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Fri, 13 Feb 2015 16:32:45 +0100 Subject: [PATCH] Path: better handling of UNC paths --- lib/path.cpp | 14 +++++++++++--- test/testpath.cpp | 7 +++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/path.cpp b/lib/path.cpp index 794be1203..34dd3800e 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -60,10 +60,17 @@ std::string Path::fromNativeSeparators(std::string path) std::string Path::simplifyPath(std::string originalPath) { - // Skip ./ at the beginning + // Remove ./, .//, ./// etc. at the beginning if (originalPath.size() > 2 && originalPath[0] == '.' && originalPath[1] == '/') { - originalPath = originalPath.erase(0, 2); + size_t toErase = 2; + for (std::size_t i = 2; i < originalPath.size(); i++) { + if (originalPath[i] == '/') + toErase++; + else + break; + } + originalPath = originalPath.erase(0, toErase); } std::string subPath = ""; @@ -93,7 +100,8 @@ std::string Path::simplifyPath(std::string originalPath) } else if (i > 0 && pathParts[i] == ".") { pathParts.erase(pathParts.begin() + static_cast(i)); i = 0; - } else if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") { + // Don't touch leading "//" which means a UNC path + } else if (i > 1 && pathParts[i] == "/" && pathParts[i-1] == "/") { pathParts.erase(pathParts.begin() + static_cast(i) - 1); i = 0; } diff --git a/test/testpath.cpp b/test/testpath.cpp index 4c65d68d7..19da77916 100644 --- a/test/testpath.cpp +++ b/test/testpath.cpp @@ -50,8 +50,15 @@ private: ASSERT_EQUALS("../path/index.h", Path::simplifyPath("../path/other/../index.h")); ASSERT_EQUALS("a/index.h", Path::simplifyPath("a/../a/index.h")); ASSERT_EQUALS("a/..", Path::simplifyPath("a/..")); + ASSERT_EQUALS("a/..", Path::simplifyPath("./a/..")); ASSERT_EQUALS("../../src/test.cpp", Path::simplifyPath("../../src/test.cpp")); ASSERT_EQUALS("../../../src/test.cpp", Path::simplifyPath("../../../src/test.cpp")); + ASSERT_EQUALS("src/test.cpp", Path::simplifyPath(".//src/test.cpp")); + ASSERT_EQUALS("src/test.cpp", Path::simplifyPath(".//src/test.cpp")); + + // Handling of UNC paths on Windows + ASSERT_EQUALS("//src/test.cpp", Path::simplifyPath("//src/test.cpp")); + ASSERT_EQUALS("//src/test.cpp", Path::simplifyPath("///src/test.cpp")); // Path::removeQuotationMarks() ASSERT_EQUALS("index.cpp", Path::removeQuotationMarks("index.cpp"));