Skip to content

Commit 3901092

Browse files
committed
parent: efficiency
simplify parent: native only The overhead and complication of multiple backends was not worth the possible 10% speedup in Python
1 parent 113e757 commit 3901092

File tree

12 files changed

+79
-83
lines changed

12 files changed

+79
-83
lines changed

+stdlib/+java/parent.m

Lines changed: 0 additions & 15 deletions
This file was deleted.

+stdlib/+native/parent.m

Lines changed: 0 additions & 23 deletions
This file was deleted.

+stdlib/+python/get_owner.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
if ~strlength(file), return, end
66

77
try %#ok<TRYNC>
8-
n = string(py.str(py.pathlib.Path(file).owner()));
8+
n = string(py.pathlib.Path(file).owner());
99
end
1010

1111
end

+stdlib/+python/is_char_device.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
try
44
y = py.pathlib.Path(p).is_char_device();
55
catch e
6-
warning(e.identifier, "Python is_char_device failed: %s", e.message);
7-
y = false;
6+
y = logical.empty;
87
end

+stdlib/+python/is_exe.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
function y = is_exe(p)
2+
arguments
3+
p (1,1) string
4+
end
25

36
y = isfile(p);
47

+stdlib/+python/is_symlink.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
try
44
y = py.pathlib.Path(p).is_symlink();
55
catch e
6-
warning(e.identifier, "Python is_symlink failed: %s", e.message);
7-
y = false;
6+
y = logical.empty;
87
end
98

109
end

+stdlib/+python/parent.m

Lines changed: 0 additions & 13 deletions
This file was deleted.

+stdlib/parent.m

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22
%
33
%% Inputs
44
% * file: path to file or folder
5-
% * backend: backend to use
65
%% Outputs
76
% * par: parent directory of path
8-
% * b: backend used
97
%% Examples:
108
% stdlib.parent("a/b/c") == "a/b"
119
% stdlib.parent("a/b/c/") == "a/b"
1210

13-
function [par, b] = parent(file, backend)
11+
function p = parent(file)
1412
arguments
1513
file string
16-
backend (1,:) string = ["java", "python", "native"]
1714
end
1815

19-
if isscalar(file)
20-
[fun, b] = hbackend(backend, "parent");
21-
par = fun(file);
22-
else
23-
b = "native";
24-
par = stdlib.native.parent(file);
16+
p = fileparts(stdlib.drop_slash(file));
17+
18+
i = ~strlength(p);
19+
p(i) = ".";
20+
21+
% the ~all(i) is for Windows Matlab < R2025a
22+
if ispc() && ~all(i)
23+
i = p(~i) == stdlib.root_name(file(~i));
24+
p(i) = p(i) + "/";
2525
end
2626

2727
end

example/+javafun/parent.m

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

example/+python/parent.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function par = parent(pth)
2+
arguments
3+
pth (1,1) string
4+
end
5+
6+
try
7+
op = py.pathlib.Path(pth);
8+
p = op.parent;
9+
par = string(py.str(p)); % 2x faster than string(op.parents(1))
10+
if ispc() && p == op.drive
11+
par = par + filesep;
12+
end
13+
catch e
14+
par = "";
15+
end
16+
17+
end

0 commit comments

Comments
 (0)