Skip to content

Commit 9900c26

Browse files
committed
canonical: more correct/simple implementation
1 parent 07ab2eb commit 9900c26

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

+stdlib/canonical.m

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,35 @@
2222

2323

2424
if expand_tilde
25-
c = stdlib.expanduser(p, use_java);
25+
e = stdlib.expanduser(p, use_java);
2626
else
27-
c = p;
27+
e = p;
2828
end
2929

30-
if ispc && (startsWith(c, "\\") || startsWith(c, "//"))
30+
if ispc && (startsWith(e, "\\") || startsWith(e, "//"))
3131
% UNC path is not canonicalized
3232
return
3333
end
3434

35-
if stdlib.exists(c, use_java)
36-
if stdlib.isoctave()
37-
% empty if any component of path does not exist
38-
c = canonicalize_file_name(c);
39-
elseif use_java
40-
% incorrect result if any component of path does not exist
41-
c = javaFileObject(c).getCanonicalPath();
42-
else
43-
% errors if any component of path does not exist.
44-
c = builtin('_canonicalizepath', c);
35+
c = "";
36+
37+
if stdlib.isoctave()
38+
% empty if any component of path does not exist
39+
c = canonicalize_file_name(e);
40+
elseif use_java && stdlib.is_absolute(e, true)
41+
% incorrect result if relative path and any component of path does not exist
42+
% disp("java")
43+
c = javaFileObject(e).getCanonicalPath();
44+
else
45+
% errors if any component of path does not exist.
46+
% disp("builtin")
47+
try %#ok<TRYNC>
48+
c = builtin('_canonicalizepath', e);
4549
end
46-
elseif stdlib.len(c)
47-
c = stdlib.normalize(c, use_java);
50+
end
51+
52+
if ~stdlib.len(c)
53+
c = stdlib.normalize(e, use_java);
4854
end
4955

5056
c = stdlib.posix(c);

test/TestCanonical.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
properties(TestParameter)
44
use_java = num2cell(unique([stdlib.has_java(), false]))
55

6-
p = {{"", ""}, {"not-exist", "not-exist"}, {"a/../b", "b"}, ...
7-
{"~", stdlib.homedir()}, {"~/", stdlib.homedir()}, ...
6+
p = {{"", stdlib.posix(pwd())}, ...
7+
{"not-exist", "not-exist"}, ...
8+
{"a/../b", "b"}, ...
9+
{"~", stdlib.homedir()}, ...
10+
{"~/", stdlib.homedir()}, ...
811
{"~/..", stdlib.parent(stdlib.homedir())}, ...
912
{mfilename("fullpath") + ".m/..", stdlib.parent(mfilename("fullpath"))}, ...
1013
{"~/not-exist/a/..", stdlib.homedir() + "/not-exist"}

0 commit comments

Comments
 (0)