Skip to content

Commit fbca1b2

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

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
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: 8 additions & 11 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,19 @@
1313
p {mustBeTextScalar}
1414
end
1515

16-
matches = regexp(p, '^([/\\])|^[A-Za-z]:([/\\])', 'tokens');
16+
sep = characterListPattern("/" + filesep());
1717

18-
if isempty(matches)
18+
pat = (textBoundary + sep) | ...
19+
(lookBehindBoundary(lettersPattern(1) + ":") + sep);
1920

21+
r = extract(p, pat);
22+
if isempty(r)
2023
r = '';
2124
if isstring(p)
2225
r = "";
2326
end
24-
25-
else
26-
27-
r = matches{1};
28-
if iscell(r)
29-
r = r{1};
30-
end
31-
27+
elseif iscell(r)
28+
r = r{1};
3229
end
3330

3431
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

test/TestRoot.m

Lines changed: 1 addition & 1 deletion
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)

0 commit comments

Comments
 (0)