File tree Expand file tree Collapse file tree 4 files changed +20
-25
lines changed Expand file tree Collapse file tree 4 files changed +20
-25
lines changed Original file line number Diff line number Diff line change 22% ROOT(P) returns the root path of P.
33
44function r = root(p )
5- arguments
6- p {mustBeTextScalar }
7- end
85
96r = strcat(stdlib .root_name(p ), stdlib .root_dir(p ));
107
Original file line number Diff line number Diff line change 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 ''
1313 p {mustBeTextScalar }
1414end
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 };
3229end
3330
3431end
Original file line number Diff line number Diff line change 1010 p {mustBeTextScalar }
1111end
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
1718else
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
2327end
2428
25- if isstring(p )
26- r = string(r );
27- end
2829
2930end
3031
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ function test_root(tc, p)
1313end
1414
1515function 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 } + " ) " )
1717end
1818
1919function test_root_name(tc , rn )
You can’t perform that action at this time.
0 commit comments