2004-02-06 16:49:41 +02:00
|
|
|
#include "names.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
#include "util.hh"
|
|
|
|
|
2020-09-21 19:22:45 +03:00
|
|
|
#include <regex>
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
|
|
|
|
namespace nix {
|
2004-02-06 16:49:41 +02:00
|
|
|
|
|
|
|
|
2020-09-21 19:22:45 +03:00
|
|
|
struct Regex
|
|
|
|
{
|
|
|
|
std::regex regex;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-02-09 13:59:39 +02:00
|
|
|
DrvName::DrvName()
|
|
|
|
{
|
|
|
|
name = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-06 16:49:41 +02:00
|
|
|
/* Parse a derivation name. The `name' part of a derivation name is
|
|
|
|
everything up to but not including the first dash *not* followed by
|
|
|
|
a letter. The `version' part is the rest (excluding the separating
|
|
|
|
dash). E.g., `apache-httpd-2.0.48' is parsed to (`apache-httpd',
|
|
|
|
'2.0.48'). */
|
2019-12-05 20:11:09 +02:00
|
|
|
DrvName::DrvName(std::string_view s) : hits(0)
|
2004-02-06 16:49:41 +02:00
|
|
|
{
|
2019-12-05 20:11:09 +02:00
|
|
|
name = fullName = std::string(s);
|
2004-02-06 16:49:41 +02:00
|
|
|
for (unsigned int i = 0; i < s.size(); ++i) {
|
|
|
|
/* !!! isalpha/isdigit are affected by the locale. */
|
|
|
|
if (s[i] == '-' && i + 1 < s.size() && !isalpha(s[i + 1])) {
|
2019-12-05 20:11:09 +02:00
|
|
|
name = s.substr(0, i);
|
|
|
|
version = s.substr(i + 1);
|
2004-02-06 16:49:41 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-21 19:22:45 +03:00
|
|
|
DrvName::~DrvName()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
2021-11-18 15:32:52 +02:00
|
|
|
bool DrvName::matches(const DrvName & n)
|
2004-02-06 16:49:41 +02:00
|
|
|
{
|
2014-10-03 19:47:16 +03:00
|
|
|
if (name != "*") {
|
2020-09-21 19:22:45 +03:00
|
|
|
if (!regex) {
|
|
|
|
regex = std::make_unique<Regex>();
|
|
|
|
regex->regex = std::regex(name, std::regex::extended);
|
|
|
|
}
|
|
|
|
if (!std::regex_match(n.name, regex->regex)) return false;
|
2014-10-03 19:47:16 +03:00
|
|
|
}
|
2004-02-06 16:49:41 +02:00
|
|
|
if (version != "" && version != n.version) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-21 15:44:00 +02:00
|
|
|
std::string_view nextComponent(std::string_view::const_iterator & p,
|
|
|
|
const std::string_view::const_iterator end)
|
2004-02-06 16:49:41 +02:00
|
|
|
{
|
|
|
|
/* Skip any dots and dashes (component separators). */
|
|
|
|
while (p != end && (*p == '.' || *p == '-')) ++p;
|
|
|
|
|
|
|
|
if (p == end) return "";
|
|
|
|
|
|
|
|
/* If the first character is a digit, consume the longest sequence
|
|
|
|
of digits. Otherwise, consume the longest sequence of
|
|
|
|
non-digit, non-separator characters. */
|
2022-01-21 15:44:00 +02:00
|
|
|
auto s = p;
|
2004-02-06 16:49:41 +02:00
|
|
|
if (isdigit(*p))
|
2022-01-21 15:44:00 +02:00
|
|
|
while (p != end && isdigit(*p)) p++;
|
2004-02-06 16:49:41 +02:00
|
|
|
else
|
|
|
|
while (p != end && (!isdigit(*p) && *p != '.' && *p != '-'))
|
2022-01-21 15:44:00 +02:00
|
|
|
p++;
|
2013-09-03 16:56:33 +03:00
|
|
|
|
2022-01-21 15:44:00 +02:00
|
|
|
return {s, size_t(p - s)};
|
2004-02-06 16:49:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-21 15:44:00 +02:00
|
|
|
static bool componentsLT(const std::string_view c1, const std::string_view c2)
|
2004-02-06 16:49:41 +02:00
|
|
|
{
|
2021-01-08 13:22:21 +02:00
|
|
|
auto n1 = string2Int<int>(c1);
|
|
|
|
auto n2 = string2Int<int>(c2);
|
2004-02-06 16:49:41 +02:00
|
|
|
|
2021-01-08 13:22:21 +02:00
|
|
|
if (n1 && n2) return *n1 < *n2;
|
|
|
|
else if (c1 == "" && n2) return true;
|
2004-02-06 16:49:41 +02:00
|
|
|
else if (c1 == "pre" && c2 != "pre") return true;
|
|
|
|
else if (c2 == "pre") return false;
|
|
|
|
/* Assume that `2.3a' < `2.3.1'. */
|
2021-01-08 13:22:21 +02:00
|
|
|
else if (n2) return true;
|
|
|
|
else if (n1) return false;
|
2004-02-06 16:49:41 +02:00
|
|
|
else return c1 < c2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-12 15:56:30 +03:00
|
|
|
std::strong_ordering compareVersions(const std::string_view v1, const std::string_view v2)
|
2004-02-06 16:49:41 +02:00
|
|
|
{
|
2022-01-21 15:44:00 +02:00
|
|
|
auto p1 = v1.begin();
|
|
|
|
auto p2 = v2.begin();
|
2013-09-03 16:56:33 +03:00
|
|
|
|
2004-02-06 16:49:41 +02:00
|
|
|
while (p1 != v1.end() || p2 != v2.end()) {
|
2022-01-21 15:44:00 +02:00
|
|
|
auto c1 = nextComponent(p1, v1.end());
|
|
|
|
auto c2 = nextComponent(p2, v2.end());
|
2024-07-12 15:56:30 +03:00
|
|
|
if (componentsLT(c1, c2)) return std::strong_ordering::less;
|
|
|
|
else if (componentsLT(c2, c1)) return std::strong_ordering::greater;
|
2004-02-06 16:49:41 +02:00
|
|
|
}
|
|
|
|
|
2024-07-12 15:56:30 +03:00
|
|
|
return std::strong_ordering::equal;
|
2004-02-06 16:49:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DrvNames drvNamesFromArgs(const Strings & opArgs)
|
|
|
|
{
|
|
|
|
DrvNames result;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : opArgs)
|
2020-09-21 19:22:45 +03:00
|
|
|
result.emplace_back(i);
|
2004-02-06 16:49:41 +02:00
|
|
|
return result;
|
|
|
|
}
|
2006-09-05 00:06:23 +03:00
|
|
|
|
2013-09-03 16:56:33 +03:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|