Skip to content

Commit 3a57fea

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

File tree

10 files changed

+47
-73
lines changed

10 files changed

+47
-73
lines changed

+stdlib/absolute.m

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,41 @@
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);
37+
else
38+
c = b;
5239
end
5340

54-
if isstring(p)
41+
if isstring(p) || isstring(base)
5542
c = string(c);
5643
end
5744

5845
end
5946

6047

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

+stdlib/canonical.m

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,36 @@
88
%
99
%%% Inputs
1010
% * p: path to make canonical
11-
% * expand_tilde: expand ~ to username if present
1211
%%% Outputs
1312
% * c: canonical path, if determined
1413

15-
function c = canonical(p, expand_tilde)
14+
function c = canonical(p)
1615
arguments
1716
p {mustBeTextScalar}
18-
expand_tilde (1,1) logical = true
1917
end
2018

2119
c = "";
2220

23-
if expand_tilde
24-
e = stdlib.expanduser(p);
25-
else
26-
e = p;
27-
end
28-
29-
if ~strlength(e) || (ispc() && (startsWith(e, {'\\', '//'})))
21+
if ~strlength(p) || (ispc() && (startsWith(p, {'\\', '//'})))
3022
% UNC path is not canonicalized
3123
return
3224
end
3325

3426
if stdlib.isoctave()
3527
% empty if any component of path does not exist
36-
c = canonicalize_file_name(e);
28+
c = canonicalize_file_name(p);
3729
else
3830
% errors if any component of path does not exist.
3931
% disp("builtin")
4032
try %#ok<TRYNC>
41-
c = builtin('_canonicalizepath', e);
33+
c = builtin('_canonicalizepath', p);
4234
end
4335
end
4436

4537
c = stdlib.posix(c);
4638

4739
if ~strlength(c)
48-
c = stdlib.normalize(e);
40+
c = stdlib.normalize(p);
4941
end
5042

5143
if isstring(p)
@@ -54,5 +46,5 @@
5446

5547
end
5648

57-
%!assert(canonical("", 1), "")
58-
%!assert(canonical("~", 1), homedir())
49+
%!assert(canonical(""), "")
50+
%!assert(canonical("~"), homedir())

+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: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@
33
% effectively canonical(absolute(p))
44
%%% Inputs
55
% * p: path to make absolute
6-
% * expand_tilde: expand ~ to username if present
76
%%% Outputs
87
% * c: resolved path
98
% distinct from canonical(), resolve() always returns absolute path
109
% non-existant path is made absolute relative to pwd
1110

12-
function r = resolve(p, expand_tilde)
11+
function r = resolve(p)
1312
arguments
1413
p {mustBeTextScalar}
15-
expand_tilde (1,1) logical = true
1614
end
1715

18-
19-
r = stdlib.canonical(stdlib.absolute(p, '', expand_tilde), false);
16+
r = stdlib.canonical(stdlib.absolute(p));
2017

2118
end
2219

23-
%!assert (resolve('', 1), stdlib.posix(pwd()))
20+
%!assert (resolve(''), stdlib.posix(pwd()))

+stdlib/samepath.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isSameFile(java.nio.file.Path,java.nio.file.Path)
2424

2525
issame = stdlib.exists(path1) && stdlib.exists(path2) && ...
26-
strcmp(stdlib.canonical(path1, false), stdlib.canonical(path2, false));
26+
strcmp(stdlib.canonical(path1), stdlib.canonical(path2));
2727

2828
end
2929

test/TestAbsolute.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function test_absolute2arg(tc, p2)
4949
r = strcat(r, '/', p2{1});
5050
end
5151

52-
if isstring(p2{1})
52+
if isstring(p2{1}) || isstring(p2{2})
5353
r = string(r);
5454
end
5555

