Merge pull request #525 from Dmitry-Me/moreDoubleSlashCases

Handles double slashes close to end of path, more tests
This commit is contained in:
Daniel Marjamäki 2015-02-16 18:02:51 +01:00
commit 42b40b0c85
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)
{
const bool isUnc = originalPath.size() > 2 && originalPath[0] == '/' && originalPath[1] == '/';
// Remove ./, .//, ./// etc. at the beginning
if (originalPath.size() > 2 && originalPath[0] == '.' &&
originalPath[1] == '/') {
@ -90,6 +92,14 @@ std::string Path::simplifyPath(std::string originalPath)
if (subPath.length() > 0)
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) {
if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > 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] == ".") {
pathParts.erase(pathParts.begin() + static_cast<int>(i));
i = 0;
// Don't touch leading "//" which means a UNC path
} else if (i > 1 && pathParts[i] == "/" && pathParts[i-1] == "/") {
} else if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") {
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
i = 0;
}
}
if (isUnc) {
// Restore the leading double slash
pathParts.insert(pathParts.begin(), "/");
}
std::ostringstream oss;
for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i) {
oss << pathParts[i];

View File

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