Skip to content

Commit 84fd7c5

Browse files
committed
relative_to: subfunction
1 parent f9c5801 commit 84fd7c5

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

+stdlib/relative_to.m

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,70 @@
1616
other {mustBeTextScalar}
1717
end
1818

19+
if stdlib.has_python()
20+
rel = relative_to_python(base, other);
21+
elseif stdlib.dotnet_api() >= 5
22+
rel = relative_to_dotnet(base, other);
23+
else
24+
error('no supported relative path method found, please install .NET or "buildtool mex"')
25+
end
1926

20-
if stdlib.dotnet_api() >= 5
27+
end
2128

22-
if strempty(base) && strempty(other), rel = "."; return, end
2329

24-
if strempty(other), rel = base; return, end
25-
if strempty(base), rel = other; return, end
30+
function rel = relative_to_dotnet(base, other)
2631

27-
bis = stdlib.is_absolute(base);
28-
ois = stdlib.is_absolute(other);
32+
if strempty(base) && strempty(other)
33+
rel = ".";
34+
return
35+
end
2936

30-
if bis ~= ois, rel = ""; return, end
37+
if strempty(other)
38+
rel = base;
39+
return
40+
end
41+
if strempty(base)
42+
rel = other;
43+
return
44+
end
3145

32-
base = fullfile(base);
33-
other = fullfile(other);
46+
bis = stdlib.is_absolute(base);
47+
ois = stdlib.is_absolute(other);
3448

35-
if bis && ~(startsWith(base, other) || startsWith(other, base))
36-
rel = "";
49+
if bis ~= ois
50+
rel = "";
51+
return
52+
end
53+
54+
base = fullfile(base);
55+
other = fullfile(other);
56+
57+
if bis && ~(startsWith(base, other) || startsWith(other, base))
58+
rel = "";
59+
else
60+
% https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getrelativepath
61+
rel = string(System.IO.Path.GetRelativePath(base, other));
62+
end
63+
64+
end
65+
66+
67+
function rel = relative_to_python(base, other)
68+
69+
try
70+
bp = py.pathlib.Path(other);
71+
if stdlib.python_version() >= "3.12"
72+
r = bp.relative_to(base, pyargs(walk_up=true));
3773
else
38-
% https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getrelativepath
39-
rel = string(System.IO.Path.GetRelativePath(base, other));
74+
r = bp.relative_to(base);
4075
end
41-
elseif stdlib.has_python()
42-
try
43-
bp = py.pathlib.Path(other);
44-
if stdlib.python_version() >= "3.12"
45-
r = bp.relative_to(base, pyargs(walk_up=true));
46-
else
47-
r = bp.relative_to(base);
48-
end
49-
rel = string(py.str(r));
50-
catch e
51-
if e.identifier == "MATLAB:Python:PyException" && startsWith(e.message, 'Python Error: ValueError')
52-
rel = "";
53-
end
76+
rel = string(py.str(r));
77+
catch e
78+
if e.identifier == "MATLAB:Python:PyException" && startsWith(e.message, 'Python Error: ValueError')
79+
rel = "";
80+
else
81+
rethrow(e)
5482
end
55-
else
56-
error('no supported relative path method found, please install .NET or "buildtool mex"')
5783
end
5884

5985
end

test/TestRelative.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
methods (Test)
99

1010
function test_relative_to(tc, pr)
11-
tc.assumeTrue((stdlib.dotnet_api() >= 5) || stdlib.is_mex_fun("stdlib.relative_to"))
11+
tc.assumeTrue(stdlib.has_python() || (stdlib.dotnet_api() >= 5) || stdlib.is_mex_fun("stdlib.relative_to"))
1212

1313
tc.verifyEqual(stdlib.relative_to(pr{1}, pr{2}), pr{3}, ...
1414
"relative_to(" + pr{1} + "," + pr{2}+")")
1515
end
1616

1717
function test_proximate_to(tc, pp)
18-
tc.assumeTrue((stdlib.dotnet_api() >= 5) || stdlib.is_mex_fun("stdlib.proximate_to"))
18+
tc.assumeTrue(stdlib.has_python() || (stdlib.dotnet_api() >= 5) || stdlib.is_mex_fun("stdlib.proximate_to"))
1919

2020
tc.verifyEqual(stdlib.proximate_to(pp{1}, pp{2}), pp{3}, ...
2121
"proximate_to(" + pp{1} + ", " + pp{2}+")")

0 commit comments

Comments
 (0)