Better handle double slashes, more tests
This commit is contained in:
parent
77a9822a97
commit
f0e0201c2d
18
lib/path.cpp
18
lib/path.cpp
|
@ -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];
|
||||
|
|
|
@ -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/.."));
|
||||
|
|
Loading…
Reference in New Issue