Skip to content

Commit d25df44

Browse files
committed
version_atleast more efficent, numeric only
1 parent 84fd7c5 commit d25df44

File tree

6 files changed

+30
-53
lines changed

6 files changed

+30
-53
lines changed

+stdlib/python_version.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
function v = python_version()
44

5-
pe = pyenv();
6-
v = pe.Version;
5+
try
6+
pe = pyenv();
7+
v = pe.Version;
8+
catch
9+
v = "";
10+
end
711

812
end

+stdlib/relative_to.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
ois = stdlib.is_absolute(other);
4848

4949
if bis ~= ois
50-
rel = "";
50+
rel = "";
5151
return
5252
end
5353

@@ -68,7 +68,7 @@
6868

6969
try
7070
bp = py.pathlib.Path(other);
71-
if stdlib.python_version() >= "3.12"
71+
if stdlib.version_atleast(stdlib.python_version(), "3.12")
7272
r = bp.relative_to(base, pyargs(walk_up=true));
7373
else
7474
r = bp.relative_to(base);

+stdlib/version_atleast.m

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
%% VERSION_ATLEAST compare two string verions: major.minor.rev.patch
22
% compare two string verions: major.minor.rev.patch
3-
% uses strings to compare so mixed number/string is OK
3+
% numeric portions only.
44
%
55
%% Inputs
66
% * in: version to examine (string)
@@ -10,8 +10,8 @@
1010

1111
function r = version_atleast(in, ref)
1212
arguments
13-
in (1,1) string
14-
ref (1,1) string
13+
in {mustBeTextScalar}
14+
ref {mustBeTextScalar}
1515
end
1616

1717

@@ -20,51 +20,27 @@
2020
return
2121
end
2222

23-
inp = split(in, ' ');
24-
in_str = split(inp(1), ".");
23+
parts1 = str2double(strsplit(in, '.'));
24+
parts2 = str2double(strsplit(ref, '.'));
2525

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

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)
6130
r = false;
6231
return
32+
elseif parts1(i) > parts2(i)
33+
return
6334
end
6435
end
6536

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
6844

6945
end
7046

test/TestRelative.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function test_proximate_to(tc, pp)
3737
};
3838
% NOTE: ".." in relative_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc.
3939

40-
if stdlib.has_dotnet() || stdlib.python_version() >= "3.12"
40+
if stdlib.has_dotnet() || stdlib.version_atleast(stdlib.python_version(), "3.12")
4141
p = [p, {
4242
{"a/b/c/d", "a/b", fullfile("..", "..")}, ...
4343
{"a/b", "a/c", fullfile("..", "c")}, ...
@@ -49,7 +49,7 @@ function test_proximate_to(tc, pp)
4949

5050
if ispc
5151

52-
if stdlib.has_dotnet() || stdlib.python_version() >= "3.12"
52+
if stdlib.has_dotnet() || stdlib.version_atleast(stdlib.python_version(), "3.12")
5353
p = [p, { ...
5454
{"C:/a/b", "C:/", fullfile("..", "..")}, ...
5555
{"c:/a/b", "c:/a", ".."}, ...

test/TestSys.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ function test_platform_int32(tc, fi32)
2020
function test_dotnet_version(tc)
2121
tc.assumeTrue(stdlib.has_dotnet())
2222
v = stdlib.dotnet_version();
23-
tc.verifyGreaterThan(v, "4.0", ".NET version should be greater than 4.0")
23+
tc.verifyTrue(stdlib.version_atleast(v, "4.0"), ".NET version should be greater than 4.0")
2424
end
2525

2626
function test_has_python(tc)
2727
tc.assumeTrue(stdlib.has_python())
2828
v = stdlib.python_version();
29-
tc.verifyGreaterThan(strlength(v), 0, "expected non-empty Python version")
29+
tc.verifyTrue(stdlib.version_atleast(v, "3.8"), "expected Python >= 3.8")
3030
end
3131

3232
function test_os_version(tc)

test/TestVersion.m

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
{"3.19.0.33", "3.19.0", true}, ...
66
{"3.19.0.33", "3.19.0.34", false}, ...
77
{"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}, ...
8+
{"1.5.0", "1.5.0.3", false}
129
}
1310
end
1411

0 commit comments

Comments
 (0)