Skip to content

Commit 1e12ec2

Browse files
committed
reuse simplecpp::simplifyPath
1 parent c4c3802 commit 1e12ec2

File tree

2 files changed

+7
-64
lines changed

2 files changed

+7
-64
lines changed

lib/path.cpp

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include <strings.h>
3737
#endif
3838

39+
#include <simplecpp.h>
40+
3941
/** Is the filesystem case insensitive? */
4042
static bool caseInsensitiveFilesystem()
4143
{
@@ -70,66 +72,7 @@ std::string Path::fromNativeSeparators(std::string path)
7072

7173
std::string Path::simplifyPath(std::string originalPath)
7274
{
73-
const bool isUnc = originalPath.compare(0,2,"//") == 0;
74-
75-
// Remove ./, .//, ./// etc. at the beginning
76-
while (originalPath.compare(0,2,"./") == 0) { // remove "./././"
77-
const size_t toErase = originalPath.find_first_not_of('/', 2);
78-
originalPath = originalPath.erase(0, toErase);
79-
}
80-
81-
std::string subPath;
82-
std::vector<std::string> pathParts;
83-
for (std::size_t i = 0; i < originalPath.size(); ++i) {
84-
if (originalPath[i] == '/' || originalPath[i] == '\\') {
85-
if (!subPath.empty()) {
86-
pathParts.push_back(subPath);
87-
subPath.clear();
88-
}
89-
90-
pathParts.push_back(std::string(1 , originalPath[i]));
91-
} else
92-
subPath.append(1, originalPath[i]);
93-
}
94-
95-
if (!subPath.empty())
96-
pathParts.push_back(subPath);
97-
98-
// First filter out all double slashes
99-
for (unsigned int i = 1; i < pathParts.size(); ++i) {
100-
if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") {
101-
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
102-
--i;
103-
}
104-
}
105-
106-
for (unsigned int i = 1; i < pathParts.size(); ++i) {
107-
if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > i + 1) {
108-
pathParts.erase(pathParts.begin() + static_cast<int>(i) + 1);
109-
pathParts.erase(pathParts.begin() + static_cast<int>(i));
110-
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
111-
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 2);
112-
i = 0;
113-
} else if (i > 0 && pathParts[i] == ".") {
114-
pathParts.erase(pathParts.begin() + static_cast<int>(i));
115-
i = 0;
116-
} else if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") {
117-
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
118-
i = 0;
119-
}
120-
}
121-
122-
if (isUnc) {
123-
// Restore the leading double slash
124-
pathParts.insert(pathParts.begin(), "/");
125-
}
126-
127-
std::ostringstream oss;
128-
for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i) {
129-
oss << pathParts[i];
130-
}
131-
132-
return oss.str();
75+
return simplecpp::simplifyPath(originalPath);
13376
}
13477

13578
std::string Path::getPathFromFilename(const std::string &filename)

test/testpath.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ class TestPath : public TestFixture {
7373
ASSERT_EQUALS("/index.h", Path::simplifyPath("/path/../other///././..///index.h"));
7474
ASSERT_EQUALS("../path/index.h", Path::simplifyPath("../path/other/../index.h"));
7575
ASSERT_EQUALS("a/index.h", Path::simplifyPath("a/../a/index.h"));
76-
ASSERT_EQUALS("a/..", Path::simplifyPath("a/.."));
77-
ASSERT_EQUALS("a/..", Path::simplifyPath("./a/.."));
76+
ASSERT_EQUALS(".", Path::simplifyPath("a/.."));
77+
ASSERT_EQUALS(".", Path::simplifyPath("./a/.."));
7878
ASSERT_EQUALS("../../src/test.cpp", Path::simplifyPath("../../src/test.cpp"));
7979
ASSERT_EQUALS("../../../src/test.cpp", Path::simplifyPath("../../../src/test.cpp"));
8080
ASSERT_EQUALS("src/test.cpp", Path::simplifyPath(".//src/test.cpp"));
8181
ASSERT_EQUALS("src/test.cpp", Path::simplifyPath(".///src/test.cpp"));
8282
ASSERT_EQUALS("test.cpp", Path::simplifyPath("./././././test.cpp"));
83-
TODO_ASSERT_EQUALS("src", "src/abc/..", Path::simplifyPath("src/abc/.."));
84-
// TODO: don't crash ASSERT_EQUALS("src", Path::simplifyPath("src/abc/../"));
83+
ASSERT_EQUALS("src/", Path::simplifyPath("src/abc/.."));
84+
ASSERT_EQUALS("src/", Path::simplifyPath("src/abc/../"));
8585

8686
// Handling of UNC paths on Windows
8787
ASSERT_EQUALS("//src/test.cpp", Path::simplifyPath("//src/test.cpp"));

0 commit comments

Comments
 (0)