Skip to content

Commit a3d34ba

Browse files
committed
add drop_slash(), relative_to and proximate_to don't normalize
1 parent 0981e0d commit a3d34ba

File tree

3 files changed

+32
-34
lines changed

3 files changed

+32
-34
lines changed

+stdlib/join.m

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
11
%% JOIN join two paths with posix file separator
22

3-
function p = join(a, b, use_java)
3+
function p = join(base, other, use_java)
44
arguments
5-
a (1,1) string
6-
b (1,1) string
5+
base (1,1) string
6+
other (1,1) string
77
use_java (1,1) logical = false
88
end
99

10-
if a == "" && b == ""
10+
if base == "" && other == ""
1111
p = "";
1212
return
1313
end
1414

15-
a = stdlib.posix(a);
16-
b = stdlib.posix(b);
15+
b = stdlib.drop_slash(base);
16+
o = stdlib.drop_slash(other);
1717

18-
if a == ""
19-
p = b;
18+
if b == ""
19+
p = o;
2020
return
2121
end
2222

23-
if b == ""
24-
p = a;
23+
if o == ""
24+
p = b;
2525
return
2626
end
2727

2828

2929
if use_java
3030

31-
p = java.io.File(a).toPath().resolve(b);
31+
p = java.io.File(b).toPath().resolve(o);
3232

3333
else
3434

35-
if startsWith(b, "/") || (ispc && stdlib.is_absolute(b))
36-
p = b;
35+
if startsWith(o, "/") || (ispc && stdlib.is_absolute(o))
36+
p = o;
3737
return
3838
end
3939

40-
p = a + "/" + b;
40+
p = b + "/" + o;
4141

4242
end
4343

44-
p = stdlib.normalize(p);
45-
4644
end

+stdlib/relative_to.m

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@
77
end
88

99
% must remove trailing slashes
10-
base = stdlib.normalize(base, true);
11-
other = stdlib.normalize(other, true);
10+
base = stdlib.drop_slash(base);
11+
other = stdlib.drop_slash(other);
1212

1313
if base == other
1414
r = ".";
15-
else
16-
b = java.io.File(base).toPath();
17-
o = java.io.File(other).toPath();
18-
try
19-
r = stdlib.posix(b.relativize(o));
20-
catch e
21-
if contains(e.message, 'java.lang.IllegalArgumentException')
22-
r = "";
23-
else
24-
rethrow(e);
25-
end
15+
return
16+
end
17+
18+
b = java.io.File(base).toPath();
19+
o = java.io.File(other).toPath();
20+
try
21+
r = stdlib.posix(b.relativize(o));
22+
catch e
23+
if contains(e.message, 'java.lang.IllegalArgumentException')
24+
r = "";
25+
else
26+
rethrow(e);
2627
end
2728
end
2829

test/TestFilePure.m

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
{"", "/", "/"}, ...
2727
{"a", "b//", "a/b"}, ...
2828
{"a//", "b//", "a/b"}, ...
29-
{"a/b/../", "c/d/../", "a/c"}, ...
30-
{"a/b", "..", "a"}, ...
29+
{"a/b/../", "c/d/../", "a/b/../c/d/.."}, ...
30+
{"a/b", "..", "a/b/.."}, ...
3131
{"a/b", "c/d", "a/b/c/d"}, ...
3232
{"ab/cd", "/ef", "/ef"} ...
3333
};
@@ -61,7 +61,7 @@
6161
{'Hello', 'Hello', '.'}, ...
6262
{'Hello', 'Hello/', '.'}, ...
6363
{'./this/one', './this/two', '../two'}, ...
64-
{'/path/same', '/path/same/hi/..', '.'}, ...
64+
{'/path/same', '/path/same/hi/..', 'hi/..'}, ...
6565
{'', '/', ''}, ...
6666
{'/', '', ''}, ...
6767
{'/', '/', '.'}, ...
@@ -71,8 +71,7 @@
7171
{'/a/b', '/a/b', '.'}, ...
7272
{'/a/b', '/a', '..'}, ...
7373
{'/a/b/c/d', '/a/b', '../..'}, ...
74-
{'/this/one', '/this/two', '../two'}, ...
75-
{'/path/same', '/path/same/hi/..', '.'}};
74+
{'/this/one', '/this/two', '../two'}};
7675

7776
p_proximate_to = p_relative_to;
7877

0 commit comments

Comments
 (0)