Skip to content

Commit b107d3e

Browse files
committed
canonical: simplify with only one method"
1 parent 07c7b77 commit b107d3e

File tree

7 files changed

+39
-92
lines changed

7 files changed

+39
-92
lines changed

+stdlib/+legacy/canonical.m

Lines changed: 0 additions & 23 deletions
This file was deleted.

+stdlib/+native/canonical.m

Lines changed: 0 additions & 20 deletions
This file was deleted.

+stdlib/canonical.m

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@
77
% This also resolves Windows short paths to long paths.
88
%
99
%%% Inputs
10-
% * p: path to make canonical
10+
% * file: path to make canonical
1111
% * strict: if true, only return canonical path if it exists. If false, return normalized path if path does not exist.
1212
%%% Outputs
1313
% * c: canonical path, if determined
14-
% * b: backend used
1514

16-
function [c, b] = canonical(p, strict)
17-
arguments
18-
p string
19-
strict (1,1) logical = false
15+
function c = canonical(file, strict)
16+
if nargin < 2
17+
strict = false;
2018
end
2119

22-
try
23-
c = stdlib.native.canonical(p, strict);
24-
b = 'native';
25-
catch e
26-
switch e.identifier
27-
case {'MATLAB:UndefinedFunction', 'MATLAB:undefinedVarOrClass'}
28-
c = stdlib.legacy.canonical(p, strict);
29-
b = 'legacy';
30-
otherwise
31-
rethrow(e)
32-
end
20+
if stdlib.strempty(file)
21+
c = extractBefore(file, 1);
22+
return
23+
end
24+
25+
[s, r] = fileattrib(file);
26+
27+
if s == 1
28+
c = r.Name;
29+
elseif ~strict
30+
c = stdlib.normalize(file);
31+
else
32+
c = extractBefore(file, 1);
3333
end
3434

3535
end

+stdlib/is_absolute.m

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
%% IS_ABSOLUTE is path absolute
22
%
33
% * Windows, absolute paths must be at least 3 character long, starting with a root name followed by a slash
4-
%
54
% * non-Windows, absolute paths must start with a slash
65

76
function y = is_absolute(p)
8-
arguments
9-
p (1,1) string
10-
end
117

128
y = ~stdlib.strempty(stdlib.root_dir(p));
139

example/+native/canonical.m

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

test/TestCanonical.m

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

55
properties (TestParameter)
6-
p = {{'', ""}, ...
6+
p = {{'', ''}, ...
77
{"", ""}, ...
88
{"not-exist", "not-exist"}, ...
99
{"a/../b", "a/../b"}, ...
10-
{append(mfilename("fullpath"), '.m/..'), string(fileparts(mfilename("fullpath")))}, ...
10+
{append(mfilename("fullpath"), '.m/..'), fileparts(mfilename("fullpath"))}, ...
1111
{"not-exist/a/..", "not-exist/a/.."}, ...
1212
{"./not-exist", "not-exist"}
1313
};
@@ -22,33 +22,10 @@ function test_dirs(tc)
2222

2323
methods (Test, TestTags={'R2019b'})
2424
function test_canonical(tc, p)
25-
[c, b] = stdlib.canonical(p{1}, false);
26-
tc.verifyEqual(c, p{2})
27-
28-
if stdlib.matlabOlderThan('R2024a')
29-
tc.assertEqual(b, 'legacy')
30-
else
31-
tc.assertEqual(b, 'native')
32-
end
33-
end
34-
35-
function test_legacy_canonical(tc, p)
36-
c = stdlib.legacy.canonical(p{1}, false);
25+
c = stdlib.canonical(p{1}, false);
3726
tc.verifyEqual(c, p{2})
3827
end
39-
end
40-
41-
42-
methods (Test, TestTags={'R2024a'})
43-
function test_canonical_array(tc)
44-
tc.assumeFalse(stdlib.matlabOlderThan('R2024a'))
4528

46-
in = ["", "hi", "/ok"];
47-
48-
c = stdlib.canonical(in, false);
49-
exp = ["", "hi", "/ok"];
50-
tc.verifyEqual(c, exp)
51-
end
5229
end
5330

5431
end

test/TestResolve.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ function test_resolve_fullpath(tc, p)
4848

4949
a = p;
5050
switch a
51-
case {'', "", '.', "."}, b = string(pwd());
52-
case {'..', ".."}, b = string(fileparts(pwd()));
51+
case {'', "", '.', "."}, b = pwd();
52+
case {'..', ".."}, b = fileparts(pwd());
5353
end
5454

5555
tc.verifyEqual(stdlib.resolve(a, false), b)

0 commit comments

Comments
 (0)