Skip to content

Commit b2401cb

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

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
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/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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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)