Skip to content

Commit 33b15fb

Browse files
committed
samepath: simplify
1 parent 2ae31b1 commit 33b15fb

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

+stdlib/samepath.m

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,22 @@
77
% * path1, path2: paths to compare
88
%%% Outputs
99
% issame: logical
10-
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isSameFile(java.nio.file.Path,java.nio.file.Path)
1110

12-
function issame = samepath(path1, path2, use_java)
11+
12+
function issame = samepath(path1, path2)
1313
arguments
1414
path1 (1,1) string
1515
path2 (1,1) string
16-
use_java (1,1) logical = false
17-
end
18-
19-
issame = false;
20-
if ~stdlib.exists(path1, use_java) || ~stdlib.exists(path2, use_java)
21-
return
2216
end
2317

24-
if use_java
25-
% needs absolute
26-
path1 = stdlib.absolute(path1, "", false, true);
27-
path2 = stdlib.absolute(path2, "", false, true);
28-
29-
issame = java.nio.file.Files.isSameFile(...
30-
java.io.File(path1).toPath(), ...
31-
java.io.File(path2).toPath());
18+
% simpler our way than
19+
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isSameFile(java.nio.file.Path,java.nio.file.Path)
3220

33-
% alternative, lower-level method is lexical only (not suitable for us):
34-
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#equals(java.lang.Object)
21+
issame = stdlib.exists(path1, false) && stdlib.exists(path2, false) && ...
22+
stdlib.canonical(path1, false, false) == stdlib.canonical(path2, false, false);
3523

36-
else
37-
issame = stdlib.canonical(path1, false, false) == stdlib.canonical(path2, false, false);
3824
end
3925

40-
end
26+
%!assert(samepath(".", "."))
27+
%!assert(samepath(".", "./"))
28+
%!assert(samepath(".", "a/.."))

0 commit comments

Comments
 (0)