changeset 14217:7eb0e47418ab 13.0

util.human.io: Add simple version comparison This is a small helper function that compares version string (it can be passed to table.sort() as a comparator). Full version sorts (e.g. semver, and natural/alphanumeric sort in general) are more complex, and I would like to eventually support those. But this is a stable branch and we don't need it for anything just yet. Regarding justification for the stable branch: The function is being committed with extensive tests, and isn't currently used anywhere. The plan is to use it in an upcoming update of how TLS profiles are configured, but the inputs in that case will be fixed (meaning that the risk of breakage is lower than if we were running it against arbitrary data).
author Matthew Wild <mwild1@gmail.com>
date Fri, 12 Jun 2026 11:38:39 +0100
parents 75d09be04f13
children 926f25af2ffe
files core/features.lua spec/util_human_io_spec.lua util/human/io.lua
diffstat 3 files changed, 100 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/core/features.lua	Sun May 31 11:51:22 2026 +0200
+++ b/core/features.lua	Fri Jun 12 11:38:39 2026 +0100
@@ -37,5 +37,8 @@
 
 		-- SIGUSR1 and 2 events
 		"signal-events";
+
+		-- util.human.io.simple_version_compare
+		"simple-version-compare";
 	};
 };
--- a/spec/util_human_io_spec.lua	Sun May 31 11:51:22 2026 +0200
+++ b/spec/util_human_io_spec.lua	Fri Jun 12 11:38:39 2026 +0100
@@ -100,6 +100,79 @@
 			return assert.is_nil(human_io.parse_duration_lax("two weeks"), "\"2 weeks\" -> nil");
 		end);
 	end);
+
+	describe("simple_version_compare", function ()
+		local version_compare = human_io.simple_version_compare;
+
+		local function cmp_version(a, b, exp)
+			assert.equal(exp, version_compare(a, b), ("%q < %q"):format(a, b));
+		end
+
+		it("orders versions with differing major numbers", function ()
+			cmp_version("1.0", "2.0", true);
+			cmp_version("2.0", "1.0", false);
+		end);
+
+		it("orders versions with differing minor numbers", function ()
+			cmp_version("1.0", "1.1", true);
+			cmp_version("1.1", "1.0", false);
+		end);
+
+		it("treats a shorter version as less than a longer one sharing the same prefix", function ()
+			cmp_version("1.0", "1.0.1", true);
+			cmp_version("1.0.1", "1.0", false);
+
+			cmp_version("1", "1.0", true);
+			cmp_version("1", "1.1", true);
+
+			cmp_version("1.0.0", "1.0.0.1", true);
+		end);
+
+		it("compares components left-to-right regardless of how many components there are", function ()
+			cmp_version("1.0.1", "1.1", true);
+			cmp_version("1.1", "1.0.1", false);
+
+			cmp_version("1.0.1", "1.1.0", true);
+			cmp_version("1.1.0", "1.0.1", false);
+
+			cmp_version("1.0.0.1", "1.0.1", true);
+		end);
+
+		it("never considers a version less than itself", function ()
+			cmp_version("0", "0", false);
+			cmp_version("1.0", "1.0", false);
+			cmp_version("1.2.3", "1.2.3", false);
+			cmp_version("foo", "foo", false);
+		end);
+
+		it("compares components numerically", function ()
+			cmp_version("1.9", "1.10", true);
+			cmp_version("1.10", "1.9", false);
+
+			cmp_version("1.9.9", "1.10.0", true);
+
+			cmp_version("2.0", "10.0", true);
+			cmp_version("10.0", "2.0", false);
+
+			cmp_version("1.2", "1.10", true);
+
+			cmp_version("1.99", "1.100", true);
+			cmp_version("1.100", "1.99", false);
+		end);
+
+		it("handles leading zeros in numeric components", function ()
+			cmp_version("1.02", "1.10", true);
+			cmp_version("1.10", "1.02", false);
+		end);
+
+		it("treats an empty or non-numeric string as a version with no components", function ()
+			cmp_version("", "1.0", true);
+			cmp_version("1.0", "", false);
+			cmp_version("foo", "1.0", true);
+			cmp_version("1.0", "foo", false);
+			cmp_version("", "", false);
+		end);
+	end);
 end);
 
 
--- a/util/human/io.lua	Sun May 31 11:51:22 2026 +0200
+++ b/util/human/io.lua	Fri Jun 12 11:38:39 2026 +0100
@@ -222,6 +222,29 @@
 	return tonumber(n) * ( multipliers_lax[m] or 1 );
 end
 
+-- Compares simple version strings (numeric only, not semver compatible)
+-- (can be passed to table.sort)
+local function simple_version_compare(a, b)
+	local a_f, a_s, a_part = a:gmatch("%d+");
+	local b_f, b_s, b_part = b:gmatch("%d+");
+
+	a_part = a_f(a_s, a_part);
+	b_part = b_f(b_s, b_part);
+
+	while a_part and b_part do
+		local an, bn = tonumber(a_part), tonumber(b_part);
+		if an ~= bn then
+			return an < bn;
+		end
+		a_part = a_f(a_s, a_part);
+		b_part = b_f(b_s, b_part);
+	end
+
+	-- return true (a is lower) if b is exhausted
+	-- otherwise, they are equal or b is longer/greater (-> return false)
+	return b_part ~= nil;
+end
+
 return {
 	getchar = getchar;
 	getline = getline;
@@ -237,4 +260,5 @@
 	table = new_table;
 	parse_duration = parse_duration;
 	parse_duration_lax = parse_duration_lax;
+	simple_version_compare = simple_version_compare;
 };