Skip to content

Commit 2c719fc

Browse files
committed
relative_to: add native method
1 parent 6ad45dd commit 2c719fc

File tree

9 files changed

+28
-22
lines changed

9 files changed

+28
-22
lines changed

+stdlib/+native/relative_to.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
%% NATIVE.RELATIVE_TO find relative path to other from base
2+
%
3+
% this only accepts "/" as filesep, hence we use stdlib.posix()
4+
5+
function rel = relative_to(base, other)
6+
7+
if stdlib.strempty(base) || stdlib.strempty(other)
8+
rel = "";
9+
else
10+
rel = matlab.io.internal.filesystem.relativepath(stdlib.posix(base), stdlib.posix(other));
11+
end
12+
13+
end

+stdlib/+python/relative_to.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function rel = relative_to(base, other)
22

33
try
4-
rel = string(py.os.path.relpath(other, base));
4+
rel = stdlib.posix(string(py.os.path.relpath(other, base)));
55
catch e
66
if e.identifier == "MATLAB:Python:PyException" && startsWith(e.message, 'Python Error: ValueError')
77
rel = "";

+stdlib/+sys/relative_to.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
%% SYS.RELATIVE_TO get relative path to other from base
2+
13
function r = relative_to(base, other)
24

35
r = "";
@@ -22,7 +24,7 @@
2224
r = string(strip(r));
2325

2426
if ispc()
25-
r = fullfile(stdlib.normalize(r));
27+
r = stdlib.posix(stdlib.normalize(r));
2628
end
2729

2830
end

+stdlib/posix.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
%
55

66
function r = posix(p)
7-
arguments
8-
p {mustBeTextScalar}
9-
end
107

118
if ispc
129
r = strrep(p, '\', '/');

+stdlib/relative_to.m

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

19-
if (stdlib.strempty(base) && stdlib.strempty(other)) || ...
20-
(stdlib.is_absolute(base) ~= stdlib.is_absolute(other))
21-
rel = "";
22-
return
23-
end
24-
25-
if stdlib.has_python()
19+
if ~isMATLABReleaseOlderThan('R2024a')
20+
rel = stdlib.native.relative_to(base, other);
21+
elseif stdlib.has_python()
2622
rel = stdlib.python.relative_to(base, other);
27-
elseif stdlib.dotnet_api() >= 5
28-
rel = stdlib.dotnet.relative_to(base, other);
2923
else
3024
rel = stdlib.sys.relative_to(base, other);
3125
end
File renamed without changes.

+stdlib/+dotnet/relative_to.m renamed to example/dotnet/relative_to.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
%% DOTNET.RELATIVE_TO find the relative path to other from base
2+
%
3+
% not normally used because it's overly complex
24

35
function rel = relative_to(base, other)
46

7+
assert(stdlib.dotnet_api() >= 5)
8+
59
if stdlib.strempty(other)
610
rel = base;
711
return

test/TestRelative.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
properties (TestParameter)
88
pr = init_rel()
99
pp = init_prox()
10-
rel_fun = {@stdlib.relative_to, @stdlib.sys.relative_to, @stdlib.dotnet.relative_to, @stdlib.python.relative_to}
10+
rel_fun = {@stdlib.relative_to, @stdlib.sys.relative_to, @stdlib.native.relative_to, @stdlib.python.relative_to}
1111
end
1212

1313
methods(TestClassSetup)
@@ -44,8 +44,7 @@ function test_proximate_to(tc, pp)
4444

4545
p = {{"", "", ""}, ...
4646
{fileparts(pwd()), pwd(), "test"}, ...
47-
{root, fullfile(root, "test", mfilename() + ".m"), fullfile("test", mfilename + ".m")}, ...
48-
{"/", "/", "."}, ...
47+
{root, root + "/test/" + mfilename() + ".m", "test/" + mfilename + ".m"}
4948
};
5049
% NOTE: ".." in relative_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc.
5150

@@ -79,6 +78,7 @@ function test_proximate_to(tc, pp)
7978
else
8079

8180
p = [p, {
81+
{"/", "/", "."}, ...
8282
{"/dev/null", "/dev/null", "."}, ...
8383
}];
8484
end

test/is_capable.m

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ function is_capable(tc, f)
1616
tc.assumeTrue(ispc(), "Windows only function")
1717
end
1818

19-
if endsWith(n, "relative_to")
20-
tc.assumeGreaterThanOrEqual(dapi, 5)
21-
end
22-
2319
if endsWith(n, ["create_symlink", "ram_total", "read_symlink"])
2420
tc.assumeGreaterThanOrEqual(dapi, 6);
2521
end
@@ -61,7 +57,7 @@ function is_capable(tc, f)
6157
tc.assumeFalse(isMATLABReleaseOlderThan('R2025a'))
6258
end
6359

64-
if endsWith(n, "canonical")
60+
if endsWith(n, ["canonical", "relative_to"])
6561
tc.assumeFalse(isMATLABReleaseOlderThan('R2024a'))
6662
end
6763

0 commit comments

Comments
 (0)