Skip to content

Commit 88df116

Browse files
committed
add mex with_suffix()
with_suffix: enhance test
1 parent 63d8175 commit 88df116

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

+stdlib/with_suffix.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
%% WITH_SUFFIX switch file extension
2+
% optional: mex
23
%
34
%%% Inputs
45
% * p: path to modify
@@ -12,7 +13,7 @@
1213
suffix {mustBeTextScalar}
1314
end
1415

15-
f = "";
16+
f = '';
1617

1718
r = stdlib.parent(p);
1819
if ~strlength(r), return, end

buildfile.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ function build_exe(context)
229229
"src/proximate_to.cpp", ...
230230
"src/disk_available.cpp", ...
231231
"src/disk_capacity.cpp", ...
232+
"src/with_suffix.cpp", ...
232233
["src/is_wsl.cpp", linux], ...
233234
"src/set_permissions.cpp", ...
234235
["src/is_rosetta.cpp", mac], ...

src/with_suffix.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "mex.hpp"
2+
#include "mexAdapter.hpp"
3+
4+
#include <string>
5+
6+
#include <vector>
7+
#include <memory>
8+
#include <filesystem>
9+
10+
#include "ffilesystem.h"
11+
12+
class MexFunction : public matlab::mex::Function {
13+
private:
14+
#include "mex2string.inl"
15+
16+
public:
17+
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
18+
19+
matlab::data::ArrayFactory factory;
20+
21+
std::string path, suffix;
22+
23+
matlab_2string(inputs, &path, &suffix);
24+
25+
std::filesystem::path pth(path);
26+
27+
std::string p = pth.replace_extension(suffix).string();
28+
29+
if (p.empty()) {
30+
outputs[0] = factory.createArray<matlab::data::MATLABString>({0, 0});
31+
} else {
32+
outputs[0] = factory.createScalar(p);
33+
}
34+
}
35+
};

test/TestWithSuffix.m

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
classdef TestWithSuffix < matlab.unittest.TestCase
22

3+
properties
4+
is_mex = stdlib.is_mex_fun("stdlib.with_suffix")
5+
end
6+
37
properties (TestParameter)
4-
p = {{"foo.h5", ".nc", "foo\.nc"},...
8+
p = {{"foo.h5", ".nc", "foo.nc"},...
59
{"c", "", "c"}, ...
610
{"c.nc", "", "c"}, ...
7-
{"", ".nc", "\.nc"}, ...
8-
{"a/b/c/", ".h5", "a/b/c/\.h5"}, ...
9-
{"a/b/.h5", ".nc", "a/b/\.h5\.nc"}, ...
10-
{'a/b', '.nc', 'a/b\.nc'}};
11+
{"", ".nc", ".nc"}, ...
12+
{'a/b/c/', '.h5', 'a/b/c/.h5'}, ...
13+
{"a/b/.h5", '.nc', "a/b/.h5.nc"}, ...
14+
{'a/b', '.nc', 'a/b.nc'}};
1115
end
1216

1317
methods (Test)
1418
function test_with_suffix(tc, p)
15-
import matlab.unittest.constraints.Matches
16-
tc.verifyThat(stdlib.with_suffix(p{1}, p{2}), Matches(p{3}))
19+
20+
if tc.is_mex
21+
r = string(p{3});
22+
else
23+
r = p{3};
24+
end
25+
26+
tc.verifyEqual(stdlib.with_suffix(p{1}, p{2}), r, ...
27+
sprintf("mex: %d", tc.is_mex))
1728
end
1829
end
1930

0 commit comments

Comments
 (0)