Skip to content

Commit c6c2e54

Browse files
committed
canonical: empty returns pwd(). Use R2026b resolveFilePath() when avail
1 parent c0821d1 commit c6c2e54

4 files changed

Lines changed: 39 additions & 31 deletions

File tree

+stdlib/+legacy/canonical.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function c = canonical(file)
2+
3+
[s, r] = fileattrib(file);
4+
if s == 1
5+
c = r.Name;
6+
else
7+
c = missing;
8+
end
9+
10+
end

+stdlib/canonical.m

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,27 @@
1818
strict (1,1) logical = false
1919
end
2020

21-
c = '';
22-
23-
try
21+
if stdlib.strempty(file)
22+
c = pwd();
23+
elseif ~stdlib.exists(file)
24+
if strict
25+
c = missing;
26+
return
27+
end
28+
c = stdlib.normalize(file);
29+
elseif stdlib.matlabOlderThan('R2025a')
30+
c = stdlib.legacy.canonical(file);
31+
elseif stdlib.matlabOlderThan('R2026b')
2432
p = filePermissions(file);
2533
c = p.AbsolutePath;
26-
if ischar(file)
27-
c = char(c);
28-
end
29-
catch e
30-
if ~stdlib.strempty(file)
31-
switch e.identifier
32-
case 'MATLAB:io:filesystem:filePermissions:CannotFindLocation'
33-
if ~strict
34-
c = stdlib.normalize(file);
35-
end
36-
case {'MATLAB:UndefinedFunction', 'Octave:undefined-function'}
37-
[s, r] = fileattrib(file);
38-
if s == 1
39-
c = r.Name;
40-
elseif ~strict
41-
c = stdlib.normalize(file);
42-
end
43-
otherwise
44-
rethrow(e)
45-
end
46-
end
34+
else
35+
c = resolveFilePath(file);
36+
end
4737

48-
if isstring(file)
49-
c = string(c);
50-
end
38+
if isstring(file)
39+
c = string(c);
40+
elseif ischar(file)
41+
c = char(c);
5142
end
5243

5344
end

example/+javafun/canonical.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getCanonicalPath()
33

44
% incorrect result if relative path and any component of path does not exist
5-
% disp("java")
5+
66
c = javaFileObject(p).getCanonicalPath();
77

88
end

test/TestCanonical.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
TestCanonical < matlab.unittest.TestCase
44

55
properties (TestParameter)
6-
p = {{'', ''}, ...
7-
{'', ''}, ...
6+
p = {
87
{'not-exist', 'not-exist'}, ...
98
{'a/../b', 'a/../b'}, ...
109
{'not-exist/a/..', 'not-exist/a/..'}, ...
@@ -27,9 +26,17 @@ function test_canonical(tc, p)
2726
tc.verifyEqual(stdlib.canonical(string(p{1}), false), string(p{2}))
2827
end
2928

30-
function test_canonical_cwd(tc)
29+
function test_canonical_empty(tc)
30+
tc.verifyEqual(stdlib.canonical('', false), pwd())
31+
tc.verifyEqual(stdlib.canonical('', true), pwd())
32+
tc.verifyEqual(stdlib.canonical(""), string(pwd()))
33+
end
34+
35+
function test_canonical_strict(tc)
3136
c = stdlib.canonical('.', true);
3237
tc.verifyEqual(c, pwd())
38+
39+
tc.verifyEqual(stdlib.canonical('not-exist', true), missing)
3340
end
3441

3542
end

0 commit comments

Comments
 (0)