Skip to content

Commit 1ee03c4

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

File tree

10 files changed

+63
-47
lines changed

10 files changed

+63
-47
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/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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
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()}, ...
8+
{'~', string(stdlib.homedir())}, ...
9+
{"~", string(stdlib.homedir())}, ...
10+
{'~/', string(stdlib.homedir())}, ...
11+
{"~/", string(stdlib.homedir())}, ...
912
{"~/..", stdlib.parent(stdlib.homedir())}, ...
1013
{mfilename("fullpath") + ".m/..", stdlib.parent(mfilename("fullpath"))}, ...
1114
{"~/not-exist/a/..", stdlib.homedir() + "/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: 7 additions & 6 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,11 +52,14 @@ 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()}, ...
59+
{'~', string(stdlib.homedir())}, ...
60+
{"~", string(stdlib.homedir())}, ...
61+
{'~/', string(stdlib.homedir())}, ...
62+
{"~/", string(stdlib.homedir())}, ...
6263
{"~/..", stdlib.parent(stdlib.homedir())}, ...
6364
{mfilename("fullpath") + ".m/..", stdlib.parent(mfilename("fullpath"))}, ...
6465
{"~/not-exist/a/..", stdlib.homedir() + "/not-exist"}

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

0 commit comments

Comments
 (0)