Better handle double slashes, more tests

This commit is contained in:
Dmitry-Me 2015-02-16 13:11:13 +03:00
parent 77a9822a97
commit f0e0201c2d
2 changed files with 36 additions and 2 deletions

View File

@ -60,6 +60,8 @@ std::string Path::fromNativeSeparators(std::string path)
std::string Path::simplifyPath(std::string originalPath) std::string Path::simplifyPath(std::string originalPath)
{ {
const bool isUnc = originalPath.size() > 2 && originalPath[0] == '/' && originalPath[1] == '/';
// Remove ./, .//, ./// etc. at the beginning // Remove ./, .//, ./// etc. at the beginning
if (originalPath.size() > 2 && originalPath[0] == '.' && if (originalPath.size() > 2 && originalPath[0] == '.' &&
originalPath[1] == '/') { originalPath[1] == '/') {
@ -90,6 +92,14 @@ std::string Path::simplifyPath(std::string originalPath)
if (subPath.length() > 0) if (subPath.length() > 0)
pathParts.push_back(subPath); 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<int>(i) - 1);
--i;
}
}
for (unsigned int i = 1; i < pathParts.size(); ++i) { for (unsigned int i = 1; i < pathParts.size(); ++i) {
if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > i + 1) { if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > i + 1) {
pathParts.erase(pathParts.begin() + static_cast<int>(i) + 1); pathParts.erase(pathParts.begin() + static_cast<int>(i) + 1);
@ -100,13 +110,17 @@ std::string Path::simplifyPath(std::string originalPath)
} else if (i > 0 && pathParts[i] == ".") { } else if (i > 0 && pathParts[i] == ".") {
pathParts.erase(pathParts.begin() + static_cast<int>(i)); pathParts.erase(pathParts.begin() + static_cast<int>(i));
i = 0; i = 0;
// Don't touch leading "//" which means a UNC path } else if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") {
} else if (i > 1 && pathParts[i] == "/" && pathParts[i-1] == "/") {
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1); pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
i = 0; i = 0;
} }
} }
if (isUnc) {
// Restore the leading double slash
pathParts.insert(pathParts.begin(), "/");
}
std::ostringstream oss; std::ostringstream oss;
for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i) { for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i) {
oss << pathParts[i]; oss << pathParts[i];

View File

@ -40,13 +40,33 @@ private:
// Path::simplifyPath() // Path::simplifyPath()
ASSERT_EQUALS("index.h", Path::simplifyPath("index.h")); ASSERT_EQUALS("index.h", Path::simplifyPath("index.h"));
ASSERT_EQUALS("index.h", Path::simplifyPath("./index.h")); ASSERT_EQUALS("index.h", Path::simplifyPath("./index.h"));
ASSERT_EQUALS("index.h", Path::simplifyPath(".//index.h"));
ASSERT_EQUALS("index.h", Path::simplifyPath(".///index.h"));
ASSERT_EQUALS("/index.h", Path::simplifyPath("/index.h")); ASSERT_EQUALS("/index.h", Path::simplifyPath("/index.h"));
ASSERT_EQUALS("/path/", Path::simplifyPath("/path/")); ASSERT_EQUALS("/path/", Path::simplifyPath("/path/"));
ASSERT_EQUALS("/", Path::simplifyPath("/")); ASSERT_EQUALS("/", Path::simplifyPath("/"));
ASSERT_EQUALS("/", Path::simplifyPath("/."));
ASSERT_EQUALS("/", Path::simplifyPath("/./"));
ASSERT_EQUALS("/index.h", Path::simplifyPath("/./index.h"));
ASSERT_EQUALS("/", Path::simplifyPath("/.//"));
ASSERT_EQUALS("/index.h", Path::simplifyPath("/.//index.h"));
ASSERT_EQUALS("../index.h", Path::simplifyPath("../index.h")); ASSERT_EQUALS("../index.h", Path::simplifyPath("../index.h"));
ASSERT_EQUALS("/index.h", Path::simplifyPath("/path/../index.h")); ASSERT_EQUALS("/index.h", Path::simplifyPath("/path/../index.h"));
ASSERT_EQUALS("index.h", Path::simplifyPath("./path/../index.h"));
ASSERT_EQUALS("index.h", Path::simplifyPath("path/../index.h"));
ASSERT_EQUALS("/index.h", Path::simplifyPath("/path//../index.h"));
ASSERT_EQUALS("index.h", Path::simplifyPath("./path//../index.h"));
ASSERT_EQUALS("index.h", Path::simplifyPath("path//../index.h"));
ASSERT_EQUALS("/index.h", Path::simplifyPath("/path/..//index.h"));
ASSERT_EQUALS("index.h", Path::simplifyPath("./path/..//index.h"));
ASSERT_EQUALS("index.h", Path::simplifyPath("path/..//index.h"));
ASSERT_EQUALS("/index.h", Path::simplifyPath("/path//..//index.h"));
ASSERT_EQUALS("index.h", Path::simplifyPath("./path//..//index.h"));
ASSERT_EQUALS("index.h", Path::simplifyPath("path//..//index.h"));
ASSERT_EQUALS("/index.h", Path::simplifyPath("/path/../other/../index.h")); ASSERT_EQUALS("/index.h", Path::simplifyPath("/path/../other/../index.h"));
ASSERT_EQUALS("/index.h", Path::simplifyPath("/path/../other///././../index.h")); ASSERT_EQUALS("/index.h", Path::simplifyPath("/path/../other///././../index.h"));
ASSERT_EQUALS("/index.h", Path::simplifyPath("/path/../other/././..///index.h"));
ASSERT_EQUALS("/index.h", Path::simplifyPath("/path/../other///././..///index.h"));
ASSERT_EQUALS("../path/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/index.h", Path::simplifyPath("a/../a/index.h"));
ASSERT_EQUALS("a/..", Path::simplifyPath("a/..")); ASSERT_EQUALS("a/..", Path::simplifyPath("a/.."));