Skip to content

Commit 53bf676

Browse files
committed
filename: selectable pattern | regexp
1 parent 48a52bd commit 53bf676

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

+stdlib/filename.m

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,21 @@
55
%%% Outputs
66
% filename (including suffix) without directory
77

8-
function f = filename(p)
8+
function f = filename(p, method)
9+
arguments
10+
p
11+
method = 'pattern'
12+
end
913

10-
f = extractAfter(p, asManyOfPattern(wildcardPattern + ("/" | filesep)));
14+
% the pattern method is a few percent faster than regexp
15+
switch method
16+
case 'pattern', f = extractAfter(p, asManyOfPattern(wildcardPattern + ("/" | filesep)));
17+
case 'regexp'
18+
f = regexp(p, ['[^/\' filesep ']*$'], 'match', 'once');
19+
try %#ok<TRYNC>
20+
f(ismissing(f)) = "";
21+
end
22+
otherwise, error('unknown method %s', method)
23+
end
1124

1225
end

test/TestFilename.m

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
properties (TestParameter)
44
p = init_p()
55
e = {'/a/b/c/', "a/b/c/"}
6+
method = {'pattern', 'regexp'}
67
end
78

89
methods(TestClassSetup)
@@ -13,12 +14,15 @@ function pkg_path(tc)
1314
end
1415

1516
methods (Test, TestTags="pure")
16-
function test_filename(tc, p)
17-
tc.verifyEqual(stdlib.filename(p{1}), p{2})
17+
18+
function test_filename(tc, p, method)
19+
fn = stdlib.filename(p{1}, method);
20+
tc.verifyEqual(fn, p{2})
1821
end
1922

20-
function test_filename_empty(tc, e)
21-
tc.verifyEqual(strlength(stdlib.filename(e)), 0)
23+
function test_filename_empty(tc, e, method)
24+
fn = stdlib.filename(e, method);
25+
tc.verifyEqual(strlength(fn), 0)
2226
end
2327

2428
end

0 commit comments

Comments
 (0)