Skip to content

Commit 82f1544

Browse files
committed
stem: vectorize
1 parent 3d53f2e commit 82f1544

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

+stdlib/stem.m

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
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)
12+
function s = stem(p)
413
arguments
5-
p {mustBeTextScalar}
14+
p string
615
end
716

8-
[~, n, s] = fileparts(p);
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');
920

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

17-
end
23+
i = strempty(s);
24+
s(i) = extractAfter(p(i), p0);
1825

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

test/TestStem.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
{"a/b/c/", ""}, ...
99
{"a/b/c.txt", "c"}, ...
1010
{"a/b/c.txt.gz", "c.txt"}, ...
11-
{'a/b/.c', '.c'}, ...
12-
{"", ""}
11+
{"a/b/.c", ".c"}, ...
12+
{'a/b/.c', ".c"}, ...
13+
{"", ""}, ...
14+
{'', ""}
1315
}
1416

1517
end

0 commit comments

Comments
 (0)