Skip to content

Commit eac6366

Browse files
committed
posix, expanduser: don't coerce
1 parent 9dbd2ab commit eac6366

12 files changed

+89
-72
lines changed

+stdlib/canonical.m

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
function c = canonical(p, expand_tilde)
1616
arguments
17-
p (1,1) string
17+
p {mustBeTextScalar}
1818
expand_tilde (1,1) logical = true
1919
end
2020

@@ -26,9 +26,7 @@
2626
e = p;
2727
end
2828

29-
if ~strlength(e), return, end
30-
31-
if ispc && (startsWith(e, "\\") || startsWith(e, "//"))
29+
if ~strlength(e) || (ispc && (startsWith(e, "\\") || startsWith(e, "//")))
3230
% UNC path is not canonicalized
3331
return
3432
end
@@ -48,7 +46,7 @@
4846
c = stdlib.normalize(e);
4947
end
5048

51-
c = stdlib.posix(c);
49+
c = string(stdlib.posix(c));
5250

5351
end
5452

+stdlib/expanduser.m

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,27 @@
99

1010
function e = expanduser(p)
1111
arguments
12-
p (1,1) string
12+
p {mustBeTextScalar}
1313
end
1414

15-
e = stdlib.posix(p);
15+
e = stdlib.posix(char(p));
1616

17-
L = strlength(e);
18-
if ~L || ~startsWith(e, "~") || (L > 1 && ~startsWith(e, "~/"))
19-
return
20-
end
21-
22-
home = stdlib.homedir();
23-
if ~strlength(home), return, end
24-
25-
if L < 2
26-
e = home;
27-
return
17+
L = length(e);
18+
if L == 0 || e(1) ~= '~' || (L > 1 && ~strcmp(e(1:2), '~/'))
19+
% noop
20+
else
21+
home = stdlib.homedir();
22+
if isempty(home)
23+
% noop
24+
elseif L < 2
25+
e = home;
26+
else
27+
e = strcat(home, '/', e(3:end));
28+
end
2829
end
2930

30-
if ischar(e)
31-
e = strcat(home, '/', e(3:end));
32-
else
33-
e = home + "/" + e{1}(3:end);
31+
if isstring(p)
32+
e = string(e);
3433
end
3534

3635
end

+stdlib/handle2filename.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
fileHandle (1,1) {mustBeInteger}
77
end
88

9-
n = "";
9+
n = '';
1010

1111
if fileHandle >= 0
1212
n = stdlib.posix(fopen(fileHandle));

+stdlib/parent.m

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,44 @@
99

1010
function p = parent(pth)
1111
arguments
12-
pth (1,1) string
12+
pth {mustBeTextScalar}
1313
end
1414

1515
p = stdlib.drop_slash(pth);
1616

1717
if ~strlength(p)
18-
p = ".";
19-
return
18+
p = '.';
2019
elseif is_root_stub(p)
2120
% 2 or 3 char drive letter
2221
if strlength(p) == 2
23-
p = strcat(p, "/");
22+
p = strcat(p, '/');
2423
end
25-
return
2624
elseif strcmp(p, stdlib.root(p))
27-
return
28-
end
29-
30-
31-
j = strfind(p, '/');
32-
if isempty(j)
33-
p = "";
34-
elseif ischar(p)
35-
p = p(1:j(end)-1);
25+
% noop
3626
else
37-
p = p{1}(1:j(end)-1);
38-
end
27+
j = strfind(p, '/');
28+
if isempty(j)
29+
p = '';
30+
elseif ischar(p)
31+
p = p(1:j(end)-1);
32+
else
33+
p = p{1}(1:j(end)-1);
34+
end
3935

40-
if is_root_stub(p)
41-
p = stdlib.root(pth);
42-
return
36+
if is_root_stub(p)
37+
p = stdlib.root(pth);
38+
return
39+
end
4340
end
4441

4542
p = stdlib.posix(p);
4643

