comparison 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
comparison
equal deleted inserted replaced
14213:75d09be04f13 14217:7eb0e47418ab
218 218
219 local function parse_duration_lax(duration_string) 219 local function parse_duration_lax(duration_string)
220 local n, m = duration_string:lower():match("(%d+)%s*([smhdwy]?[io]?)"); 220 local n, m = duration_string:lower():match("(%d+)%s*([smhdwy]?[io]?)");
221 if not n then return nil; end 221 if not n then return nil; end
222 return tonumber(n) * ( multipliers_lax[m] or 1 ); 222 return tonumber(n) * ( multipliers_lax[m] or 1 );
223 end
224
225 -- Compares simple version strings (numeric only, not semver compatible)
226 -- (can be passed to table.sort)
227 local function simple_version_compare(a, b)
228 local a_f, a_s, a_part = a:gmatch("%d+");
229 local b_f, b_s, b_part = b:gmatch("%d+");
230
231 a_part = a_f(a_s, a_part);
232 b_part = b_f(b_s, b_part);
233
234 while a_part and b_part do
235 local an, bn = tonumber(a_part), tonumber(b_part);
236 if an ~= bn then
237 return an < bn;
238 end
239 a_part = a_f(a_s, a_part);
240 b_part = b_f(b_s, b_part);
241 end
242
243 -- return true (a is lower) if b is exhausted
244 -- otherwise, they are equal or b is longer/greater (-> return false)
245 return b_part ~= nil;
223 end 246 end
224 247
225 return { 248 return {
226 getchar = getchar; 249 getchar = getchar;
227 getline = getline; 250 getline = getline;
235 term_width = term_width; 258 term_width = term_width;
236 ellipsis = ellipsis; 259 ellipsis = ellipsis;
237 table = new_table; 260 table = new_table;
238 parse_duration = parse_duration; 261 parse_duration = parse_duration;
239 parse_duration_lax = parse_duration_lax; 262 parse_duration_lax = parse_duration_lax;
263 simple_version_compare = simple_version_compare;
240 }; 264 };