mirror of
https://github.com/NixOS/nix.git
synced 2025-11-11 13:06:01 +01:00
Add CanonPath::makeRelative()
This commit is contained in:
parent
1829e7ccac
commit
7ebaf0252a
3 changed files with 44 additions and 0 deletions
|
|
@ -100,4 +100,30 @@ std::ostream & operator << (std::ostream & stream, const CanonPath & path)
|
|||
return stream;
|
||||
}
|
||||
|
||||
std::string CanonPath::makeRelative(const CanonPath & path) const
|
||||
{
|
||||
auto p1 = begin();
|
||||
auto p2 = path.begin();
|
||||
|
||||
for (; p1 != end() && p2 != path.end() && *p1 == *p2; ++p1, ++p2) ;
|
||||
|
||||
if (p1 == end() && p2 == path.end())
|
||||
return ".";
|
||||
else if (p1 == end())
|
||||
return std::string(p2.remaining);
|
||||
else {
|
||||
std::string res;
|
||||
while (p1 != end()) {
|
||||
++p1;
|
||||
if (!res.empty()) res += '/';
|
||||
res += "..";
|
||||
}
|
||||
if (p2 != path.end()) {
|
||||
if (!res.empty()) res += '/';
|
||||
res += p2.remaining;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue