Skip to content

Commit 3889872

Browse files
committed
absolute: speed and don't coerce
1 parent 2fe2cf5 commit 3889872

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

+stdlib/absolute.m

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,31 @@
1616
% non-existant path is made absolute relative to pwd
1717

1818
function c = absolute(p, base)
19-
arguments
20-
p (1,1) string
21-
base (1,1) string = pwd()
19+
if nargin < 2
20+
base = pwd();
2221
end
2322

24-
i = stdlib.is_absolute(p);
25-
c(i) = p(i);
23+
if stdlib.is_absolute(p)
24+
c = p;
25+
return
26+
end
2627

2728
% avoid infinite recursion
28-
if ~stdlib.is_absolute(base)
29-
base = fullfile(pwd(), base);
29+
if stdlib.is_absolute(base)
30+
c = base;
31+
else
32+
c = pwd();
33+
if ~stdlib.strempty(base)
34+
c = append(c, '/', base);
35+
end
3036
end
3137

32-
i = ~i;
33-
c(i) = base;
34-
35-
i = i & ~stdlib.strempty(p);
36-
c(i) = fullfile(base, p(i));
38+
if ~stdlib.strempty(p)
39+
if endsWith(c, ["/", filesep])
40+
c = append(c, p);
41+
else
42+
c = append(c, '/', p);
43+
end
44+
end
3745

3846
end

test/TestAbsolute.m

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
TestAbsolute < matlab.unittest.TestCase
44

55
properties (TestParameter)
6-
p1 = {'', "", "hi", "./hi", "../hi"};
7-
p2 = {{'', ""}, {'', ""}, {"", ""}, {"", ""}, {"", "hi"}, {"hi", ""}, {'there', "hi"}};
6+
p1 = {'', "hi", "./hi", "../hi"};
7+
p2 = {{'', ''}, {'', 'hi'}, {"hi", ""}, {'there', 'hi'}};
88
end
99

1010

@@ -19,35 +19,35 @@ function test_dirs(tc)
1919

2020
function test_absolute1arg(tc, p1)
2121

22-
r = pwd();
23-
2422
if strlength(p1)
25-
r = fullfile(r, p1);
23+
r = append(pwd(), '/', p1);
24+
else
25+
r = pwd();
2626
end
2727

2828
rabs = stdlib.absolute(p1);
2929

30-
tc.verifyClass(rabs, 'string')
31-
tc.verifyEqual(rabs, string(r))
30+
tc.verifyClass(rabs, class(p1))
31+
tc.verifyEqual(rabs, r)
3232
end
3333

3434

3535
function test_absolute2arg(tc, p2)
3636

37-
r = pwd();
38-
3937
if strlength(p2{2})
40-
r = fullfile(r, p2{2});
38+
r = append(pwd(), '/', p2{2});
39+
else
40+
r = pwd();
4141
end
4242

4343
if strlength(p2{1})
44-
r = fullfile(r, p2{1});
44+
r = append(r, '/', p2{1});
4545
end
4646

4747
rabs = stdlib.absolute(p2{1}, p2{2});
4848

49-
tc.verifyClass(rabs, 'string')
50-
tc.verifyEqual(rabs, string(r))
49+
tc.verifyClass(rabs, class(p2{1}))
50+
tc.verifyEqual(rabs, r)
5151
end
5252

5353
end

0 commit comments

Comments
 (0)