Skip to content

Commit 52a5a70

Browse files
committed
parent: use choose_method
1 parent 1f037a0 commit 52a5a70

File tree

6 files changed

+17
-38
lines changed

6 files changed

+17
-38
lines changed

+stdlib/+native/parent.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
p = strcat(p, filesep);
1414
end
1515

16-
if isstring(pth)
17-
p = string(p);
18-
end
16+
p = string(p);
1917

2018
end

+stdlib/+python/parent.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
try
44
p = string(py.str(py.pathlib.Path(pth).parent));
5-
if ispc() && strcmp(p, stdlib.root_name(pth))
6-
p = strcat(p, filesep);
5+
if ispc() && p == stdlib.root_name(pth)
6+
p = p + filesep;
77
end
88
catch e
99
p = "";
10-
warning(e.identifier, "%s", e.message)
10+
warning(e.identifier, "parent(%s) failed: %s", pth, e.message)
1111
end
1212

1313
end

+stdlib/parent.m

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
11
%% PARENT parent directory of path
22
%
33
%% Examples:
4-
% parent("a/b/c") == "a/b"
5-
% parent("a/b/c/") == "a/b"
6-
%
7-
% MEX is about 10x faster than plain Matlab for this function
4+
% stdlib.parent("a/b/c") == "a/b"
5+
% stdlib.parent("a/b/c/") == "a/b"
86

9-
function p = parent(pth)
7+
function p = parent(pth, method)
108
arguments
119
pth {mustBeTextScalar}
10+
method (1,:) string = ["java", "python", "native"]
1211
end
1312

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.parent(pth);
20-
end
13+
fun = choose_method(method, "parent");
2114

22-
end
15+
p = fun(pth);
2316

24-
%!assert(parent("/a/b/c"), fullfile('/a','b'))
25-
%!assert(parent("/a/b/c/"), fullfile('/a','b'))
26-
%!assert(parent('/a///b'), fullfile('/a'))
27-
%!assert(parent('a/b/'), 'a')
28-
%!assert(parent('a//b/'), 'a')
29-
%!assert(parent('a//b'), 'a')
30-
%!test
31-
%! if ispc
32-
%! assert(parent('c:/a'), 'c:\')
33-
%! assert(parent('c:\a\'), 'c:\')
34-
%! assert(parent('c:\'), 'c:\')
35-
%! assert(parent('c:'), 'c:\')
36-
%! end
17+
end

+stdlib/private/choose_method.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626
end
2727
end
2828

29-
error("Could not find %s using %s", name, method)
29+
error("Could not find %s using %s", name, join(method, ','))
3030

3131
end

test/TestParent.m

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

33
properties (TestParameter)
44
p = init_parent()
5-
fun = {@stdlib.parent, @stdlib.java.parent, @stdlib.python.parent}
5+
method = {"java", "python", "native"}
66
end
77

88
methods(TestClassSetup)
@@ -14,10 +14,10 @@ function pkg_path(tc)
1414

1515
methods (Test, TestTags="pure")
1616

17-
function test_parent(tc, p, fun)
18-
is_capable(tc, fun)
17+
function test_parent(tc, p, method)
18+
is_capable(tc, str2func("stdlib." + method + ".parent"))
1919

20-
tc.verifyEqual(fun(p{1}), p{2}, sprintf("parent(%s)", p{1}))
20+
tc.verifyEqual(stdlib.parent(p{1}, method), p{2}, sprintf("parent(%s, %s)", p{1}, method))
2121
end
2222

2323
end

test/TestResolve.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function test_resolve_fullpath(tc, p)
7272
a = p;
7373
switch a
7474
case {'', "", '.', "."}, b = string(tc.td);
75-
case {'..', ".."}, b = string(stdlib.parent(tc.td));
75+
case {'..', ".."}, b = string(fileparts(tc.td));
7676
end
7777

7878
tc.verifyEqual(stdlib.resolve(a), b)

0 commit comments

Comments
 (0)