Skip to content

Commit 2694232

Browse files
committed
stem: 100x speedup
1 parent e60c6b4 commit 2694232

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

+stdlib/stem.m

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
%% STEM base file name without directory or suffix
2-
% STEM Extracts the file name without directory or suffix from a path.
32
% s = stem(p) returns the stem (base name) of the file specified by the path p.
43
% leading dot filenames are allowed.
54
%
6-
% Input:
7-
% p - Character vector or string scalar specifying the file path.
5+
%%% Inputs:
6+
% * p: Character vector or string scalar specifying the file path.
87
%
9-
% Output:
10-
% s - Character vector or string scalar containing the file name without directory or suffix.
11-
12-
function s = stem(p)
8+
%%% Output:
9+
% * s: Character vector or string scalar containing the file name without directory or suffix.
10+
%
11+
% Note: fileparts() was about 100x faster then using multiple steps with pattern.
12+
function s = stem(filepath)
1313
arguments
14-
p string
14+
filepath string
1515
end
1616

17-
p0 = asManyOfPattern(wildcardPattern + ("/" | filesep));
18-
% p1 matches a file extension (e.g., '.txt') or the end of the string
19-
p1 = ("." + alphanumericsPattern + textBoundary('end')) | textBoundary('end');
20-
21-
s = extractBetween(p, p0, p1);
17+
[~, s, e] = fileparts(filepath);
2218

2319
i = stdlib.strempty(s);
24-
s(i) = extractAfter(p(i), p0);
20+
s(i) = e(i);
2521

2622
end

0 commit comments

Comments
 (0)