Skip to content

Commit 2b5de28

Browse files
committed
filename: 2x speed
1 parent db3125f commit 2b5de28

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

+stdlib/filename.m

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@
22
% filename (including suffix) without directory
33

44
function f = filename(p)
5-
% arguments
6-
% p (1,1) string
7-
% end
5+
arguments
6+
p (1,1) string
7+
end
88

99
% NOT https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getName()
1010
% because by our definition, a trailing directory component is not part of the filename
1111
% this is like C++17 filesystem::path::filename
1212

13-
[~, n, e] = fileparts(p);
13+
p = stdlib.posix(p);
14+
15+
i = strfind(p, "/");
1416

15-
if ischar(n)
16-
f = strcat(n, e);
17+
if isempty(i)
18+
f = p;
19+
elseif ischar(p)
20+
f = p(i(end)+1:end);
1721
else
18-
f = n + e;
22+
f = extractAfter(p, i(end));
1923
end
2024

25+
2126
end
2227

2328

0 commit comments

Comments
 (0)