Skip to content

Commit 59cc72a

Browse files
committed
add relative_to()
1 parent f39e668 commit 59cc72a

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

+stdlib/+fileio/relative_to.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function r = relative_to(base, other)
2+
arguments
3+
base (1,1) string
4+
other (1,1) string
5+
end
6+
7+
% must remove trailing slashes
8+
base = stdlib.fileio.normalize(base);
9+
other = stdlib.fileio.normalize(other);
10+
11+
if base == "" || other == ""
12+
r = "";
13+
elseif base == other
14+
r = ".";
15+
else
16+
b = java.io.File(base).toPath();
17+
o = java.io.File(other).toPath();
18+
try
19+
r = stdlib.fileio.posix(b.relativize(o));
20+
catch e
21+
if contains(e.message, 'java.lang.IllegalArgumentException')
22+
r = "";
23+
else
24+
rethrow(e);
25+
end
26+
end
27+
end
28+
29+
end

+stdlib/relative_to.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function r = relative_to(base, other)
2+
arguments
3+
base (1,1) string
4+
other (1,1) string
5+
end
6+
7+
if base == "" && other == ""
8+
r = "."; % like C++ std::filesystem::relative
9+
else
10+
r = stdlib.fileio.relative_to(base, other);
11+
end
12+
13+
end

Readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ buildtool
1717
```
1818

1919
[API Documentation](https://geospace-code.github.io/matlab-stdlib)
20+
21+
## Developer notes
22+
23+
The "matlab-stdlib" package uses Java functions throughout, with the higher-level
24+
[java.nio.Files](https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/Files.html)
25+
and the classic
26+
[java.io.File](https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/io/File.html) classes.

test/TestFilePure.m

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,70 @@
11
classdef TestFilePure < matlab.unittest.TestCase
22

3+
properties (ClassSetupParameter)
4+
classToTest = {"TestFilePure"};
5+
end
6+
7+
properties (TestParameter)
8+
base
9+
other
10+
ref
11+
end
12+
13+
properties
14+
tobj
15+
end
16+
17+
methods (TestParameterDefinition,Static)
18+
function [base, other, ref] = initializeProperty(classToTest) %#ok<INUSD>
19+
if ispc
20+
21+
base = {'', 'Hello', 'Hello', ...
22+
'c:\a\b', 'c:\', 'c:/a/b', 'c:/a/b', 'c:\a/b\c/d', 'c:/path'};
23+
24+
other = {'', 'Hello', 'Hello/', ...
25+
'c:/', 'c:/a/b', 'c:/a/b', 'c:/a', 'c:/a\b', 'd:/path'};
26+
27+
ref = {'.', '.', '.', '../..', 'a/b', '.', '..', '../..', ''};
28+
29+
else
30+
31+
base = {'', '', '/', '/', 'Hello', 'Hello', '/dev/null', '/a/b', 'c', ...
32+
'/a/b', '/a/b', '/a/b/c/d', './this/one', '/this/one', '/path/same'};
33+
34+
other = {'', '/', '', '/', 'Hello', 'Hello/', '/dev/null', 'c', '/a/b', ...
35+
'/a/b', '/a', '/a/b', './this/two', '/this/two', '/path/same/hi/..'};
36+
37+
ref = {'.', '', '', '.', '.', '.', '.', '', '', ...
38+
'.', '..', '../..', '../two', '../two', '.'};
39+
40+
end
41+
end
42+
end
43+
44+
45+
methods (TestClassSetup)
46+
function classSetup(testCase, classToTest)
47+
constructor = str2func(classToTest);
48+
testCase.tobj = constructor();
49+
end
50+
end
51+
52+
53+
354
methods (TestClassSetup)
55+
456
function setup_path(tc)
557
import matlab.unittest.fixtures.PathFixture
658
cwd = fileparts(mfilename("fullpath"));
759
top = fullfile(cwd, "..");
860
tc.applyFixture(PathFixture(top))
961
end
62+
1063
end
1164

1265

13-
methods (Test)
66+
67+
methods (Test, ParameterCombination = 'sequential')
1468

1569
function test_posix(tc)
1670
import matlab.unittest.constraints.ContainsSubstring
@@ -140,5 +194,14 @@ function test_with_suffix(tc)
140194
tc.verifyEqual(stdlib.with_suffix("", ".nc"), ".nc")
141195
end
142196

197+
198+
function test_relative_to(tc, base, other, ref)
199+
200+
r = stdlib.relative_to(base, other);
201+
tc.verifyEqual(r, string(ref))
202+
203+
end
204+
205+
143206
end
144207
end

0 commit comments

Comments
 (0)