2023-06-23 20:51:25 +03:00
|
|
|
#include "search-path.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2024-04-13 18:35:15 +03:00
|
|
|
std::optional<std::string_view> LookupPath::Prefix::suffixIfPotentialMatch(
|
2023-06-23 20:51:25 +03:00
|
|
|
std::string_view path) const
|
|
|
|
{
|
|
|
|
auto n = s.size();
|
|
|
|
|
|
|
|
/* Non-empty prefix and suffix must be separated by a /, or the
|
|
|
|
prefix is not a valid path prefix. */
|
2023-08-15 00:07:37 +03:00
|
|
|
bool needSeparator = n > 0 && n < path.size();
|
2023-06-23 20:51:25 +03:00
|
|
|
|
|
|
|
if (needSeparator && path[n] != '/') {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prefix must be prefix of this path. */
|
|
|
|
if (path.compare(0, n, s) != 0) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip next path separator. */
|
|
|
|
return {
|
|
|
|
path.substr(needSeparator ? n + 1 : n)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-13 18:35:15 +03:00
|
|
|
LookupPath::Elem LookupPath::Elem::parse(std::string_view rawElem)
|
2023-06-23 20:51:25 +03:00
|
|
|
{
|
|
|
|
size_t pos = rawElem.find('=');
|
|
|
|
|
2024-04-13 18:35:15 +03:00
|
|
|
return LookupPath::Elem {
|
2023-06-23 20:51:25 +03:00
|
|
|
.prefix = Prefix {
|
|
|
|
.s = pos == std::string::npos
|
|
|
|
? std::string { "" }
|
|
|
|
: std::string { rawElem.substr(0, pos) },
|
|
|
|
},
|
|
|
|
.path = Path {
|
|
|
|
.s = std::string { rawElem.substr(pos + 1) },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-13 18:35:15 +03:00
|
|
|
LookupPath LookupPath::parse(const Strings & rawElems)
|
2023-06-23 20:51:25 +03:00
|
|
|
{
|
2024-04-13 18:35:15 +03:00
|
|
|
LookupPath res;
|
2023-06-23 20:51:25 +03:00
|
|
|
for (auto & rawElem : rawElems)
|
2024-04-13 18:35:15 +03:00
|
|
|
res.elements.emplace_back(LookupPath::Elem::parse(rawElem));
|
2023-06-23 20:51:25 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|