Skip to content

Commit 155c0d7

Browse files
committed
join: do not posix()
simplify add root_dir() and use join work like <filesystem> append
1 parent 4ba56e6 commit 155c0d7

File tree

6 files changed

+112
-44
lines changed

6 files changed

+112
-44
lines changed

+stdlib/is_absolute.m

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,22 @@
66

77
function y = is_absolute(p)
88
arguments
9-
p (1,:) char
9+
p {mustBeTextScalar}
1010
end
1111

1212
% not Octave is_absolute_filename() because this is a stricter check for "c:" false
1313

14-
y = false;
14+
y = strlength(stdlib.root_dir(p)) > 0;
1515

16-
L = strlength(p);
17-
if ~L || (ispc() && L < 3)
18-
return
19-
end
20-
21-
if ispc
22-
y = strlength(stdlib.root_name(p)) && any(p(3) == ['/', '\']);
23-
else
24-
y = p(1) == '/';
16+
if ispc()
17+
y = y && strlength(stdlib.root_name(p)) > 0;
2518
end
2619

2720
end
2821

2922
%!assert(is_absolute(''), false)
3023
%!test
31-
%! if ispc
24+
%! if ispc()
3225
%! assert(is_absolute('C:\'))
3326
%! assert(is_absolute('C:/'))
3427
%! assert(!is_absolute('C:'))

+stdlib/join.m

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,38 @@
66
other {mustBeTextScalar}
77
end
88

9+
rno = stdlib.root_name(other);
10+
rnb = stdlib.root_name(base);
11+
rdo = stdlib.root_dir(other);
912

10-
b = stdlib.posix(base);
11-
o = stdlib.posix(other);
13+
if stdlib.is_absolute(other) || (strlength(rno) && ~strcmp(rnb, rno))
1214

13-
if startsWith(o, '/') || (ispc() && stdlib.is_absolute(o))
14-
p = o;
15-
return
16-
end
15+
p = other;
16+
17+
elseif strlength(rdo)
1718

18-
p = b;
19-
if strlength(o)
20-
if endsWith(p, '/')
21-
p = strcat(p, o);
22-
elseif strlength(p)
23-
p = strcat(p, '/', o);
19+
if strlength(rnb)
20+
p = strcat(rnb, '/', other);
2421
else
25-
p = o;
22+
p = other;
2623
end
24+
25+
elseif strlength(base)
26+
27+
if strlength(other)
28+
if endsWith(base, {'/', filesep})
29+
p = strcat(base, other);
30+
else
31+
p = strcat(base, '/', other);
32+
end
33+
else
34+
p = base;
35+
end
36+
37+
else
38+
39+
p = other;
40+
2741
end
2842

2943
end

+stdlib/root.m

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
%% ROOT get root path
22
% ROOT(P) returns the root path of P.
3-
% root is the root_name + root_directory.
4-
53

64
function r = root(p)
75
arguments
86
p {mustBeTextScalar}
97
end
108

11-
r = stdlib.root_name(p);
12-
13-
if ~strlength(r)
14-
if strncmp(p, '/', 1)
15-
r = '/';
16-
end
17-
elseif ~(ispc() && strcmp(r, p))
18-
r = strcat(r, '/');
19-
end
20-
21-
if isstring(p)
22-
r = string(r);
23-
end
9+
r = strcat(stdlib.root_name(p), stdlib.root_dir(p));
2410

2511
end
2612

+stdlib/root_dir.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
%% ROOT_DIR get root directory
2+
% Examples:
3+
4+
%% Windows
5+
% * root_dir('C:\path\to\file') returns '\'
6+
% * root_dir('C:path\to\file') returns ''
7+
%% Unix
8+
% * root_dir('/path/to/file') returns '/'
9+
% * root_dir('path/to/file') returns ''
10+
11+
function r = root_dir(p)
12+
arguments
13+
p {mustBeTextScalar}
14+
end
15+
16+
r = '';
17+
pc = char(p);
18+
19+
if startsWith(p, {'/', filesep})
20+
r = pc(1);
21+
elseif strlength(p) > 2 && strlength(stdlib.root_name(p))
22+
if any(pc(3) == ['/', filesep])
23+
r = pc(3);
24+
end
25+
end
26+
27+
if isstring(p)
28+
r = string(r);
29+
end
30+
31+
end

test/TestJoin.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{"a/b", "c/d", "a/b/c/d"}, ...
1414
{"ab/cd", "/ef", "/ef"}, ...
1515
{stdlib.homedir(), "", stdlib.homedir()}, ...
16-
{matlabroot, "bin", stdlib.posix(matlabroot + "/bin")}
16+
{matlabroot, "bin", matlabroot + "/bin"}
1717
}
1818
end
1919

test/TestRoot.m

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

33
properties (TestParameter)
44
p = init_root()
5+
rn = init_root_name()
6+
rd = init_root_dir()
57
end
68

79
methods (Test)
10+
811
function test_root(tc, p)
912
tc.verifyEqual(stdlib.root(p{1}), p{2})
1013
end
14+
15+
function test_root_dir(tc, rd)
16+
tc.verifyEqual(stdlib.root_dir(rd{1}), rd{2})
1117
end
1218

19+
function test_root_name(tc, rn)
20+
tc.verifyEqual(stdlib.root_name(rn{1}), rn{2})
1321
end
1422

23+
end
24+
25+
end % class
26+
1527

1628
function p = init_root()
1729

1830
p = {{"", ""}, ...
1931
{"a/b", ""}, ...
2032
{"./a/b", ""}, ...
33+
{"/etc", "/"}, ...
2134
{'/etc', '/'}, ...
2235
{"c:", ""}, ...
23-
{"c:/etc", ""}};
36+
{"c:/etc", ""}, ...
37+
{'c:\etc', ''}};
2438

2539
if ispc
26-
p{5}{2} = "c:";
27-
p{6}{2} = "c:/";
40+
p{6}{2} = "c:";
41+
p{7}{2} = "c:/";
42+
p{8}{2} = 'c:\';
43+
end
44+
45+
end
46+
47+
48+
function rn = init_root_name()
49+
50+
rn = init_root();
51+
52+
rn{4}{2} = "";
53+
rn{5}{2} = '';
54+
55+
if ispc()
56+
rn{6}{2} = "c:";
57+
rn{7}{2} = "c:";
58+
rn{8}{2} = 'c:';
59+
end
60+
61+
end
62+
63+
64+
function rd = init_root_dir()
65+
66+
rd = init_root();
67+
68+
if ispc()
69+
rd{6}{2} = "";
70+
rd{7}{2} = "/";
71+
rd{8}{2} = '\';
2872
end
2973

3074
end

0 commit comments

Comments
 (0)