Skip to content

Commit c830d03

Browse files
committed
root* handle more effeciently
1 parent 65f2c8c commit c830d03

2 files changed

Lines changed: 7 additions & 21 deletions

File tree

+stdlib/root_dir.m

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
p {mustBeTextScalar}
1414
end
1515

16-
c = char(p);
1716
r = '';
1817

19-
if isempty(c)
20-
% pass
21-
elseif ismember(c(1), {'/', filesep})
22-
r = c(1);
18+
if stdlib.strempty(p)
19+
r = p;
20+
elseif ismember(p(1), {'/', filesep})
21+
r = extractBefore(p, 2);
2322
elseif ispc()
24-
m = regexp(c, '^[A-Za-z]:([\\/])', 'tokens', 'once');
23+
m = regexp(p, '^[A-Za-z]:([\\/])', 'tokens', 'once');
2524
if ~isempty(m)
2625
r = m{1};
2726
end
@@ -32,10 +31,3 @@
3231
end
3332

3433
end
35-
36-
%!test
37-
%! if ispc()
38-
%! assert(strcmp(strcmp.root_dir('c:/hi'), '/'))
39-
%! else
40-
%! assert(strcmp(stdlib.root_dir('/a/.bc'), '/'))
41-
%! end

+stdlib/root_name.m

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

13-
c = char(p);
14-
15-
if ispc() && ~isempty(regexp(c, '^[A-Za-z]:', 'once'))
16-
r = c(1:2);
13+
if ispc() && ~isempty(regexp(p, '^[A-Za-z]:', 'once'))
14+
r = extractBefore(p, 3);
1715
else
1816
r = '';
1917
end
2018

21-
if isstring(p)
22-
r = string(r);
23-
end
24-
2519
end

0 commit comments

Comments
 (0)