Skip to content

Commit 9fc8b8d

Browse files
committed
which: simplify types
1 parent 24bc10c commit 9fc8b8d

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

+stdlib/which.m

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,20 @@
99

1010
function exe = which(cmd, fpath, find_all)
1111
arguments
12-
cmd {mustBeTextScalar}
12+
cmd (1,1) string
1313
fpath (1,:) string = string.empty
1414
find_all (1,1) logical = false
1515
end
1616

17-
if find_all
18-
exe = string.empty;
19-
else
20-
exe = '';
21-
end
17+
exe = string.empty;
2218

2319
%% on Windows, append .exe if not suffix is given
2420
if ispc() && strempty(stdlib.suffix(cmd))
25-
cmd = strcat(cmd, '.exe');
21+
cmd = cmd + ".exe";
2622
end
2723
%% full filename was given
28-
if isfile(cmd) && stdlib.is_exe(cmd)
24+
if stdlib.is_exe(cmd)
25+
% is_exe implies isfile
2926
exe = cmd;
3027
return
3128
end
@@ -37,20 +34,14 @@
3734

3835
% path given
3936
if isempty(fpath)
40-
fpath = getenv("PATH");
37+
fpath = string(getenv("PATH"));
4138
end
4239

43-
if isscalar(fpath) || ischar(fpath)
44-
fpath = strsplit(fpath, pathsep);
40+
if isscalar(fpath)
41+
fpath = split(fpath, pathsep).';
4542
end
4643

47-
for fp = fpath
48-
if iscell(fp)
49-
p = fp{1};
50-
else
51-
p = fp;
52-
end
53-
44+
for p = fpath
5445
if strempty(p), continue, end
5546

5647
e = fullfile(p, cmd);
@@ -65,5 +56,3 @@
6556
end
6657

6758
end
68-
69-
%!assert(~isempty(which("octave", [], false)))

test/TestWhich.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
properties (TestParameter)
44
mexe = {matlabroot + "/bin/matlab", ...
5-
fullfile(matlabroot, 'bin', 'matlab')}
5+
fullfile(matlabroot, 'bin/matlab')}
66
end
77

88
methods (Test, TestTags="impure")
@@ -33,11 +33,13 @@ function test_which_name(tc)
3333

3434
function test_which_absolute(tc, mexe)
3535

36-
r = mexe;
36+
r = string(mexe);
3737
if ispc()
38-
r = strcat(r, '.exe');
38+
r = r + ".exe";
3939
end
4040

41+
tc.assumeThat(r, matlab.unittest.constraints.IsFile)
42+
4143
tc.verifyEqual(stdlib.which(mexe), r)
4244

4345
end

0 commit comments

Comments
 (0)