Skip to content

Commit 5058ece

Browse files
committed
suffix: simplicity and reliability
1 parent 3021255 commit 5058ece

File tree

4 files changed

+10
-13
lines changed

4 files changed

+10
-13
lines changed

+stdlib/suffix.m

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
p string
66
end
77

8-
pat = textBoundary("start") + asManyOfPattern(wildcardPattern + ".", 1);
8+
[~, n, s] = fileparts(p);
99

10-
s = extractAfter(p, pat);
11-
12-
i = ~ismissing(s);
13-
s(~i) = "";
14-
15-
s(i) = strcat('.', s(i));
10+
if strempty(n)
11+
s = n;
12+
end
1613

1714
end

+stdlib/with_suffix.m

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
suffix {mustBeTextScalar}
1313
end
1414

15-
i = ~lookBehindBoundary("/" | filesep);
16-
17-
pat = (i + "." + alphanumericsPattern + textBoundary('end')) | textBoundary('end');
18-
19-
f = extractBefore(p, pat);
15+
f = extractBefore(p, stdlib.suffix(p));
2016

2117
i = strempty(f);
2218
f(i) = p(i);

test/TestSuffix.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
properties (TestParameter)
44
p = {{"", ""}, {"/a/b/c", ""}, ...
55
{"/a/b/c/", ""}, {"a/b/c.txt", ".txt"}, ...
6+
{"a/a.b/hi.txt", ".txt"}, ...
7+
{"a/a.b/matlab", ""}, ...
68
{"a/b/c.txt.gz", ".gz"}, ...
7-
{'.stat', ".stat"}, ...
9+
{'.stat', ""}, ...
10+
{'a/.stat', ""}, ...
811
{'.stat.txt', ".txt"}}
912
end
1013

test/TestWithSuffix.m

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

0 commit comments

Comments
 (0)