4744
if ~strlength(p)
48-
p = ".";
45+
p = '.';
46+
end
47+
48+
if isstring(pth)
49+
p = string(p);
4950
end
5051

5152
end

+stdlib/posix.m

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

66
function r = posix(p)
77
arguments
8-
p (1,1) string
8+
p {mustBeTextScalar}
99
end
1010

1111
if ispc

test/TestAbsolute.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
methods(TestClassSetup)
2828
function set_cwd(tc)
2929
import matlab.unittest.fixtures.CurrentFolderFixture
30-
tc.td = stdlib.posix(tc.createTemporaryFolder());
30+
tc.td = string(stdlib.posix(tc.createTemporaryFolder()));
3131
tc.applyFixture(CurrentFolderFixture(tc.td))
3232
end
3333
end

test/TestCanonical.m

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
classdef TestCanonical < matlab.unittest.TestCase
22

33
properties(TestParameter)
4-
p = {{"", ""}, ...
4+
p = {{'', ""}, ...
5+
{"", ""}, ...
56
{"not-exist", "not-exist"}, ...
67
{"a/../b", "b"}, ...
7-
{"~", stdlib.homedir()}, ...
8-
{"~/", stdlib.homedir()}, ...
9-
{"~/..", stdlib.parent(stdlib.homedir())}, ...
10-
{mfilename("fullpath") + ".m/..", stdlib.parent(mfilename("fullpath"))}, ...
8+
{'~', string(stdlib.homedir())}, ...
9+
{"~", string(stdlib.homedir())}, ...
10+
{'~/', string(stdlib.homedir())}, ...
11+
{"~/", string(stdlib.homedir())}, ...
12+
{"~/..", string(stdlib.parent(stdlib.homedir()))}, ...
13+
{mfilename("fullpath") + ".m/..", string(stdlib.parent(mfilename("fullpath")))}, ...
1114
{"~/not-exist/a/..", stdlib.homedir() + "/not-exist"}, ...
1215
{"./not-exist", "not-exist"}, ...
1316
{"../not-exist", "../not-exist"}

test/TestExpanduser.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
classdef TestExpanduser < matlab.unittest.TestCase
2+
3+
properties(TestParameter)
4+
5+
p = {{'', ''}, {"", ""}, ...
6+
{"~abc", "~abc"}, ...
7+
{'~', stdlib.homedir()},...
8+
{"~", string(stdlib.homedir())}, ...
9+
{'~/', strcat(stdlib.homedir(), '/')},...
10+
{"~/", stdlib.homedir() + "/"}, ...
11+
{"~/c", stdlib.homedir() + "/c"}, ...
12+
{"~//c", stdlib.homedir() + "//c"}};
13+
end
14+
15+
methods(Test)
16+
17+
function test_expanduser(tc, p)
18+
tc.verifyEqual(stdlib.expanduser(p{1}), p{2})
19+
end
20+
21+
end
22+
23+
end

test/TestFileImpure.m

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
properties(TestParameter)
44
p_is_writable = {{pwd(), true}, {"not-exists", false}, {"", false}};
55

6-
p_expand = {{"", ""}, {"~abc", "~abc"}, {"~", stdlib.homedir()}, ...
7-
{"~/c", stdlib.homedir() + "/c"}, {'~//c', stdlib.homedir() + "//c"}};
8-
96
p_same = {...
107
{"","", false}, ...
118
{tempname(), tempname(), false}, ...
129
{"..", "./..", true}, ...
1310
{"..", pwd() + "/..", true}}
1411

15-
ph = {{0, '"stdin"'}, {1, '"stdout"'}, {2, '"stderr"'}, {fopen(tempname()), ""}}
12+
ph = {{0, '"stdin"'}, {1, '"stdout"'}, {2, '"stderr"'}, {fopen(tempname()), ''}}
1613

1714
p_file_size = {mfilename("fullpath") + ".m"}
1815
end
@@ -32,11 +29,6 @@ function test_is_writable(tc, p_is_writable)
3229
end
3330

3431

35-
function test_expanduser(tc, p_expand)
36-
tc.verifyEqual(stdlib.expanduser(p_expand{1}), p_expand{2})
37-
end
38-
39-
4032
function test_null_file(tc)
4133
import matlab.unittest.constraints.IsFile
4234
tc.assumeFalse(ispc)
@@ -66,7 +58,7 @@ function test_get_pid(tc)
6658

6759

6860
function test_handle2filename(tc, ph)
69-
tc.verifyEqual(stdlib.handle2filename(ph{1}), string(ph{2}))
61+
tc.verifyEqual(stdlib.handle2filename(ph{1}), ph{2})
7062
end
7163

7264
end

test/TestResolve.m

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ function test_resolve_relative(tc)
1111
import matlab.unittest.constraints.StartsWithSubstring
1212
import matlab.unittest.constraints.ContainsSubstring
1313

14-
td = stdlib.posix(pwd());
15-
1614
% all non-existing files
1715

18-
tc.verifyEqual(stdlib.resolve("", true), stdlib.posix(td))
16+
tc.verifyEqual(stdlib.resolve(""), string(stdlib.posix(pwd())))
1917

2018
pabs = stdlib.resolve('2foo', true);
2119
pabs2 = stdlib.resolve('4foo', true);
@@ -54,13 +52,16 @@ function test_resolve_fullpath(tc, p)
5452
c = stdlib.posix(pwd());
5553

5654
p = {...
57-
{"", c}, ...
55+
{'', string(c)}, ...
56+
{"", string(c)}, ...
5857
{"not-exist", c + "/not-exist"}, ...
5958
{"a/../b", c + "/b"}, ...
60-
{"~", stdlib.homedir()}, ...
61-
{"~/", stdlib.homedir()}, ...
62-
{"~/..", stdlib.parent(stdlib.homedir())}, ...
63-
{mfilename("fullpath") + ".m/..", stdlib.parent(mfilename("fullpath"))}, ...
59+
{'~', string(stdlib.homedir())}, ...
60+
{"~", string(stdlib.homedir())}, ...
61+
{'~/', string(stdlib.homedir())}, ...
62+
{"~/", string(stdlib.homedir())}, ...
63+
{"~/..", string(stdlib.parent(stdlib.homedir()))}, ...
64+
{mfilename("fullpath") + ".m/..", string(stdlib.parent(mfilename("fullpath")))}, ...
6465
{"~/not-exist/a/..", stdlib.homedir() + "/not-exist"}
6566
};
6667
end

test/TestWindowsCOM.m

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

33
properties (TestParameter)
44
Pn = {""}
5-
Pmr = {string(matlabroot), stdlib.posix(matlabroot)}
5+
Pmr = {matlabroot, stdlib.posix(matlabroot)}
66
end
77

88
methods (Test)
@@ -38,9 +38,9 @@ function test_short_file(tc, Pmr)
3838
if contains(Pmr, " ")
3939
tc.verifySubstring(s, "~")
4040
end
41-
tc.verifyEqual(stdlib.canonical(s), stdlib.posix(Pmr), "shortname didn't resolve same as canonical")
41+
tc.verifyEqual(stdlib.canonical(s), string(stdlib.posix(Pmr)), "shortname didn't resolve same as canonical")
4242
else
43-
tc.verifyEqual(s, Pmr)
43+
tc.verifyEqual(s, string(Pmr))
4444
end
4545

4646
end

test/TestWithSuffix.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{"", ".nc", ".nc"}, ...
88
{"a/b/c/", ".h5", "a/b/c/.h5"}, ...
99
{"a/b/.h5", ".nc", "a/b/.h5.nc"}, ...
10-
{'a/b', '.nc', "a/b.nc"}};
10+
{'a/b', '.nc', 'a/b.nc'}};
1111
end
1212

1313
methods (Test)

0 commit comments

Comments
 (0)