Skip to content

Commit 019b8ab

Browse files
committed
add drop_slash(), relative_to and proximate_to don't normalize
1 parent ce7ca9b commit 019b8ab

File tree

4 files changed

+54
-34
lines changed

4 files changed

+54
-34
lines changed

+stdlib/drop_slash.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
%% DROP_SLASH drop repeated and trailing slash
2+
3+
function d = drop_slash(p)
4+
arguments
5+
p (1,1) string
6+
end
7+
8+
d = stdlib.posix(p);
9+
10+
% drop repeated slashes inside string
11+
d = regexprep(d, "/+", "/");
12+
13+
if d == "/"
14+
return;
15+
end
16+
17+
if ~ispc || (strlength(d) ~= 3 || d ~= stdlib.root(p))
18+
d = strip(d, "right", "/");
19+
end
20+
21+
end

+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: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@
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+
disp('ow')
25+
r = "";
26+
else
27+
rethrow(e);
2628
end
2729
end
2830

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)