cmFindPackageCommand: Deduplicate version string comparator code

This commit is contained in:
Alex Turbov
2022-06-27 08:17:46 +04:00
parent c6d3ef1c95
commit 6a95ab97e7

View File

@@ -45,6 +45,19 @@
class cmExecutionStatus;
class cmFileList;
namespace {
template <template <typename> class Op>
struct StrverscmpOp
{
bool operator()(const std::string& lhs, const std::string& rhs) const
{
return Op<int>()(cmSystemTools::strverscmp(lhs, rhs), 0);
}
};
} // anonymous namespace
cmFindPackageCommand::PathLabel
cmFindPackageCommand::PathLabel::PackageRedirect("PACKAGE_REDIRECT");
cmFindPackageCommand::PathLabel cmFindPackageCommand::PathLabel::UserRegistry(
@@ -59,22 +72,6 @@ const cm::string_view cmFindPackageCommand::VERSION_ENDPOINT_INCLUDED(
const cm::string_view cmFindPackageCommand::VERSION_ENDPOINT_EXCLUDED(
"EXCLUDE");
struct StrverscmpGreater
{
bool operator()(const std::string& lhs, const std::string& rhs) const
{
return cmSystemTools::strverscmp(lhs, rhs) > 0;
}
};
struct StrverscmpLesser
{
bool operator()(const std::string& lhs, const std::string& rhs) const
{
return cmSystemTools::strverscmp(lhs, rhs) < 0;
}
};
void cmFindPackageCommand::Sort(std::vector<std::string>::iterator begin,
std::vector<std::string>::iterator end,
SortOrderType order, SortDirectionType dir)
@@ -90,9 +87,9 @@ void cmFindPackageCommand::Sort(std::vector<std::string>::iterator begin,
// compared such that e.g. 000 00 < 01 < 010 < 09 < 0 < 1 < 9 < 10
{
if (dir == Dec) {
std::sort(begin, end, StrverscmpGreater());
std::sort(begin, end, StrverscmpOp<std::greater>());
} else {
std::sort(begin, end, StrverscmpLesser());
std::sort(begin, end, StrverscmpOp<std::less>());
}
}
// else do not sort