Skip to content

Commit 7961bba

Browse files
Dmitry-Medanmar
authored andcommitted
Path: better handling of UNC paths
1 parent e0d90c0 commit 7961bba

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

lib/path.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,17 @@ std::string Path::fromNativeSeparators(std::string path)
6060

6161
std::string Path::simplifyPath(std::string originalPath)
6262
{
63-
// Skip ./ at the beginning
63+
// Remove ./, .//, ./// etc. at the beginning
6464
if (originalPath.size() > 2 && originalPath[0] == '.' &&
6565
originalPath[1] == '/') {
66-
originalPath = originalPath.erase(0, 2);
66+
size_t toErase = 2;
67+
for (std::size_t i = 2; i < originalPath.size(); i++) {
68+
if (originalPath[i] == '/')
69+
toErase++;
70+
else
71+
break;
72+
}
73+
originalPath = originalPath.erase(0, toErase);
6774
}
6875

6976
std::string subPath = "";
@@ -93,7 +100,8 @@ std::string Path::simplifyPath(std::string originalPath)
93100
} else if (i > 0 && pathParts[i] == ".") {
94101
pathParts.erase(pathParts.begin() + static_cast<int>(i));
95102
i = 0;
96-
} else if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") {
103+
// Don't touch leading "//" which means a UNC path
104+
} else if (i > 1 && pathParts[i] == "/" && pathParts[i-1] == "/") {
97105
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
98106
i = 0;
99107
}

test/testpath.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,15 @@ class TestPath : public TestFixture {
5050
ASSERT_EQUALS("../path/index.h", Path::simplifyPath("../path/other/../index.h"));
5151
ASSERT_EQUALS("a/index.h", Path::simplifyPath("a/../a/index.h"));
5252
ASSERT_EQUALS("a/..", Path::simplifyPath("a/.."));
53+
ASSERT_EQUALS("a/..", Path::simplifyPath("./a/.."));
5354
ASSERT_EQUALS("../../src/test.cpp", Path::simplifyPath("../../src/test.cpp"));
5455
ASSERT_EQUALS("../../../src/test.cpp", Path::simplifyPath("../../../src/test.cpp"));
56+
ASSERT_EQUALS("src/test.cpp", Path::simplifyPath(".//src/test.cpp"));
57+
ASSERT_EQUALS("src/test.cpp", Path::simplifyPath(".//src/test.cpp"));
58+
59+
// Handling of UNC paths on Windows
60+
ASSERT_EQUALS("//src/test.cpp", Path::simplifyPath("//src/test.cpp"));
61+
ASSERT_EQUALS("//src/test.cpp", Path::simplifyPath("///src/test.cpp"));
5562

5663
// Path::removeQuotationMarks()
5764
ASSERT_EQUALS("index.cpp", Path::removeQuotationMarks("index.cpp"));

0 commit comments

Comments
 (0)