Skip to content

Commit cc86060

Browse files
committed
version_atleast: more robust and correct
1 parent 55dacd8 commit cc86060

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

+stdlib/version_atleast.m

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,53 @@
2020
return
2121
end
2222

23-
in = split(in, ' ');
24-
in = split(in(1), '.');
23+
inp = split(in, ' ');
24+
in_str = split(inp(1), ".");
2525

26-
ref = split(ref, '.');
26+
refp = split(ref, ' ');
27+
ref_str = split(refp(1), ".");
2728

28-
for i = 1:min(length(in), length(ref))
29-
if in(i) > ref(i)
29+
% Compare numeric parts first
30+
for i = 1:min(length(in_str), length(ref_str))
31+
in_num = double(in_str(i));
32+
ref_num = double(ref_str(i));
33+
34+
if isnan(in_num) || isnan(ref_num)
35+
% assume values are leading integer with trailing string
36+
% extract integer part and compare
37+
in_num = double(regexp(in_str(i), "\d+", "match", "once"));
38+
ref_num = double(regexp(ref_str(i), "\d+", "match", "once"));
39+
40+
if isnan(in_num) || isnan(ref_num) || in_num == ref_num
41+
% compare string parts
42+
in_str_part = regexp(in_str(i), "\D+", "match", "once");
43+
ref_str_part = regexp(ref_str(i), "\D+", "match", "once");
44+
if in_str_part > ref_str_part
45+
r = true;
46+
return
47+
elseif in_str_part < ref_str_part
48+
r = false;
49+
return
50+
end
51+
52+
continue
53+
end
54+
end
55+
56+
% Compare numerically
57+
if in_num > ref_num
3058
r = true;
3159
return
32-
elseif in(i) < ref(i)
60+
elseif in_num < ref_num
3361
r = false;
3462
return
3563
end
3664
end
3765

38-
r = in(end) >= ref(end);
66+
% If all compared parts are equal, compare lengths
67+
r = length(in_str) >= length(ref_str);
3968

4069
end
4170

4271
%!assert(version_atleast("1.2.3", "1.2"))
72+
%!assert(version_atleast("20.11a", "20.3b"))

test/TestVersion.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
classdef TestVersion < matlab.unittest.TestCase
22

33
properties (TestParameter)
4-
v = {{"3.19.0.33", "3.19.0", true}, {"3.19.0.33", "3.19.0.34", false}}
4+
v = {{"11.1", "9.3", true}, ...
5+
{"3.19.0.33", "3.19.0", true}, ...
6+
{"3.19.0.33", "3.19.0.34", false}, ...
7+
{"1.5.0.3", "1.5.0", true}, ...
8+
{"1.5.0", "1.5.0.3", false}, ...
9+
{"11.5.1a", "11.5.1b", false}, ...
10+
{"1.13a", "1.2c", true}, ...
11+
{"1.2c", "1.13b", false}, ...
12+
}
513
end
614

715

0 commit comments

Comments
 (0)