Skip to content

Commit 4e37550

Browse files
committed
canonical, resolve: any size array
1 parent d0fcdcf commit 4e37550

File tree

13 files changed

+94
-23
lines changed

13 files changed

+94
-23
lines changed

+stdlib/+native/canonical.m

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
function c = canonical(file, strict)
2+
arguments
3+
file string
4+
strict (1,1) logical = false
5+
end
6+
7+
rp = matlab.io.internal.filesystem.resolvePath(file);
8+
c = string({rp.ResolvedPath});
29

3-
if stdlib.strempty(file)
4-
c = "";
10+
if strict
511
return
612
end
713

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));
14+
for i = 1:numel(file)
15+
if strlength(c(i)) == 0 && strlength(file(i)) > 0
16+
c(i) = stdlib.normalize(file(i));
17+
end
1318
end
1419

1520
end

+stdlib/+native/normalize.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
function n = normalize(p)
2+
arguments
3+
p (1,1) string
4+
end
25

3-
n = stdlib.posix(string(p));
6+
n = stdlib.posix(p);
47

58
uncslash = ispc() && startsWith(n, "//");
69

+stdlib/absolute.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
function c = absolute(p, base)
1919
arguments
2020
p string
21-
base {mustBeTextScalar} = pwd()
21+
base (1,1) string = pwd()
2222
end
2323

2424
i = stdlib.is_absolute(p);
@@ -29,6 +29,10 @@
2929
base = fullfile(pwd(), base);
3030
end
3131

32-
c(~i) = fullfile(base, p(~i));
32+
i = ~i;
33+
c(i) = base;
34+
35+
i = i & ~stdlib.strempty(p);
36+
c(i) = fullfile(base, p(i));
3337

3438
end

+stdlib/canonical.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414

1515
function c = canonical(p, strict, backend)
1616
arguments
17-
p {mustBeTextScalar}
17+
p string
1818
strict (1,1) logical = false
1919
backend (1,:) string = ["native", "legacy"]
2020
end
2121

22-
fun = hbackend(backend, "canonical", 'R2024a');
23-
24-
c = fun(p, strict);
22+
if isscalar(p)
23+
fun = hbackend(backend, "canonical", 'R2024a');
24+
c = fun(p, strict);
25+
else
26+
c = stdlib.native.canonical(p, strict);
27+
end
2528

2629
end

+stdlib/resolve.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
function r = resolve(p)
1212
arguments
13-
p {mustBeTextScalar}
13+
p string
1414
end
1515

1616
r = stdlib.canonical(stdlib.absolute(p));

+stdlib/suffix.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
[~, n, s] = fileparts(p);
99

10-
if stdlib.strempty(n)
11-
s = n;
12-
end
10+
i = stdlib.strempty(n);
11+
s(i) = n(i);
1312

1413
end

+stdlib/with_suffix.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
function f = with_suffix(p, suffix)
1010
arguments
1111
p string
12-
suffix {mustBeTextScalar}
12+
suffix string
1313
end
1414

1515
f = extractBefore(p, stdlib.suffix(p));

test/TestAbsolute.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ function test_absolute2arg(tc, p2)
7272
tc.verifyEqual(rabs, string(r))
7373
end
7474

75+
76+
function test_absolute_array(tc)
77+
78+
in = ["", "hi"];
79+
r = stdlib.absolute(in);
80+
81+
tc.verifyEqual(r, [pwd(), fullfile(pwd(), "hi")])
82+
83+
end
84+
7585
end
7686

7787
end

test/TestCanonical.m

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{"not-exist/a/..", "not-exist"}, ...
1010
{"./not-exist", "not-exist"}
1111
};
12-
fun = {"native", "legacy"}
12+
backend = {"native", "legacy"}
1313
end
1414

1515
methods(TestClassSetup)
@@ -22,16 +22,25 @@ function pkg_path(tc)
2222

2323
methods(Test, TestTags="impure")
2424

25-
function test_canonical(tc, p, fun)
25+
function test_canonical(tc, p, backend)
2626
try
27-
c = stdlib.canonical(p{1}, false, fun);
27+
c = stdlib.canonical(p{1}, false, backend);
2828
catch e
29-
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
29+
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.stack(1).file + ":" + string(e.stack(1).line) + " " + e.message)
3030
return
3131
end
3232
tc.verifyEqual(c, p{2})
3333
end
3434

35+
function test_canonical_array(tc)
36+
tc.assumeFalse(isMATLABReleaseOlderThan('R2024a'))
37+
38+
in = ["", "hi", "/ok", "not-exist/a/.."];
39+
c = stdlib.native.canonical(in);
40+
41+
tc.verifyEqual(c, ["", "hi", filesep + "ok", "not-exist"])
42+
end
43+
3544
end
3645

3746
end

test/TestParent.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ function test_parent(tc, p, backend)
2424
tc.verifyEqual(pr, p{2}, sprintf("parent(%s, %s)", p{1}, backend))
2525
end
2626

27+
function test_parent_array(tc)
28+
in = ["", ".", "..", "../..", "a/", "a/b", "ab/.parent", "ab/.parent.txt", "a/b/../.parent.txt"];
29+
exp = [".", ".", ".", "..", ".", "a", "ab", "ab", fullfile("a", "b", "..")];
30+
31+
out = stdlib.parent(in, "native");
32+
tc.verifyEqual(out, exp)
33+
end
34+
2735
end
2836

2937
end

0 commit comments

Comments
 (0)