Skip to content

Commit 72e1629

Browse files
committed
root*: string, any size array
1 parent 5789f2a commit 72e1629

File tree

4 files changed

+29
-60
lines changed

4 files changed

+29
-60
lines changed

+stdlib/root_dir.m

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,19 @@
1010

1111
function r = root_dir(p)
1212
arguments
13-
p {mustBeTextScalar}
13+
p string
1414
end
1515

1616
sep = characterListPattern("/" + filesep);
17-
pat = (textBoundary + sep);
18-
if ispc()
19-
pat = pat | (lookBehindBoundary(lettersPattern(1) + ":") + sep);
20-
end
2117

22-
r = extract(p, pat);
23-
if isempty(r)
24-
r = '';
25-
if isstring(p)
26-
r = "";
27-
end
28-
elseif iscell(r)
29-
r = r{1};
18+
r = repmat("", size(p));
19+
20+
i = startsWith(p, sep);
21+
r(i) = extractBefore(p(i), 2);
22+
23+
if ispc()
24+
i = startsWith(p, lettersPattern(1) + ":" + sep);
25+
r(i) = extractBetween(p(i), 3, 3);
3026
end
3127

3228
end

+stdlib/root_name.m

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,14 @@
77

88
function r = root_name(p)
99
arguments
10-
p {mustBeTextScalar}
10+
p string
1111
end
1212

13-
if ispc()
14-
r = extract(p, textBoundary + lettersPattern(1) + ":");
15-
if ~isempty(r) && iscell(r)
16-
r = r{1};
17-
end
18-
else
19-
r = '';
20-
end
13+
r = repmat("", size(p));
2114

22-
if isempty(r)
23-
r = '';
24-
if isstring(p)
25-
r = "";
26-
end
15+
if ispc()
16+
i = startsWith(p, textBoundary + lettersPattern(1) + ":");
17+
r(i) = extractBefore(p(i), 3);
2718
end
2819

29-
3020
end

test/TestFilePure.m

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
classdef TestFilePure < matlab.unittest.TestCase
22

3-
properties (TestParameter)
4-
p = {{"", ""}, {"a/b", ""}, {'/etc', ''}}
5-
pu = {{"c:/etc", ""}}
6-
pw = {{"c:/etc", "c:"}}
7-
end
8-
93
methods(TestClassSetup)
104
function pkg_path(tc)
115
fsp = matlab.unittest.fixtures.PathFixture(fileparts(fileparts(mfilename('fullpath'))));
@@ -27,23 +21,6 @@ function test_posix(tc)
2721

2822
end
2923

30-
function test_root_name(tc, p)
31-
tc.verifyEqual(stdlib.root_name(p{1}), p{2})
32-
end
33-
end
34-
35-
methods(Test, TestTags=["pure", "unix"])
36-
function test_root_name_unix(tc, pu)
37-
tc.assumeTrue(isunix)
38-
tc.verifyEqual(stdlib.root_name(pu{1}), pu{2})
39-
end
40-
end
41-
42-
methods(Test, TestTags=["pure", "windows"])
43-
function test_root_name_windows(tc, pw)
44-
tc.assumeTrue(ispc, "This test is only for Windows")
45-
tc.verifyEqual(stdlib.root_name(pw{1}), pw{2})
46-
end
4724
end
4825

4926
end

test/TestRoot.m

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ function pkg_path(tc)
1616
methods (Test, TestTags="pure")
1717

1818
function test_root(tc, p)
19-
tc.verifyEqual(stdlib.root(p{1}), p{2})
19+
r = stdlib.root(p{1});
20+
tc.assertClass(r, 'string')
21+
tc.verifyEqual(r, p{2})
2022
end
2123

2224
function test_root_dir(tc, rd)
23-
tc.verifyEqual(stdlib.root_dir(rd{1}), rd{2}, "root_dir(" + rd{1} + ")")
25+
r = stdlib.root_dir(rd{1});
26+
tc.assertClass(r, 'string')
27+
tc.verifyEqual(r, rd{2}, "root_dir(" + rd{1} + ")")
2428
end
2529

2630
function test_root_name(tc, rn)
27-
tc.verifyEqual(stdlib.root_name(rn{1}), rn{2})
31+
r = stdlib.root_name(rn{1});
32+
tc.assertClass(r, 'string')
33+
tc.verifyEqual(r, rn{2})
2834
end
2935

3036
end
@@ -38,15 +44,15 @@ function test_root_name(tc, rn)
3844
{"a/b", ""}, ...
3945
{"./a/b", ""}, ...
4046
{"/etc", "/"}, ...
41-
{'/etc', '/'}, ...
47+
{'/etc', "/"}, ...
4248
{"c:", ""}, ...
4349
{"c:/etc", ""}, ...
44-
{'c:\etc', ''}};
50+
{'c:\etc', ""}};
4551

4652
if ispc()
4753
p{6}{2} = "c:";
4854
p{7}{2} = "c:/";
49-
p{8}{2} = 'c:\';
55+
p{8}{2} = "c:\";
5056
end
5157

5258
end
@@ -57,12 +63,12 @@ function test_root_name(tc, rn)
5763
rn = init_root();
5864

5965
rn{4}{2} = "";
60-
rn{5}{2} = '';
66+
rn{5}{2} = "";
6167

6268
if ispc()
6369
rn{6}{2} = "c:";
6470
rn{7}{2} = "c:";
65-
rn{8}{2} = 'c:';
71+
rn{8}{2} = "c:";
6672
end
6773

6874
end
@@ -75,7 +81,7 @@ function test_root_name(tc, rn)
7581
if ispc()
7682
rd{6}{2} = "";
7783
rd{7}{2} = "/";
78-
rd{8}{2} = '\';
84+
rd{8}{2} = "\";
7985
end
8086

8187
end

0 commit comments

Comments
 (0)