Skip to content

Commit dd1b3ea

Browse files
committed
Matlab < R2020b compatibility: filename, root
1 parent 3afaa31 commit dd1b3ea

File tree

13 files changed

+91
-153
lines changed

13 files changed

+91
-153
lines changed

+stdlib/absolute.m

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

1818
function c = absolute(p, base)
1919
arguments
20-
p string
20+
p (1,1) string
2121
base (1,1) string = pwd()
2222
end
2323

+stdlib/filename.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@
77

88
function f = filename(p, backend)
99
arguments
10-
p string
10+
p (1,1) string
1111
backend = 'fileparts'
1212
end
1313

14-
% the pattern backend is a litle slower than
1514
% fileparts is 5x to 10x faster than regexp and pattern
1615
switch backend
1716
case 'fileparts'
1817
[~, f, ext] = fileparts(p);
1918
f = f + ext;
20-
case 'pattern'
21-
f = extractAfter(p, asManyOfPattern(wildcardPattern + ("/" | filesep)));
19+
% the pattern backend is a litle slower than regexp. Commented out for < R2020b compatibility
20+
% case 'pattern'
21+
% f = extractAfter(p, asManyOfPattern(wildcardPattern + ("/" | filesep)));
2222
case 'regexp'
2323
f = regexp(p, ['[^/\' filesep ']*$'], 'match', 'once');
24-
f(ismissing(f)) = "";
24+
if ismissing(f)
25+
f = "";
26+
end
2527
otherwise, error('must be backend "pattern", "regexp" or "fileparts"')
2628
end
2729

+stdlib/is_absolute.m

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@
66

77
function y = is_absolute(p)
88
arguments
9-
p string
9+
p (1,1) string
1010
end
1111

12-
y = false(size(p));
12+
y = ~stdlib.strempty(stdlib.root_dir(p));
1313

14-
i = ~stdlib.strempty(stdlib.root_dir(p));
15-
16-
y(i) = true;
17-
18-
if ispc()
19-
y(i) = ~stdlib.strempty(stdlib.root_name(p(i)));
14+
if ispc() && y
15+
y = ~stdlib.strempty(stdlib.root_name(p));
2016
end
2117

2218
end

+stdlib/parent.m

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,15 @@
1010

1111
function p = parent(file)
1212
arguments
13-
file string
13+
file (1,1) string
1414
end
1515

1616
p = fileparts(stdlib.drop_slash(file));
1717

18-
i = stdlib.strempty(p);
19-
p(i) = ".";
20-
21-
% the ~all(i) is for Windows Matlab < R2025a
22-
if ispc() && ~all(i)
23-
i = p(~i) == stdlib.root_name(file(~i));
24-
p(i) = p(i) + "/";
18+
if stdlib.strempty(p)
19+
p = ".";
20+
elseif ispc() && p == stdlib.root_name(file)
21+
p = p + "/";
2522
end
2623

2724
end

+stdlib/resolve.m

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

1212
function r = resolve(p, strict)
1313
arguments
14-
p string
14+
p (1,1) string
1515
strict (1,1) logical = false
1616
end
1717

+stdlib/root_dir.m

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@
1010

1111
function r = root_dir(p)
1212
arguments
13-
p string
13+
p (1,1) string
1414
end
1515

16-
r = repmat("", size(p));
16+
r = "";
1717

18-
i = startsWith(p, ["/", filesep]);
19-
r(i) = extractBefore(p(i), 2);
20-
21-
if ispc()
22-
i = startsWith(p, lettersPattern(1) + ":" + characterListPattern("/" + filesep));
23-
r(i) = extractBetween(p(i), 3, 3);
18+
if startsWith(p, ["/", filesep])
19+
r = extractBefore(p, 2);
20+
elseif ispc() && ~isempty(regexp(p, '^[A-Za-z]:[\\/]','once'))
21+
r = extractBetween(p, 3, 3);
2422
end
2523

2624
end

+stdlib/root_name.m

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

88
function r = root_name(p)
99
arguments
10-
p string
10+
p (1,1) string
1111
end
1212

13-
r = repmat("", size(p));
13+
r = "";
1414

15-
if ispc()
16-
i = startsWith(p, lettersPattern(1) + ":");
17-
r(i) = extractBefore(p(i), 3);
15+
if ispc() && ~isempty(regexp(p, '^[A-Za-z]:', 'once'))
16+
r = extractBefore(p, 3);
1817
end
1918

2019
end

test/TestAbsolute.m

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,6 @@ function test_absolute2arg(tc, p2)
5050
tc.verifyEqual(rabs, string(r))
5151
end
5252

53-
54-
function test_absolute_array(tc)
55-
56-
in = ["", "hi"];
57-
r = stdlib.absolute(in);
58-
59-
tc.verifyEqual(r, [pwd(), fullfile(pwd(), "hi")])
60-
61-
end
62-
6353
end
6454

6555
end

test/TestExpanduser.m

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
classdef (SharedTestFixtures={ matlab.unittest.fixtures.PathFixture(fileparts(fileparts(mfilename('fullpath'))))}, ...
2-
TestTags = {'R2021a', 'impure'}) ...
2+
TestTags = {'R2019b', 'impure'}) ...
33
TestExpanduser < matlab.unittest.TestCase
44

