From 1e12ec241cebf5cc1e84fb47d423392be57f6dbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 21 Jun 2017 14:27:46 +0200 Subject: [PATCH] reuse simplecpp::simplifyPath --- lib/path.cpp | 63 +++-------------------------------------------- test/testpath.cpp | 8 +++--- 2 files changed, 7 insertions(+), 64 deletions(-) diff --git a/lib/path.cpp b/lib/path.cpp index 9ab8c4a02..e5b4caee0 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -36,6 +36,8 @@ #include #endif +#include + /** Is the filesystem case insensitive? */ static bool caseInsensitiveFilesystem() { @@ -70,66 +72,7 @@ std::string Path::fromNativeSeparators(std::string path) std::string Path::simplifyPath(std::string originalPath) { - const bool isUnc = originalPath.compare(0,2,"//") == 0; - - // Remove ./, .//, ./// etc. at the beginning - while (originalPath.compare(0,2,"./") == 0) { // remove "./././" - const size_t toErase = originalPath.find_first_not_of('/', 2); - originalPath = originalPath.erase(0, toErase); - } - - std::string subPath; - std::vector pathParts; - for (std::size_t i = 0; i < originalPath.size(); ++i) { - if (originalPath[i] == '/' || originalPath[i] == '\\') { - if (!subPath.empty()) { - pathParts.push_back(subPath); - subPath.clear(); - } - - pathParts.push_back(std::string(1 , originalPath[i])); - } else - subPath.append(1, originalPath[i]); - } - - if (!subPath.empty()) - pathParts.push_back(subPath); - - // First filter out all double slashes - for (unsigned int i = 1; i < pathParts.size(); ++i) { - if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") { - pathParts.erase(pathParts.begin() + static_cast(i) - 1); - --i; - } - } - - for (unsigned int i = 1; i < pathParts.size(); ++i) { - if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > i + 1) { - pathParts.erase(pathParts.begin() + static_cast(i) + 1); - pathParts.erase(pathParts.begin() + static_cast(i)); - pathParts.erase(pathParts.begin() + static_cast(i) - 1); - pathParts.erase(pathParts.begin() + static_cast(i) - 2); - i = 0; - } else if (i > 0 && pathParts[i] == ".") { - pathParts.erase(pathParts.begin() + static_cast(i)); - i = 0; - } else if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") { - pathParts.erase(pathParts.begin() + static_cast(i) - 1); - i = 0; - } - } - - if (isUnc) { - // Restore the leading double slash - pathParts.insert(pathParts.begin(), "/"); - } - - std::ostringstream oss; - for (std::vector::size_type i = 0; i < pathParts.size(); ++i) { - oss << pathParts[i]; - } - - return oss.str(); + return simplecpp::simplifyPath(originalPath); } std::string Path::getPathFromFilename(const std::string &filename) diff --git a/test/testpath.cpp b/test/testpath.cpp index 54092eed5..a687d0a22 100644 --- a/test/testpath.cpp +++ b/test/testpath.cpp @@ -73,15 +73,15 @@ private: ASSERT_EQUALS("/index.h", Path::simplifyPath("/path/../other///././..///index.h")); 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(".", Path::simplifyPath("a/..")); + ASSERT_EQUALS(".", 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")); ASSERT_EQUALS("test.cpp", Path::simplifyPath("./././././test.cpp")); - TODO_ASSERT_EQUALS("src", "src/abc/..", Path::simplifyPath("src/abc/..")); - // TODO: don't crash ASSERT_EQUALS("src", Path::simplifyPath("src/abc/../")); + ASSERT_EQUALS("src/", Path::simplifyPath("src/abc/..")); + ASSERT_EQUALS("src/", Path::simplifyPath("src/abc/../")); // Handling of UNC paths on Windows ASSERT_EQUALS("//src/test.cpp", Path::simplifyPath("//src/test.cpp"));