Skip to content

Commit 9ab39ee

Browse files
committed
native.parent: any size array
1 parent 055234d commit 9ab39ee

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

+stdlib/+native/parent.m

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
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);
2+
arguments
3+
pth string
64
end
75

6+
f = fullfile(pth);
7+
8+
i = endsWith(f, ["/", filesep]) & ~strcmp(f, stdlib.root(f));
9+
10+
f(i) = extractBefore(f(i), strlength(f(i)));
11+
812
p = fileparts(f);
913

10-
if stdlib.strempty(p)
11-
p = '.';
12-
elseif ispc() && strcmp(p, stdlib.root_name(pth))
13-
p = strcat(p, filesep);
14-
end
14+
i = stdlib.strempty(p);
15+
p(i) = ".";
1516

16-
p = string(p);
17+
% the ~all(i) is for Windows Matlab < R2025a
18+
if ispc() && ~all(i)
19+
i = p(~i) == stdlib.root_name(pth(~i));
20+
p(i) = strcat(p(i), filesep);
21+
end
1722

1823
end

+stdlib/parent.m

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
% stdlib.parent("a/b/c") == "a/b"
55
% stdlib.parent("a/b/c/") == "a/b"
66

7-
function p = parent(pth, backend)
7+
function par = parent(file, backend)
88
arguments
9-
pth {mustBeTextScalar}
10-
backend (1,:)string = ["java", "python", "native"]
9+
file string
10+
backend (1,:) string = ["java", "python", "native"]
1111
end
1212

13-
fun = hbackend(backend, "parent");
14-
15-
p = fun(pth);
13+
if isscalar(file)
14+
fun = hbackend(backend, "parent");
15+
par = fun(file);
16+
else
17+
par = stdlib.native.parent(file);
18+
end
1619

1720
end

private/get_compiler.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
case "fortran"
1515
comp = getenv("FC");
1616
if isempty(comp)
17-
disp("set FC environment variable to the Fortran compiler path via get_compiler('fortran'), or do 'mex -setup c++")
17+
disp("set FC environment variable to the Fortran compiler path via get_compiler('fortran'), or do 'mex -setup Fortran")
1818
end
1919
case "c++"
2020
comp = getenv("CXX");
@@ -49,5 +49,3 @@
4949
end
5050

5151
end
52-
53-

test/TestParent.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function test_parent(tc, p, backend)
1818
try
1919
pr = stdlib.parent(p{1}, backend);
2020
catch e
21-
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
21+
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.stack(1).file + ":" + string(e.stack(1).line) + " " + e.message)
2222
return
2323
end
2424
tc.verifyEqual(pr, p{2}, sprintf("parent(%s, %s)", p{1}, backend))

0 commit comments

Comments
 (0)