Skip to content

Commit 23415f3

Browse files
committed
canonical: functionalize, test
1 parent 2cf504c commit 23415f3

File tree

5 files changed

+44
-32
lines changed

5 files changed

+44
-32
lines changed

+stdlib/+native/canonical.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function c = canonical(file, strict)
2+
3+
if stdlib.strempty(file)
4+
c = "";
5+
return
6+
end
7+
8+
p = matlab.io.internal.filesystem.resolvePath(file);
9+
c = p.ResolvedPath;
10+
11+
if ~strict && stdlib.strempty(c)
12+
c = string(stdlib.normalize(file));
13+
end
14+
15+
end

+stdlib/+native/canonical_legacy.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function c = canonical_legacy(file, strict)
2+
3+
c = "";
4+
5+
if stdlib.strempty(file), return, end
6+
7+
[s, r] = fileattrib(file);
8+
9+
if s == 1
10+
c = r.Name;
11+
elseif ~strict
12+
c = stdlib.normalize(file);
13+
end
14+
15+
c = string(c);
16+
17+
end

+stdlib/canonical.m

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,14 @@
1515
function c = canonical(p, strict)
1616
arguments
1717
p {mustBeTextScalar}
18-
strict logical = false
18+
strict (1,1) logical = false
1919
end
2020

21-
if stdlib.strempty(p)
22-
c = "";
23-
return
24-
end
2521

2622
if isMATLABReleaseOlderThan('R2024a')
27-
c = acanon(p, strict);
23+
c = stdlib.native.canonical_legacy(p, strict);
2824
else
29-
pth = matlab.io.internal.filesystem.resolvePath(p);
30-
c = pth.ResolvedPath;
31-
if ~strict && stdlib.strempty(c)
32-
c = stdlib.normalize(p);
33-
end
34-
end
35-
36-
c = string(c);
37-
38-
end
39-
40-
41-
function c = acanon(p, strict)
42-
43-
c = "";
44-
45-
if stdlib.strempty(p), return, end
46-
47-
[s, r] = fileattrib(p);
48-
49-
if s == 1
50-
c = r.Name;
51-
elseif ~strict
52-
c = stdlib.normalize(p);
25+
c = stdlib.native.canonical(p, strict);
5326
end
5427

5528
end

test/TestCanonical.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
{"not-exist/a/..", "not-exist"}, ...
1010
{"./not-exist", "not-exist"}
1111
};
12+
fun = {@stdlib.canonical, @stdlib.native.canonical, @stdlib.native.canonical_legacy}
1213
end
1314

1415
methods(TestClassSetup)
@@ -21,8 +22,10 @@ function pkg_path(tc)
2122

2223
methods(Test, TestTags="impure")
2324

24-
function test_canonical(tc, p)
25-
tc.verifyEqual(stdlib.canonical(p{1}), p{2})
25+
function test_canonical(tc, p, fun)
26+
is_capable(tc, fun)
27+
28+
tc.verifyEqual(fun(p{1}, false), p{2})
2629
end
2730

2831
end

test/is_capable.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ function is_capable(tc, f)
6161
tc.assumeFalse(isMATLABReleaseOlderThan('R2025a'))
6262
end
6363

64+
if endsWith(n, "canonical")
65+
tc.assumeFalse(isMATLABReleaseOlderThan('R2024a'))
66+
end
67+
6468
end
6569

6670
end

0 commit comments

Comments
 (0)