Skip to content

Commit 21286ae

Browse files
committed
filename: more efficient
1 parent eed2abd commit 21286ae

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

+stdlib/filename.m

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,21 @@
1414
% because by our definition, a trailing directory component is not part of the filename
1515
% this is like C++17 filesystem::path::filename
1616

17-
parts = strsplit(stdlib.posix(p), '/');
17+
s = '/';
18+
if ispc()
19+
s = '[/\\]';
20+
end
21+
22+
f = char(p);
1823

19-
f = parts{end};
24+
im = regexp(f, s, 'end');
25+
if ~isempty(im)
26+
if im(end) == length(f)
27+
f = '';
28+
else
29+
f = f(im(end)+1:end);
30+
end
31+
end
2032

2133
if isstring(p)
2234
f = string(f);

test/TestFilename.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{'', ''}, ...
66
{"", ""}, ...
77
{"/a/b/c", "c"}, ...
8-
{'/a/b/c/', ''}, ...
8+
{'/a/b/', ''}, ...
99
{'a/b/c.txt', 'c.txt'}, ...
1010
{"a/b/c.txt.gz", "c.txt.gz"}, ...
1111
};

0 commit comments

Comments
 (0)