aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/names.cc
diff options
context:
space:
mode:
authorJade Lovelace <lix@jade.fyi>2024-07-12 14:56:30 +0200
committerJade Lovelace <lix@jade.fyi>2024-07-12 16:48:28 +0200
commitdde51af97df647e423943d63b5f65b55bf35d811 (patch)
tree7599bf76cba6c00f36a0bc7854df33ac02e92863 /src/libstore/names.cc
parent4b109ec1a8fc4550150f56f0f46f2f41d844bda8 (diff)
Use std::strong_ordering for version comparison
The actual motive here is the avoidance of integer overflow if we were to make these use checked NixInts and retain the subtraction. However, the actual *intent* of this code is a three-way comparison, which can be done with operator<=>, so we should just do *that* instead. Change-Id: I7f9a7da1f3176424b528af6d1b4f1591e4ab26bf
Diffstat (limited to 'src/libstore/names.cc')
-rw-r--r--src/libstore/names.cc8
1 files changed, 4 insertions, 4 deletions
diff --git a/src/libstore/names.cc b/src/libstore/names.cc
index b903a99f0..d72530d6f 100644
--- a/src/libstore/names.cc
+++ b/src/libstore/names.cc
@@ -94,7 +94,7 @@ static bool componentsLT(const std::string_view c1, const std::string_view c2)
}
-int compareVersions(const std::string_view v1, const std::string_view v2)
+std::strong_ordering compareVersions(const std::string_view v1, const std::string_view v2)
{
auto p1 = v1.begin();
auto p2 = v2.begin();
@@ -102,11 +102,11 @@ int compareVersions(const std::string_view v1, const std::string_view v2)
while (p1 != v1.end() || p2 != v2.end()) {
auto c1 = nextComponent(p1, v1.end());
auto c2 = nextComponent(p2, v2.end());
- if (componentsLT(c1, c2)) return -1;
- else if (componentsLT(c2, c1)) return 1;
+ if (componentsLT(c1, c2)) return std::strong_ordering::less;
+ else if (componentsLT(c2, c1)) return std::strong_ordering::greater;
}
- return 0;
+ return std::strong_ordering::equal;
}