Skip to content

Commit 685fe9b

Browse files
committed
which: simplify logic, array path
1 parent 3eb91d6 commit 685fe9b

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

+stdlib/+fileio/which.m

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,31 @@
44

55
arguments
66
name (1,1) string {mustBeNonzeroLengthText}
7-
fpath string = getenv('PATH')
7+
fpath (1,:) string = getenv('PATH')
88
subdir (1,:) string {mustBeNonempty} = ""
99
end
1010

1111
import stdlib.fileio.is_exe
12-
13-
exe = string.empty;
12+
import stdlib.fileio.expanduser
1413

1514
if ispc
1615
pathext = ".exe";
1716
if ~endsWith(name, pathext)
18-
name = [name + pathext, name];
17+
name = name + pathext;
1918
end
2019
end
2120

22-
if any(strlength(fileparts(name)) > 0)
21+
if strlength(fileparts(name)) > 0
2322
% has directory part
24-
for n = name
25-
if is_exe(n)
26-
exe = n;
27-
break
28-
end
23+
if is_exe(name)
24+
exe = name;
25+
return
2926
end
30-
return
3127
end
3228

33-
fpath = split(stdlib.fileio.expanduser(fpath), pathsep).';
29+
if isscalar(fpath)
30+
fpath = split(expanduser(fpath), pathsep).';
31+
end
3432
fpath = fpath(strlength(fpath)>0);
3533

3634
if ispc
@@ -40,15 +38,13 @@
4038

4139
for p = fpath
4240
for s = subdir
43-
for n = name
44-
e = fullfile(p, s, n);
45-
if is_exe(e)
46-
exe = e;
47-
return
48-
end
41+
exe = fullfile(p, s, name);
42+
if is_exe(exe)
43+
return
4944
end
5045
end
51-
5246
end
5347

48+
exe = string.empty;
49+
5450
end

0 commit comments

Comments
 (0)