Skip to content

Commit 5366ac7

Browse files
committed
canonical: more correct/simple implementation
1 parent 07ab2eb commit 5366ac7

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

+stdlib/canonical.m

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,37 @@
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+
c = "";
31+
32+
if ~stdlib.len(e), return, end
33+
34+
if ispc && (startsWith(e, "\\") || startsWith(e, "//"))
3135
% UNC path is not canonicalized
3236
return
3337
end
3438

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);
39+
if stdlib.isoctave()
40+
% empty if any component of path does not exist
41+
c = canonicalize_file_name(e);
42+
elseif use_java && stdlib.is_absolute(e, true)
43+
% incorrect result if relative path and any component of path does not exist
44+
% disp("java")
45+
c = javaFileObject(e).getCanonicalPath();
46+
else
47+
% errors if any component of path does not exist.
48+
% disp("builtin")
49+
try %#ok<TRYNC>
50+
c = builtin('_canonicalizepath', e);
4551
end
46-
elseif stdlib.len(c)
47-
c = stdlib.normalize(c, use_java);
52+
end
53+
54+
if ~stdlib.len(c)
55+
c = stdlib.normalize(e, use_java);
4856
end
4957

5058
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 = {{"", ""}, ...
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)