Skip to content

Commit 2ae31b1

Browse files
committed
canonical: simplify
1 parent 2a6f539 commit 2ae31b1

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

+stdlib/canonical.m

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,38 @@
2626
c = p;
2727
end
2828

29+
if ~stdlib.len(c)
30+
return
31+
end
32+
2933
if ispc && startsWith(c, "\\")
3034
% UNC path is not canonicalized
3135
return
3236
end
3337

34-
e = stdlib.exists(c, use_java);
35-
36-
if ~stdlib.is_absolute(c)
37-
if e
38-
if ~expand_tilde && ~use_java && startsWith(c, "~")
39-
c = stdlib.normalize(c, use_java);
40-
return
41-
else
42-
% workaround Java/Matlab limitations
43-
c = stdlib.posix(pwd()) + "/" + c;
44-
end
38+
if stdlib.exists(c, use_java)
39+
if stdlib.isoctave()
40+
c = canonicalize_file_name(c);
4541
else
46-
% for non-existing path, return normalized relative path
47-
% like C++ filesystem weakly_canonical()
48-
c = stdlib.normalize(c, use_java);
49-
return
42+
% errors if path does not exist. Errors on leading ~
43+
c = builtin('_canonicalizepath', c);
5044
end
45+
46+
c = stdlib.posix(c);
47+
return
5148
end
5249

50+
% like C++ filesystem weakly_canonical()
51+
5352
if use_java
5453
c = java.io.File(c).getCanonicalPath();
55-
elseif e
56-
% errors if path does not exist. Errors on leading ~
57-
c = builtin('_canonicalizepath', c);
5854
else
5955
c = stdlib.normalize(c, use_java);
6056
end
6157

6258
c = stdlib.posix(c);
6359

64-
end % function
60+
end
61+
62+
%!assert(canonical("", 0,0), "")
63+
%!assert(canonical("~",1,0), homedir(0))

+stdlib/is_absolute.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/io/File.html#isAbsolute()
1515
isabs = java.io.File(p).toPath().isAbsolute();
1616
elseif ischar(p)
17+
% not is_absolute_filename() because this is a stricter check for "c:" false
1718
L = length(p);
1819
if ispc
1920
isabs = L > 2 && ~isempty(stdlib.root_name(p)) && (p(3) == '\' || p(3) == '/');

+stdlib/normalize.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
n = stdlib.posix(p);
2222

2323
% use split to remove /../ and /./ and duplicated /
24-
parts = split(n, "/");
24+
parts = split(n, '/');
2525
i0 = 1;
2626
if startsWith(n, "/")
2727
n = "/";

test/TestResolve.m

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@ function test_canonical(tc)
4747
import matlab.unittest.fixtures.TemporaryFolderFixture
4848
import matlab.unittest.fixtures.CurrentFolderFixture
4949
import matlab.unittest.constraints.StartsWithSubstring
50-
import matlab.unittest.constraints.EndsWithSubstring
5150

5251
td = tc.applyFixture(TemporaryFolderFixture).Folder;
5352
tc.applyFixture(CurrentFolderFixture(td))
5453

5554
% all non-existing files
5655

57-
tc.verifyEqual(stdlib.canonical(""), ".")
56+
tc.verifyEqual(stdlib.canonical(""), "")
5857

5958
pabs = stdlib.canonical('2foo');
6059
tc.verifyThat(pabs, StartsWithSubstring("2foo"))
@@ -69,10 +68,8 @@ function test_canonical(tc)
6968
r = stdlib.parent(mfilename('fullpath'));
7069
tc.verifyEqual(stdlib.canonical(fullfile(r, "..")), stdlib.parent(r))
7170

72-
% on windows, ~ is expanded even without expanduser
73-
if ~ispc
74-
tc.verifyThat(stdlib.canonical("~", false), EndsWithSubstring("~"))
75-
end
71+
% ~ is expanded even without expanduser when path exists
72+
tc.verifyEqual(stdlib.canonical("~/nobody/a/..", false), "~/nobody")
7673

7774
h = stdlib.homedir;
7875
tc.verifyEqual(stdlib.canonical("~"), h)

0 commit comments

Comments
 (0)