File tree Expand file tree Collapse file tree 6 files changed +108
-44
lines changed Expand file tree Collapse file tree 6 files changed +108
-44
lines changed Original file line number Diff line number Diff line change 66
77function y = is_absolute(p )
88arguments
9- p ( 1 , : ) char
9+ p { mustBeTextScalar }
1010end
1111
1212% not Octave is_absolute_filename() because this is a stricter check for "c:" false
1313
14- y = false ;
14+ y = strlength( stdlib .root_dir( p )) > 0 ;
1515
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 ) == ' /' ;
16+ if ispc()
17+ y = y && strlength(stdlib .root_name(p )) > 0 ;
2518end
2619
2720end
2821
2922% !assert(is_absolute(''), false)
3023% !test
31- % ! if ispc
24+ % ! if ispc()
3225% ! assert(is_absolute('C:\'))
3326% ! assert(is_absolute('C:/'))
3427% ! assert(!is_absolute('C:'))
Original file line number Diff line number Diff line change 66 other {mustBeTextScalar }
77end
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+
2737end
2838
2939end
Original file line number Diff line number Diff line change 11%% ROOT get root path
22% ROOT(P) returns the root path of P.
3- % root is the root_name + root_directory.
4-
53
64function r = root(p )
75arguments
86 p {mustBeTextScalar }
97end
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
2511end
2612
Original file line number Diff line number Diff line change 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+ pc = char(p );
18+
19+ if startsWith(p , {' /' , filesep })
20+ r = pc(1 );
21+ elseif strlength(p ) > 2 && strlength(stdlib .root_name(p ))
22+ if any(pc(3 ) == [' /' , filesep ])
23+ r = pc(3 );
24+ end
25+ end
26+
27+ if isstring(p )
28+ r = string(r );
29+ end
30+
31+ end
Original file line number Diff line number Diff line change 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}
1818end
1919
Original file line number Diff line number Diff line change 22
33properties (TestParameter )
44p = init_root()
5+ rn = init_root_name()
6+ rd = init_root_dir()
57end
68
79methods (Test )
10+
811function test_root(tc , p )
912tc .verifyEqual(stdlib .root(p{1 }), p{2 })
1013end
14+
15+ function test_root_dir(tc , rd )
16+ tc .verifyEqual(stdlib .root_dir(rd{1 }), rd{2 })
1117end
1218
19+ function test_root_name(tc , rn )
20+ tc .verifyEqual(stdlib .root_name(rn{1 }), rn{2 })
1321end
1422
23+ end
24+
25+ end % class
26+
1527
1628function p = init_root()
1729
1830p = {{" " , " " }, ...
1931{" a/b" , " " }, ...
2032{" ./a/b" , " " }, ...
33+ {" /etc" , " /" }, ...
2134{' /etc' , ' /' }, ...
2235{" c:" , " " }, ...
23- {" c:/etc" , " " }};
36+ {" c:/etc" , " " }, ...
37+ {' c:\etc' , ' ' }};
2438
2539if ispc
26- p{5 }{2 } = " c:" ;
27- p{6 }{2 } = " c:/" ;
40+ p{6 }{2 } = " c:" ;
41+ p{7 }{2 } = " c:/" ;
42+ p{8 }{2 } = ' c:\' ;
43+ end
44+
45+ end
46+
47+
48+ function rn = init_root_name()
49+
50+ rn = init_root();
51+
52+ rn{4 }{2 } = " " ;
53+ rn{5 }{2 } = ' ' ;
54+
55+ if ispc()
56+ rn{6 }{2 } = " c:" ;
57+ rn{7 }{2 } = " c:" ;
58+ rn{8 }{2 } = ' c:' ;
59+ end
60+
61+ end
62+
63+
64+ function rd = init_root_dir()
65+
66+ rd = init_root();
67+
68+ if ispc()
69+ rd{6 }{2 } = " " ;
70+ rd{7 }{2 } = " /" ;
71+ rd{8 }{2 } = ' \' ;
2872end
2973
3074end
You can’t perform that action at this time.
0 commit comments