reuse simplecpp::simplifyPath

This commit is contained in:
Daniel Marjamäki 2017-06-21 14:27:46 +02:00
parent c4c3802c54
commit 1e12ec241c
2 changed files with 7 additions and 64 deletions

View File

@ -36,6 +36,8 @@
#include <strings.h>
#endif
#include <simplecpp.h>
/** Is the filesystem case insensitive? */
static bool caseInsensitiveFilesystem()
{
@ -70,66 +72,7 @@ std::string Path::fromNativeSeparators(std::string path)
std::string Path::simplifyPath(std::string originalPath)
{
const bool isUnc = originalPath.compare(0,2,"//") == 0;
// Remove ./, .//, ./// etc. at the beginning
while (originalPath.compare(0,2,"./") == 0) { // remove "./././"
const size_t toErase = originalPath.find_first_not_of('/', 2);
originalPath = originalPath.erase(0, toErase);
}
std::string subPath;
std::vector<std::string> pathParts;
for (std::size_t i = 0; i < originalPath.size(); ++i) {
if (originalPath[i] == '/' || originalPath[i] == '\\') {
if (!subPath.empty()) {
pathParts.push_back(subPath);
subPath.clear();
}
pathParts.push_back(std::string(1 , originalPath[i]));
} else
subPath.append(1, originalPath[i]);
}
if (!subPath.empty())
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);
pathParts.erase(pathParts.begin() + static_cast<int>(i));
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 2);
i = 0;
} else if (i > 0 && pathParts[i] == ".") {
pathParts.erase(pathParts.begin() + static_cast<int>(i));
i = 0;
} 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];
}
return oss.str();
return simplecpp::simplifyPath(originalPath);
}
std::string Path::getPathFromFilename(const std::string &filename)

View File

@ -73,15 +73,15 @@ private:
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/.."));
ASSERT_EQUALS("a/..", Path::simplifyPath("./a/.."));
ASSERT_EQUALS(".", Path::simplifyPath("a/.."));
ASSERT_EQUALS(".", 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("test.cpp", Path::simplifyPath("./././././test.cpp"));
TODO_ASSERT_EQUALS("src", "src/abc/..", Path::simplifyPath("src/abc/.."));
// TODO: don't crash ASSERT_EQUALS("src", Path::simplifyPath("src/abc/../"));
ASSERT_EQUALS("src/", Path::simplifyPath("src/abc/.."));
ASSERT_EQUALS("src/", Path::simplifyPath("src/abc/../"));
// Handling of UNC paths on Windows
ASSERT_EQUALS("//src/test.cpp", Path::simplifyPath("//src/test.cpp"));