Skip to content

Commit 11521c3

Browse files
committed
java.samepath: nearly twice as fast by using Java internally
1 parent c9c1589 commit 11521c3

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

+stdlib/+java/samepath.m

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
%% JAVA.SAMEPATH are inputs the same path
22

33
function y = samepath(path1, path2)
4-
arguments
5-
path1 (1,1) string
6-
path2 (1,1) string
7-
end
84

9-
if stdlib.exists(path1) && stdlib.exists(path2)
10-
j1 = javaPathObject(stdlib.absolute(path1));
11-
j2 = javaPathObject(stdlib.absolute(path2));
5+
f1 = java.io.File(path1);
6+
f2 = java.io.File(path2);
127

13-
y = java.nio.file.Files.isSameFile(j1, j2);
8+
if f1.exists() && f2.exists()
9+
p1 = file2absPath(f1);
10+
p2 = file2absPath(f2);
11+
y = java.nio.file.Files.isSameFile(p1, p2);
1412
else
1513
y = false;
1614
end
1715

1816
end
17+
18+
19+
function jpath = file2absPath(jfile)
20+
% java.lang.System.getProperty('user.path') is stuck to where Java started
21+
22+
jpath = jfile.toPath();
23+
24+
if ~jfile.isAbsolute()
25+
jpath = javaPathObject(pwd()).resolve(jpath);
26+
end
27+
28+
end

0 commit comments

Comments
 (0)