-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9881 from obsidiansystems/purify-canon-path
Purify `CanonPath`
- Loading branch information
Showing
5 changed files
with
155 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#pragma once | ||
/** | ||
* @file | ||
* | ||
* Pure (no IO) infrastructure just for defining other path types; | ||
* should not be used directly outside of utilities. | ||
*/ | ||
#include <string> | ||
#include <string_view> | ||
|
||
namespace nix { | ||
|
||
/** | ||
* Core pure path canonicalization algorithm. | ||
* | ||
* @param hookComponent | ||
* A callback which is passed two arguments, | ||
* references to | ||
* | ||
* 1. the result so far | ||
* | ||
* 2. the remaining path to resolve | ||
* | ||
* This is a chance to modify those two paths in arbitrary way, e.g. if | ||
* "result" points to a symlink. | ||
*/ | ||
typename std::string canonPathInner( | ||
std::string_view remaining, | ||
auto && hookComponent) | ||
{ | ||
assert(remaining != ""); | ||
|
||
std::string result; | ||
result.reserve(256); | ||
|
||
while (true) { | ||
|
||
/* Skip slashes. */ | ||
while (!remaining.empty() && remaining[0] == '/') | ||
remaining.remove_prefix(1); | ||
|
||
if (remaining.empty()) break; | ||
|
||
auto nextComp = ({ | ||
auto nextPathSep = remaining.find('/'); | ||
nextPathSep == remaining.npos ? remaining : remaining.substr(0, nextPathSep); | ||
}); | ||
|
||
/* Ignore `.'. */ | ||
if (nextComp == ".") | ||
remaining.remove_prefix(1); | ||
|
||
/* If `..', delete the last component. */ | ||
else if (nextComp == "..") | ||
{ | ||
if (!result.empty()) result.erase(result.rfind('/')); | ||
remaining.remove_prefix(2); | ||
} | ||
|
||
/* Normal component; copy it. */ | ||
else { | ||
result += '/'; | ||
if (const auto slash = remaining.find('/'); slash == result.npos) { | ||
result += remaining; | ||
remaining = {}; | ||
} else { | ||
result += remaining.substr(0, slash); | ||
remaining = remaining.substr(slash); | ||
} | ||
|
||
hookComponent(result, remaining); | ||
} | ||
} | ||
|
||
if (result.empty()) | ||
result = "/"; | ||
|
||
return result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters