Skip to content

Commit 289b06c

Browse files
committed
stem: vectorize
1 parent 3d53f2e commit 289b06c

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

+stdlib/stem.m

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
%% STEM file name without directory or suffix
1+
%% STEM base file name without directory or suffix
2+
% STEM Extracts the file name without directory or suffix from a path.
3+
% s = stem(p) returns the stem (base name) of the file specified by the path p.
4+
% leading dot filenames are allowed.
5+
%
6+
% Input:
7+
% p - Character vector or string scalar specifying the file path.
8+
%
9+
% Output:
10+
% s - Character vector or string scalar containing the file name without directory or suffix.
211

3-
function st = stem(p)
4-
arguments
5-
p {mustBeTextScalar}
6-
end
12+
function s = stem(p)
713

8-
[~, n, s] = fileparts(p);
14+
p0 = asManyOfPattern(wildcardPattern + ("/" | filesep));
15+
% p1 matches a file extension (e.g., '.txt') or the end of the string
16+
p1 = ("." + alphanumericsPattern + textBoundary('end')) | textBoundary('end');
917

10-
if strempty(n)
11-
% leading dot filename
12-
st = s;
13-
else
14-
st = n;
15-
end
18+
s = extractBetween(p, p0, p1);
1619

17-
end
20+
i = strempty(s);
21+
s(i) = extractAfter(p(i), p0);
1822

19-
%!assert(stem('/a/b.c'), 'b')
20-
%!assert(stem("a/b/.c"), ".c")
23+
end

test/TestStem.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
{"a/b/c/", ""}, ...
99
{"a/b/c.txt", "c"}, ...
1010
{"a/b/c.txt.gz", "c.txt"}, ...
11+
{"a/b/.c", ".c"}, ...
1112
{'a/b/.c', '.c'}, ...
1213
{"", ""}
1314
}

0 commit comments

Comments
 (0)