Skip to content

Commit bbf9962

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

File tree

6 files changed

+99
-43
lines changed

6 files changed

+99
-43
lines changed

+stdlib/is_absolute.m

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,18 @@
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;
15-
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) == '/';
25-
end
14+
y = strlength(stdlib.root_dir(p)) > 0;
2615

2716
end
2817

2918
%!assert(is_absolute(''), false)
3019
%!test
31-
%! if ispc
20+
%! if ispc()
3221
%! assert(is_absolute('C:\'))
3322
%! assert(is_absolute('C:/'))
3423
%! assert(!is_absolute('C:'))

+stdlib/join.m

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,34 @@
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)
18+
19+
p = strcat(rnb, '/', other);
20+
21+
elseif strlength(base)
1722

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);
23+
if strlength(other)
24+
if endsWith(base, {'/', filesep})
25+
p = strcat(base, other);
26+
else
27+
p = strcat(base, '/', other);
28+
end
2429
else
25-
p = o;
30+
p = base;
2631
end
32+
33+
else
34+
35+
p = other;
36+
2737
end
2838

2939
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
18+
if isunix()
19+
if strncmp(p, '/', 1)
20+
r = '/';
21+
end
22+
elseif strlength(p) >= 2 && strlength(stdlib.root_name(p))
23+
p = char(p);
24+
if any(p(3) == ['/', filesep])
25+
r = p(3);
26+
end
27+
end
28+
29+
if isstring(p)
30+
r = string(r);
31+
end
32+
33+
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,35 @@
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:", ""}, ...
2336
{"c:/etc", ""}};
@@ -28,3 +41,28 @@ function test_root(tc, p)
2841
end
2942

3043
end
44+
45+
46+
function rn = init_root_name()
47+
48+
rn = init_root();
49+
50+
rn{4}{2} = "";
51+
rn{5}{2} = '';
52+
53+
if ispc()
54+
rn{6}{2} = "c:";
55+
end
56+
57+
end
58+
59+
60+
function rd = init_root_dir()
61+
62+
rd = init_root();
63+
64+
if ispc()
65+
rd{6}{2} = "/";
66+
end
67+
68+
end

0 commit comments

Comments
 (0)