test/TestCanonical.m

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@
55
{"", ""}, ...
66
{"not-exist", "not-exist"}, ...
77
{"a/../b", "b"}, ...
8-
{'~', stdlib.homedir()}, ...
9-
{"~", string(stdlib.homedir())}, ...
10-
{'~/', stdlib.homedir()}, ...
11-
{"~/", string(stdlib.homedir())}, ...
12-
{"~/..", string(stdlib.parent(stdlib.homedir()))}, ...
138
{mfilename("fullpath") + ".m/..", string(stdlib.parent(mfilename("fullpath")))}, ...
14-
{"~/not-exist/a/..", stdlib.homedir() + "/not-exist"}, ...
9+
{"not-exist/a/..", "not-exist"}, ...
1510
{"./not-exist", "not-exist"}, ...
1611
{"../not-exist", "../not-exist"}
1712
};
@@ -21,7 +16,7 @@
2116
methods(Test)
2217

2318
function test_canonical(tc, p)
24-
tc.verifyEqual(stdlib.canonical(p{1}, true), p{2})
19+
tc.verifyEqual(stdlib.canonical(p{1}), p{2})
2520
end
2621

2722
end

test/TestResolve.m

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,32 @@ function test_resolve_relative(tc)
1313

1414
% all non-existing files
1515

16-
pabs = stdlib.resolve('2foo', true);
17-
pabs2 = stdlib.resolve('4foo', true);
16+
pabs = stdlib.resolve('2foo');
17+
pabs2 = stdlib.resolve('4foo');
1818
tc.verifyThat(pabs, ~StartsWithSubstring("2"))
1919
tc.verifyTrue(strncmp(pabs, pabs2, 2))
2020

21-
par1 = stdlib.resolve("../2foo", true);
21+
par1 = stdlib.resolve("../2foo");
2222
tc.verifyNotEmpty(par1)
2323
tc.verifyThat(par1, ~ContainsSubstring(".."))
2424

25-
par2 = stdlib.resolve("../4foo", true);
25+
par2 = stdlib.resolve("../4foo");
2626
tc.verifyTrue(strncmp(par2, pabs2, 2))
2727

28-
pt1 = stdlib.resolve("bar/../2foo", true);
28+
pt1 = stdlib.resolve("bar/../2foo");
2929
tc.verifyNotEmpty(pt1)
3030
tc.verifyThat(pt1, ~ContainsSubstring(".."))
3131

32-
va = stdlib.resolve("2foo", true);
33-
vb = stdlib.resolve("4foo", true);
32+
va = stdlib.resolve("2foo");
33+
vb = stdlib.resolve("4foo");
3434
tc.verifyThat(va, ~StartsWithSubstring("2"))
3535
tc.verifyTrue(strncmp(va, vb, 2))
3636

3737
end
3838

3939
function test_resolve_fullpath(tc, p)
40-
tc.verifyEqual(stdlib.resolve(p{1}, true), p{2})
40+
tc.verifyEqual(stdlib.resolve(p{1}), p{2}, ...
41+
sprintf("mex: %d", stdlib.is_mex_fun("stdlib.parent")))
4142
end
4243

4344
end
@@ -50,14 +51,16 @@ function test_resolve_fullpath(tc, p)
5051
p = {...
5152
{'', stdlib.posix(pwd())}, ...
5253
{"", string(stdlib.posix(pwd()))}, ...
53-
{'~', stdlib.homedir()}, ...
54-
{"~", string(stdlib.homedir())}, ...
55-
{'~/', stdlib.homedir()}, ...
56-
{"~/", string(stdlib.homedir())}, ...
57-
{"~/..", string(stdlib.parent(stdlib.homedir()))}, ...
58-
{mfilename("fullpath") + ".m/..", string(stdlib.parent(mfilename("fullpath")))}, ...
5954
{"a/../b", stdlib.posix(pwd()) + "/b"}, ...
6055
{"not-exist", stdlib.posix(pwd()) + "/not-exist"}, ...
61-
{"~/not-exist/a/..", stdlib.homedir() + "/not-exist"}...
56+
{"./not-exist/a/..", stdlib.posix(pwd()) + "/not-exist"}...
6257
};
58+
59+
p{end+1} = {strcat(mfilename("fullpath"), '.m/..'), stdlib.parent(mfilename("fullpath"))};
60+
p{end+1} = cellfun(@string, p{end});
61+
62+
if stdlib.is_mex_fun("stdlib.parent")
63+
p{end-1,2} = string(p{end-1,2});
64+
end
65+
6366
end

0 commit comments

Comments
 (0)