|
7 | 7 | % * path1, path2: paths to compare
|
8 | 8 | %%% Outputs
|
9 | 9 | % 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) |
11 | 10 |
|
12 |
| -function issame = samepath(path1, path2, use_java) |
| 11 | + |
| 12 | +function issame = samepath(path1, path2) |
13 | 13 | arguments
|
14 | 14 | path1 (1,1) string
|
15 | 15 | 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 |
22 | 16 | end
|
23 | 17 |
|
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) |
32 | 20 |
|
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); |
35 | 23 |
|
36 |
| -else |
37 |
| - issame = stdlib.canonical(path1, false, false) == stdlib.canonical(path2, false, false); |
38 | 24 | end
|
39 | 25 |
|
40 |
| -end |
| 26 | +%!assert(samepath(".", ".")) |
| 27 | +%!assert(samepath(".", "./")) |
| 28 | +%!assert(samepath(".", "a/..")) |
0 commit comments