File tree Expand file tree Collapse file tree 6 files changed +99
-43
lines changed Expand file tree Collapse file tree 6 files changed +99
-43
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 ;
15-
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 ) == ' /' ;
25- end
14+ y = strlength(stdlib .root_dir(p )) > 0 ;
2615
2716end
2817
2918% !assert(is_absolute(''), false)
3019% !test
31- % ! if ispc
20+ % ! if ispc()
3221% ! assert(is_absolute('C:\'))
3322% ! assert(is_absolute('C:/'))
3423% ! 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+
18+ if isunix()
19+ if strncmp(p , ' /' , 1 )
20+ r = ' /' ;
21+ end
22+ elseif strlength(p ) >= 2 && strlength(stdlib .root_name(p ))
23+ p = char(p );
24+ if any(p(3 ) == [' /' , filesep ])
25+ r = p(3 );
26+ end
27+ end
28+
29+ if isstring(p )
30+ r = string(r );
31+ end
32+
33+ 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:" , " " }, ...
2336{" c:/etc" , " " }};
@@ -28,3 +41,28 @@ function test_root(tc, p)
2841end
2942
3043end
44+
45+
46+ function rn = init_root_name()
47+
48+ rn = init_root();
49+
50+ rn{4 }{2 } = " " ;
51+ rn{5 }{2 } = ' ' ;
52+
53+ if ispc()
54+ rn{6 }{2 } = " c:" ;
55+ end
56+
57+ end
58+
59+
60+ function rd = init_root_dir()
61+
62+ rd = init_root();
63+
64+ if ispc()
65+ rd{6 }{2 } = " /" ;
66+ end
67+
68+ end
You can’t perform that action at this time.
0 commit comments