forked from approvals/ApprovalTests.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Path.cpp
73 lines (61 loc) · 1.9 KB
/
Path.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "Path.h"
namespace ApprovalTests
{
Path::Path(const std::string& start) : path_(normalizeSeparators(start))
{
}
std::string Path::toString() const
{
return toString(separator_);
}
std::string Path::toString(const std::string& directoryPathSeparator) const
{
std::string path = removeRedundantDirectorySeparators(path_);
if (separator_ == directoryPathSeparator)
{
return path;
}
return StringUtils::replaceAll(path, separator_, directoryPathSeparator);
}
std::string Path::removeRedundantDirectorySeparators(std::string path) const
{
bool changed = true;
while (changed)
{
std::string reducePath = path;
reducePath = StringUtils::replaceAll(reducePath, "//", "/");
reducePath = StringUtils::replaceAll(reducePath, "\\\\", "\\");
changed = (reducePath != path);
path = reducePath;
};
return path;
}
Path Path::operator+(const std::string& addition) const
{
return Path(path_ + addition);
}
Path Path::operator/(const std::string& addition) const
{
auto first = path_;
if (StringUtils::endsWith(first, separator_))
{
first = first.substr(0, path_.size() - 1);
}
auto second = addition;
if (StringUtils::beginsWith(second, separator_))
{
second = second.substr(1);
}
return Path(first + separator_ + second);
}
Path Path::operator/(const Path addition) const
{
return *this / addition.path_;
}
std::string Path::normalizeSeparators(const std::string& path)
{
auto separator = SystemUtils::getDirectorySeparator();
auto otherSeparator = (separator == "/" ? "\\" : "/");
return StringUtils::replaceAll(path, otherSeparator, separator);
}
}