Skip to content

Commit 6f4b188

Browse files
committed
is_subdir: drop duplicated slashes. add test cases
1 parent ae7ed4d commit 6f4b188

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

+stdlib/is_subdir.m

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
%% IS_SUBDIR is subdir a subdirectory of dir?
2+
% canonicalization and normalization are NOT performed
3+
% duplicated slashes are dropped
24

35
function s = is_subdir(subdir, dir)
46
arguments
@@ -7,16 +9,19 @@
79
end
810

911

12+
s = stdlib.drop_slash(subdir);
13+
d = stdlib.drop_slash(dir);
14+
1015
if ischar(subdir)
11-
w = ~isempty(strfind(dir, "..")) || ~isempty(strfind(subdir, "..")); %#ok<STREMP,UNRCH>
12-
s = strfind(subdir, dir) == 1 && (length(subdir) > length(dir));
16+
w = ~isempty(strfind(d, "..")) || ~isempty(strfind(s, "..")); %#ok<STREMP,UNRCH>
17+
s = strfind(s, d) == 1 && (length(s) > length(d));
1318
else
14-
w = contains(dir, "..") || contains(subdir, "..");
15-
s = startsWith(subdir, dir) && (strlength(subdir) > strlength(dir));
19+
w = contains(d, "..") || contains(s, "..");
20+
s = startsWith(s, d) && (strlength(s) > strlength(d));
1621
end
1722

18-
if ~strcmp(subdir, dir) && w
19-
warning("is_subdir: %s and/or %s is ambiguous input with '..' consider using stdlib.canonical() first", subdir, dir)
23+
if ~strcmp(s, d) && w
24+
warning("is_subdir: %s and/or %s is ambiguous input with '..' consider using stdlib.canonical() first", s, d)
2025
end
2126

2227
end
@@ -27,5 +32,6 @@
2732
%!assert(!is_subdir("/a/b", "d"))
2833
%!assert(is_subdir("a/b", "a"))
2934
%!assert(!is_subdir("a", "a/.c"))
35+
%!assert(!is_subdir("a/./b/c", "a/b"))
3036

3137
% this is incorrect on Windows at least %assert(is_subdir("a/b", "a/b/.."))

+stdlib/relative_to.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
b1 = stdlib.drop_slash(base);
1111
o1 = stdlib.drop_slash(other);
1212

13+
b1 = strrep(b1, "/./", "/");
14+
o1 = strrep(o1, "/./", "/");
15+
1316
if strcmp(b1, o1)
1417
r = ".";
1518
return

test/TestFilePure.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
p_relative_to = {{"", "", "."}, ...
8181
{"Hello", "Hello", "."}, ...
8282
{"Hello", "Hello/", "."}, ...
83+
{"a/./b", "a/b", "."}, ...
84+
{"a/b", "a/./b", "."}, ...
8385
{"./this/one", "./this/two", "../two"}, ...
8486
{"/path/same", "/path/same/hi/..", "hi/.."}, ...
8587
{"", "/", ""}, ...
@@ -113,9 +115,9 @@
113115
p_proximate_to = p_relative_to;
114116
% NOTE: ".." in proximate_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc
115117

116-
p_proximate_to{6}{3} = "/";
117-
p_proximate_to{10}{3} = "c";
118-
p_proximate_to{11}{3} = "/a/b";
118+
p_proximate_to{8}{3} = "/";
119+
p_proximate_to{12}{3} = "c";
120+
p_proximate_to{13}{3} = "/a/b";
119121

120122
p_proximate_to{end}{3} = "d:/path";
121123

@@ -152,6 +154,10 @@
152154

153155
p_is_subdir = {
154156
{"a/b", "a/b", false}, ...
157+
{"a//b/c", "a/b", false}, ...
158+
{"a/b", "a//b", false}, ...
159+
{"a/./b/c", "a/b", false}, ...
160+
{"a/b/c", "a/./b", true}, ...
155161
{"a/b", "a/b/", false}, ...
156162
{"a/b", "a", true}, ...
157163
{"a/.c", "a", true}
@@ -267,7 +273,7 @@ function test_with_suffix(tc)
267273

268274
function test_relative_to(tc, p_relative_to)
269275
tc.assumeTrue(stdlib.has_java)
270-
tc.verifyEqual(stdlib.relative_to(p_relative_to{1}, p_relative_to{2}), p_relative_to{3})
276+
tc.verifyEqual(stdlib.relative_to(p_relative_to{1}, p_relative_to{2}), p_relative_to{3}, "relative_to(" + p_relative_to{1} + "," + p_relative_to{2}+")")
271277
end
272278

273279

0 commit comments

Comments
 (0)