Skip to content

Commit c6cd0cc

Browse files
committed
canonical: use matlab.io to simplify
1 parent efbc9f5 commit c6cd0cc

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

+stdlib/canonical.m

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,40 @@
1717
end
1818

1919
if strempty(p)
20-
c = '';
21-
else
22-
[s, r] = fileattrib(p);
20+
c = "";
21+
return
22+
end
2323

24-
if s == 1
25-
c = stdlib.posix(r.Name);
26-
else
24+
if stdlib.isoctave(p) || isMATLABReleaseOlderThan('R2024a')
25+
c = acanon(p);
26+
else
27+
pth = matlab.io.internal.filesystem.resolvePath(p);
28+
c = pth.ResolvedPath;
29+
if strempty(c)
2730
c = stdlib.normalize(p);
2831
end
2932
end
3033

31-
if isstring(p)
34+
try %#ok<*TRYNC>
3235
c = string(c);
3336
end
3437

3538
end
3639

40+
41+
function c = acanon(p)
42+
43+
if strempty(p), c = ''; return, end
44+
45+
[s, r] = fileattrib(p);
46+
47+
if s == 1
48+
c = r.Name;
49+
else
50+
c = stdlib.normalize(p);
51+
end
52+
53+
end
54+
3755
%!assert(canonical(""), "")
3856
%!assert(canonical("."), pwd())

test/TestCanonical.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
classdef TestCanonical < matlab.unittest.TestCase
22

33
properties(TestParameter)
4-
p = {{'', ''}, ...
4+
p = {{'', ""}, ...
55
{"", ""}, ...
66
{"not-exist", "not-exist"}, ...
77
{"a/../b", "b"}, ...
8-
{strcat(mfilename("fullpath"), '.m/..'), stdlib.parent(mfilename("fullpath"))}, ...
8+
{strcat(mfilename("fullpath"), '.m/..'), string(fullfile(stdlib.parent(mfilename("fullpath"))))}, ...
99
{"not-exist/a/..", "not-exist"}, ...
1010
{"./not-exist", "not-exist"}, ...
1111
{"../not-exist", "../not-exist"}

0 commit comments

Comments
 (0)