Skip to content

Commit 64ca9c4

Browse files
committed
relative_to: simplify
1 parent dffe14b commit 64ca9c4

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

+stdlib/canonical.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@
6161

6262
%!assert(canonical("", 0,0), "")
6363
%!assert(canonical("~",1,0), homedir())
64+
%!assert(canonical("a/b/..",0,0), "a")

+stdlib/normalize.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
if stdlib.isoctave()
1818
n = stdlib.posix(javaObject("java.io.File", p).toPath().normalize().toString());
1919
elseif use_java
20-
n = stdlib.posix(java.io.File(p).toPath().normalize());
20+
n = stdlib.posix(java.io.File(p).toPath().normalize().toString());
2121
else
2222

2323
n = stdlib.posix(p);

+stdlib/proximate_to.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
end
88

99
r = stdlib.relative_to(base, other);
10-
if strlength(r) > 0
10+
if stdlib.len(r) > 0
1111
return;
1212
end
1313

1414
r = other;
1515

1616
end
17+
18+
%!assert(proximate_to("/a/b", "/a/b"), ".")
19+
%!assert(proximate_to("/a/b", "/a/b/c"), "c")
20+
%!assert(proximate_to("/a/b", "/a/b/c/"), "c")
21+
%!assert(proximate_to("/a/b", "d"), "d")

+stdlib/relative_to.m

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,37 @@
1010
base = stdlib.drop_slash(base);
1111
other = stdlib.drop_slash(other);
1212

13-
if base == other
13+
if strcmp(base, other)
1414
r = ".";
1515
return
1616
end
1717

18-
b = java.io.File(base).toPath();
19-
o = java.io.File(other).toPath();
18+
if stdlib.isoctave()
19+
b = javaObject("java.io.File", base).toPath();
20+
o = javaObject("java.io.File", other).toPath();
21+
else
22+
b = java.io.File(base).toPath();
23+
o = java.io.File(other).toPath();
24+
end
25+
2026
try
21-
r = stdlib.posix(b.relativize(o));
27+
r = stdlib.posix(b.relativize(o).toString());
2228
catch e
23-
if contains(e.message, 'java.lang.IllegalArgumentException')
24-
r = "";
29+
r = "";
30+
if stdlib.isoctave()
31+
if isempty(strfind(e.message, "'other' is different type of Path")) && isempty(strfind(e.message, "'other' has different root"))
32+
rethrow(e);
33+
end
2534
else
26-
rethrow(e);
35+
if ~any(contains(e.message, ["'other' is different type of Path", "'other' has different root"]))
36+
rethrow(e);
37+
end
2738
end
2839
end
2940

3041
end
42+
43+
%!assert(relative_to("/a/b", "/a/b"), ".")
44+
%!assert(relative_to("/a/b", "/a/b/c"), "c")
45+
%!assert(relative_to("/a/b", "/a/b/c/"), "c")
46+
%!assert(relative_to("/a/b", "d"), "")

+stdlib/root.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
r = stdlib.posix(o.toString());
2222
end
2323
elseif use_java
24-
r = stdlib.posix(java.io.File(p).toPath().getRoot());
24+
r = stdlib.posix(java.io.File(p).toPath().getRoot().toString());
2525
else
2626

2727
r = stdlib.root_name(p);

0 commit comments

Comments
 (0)