File tree Expand file tree Collapse file tree 3 files changed +34
-10
lines changed Expand file tree Collapse file tree 3 files changed +34
-10
lines changed Original file line number Diff line number Diff line change 66function ok = is_exe(p , use_java )
77arguments
88 p (1 ,1 ) string
9- use_java (1 ,1 ) logical = false
9+ use_java (1 ,1 ) logical = true
1010end
1111
1212if ~isfile(p )
1313 ok = false ;
1414 return
1515end
1616
17- if stdlib .isoctave()
18- ok = javaObject(" java.io.File" , p ).canExecute();
19- elseif use_java
17+ if use_java
2018% about the same time as fileattrib
21- % doesn't need absolute path like other Java functions
2219% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/io/File.html#canExecute()
23- ok = java .io .File(p ).canExecute();
24-
2520% more complicated
2621% ok = java.nio.file.Files.isExecutable(java.io.File(stdlib.canonical(p)).toPath());
2722
23+ try
24+ ok = java .io .File(p ).canExecute();
25+ catch e
26+ if strcmp(e .identifier , " Octave:undefined-function" )
27+ ok = javaObject(" java.io.File" , p ).canExecute();
28+ else
29+ rethrow(e );
30+ end
31+ end
32+
2833else
2934
3035 if ~strlength(p )
Original file line number Diff line number Diff line change 1+ %% benchmark
2+
3+ f = mfilename(" fullpath" ) + " .m" ;
4+ addpath(fullfile(fileparts(f ), " .." ))
5+
6+ % f = tempname;
7+
8+ fno = @() stdlib .is_exe(f , false );
9+ fjava = @() stdlib .is_exe(f , true );
10+
11+ t_no = timeit(fno );
12+ t_java = timeit(fjava );
13+
14+ disp(" No Java: " + t_no + " s" )
15+ disp(" Java: " + t_java + " s" )
16+
17+ disp(" Java is " + t_no / t_java + " times faster" )
Original file line number Diff line number Diff line change @@ -16,6 +16,8 @@ function test_which_name(tc)
1616
1717tc .verifyEmpty(stdlib .which(tempname ))
1818
19+ u = stdlib .has_java();
20+
1921if ispc
2022 n = " pwsh.exe" ;
2123else
@@ -35,8 +37,8 @@ function test_is_exe_which_fullpath(tc)
3537import matlab .unittest .constraints .IsFile
3638import matlab .unittest .constraints .EndsWithSubstring
3739
38- tc .verifyFalse(stdlib .is_exe(" " ))
39- tc .verifyFalse(stdlib .is_exe(tempname ))
40+ tc .verifyFalse(stdlib .is_exe(" " , u ))
41+ tc .verifyFalse(stdlib .is_exe(tempname , u ))
4042
4143n = " matlab" ;
4244%% is_exe test
@@ -46,7 +48,7 @@ function test_is_exe_which_fullpath(tc)
4648else
4749 fp = p ;
4850end
49- tc .verifyTrue(stdlib .is_exe(fp ))
51+ tc .verifyTrue(stdlib .is_exe(fp , u ))
5052%% which: test absolute path
5153exe = stdlib .which(p );
5254
You can’t perform that action at this time.
0 commit comments