diff util/human/io.lua @ 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 519c16c2eb21
children 565debd9c37a
line wrap: on
line diff
--- 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;
 };