Skip to content

Commit 3d1f66a

Browse files
committed
add fileio.parent()
1 parent 5043b7b commit 3d1f66a

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

+stdlib/+fileio/parent.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function p = parent(path)
2+
% PARENT parent directory of path
3+
arguments
4+
path string
5+
end
6+
7+
% must drop trailing slash - need to as_posix for windows
8+
p = fileparts(strip(stdlib.fileio.posix(path), 'right', '/'));
9+
10+
end

+stdlib/+test/TestFilePure.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ function test_posix(tc)
1414
end
1515

1616

17+
function test_parent(tc)
18+
19+
tc.assertEmpty(stdlib.fileio.parent(string.empty))
20+
tc.verifyEqual(stdlib.fileio.parent(""), "")
21+
22+
tc.verifyEqual(stdlib.fileio.parent("/foo/bar/baz"), "/foo/bar")
23+
tc.verifyEqual(stdlib.fileio.parent("/foo/bar/baz/"), "/foo/bar")
24+
25+
tc.verifyEqual(stdlib.fileio.parent("foo/bar/baz/"), "foo/bar")
26+
27+
end
28+
29+
1730
function test_path_tail(tc)
1831

1932
import stdlib.path_tail

+stdlib/parent.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function p = parent(path)
2+
% PARENT parent of path
3+
arguments
4+
path string
5+
end
6+
7+
p = stdlib.fileio.parent(path);
8+
9+
end

0 commit comments

Comments
 (0)