Skip to content

Commit a17439a

Browse files
committed
add root()
1 parent d139e81 commit a17439a

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

+stdlib/root.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function r = root(p, use_java)
2+
arguments
3+
p (1,1) string
4+
use_java (1,1) logical = false
5+
end
6+
7+
if use_java
8+
r = string(java.io.File(p).toPath().getRoot());
9+
else
10+
if ispc
11+
if strlength(p) >= 2 && isletter(extractBetween(p, 1, 1)) && extractBetween(p, 2, 2) == ":"
12+
r = extractBetween(p, 1, 2) + "/";
13+
else
14+
r = "";
15+
end
16+
elseif startsWith(p, "/")
17+
r = "/";
18+
else
19+
r = "";
20+
end
21+
end
22+
23+
end

test/TestFilePure.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,20 @@
2727
"a/b/../../c/../..", "a////b"}
2828
ref_norm = {".", "a/b", "c", "a/c", "c", ".", ...
2929
"..", "a/b"}
30+
31+
in_root
32+
ref_root
3033
end
3134

3235
properties
3336
tobj
3437
end
3538

3639
methods (TestParameterDefinition, Static)
37-
function [base_relative_to, other_relative_to, ref_relative_to, ref_proximate_to] = init_relative_to(classToTest) %#ok<INUSD>
40+
function [base_relative_to, other_relative_to, ref_relative_to, ref_proximate_to, in_root, ref_root] = init_relative_to(classToTest) %#ok<INUSD>
41+
42+
in_root = {"", "a/b", "./a/b", "../a/b", "/etc", "c:/etc"};
43+
ref_root = {"", "", "", "", "/", ""};
3844

3945
if ispc
4046

@@ -49,6 +55,8 @@
4955
ref_proximate_to = ref_relative_to;
5056
ref_proximate_to{end} = other_relative_to{end};
5157

58+
ref_root{end} = "c:/";
59+
5260
else
5361

5462
base_relative_to = {'', '', '/', '/', 'Hello', 'Hello', '/dev/null', '/a/b', 'c', ...
@@ -191,6 +199,13 @@ function test_normalize(tc, in_norm, ref_norm)
191199
end
192200

193201

202+
function test_root(tc, in_root, ref_root)
203+
204+
tc.verifyEqual(stdlib.root(in_root), ref_root)
205+
206+
end
207+
208+
194209
function test_with_suffix(tc)
195210

196211
tc.verifyEqual(stdlib.with_suffix("", ""), "")

0 commit comments

Comments
 (0)