Skip to content

Commit 0361e19

Browse files
committed
absolute: remove expand_tilde option
This was an anti-pattern, as the user can trivially call expanduser in the same line
1 parent dddac9f commit 0361e19

File tree

6 files changed

+20
-40
lines changed

6 files changed

+20
-40
lines changed

+stdlib/absolute.m

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,37 @@
99
%%% Inputs
1010
% * p: path to make absolute
1111
% * base: if present, base on this instead of cwd
12-
% * expand_tilde: expand ~ to username if present
1312
%%% Outputs
1413
% * c: absolute path
1514
%
1615
% does not normalize path
1716
% non-existant path is made absolute relative to pwd
1817

19-
function c = absolute(p, base, expand_tilde)
18+
function c = absolute(p, base)
2019
arguments
2120
p {mustBeTextScalar}
22-
base {mustBeTextScalar} = ''
23-
expand_tilde (1,1) logical = true
24-
end
25-
26-
if expand_tilde
27-
c = stdlib.expanduser(p);
28-
else
29-
c = p;
21+
base {mustBeTextScalar} = pwd()
3022
end
3123

24+
c = p;
3225
if stdlib.is_absolute(c)
3326
return
3427
end
3528

36-
if strlength(base) == 0
37-
b = pwd();
38-
elseif expand_tilde
39-
b = stdlib.expanduser(base);
29+
if strlength(base)
30+
b = stdlib.absolute(base);
4031
else
41-
b = base;
42-
end
43-
44-
if ~stdlib.is_absolute(b)
45-
b = strcat(pwd(), '/', b);
32+
b = pwd();
4633
end
4734

48-
if strlength(c) == 0
49-
c = b;
50-
else
35+
if strlength(c)
5136
c = strcat(b, '/', c);
52-
end
53-
54-
if isstring(p)
55-
c = string(c);
37+
else
38+
c = b;
5639
end
5740

5841
end
5942

6043

61-
%!assert(absolute('', '', false), pwd)
62-
%!assert(absolute('a/b', '', false), strcat(pwd(), '/a/b'))
44+
%!assert(absolute('', ''), pwd)
45+
%!assert(absolute('a/b', ''), strcat(pwd(), '/a/b'))

+stdlib/is_regular_file.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
end
88

99
% needs absolute()
10-
p = stdlib.absolute(p, '', false);
10+
p = stdlib.absolute(p);
1111

1212
op = javaPathObject(p);
1313
opt = javaLinkOption();

+stdlib/is_symlink.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
ok = isSymbolicLink(p);
1212
catch e
1313
switch e.identifier
14-
case "MATLAB:UndefinedFunction", ok = java.nio.file.Files.isSymbolicLink(javaPathObject(stdlib.absolute(p, '', false)));
14+
case "MATLAB:UndefinedFunction", ok = java.nio.file.Files.isSymbolicLink(javaPathObject(stdlib.absolute(p)));
1515
case "Octave:undefined-function", ok = S_ISLNK(stat(p).mode);
1616
otherwise, rethrow(e)
1717
end

+stdlib/read_symlink.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
if stdlib.is_symlink(p)
2020
% must be absolute path
2121
% must not be .canonical or symlink is gobbled!
22-
r = stdlib.absolute(p, '', false);
22+
r = stdlib.absolute(p);
2323

2424
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#readSymbolicLink(java.nio.file.Path)
2525
r = java.nio.file.Files.readSymbolicLink(javaPathObject(r)).string;

+stdlib/resolve.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
expand_tilde (1,1) logical = true
1616
end
1717

18+
if expand_tilde
19+
c = stdlib.expand_tilde(p);
20+
else
21+
c = p;
22+
end
1823

19-
r = stdlib.canonical(stdlib.absolute(p, '', expand_tilde), false);
24+
r = stdlib.canonical(stdlib.absolute(c), false);
2025

2126
end
2227

test/TestAbsolute.m

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ function test_absolute1arg(tc, p1)
2929
r = strcat(r, '/', p1);
3030
end
3131

32-
if isstring(p1)
33-
r = string(r);
34-
end
35-
3632
tc.verifyEqual(stdlib.absolute(p1), r)
3733
end
3834

@@ -49,10 +45,6 @@ function test_absolute2arg(tc, p2)
4945
r = strcat(r, '/', p2{1});
5046
end
5147

52-
if isstring(p2{1})
53-
r = string(r);
54-
end
55-
5648
tc.verifyEqual(stdlib.absolute(p2{1}, p2{2}), r)
5749
end
5850

0 commit comments

Comments
 (0)