Skip to content

Commit b77c412

Browse files
committed
with_suffix: vectorize
1 parent e718c98 commit b77c412

File tree

3 files changed

+13
-25
lines changed

3 files changed

+13
-25
lines changed

+stdlib/with_suffix.m

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,19 @@
88

99
function f = with_suffix(p, suffix)
1010
arguments
11-
p {mustBeTextScalar}
11+
p string
1212
suffix {mustBeTextScalar}
1313
end
1414

15-
f = '';
15+
i = ~lookBehindBoundary("/" | filesep);
1616

17-
r = stdlib.parent(p);
18-
if strempty(r), return, end
17+
pat = (i + "." + alphanumericsPattern + textBoundary('end')) | textBoundary('end');
1918

20-
s = stdlib.stem(p);
19+
f = extractBefore(p, pat);
2120

22-
if strempty(s)
23-
f = stdlib.join(p, suffix);
24-
return
25-
end
26-
27-
if strcmp(r, '.')
28-
f = s;
29-
else
30-
f = fullfile(r, s);
31-
end
21+
i = strempty(f);
22+
f(i) = p(i);
3223

33-
f = strcat(f, suffix);
24+
f = f + suffix;
3425

3526
end
36-
37-
%!assert(with_suffix("ab.h5", ".nc"), "ab.nc")
38-
%!assert(with_suffix("ab", ".nc"), "ab.nc")
39-
%!assert(with_suffix("ab.h5", ""), "ab")
40-
%!assert(with_suffix("ab", ""), "ab")
41-
%!assert(with_suffix("ab/.h5", ".nc"), fullfile("ab", ".h5.nc"))

test/TestStem.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
{"a/b/c.txt.gz", "c.txt"}, ...
1111
{"a/b/.c", ".c"}, ...
1212
{'a/b/.c', ".c"}, ...
13+
{".config", ".config"}, ...
1314
{"", ""}, ...
1415
{'', ""}
1516
}

test/TestWithSuffix.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
{"c", "", "c"}, ...
1010
{"c.nc", "", "c"}, ...
1111
{"", ".nc", ".nc"}, ...
12-
{'a/b/', '.h5', fullfile('a', 'b', '.h5')}, ...
13-
{"a/b/.h5", '.nc', fullfile("a", "b", ".h5.nc")}, ...
14-
{'a/b', '.nc', fullfile('a', 'b.nc')}};
12+
{"hello.txt.gz", ".bz", "hello.txt.bz"}, ...
13+
{'a/b/', '.h5', "a/b/.h5"}, ...
14+
{"a/b/.h5", '.nc', "a/b/.h5.nc"}, ...
15+
{".h5", ".nc", ".h5.nc"}, ...
16+
{'a/b', '.nc', "a/b.nc"}};
1517
end
1618

1719
methods (Test, TestTags="true")

0 commit comments

Comments
 (0)