|
1 | 1 | %% VERSION_ATLEAST compare two string verions: major.minor.rev.patch |
2 | 2 | % compare two string verions: major.minor.rev.patch |
3 | | -% uses strings to compare so mixed number/string is OK |
| 3 | +% numeric portions only. |
4 | 4 | % |
5 | 5 | %% Inputs |
6 | 6 | % * in: version to examine (string) |
|
10 | 10 |
|
11 | 11 | function r = version_atleast(in, ref) |
12 | 12 | arguments |
13 | | - in (1,1) string |
14 | | - ref (1,1) string |
| 13 | + in {mustBeTextScalar} |
| 14 | + ref {mustBeTextScalar} |
15 | 15 | end |
16 | 16 |
|
17 | 17 |
|
|
20 | 20 | return |
21 | 21 | end |
22 | 22 |
|
23 | | -inp = split(in, ' '); |
24 | | -in_str = split(inp(1), "."); |
| 23 | +parts1 = str2double(strsplit(in, '.')); |
| 24 | +parts2 = str2double(strsplit(ref, '.')); |
25 | 25 |
|
26 | | -refp = split(ref, ' '); |
27 | | -ref_str = split(refp(1), "."); |
| 26 | +r = true; |
28 | 27 |
|
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 |
58 | | - r = true; |
59 | | - return |
60 | | - elseif in_num < ref_num |
| 28 | +for i = 1:min(length(parts1), length(parts2)) |
| 29 | + if parts1(i) < parts2(i) |
61 | 30 | r = false; |
62 | 31 | return |
| 32 | + elseif parts1(i) > parts2(i) |
| 33 | + return |
63 | 34 | end |
64 | 35 | end |
65 | 36 |
|
66 | | -% If all compared parts are equal, compare lengths |
67 | | -r = length(in_str) >= length(ref_str); |
| 37 | +% If all common parts are equal, check for longer versions |
| 38 | +% If "ref" is longer and its remaining parts are not all zeros, then "in" is less. |
| 39 | +if length(parts1) < length(parts2) |
| 40 | + if any(parts2(length(parts1)+1:end) > 0) |
| 41 | + r = false; |
| 42 | + end |
| 43 | +end |
68 | 44 |
|
69 | 45 | end |
70 | 46 |
|
|
0 commit comments