55
properties (TestParameter)
6-
p
6+
p = init_exp()
7+
end
8+
9+
10+
methods (Test)
11+
12+
function test_expanduser(tc, p)
13+
tc.verifyEqual(stdlib.expanduser(p{1}), p{2})
14+
end
15+
16+
end
17+
718
end
819

920

10-
methods (TestParameterDefinition, Static)
1121
function p = init_exp()
1222

1323
if ispc
@@ -27,14 +37,3 @@
2737
{"~" + filesep() + "c", fullfile(h, "c")}};
2838

2939
end
30-
end
31-
32-
methods (Test)
33-
34-
function test_expanduser(tc, p)
35-
tc.verifyEqual(stdlib.expanduser(p{1}), p{2})
36-
end
37-
38-
end
39-
40-
end

test/TestFilename.m

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
classdef (SharedTestFixtures={ matlab.unittest.fixtures.PathFixture(fileparts(fileparts(mfilename('fullpath'))))}, ...
2-
TestTags = {'R2021a', 'pure'}) ...
2+
TestTags = {'R2019b', 'pure'}) ...
33
TestFilename < matlab.unittest.TestCase
44

55
properties (TestParameter)
6-
p
7-
backend = {'regexp', 'pattern'}
6+
p = init_p()
7+
backend = {'regexp'}
8+
end
9+
10+
11+
methods (Test)
12+
13+
function test_filename(tc, p, backend)
14+
fn = stdlib.filename(p{1}, backend);
15+
tc.verifyEqual(fn, p{2})
16+
end
17+
18+
end
19+
820
end
921

1022

11-
methods (TestParameterDefinition, Static)
1223
function p = init_p()
1324
p = {
1425
{'', ""}, ...
@@ -24,23 +35,3 @@
2435
p{end+1} = {"c:/df\df.txt", "df.txt"};
2536
end
2637
end
27-
end
28-
29-
30-
methods (Test)
31-
32-
function test_filename(tc, p, backend)
33-
fn = stdlib.filename(p{1}, backend);
34-
tc.verifyEqual(fn, p{2})
35-
end
36-
37-
function test_filename_array(tc, backend)
38-
in = ["", "a", "a/b/c", "a/b/", "a/b/c.txt", "a/b/.hidden", "a/b/c/"];
39-
exp = ["", "a", "c", "", "c.txt", ".hidden", ""];
40-
out = stdlib.filename(in, backend);
41-
tc.verifyEqual(out, exp)
42-
end
43-
44-
end
45-
46-
end

0 commit comments

Comments
 (0)