Skip to content

Commit d99ba80

Browse files
committed
suffix: simplicity and reliability
1 parent abb70f3 commit d99ba80

File tree

5 files changed

+12
-15
lines changed

5 files changed

+12
-15
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/TestWhich.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ function test_which_absolute(tc, mexe)
4141
tc.assumeThat(r, matlab.unittest.constraints.IsFile)
4242
tc.assumeTrue(stdlib.is_exe(r))
4343

44-
tc.verifyNotEmpty(stdlib.which(r), r)
45-
tc.verifyNotEmpty(stdlib.which(mexe), r)
44+
tc.verifyGreaterThan(strlength(stdlib.which(r)), 0, "Expected which(%s) to find %s", r, r)
45+
tc.verifyGreaterThan(strlength(stdlib.which(mexe)), 0, "Expected which(%s) to find %s", mexe, r)
4646

4747
end
4848

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)