Path: better handling of UNC paths
This commit is contained in:
parent
e0d90c00be
commit
7961bba0da
14
lib/path.cpp
14
lib/path.cpp
|
@ -60,10 +60,17 @@ std::string Path::fromNativeSeparators(std::string path)
|
||||||
|
|
||||||
std::string Path::simplifyPath(std::string originalPath)
|
std::string Path::simplifyPath(std::string originalPath)
|
||||||
{
|
{
|
||||||
// Skip ./ at the beginning
|
// Remove ./, .//, ./// etc. at the beginning
|
||||||
if (originalPath.size() > 2 && originalPath[0] == '.' &&
|
if (originalPath.size() > 2 && originalPath[0] == '.' &&
|
||||||
originalPath[1] == '/') {
|
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 = "";
|
std::string subPath = "";
|
||||||
|
@ -93,7 +100,8 @@ 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;
|
||||||
} 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<int>(i) - 1);
|
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,8 +50,15 @@ private:
|
||||||
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/.."));
|
||||||
|
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"));
|
||||||
|
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()
|
// Path::removeQuotationMarks()
|
||||||
ASSERT_EQUALS("index.cpp", Path::removeQuotationMarks("index.cpp"));
|
ASSERT_EQUALS("index.cpp", Path::removeQuotationMarks("index.cpp"));
|
||||||
|
|
Loading…
Reference in New Issue