Skip to content

Commit df17d96

Browse files
committed
canonical, resolve: any size array
1 parent 1a5dde1 commit df17d96

File tree

14 files changed

+87
-20
lines changed

14 files changed

+87
-20
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/+native/parent.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
i = stdlib.strempty(p);
1515
p(i) = ".";
1616

17-
i = ispc() & strcmp(p(~i), stdlib.root_name(pth));
17+
i = ispc() & strcmp(p(~i), stdlib.root_name(pth(~i)));
1818
p(i) = strcat(p(i), filesep);
1919

2020
end

+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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ function test_canonical(tc, p, fun)
3232
tc.verifyEqual(c, p{2})
3333
end
3434

35+
function test_canonical_array(tc)
36+
in = ["", "hi", "/ok", "not-exist/a/.."];
37+
c = stdlib.canonical(in);
38+
39+
tc.verifyEqual(c, ["", "hi", filesep + "ok", "not-exist"])
40+
end
41+
3542
end
3643

3744
end

0 commit comments

Comments
 (0)