Skip to content

Commit 73c63d8

Browse files
committed
parent: java, python, native options
1 parent 8571b9a commit 73c63d8

File tree

5 files changed

+60
-20
lines changed

5 files changed

+60
-20
lines changed

+stdlib/+java/parent.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function p = parent(pth)
2+
3+
p = javaObject("java.io.File", pth).getParent();
4+
if isempty(p)
5+
rn = stdlib.root_name(pth);
6+
if ispc() && ~stdlib.strempty(pth) && ~stdlib.strempty(rn) && startsWith(pth, rn)
7+
p = strcat(rn, filesep);
8+
else
9+
p = ".";
10+
end
11+
else
12+
p = string(p);
13+
end
14+
15+
end

+stdlib/+native/parent.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function p = parent(pth)
2+
3+
f = fullfile(char(pth));
4+
if endsWith(f, {'/', filesep}) && ~strcmp(f, stdlib.root(f))
5+
f = f(1:end-1);
6+
end
7+
8+
p = fileparts(f);
9+
10+
if stdlib.strempty(p)
11+
p = '.';
12+
elseif ispc() && strcmp(p, stdlib.root_name(pth))
13+
p = strcat(p, filesep);
14+
end
15+
16+
if isstring(pth)
17+
p = string(p);
18+
end
19+
20+
end

+stdlib/+python/parent.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function p = parent(pth)
2+
3+
try
4+
p = string(py.str(py.pathlib.Path(pth).parent));
5+
if ispc() && strcmp(p, stdlib.root_name(pth))
6+
p = strcat(p, filesep);
7+
end
8+
catch e
9+
p = "";
10+
warning(e.identifier, "%s", e.message)
11+
end
12+
13+
end

+stdlib/parent.m

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,12 @@
1111
pth {mustBeTextScalar}
1212
end
1313

14-
f = fullfile(char(pth));
15-
if endsWith(f, {'/', filesep}) && ~strcmp(f, stdlib.root(f))
16-
f = f(1:end-1);
17-
end
18-
19-
p = fileparts(f);
20-
21-
if stdlib.strempty(p)
22-
p = '.';
23-
elseif ispc() && strcmp(p, stdlib.root_name(pth))
24-
p = strcat(p, filesep);
25-
end
26-
27-
if isstring(pth)
28-
p = string(p);
14+
if stdlib.has_java()
15+
p = stdlib.java.parent(pth);
16+
elseif stdlib.has_python()
17+
p = stdlib.python.parent(pth);
18+
else
19+
p = stdlib.native.normalize(pth);
2920
end
3021

3122
end

test/TestParent.m

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
properties (TestParameter)
44
p = init_parent()
5+
fun = {@stdlib.parent, @stdlib.java.parent, @stdlib.python.parent}
56
end
67

78
methods(TestClassSetup)
@@ -13,10 +14,10 @@ function pkg_path(tc)
1314

1415
methods (Test, TestTags="pure")
1516

16-
function test_parent(tc, p)
17-
r = p{2};
17+
function test_parent(tc, p, fun)
18+
is_capable(tc, fun)
1819

19-
tc.verifyEqual(stdlib.parent(p{1}), r, sprintf("mex: %d", stdlib.is_mex_fun("stdlib.parent")))
20+
tc.verifyEqual(fun(p{1}), p{2}, sprintf("parent(%s)", p{1}))
2021
end
2122

2223
end
@@ -46,7 +47,7 @@ function test_parent(tc, p)
4647
p{end+1} = {"c:", "c:\"};
4748
end
4849

49-
p{end+1} = {'a/b/', 'a'};
50-
p{end+1} = {'a//b', 'a'};
50+
p{end+1} = {'a/b/', "a"};
51+
p{end+1} = {'a//b', "a"};
5152

5253
end

0 commit comments

Comments
 (0)