Skip to content

Commit a3f1415

Browse files
committed
relative_to: faster, simpler
1 parent d25df44 commit a3f1415

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

+stdlib/relative_to.m

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
other {mustBeTextScalar}
1717
end
1818

19+
if strempty(base) && strempty(other)
20+
rel = ".";
21+
return
22+
end
23+
1924
if stdlib.has_python()
2025
rel = relative_to_python(base, other);
2126
elseif stdlib.dotnet_api() >= 5
@@ -29,11 +34,6 @@
2934

3035
function rel = relative_to_dotnet(base, other)
3136

32-
if strempty(base) && strempty(other)
33-
rel = ".";
34-
return
35-
end
36-
3737
if strempty(other)
3838
rel = base;
3939
return
@@ -67,13 +67,7 @@
6767
function rel = relative_to_python(base, other)
6868

6969
try
70-
bp = py.pathlib.Path(other);
71-
if stdlib.version_atleast(stdlib.python_version(), "3.12")
72-
r = bp.relative_to(base, pyargs(walk_up=true));
73-
else
74-
r = bp.relative_to(base);
75-
end
76-
rel = string(py.str(r));
70+
rel = string(py.os.path.relpath(other, base));
7771
catch e
7872
if e.identifier == "MATLAB:Python:PyException" && startsWith(e.message, 'Python Error: ValueError')
7973
rel = "";

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

0 commit comments

Comments
 (0)