Skip to content

Commit 4199eac

Browse files
committed
root_*: patttern, simplify
1 parent 6cacd51 commit 4199eac

File tree

5 files changed

+25
-27
lines changed

5 files changed

+25
-27
lines changed

+stdlib/root.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
% ROOT(P) returns the root path of P.
33

44
function r = root(p)
5-
arguments
6-
p {mustBeTextScalar}
7-
end
85

96
r = strcat(stdlib.root_name(p), stdlib.root_dir(p));
107

+stdlib/root_dir.m

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
%% ROOT_DIR get root directory
22
% Examples:
3-
3+
%
44
%% Windows
55
% * root_dir('C:\path\to\file') returns '\'
66
% * root_dir('C:path\to\file') returns ''
@@ -13,22 +13,20 @@
1313
p {mustBeTextScalar}
1414
end
1515

16-
matches = regexp(p, '^([/\\])|^[A-Za-z]:([/\\])', 'tokens');
17-
18-
if isempty(matches)
16+
sep = characterListPattern("/" + filesep());
17+
pat = (textBoundary + sep);
18+
if ispc()
19+
pat = pat | (lookBehindBoundary(lettersPattern(1) + ":") + sep);
20+
end
1921

22+
r = extract(p, pat);
23+
if isempty(r)
2024
r = '';
2125
if isstring(p)
2226
r = "";
2327
end
24-
25-
else
26-
27-
r = matches{1};
28-
if iscell(r)
29-
r = r{1};
30-
end
31-
28+
elseif iscell(r)
29+
r = r{1};
3230
end
3331

3432
end

+stdlib/root_name.m

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@
1010
p {mustBeTextScalar}
1111
end
1212

13-
r = '';
14-
15-
if ~ispc() || strlength(p) < 2
16-
% noop
13+
if ispc()
14+
r = extract(p, textBoundary + lettersPattern(1) + ":");
15+
if ~isempty(r) && iscell(r)
16+
r = r{1};
17+
end
1718
else
18-
c = char(p);
19+
r = '';
20+
end
1921

20-
if c(2) == ':' && isletter(c(1))
21-
r = c(1:2);
22+
if isempty(r)
23+
r = '';
24+
if isstring(p)
25+
r = "";
2226
end
2327
end
2428

25-
if isstring(p)
26-
r = string(r);
27-
end
2829

2930
end
3031

buildfile.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
cnomex = ~HasTag("exe") & ~HasTag("mex") & ~HasTag("java");
1818
if ispc()
1919
cnomex = cnomex & ~HasTag("unix");
20+
else
21+
cnomex = cnomex & ~HasTag("windows");
2022
end
2123

2224
cmex = HasTag("mex");

test/TestRoot.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function test_root(tc, p)
1313
end
1414

1515
function test_root_dir(tc, rd)
16-
tc.verifyEqual(stdlib.root_dir(rd{1}), rd{2})
16+
tc.verifyEqual(stdlib.root_dir(rd{1}), rd{2}, "root_dir(" + rd{1} + ")")
1717
end
1818

1919
function test_root_name(tc, rn)
@@ -36,7 +36,7 @@ function test_root_name(tc, rn)
3636
{"c:/etc", ""}, ...
3737
{'c:\etc', ''}};
3838

39-
if ispc
39+
if ispc()
4040
p{6}{2} = "c:";
4141
p{7}{2} = "c:/";
4242
p{8}{2} = 'c:\';

0 commit comments

Comments
 (0)