Skip to content

Commit 5fc2df9

Browse files
committed
drop_slash: no input coerce
1 parent 5dea3e1 commit 5fc2df9

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

+stdlib/drop_slash.m

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,32 @@
55

66
function d = drop_slash(p)
77
arguments
8-
p (1,1) string
8+
p {mustBeTextScalar}
99
end
1010

1111
s = stdlib.posix(p);
1212

13-
uncslash = ispc && startsWith(s, "//");
13+
uncslash = ispc && startsWith(s, '//');
1414

1515
% drop repeated slashes inside string
16-
d = regexprep(s, "/+", "/");
16+
d = regexprep(s, '/+', '/');
1717

1818
L = strlength(d);
1919

2020
if L < 2
2121
if uncslash
22-
d = "//";
22+
d = '//';
2323
end
24-
return;
24+
elseif ~ispc || (L ~= 3 || ~strcmp(d, stdlib.root(s)))
25+
d = regexprep(d, '/$', '');
2526
end
2627

27-
if ~ispc || (L ~= 3 || ~strcmp(d, stdlib.root(s)))
28-
if ischar(s)
29-
if d(end) == '/'
30-
d = d(1:end-1);
31-
end
32-
else
33-
d = strip(d, "right", "/");
34-
end
28+
if uncslash
29+
d = strcat('/', d);
3530
end
3631

37-
if uncslash
38-
d = strcat("/", d);
32+
if isstring(p)
33+
d = string(d);
3934
end
4035

4136
end

test/TestFilePure.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@
22

33
properties (TestParameter)
44
p = init_root_name()
5+
pds = {{'/', '/'}, {'a//b', 'a/b'}, {'a//b/', 'a/b'}, {'a//b/c', 'a/b/c'}};
56
end
67

78

89
methods (Test)
910

1011
function test_posix(tc)
11-
import matlab.unittest.constraints.ContainsSubstring
1212

13+
tc.verifyEqual(stdlib.posix(''), '')
1314
tc.verifyEqual(stdlib.posix(""), "")
1415

1516
if ispc
16-
tc.verifyThat(stdlib.posix("c:\foo"), ~ContainsSubstring("\"))
17+
tc.verifyEqual(stdlib.posix("c:\abc"), "c:/abc")
1718
end
1819
end
1920

21+
22+
function test_drop_slash(tc, pds)
23+
import matlab.unittest.constraints.Matches
24+
tc.verifyThat(stdlib.drop_slash(pds{1}), Matches(pds{2}))
25+
end
26+
2027
function test_root_name(tc, p)
2128
tc.verifyEqual(stdlib.root_name(p{1}), p{2})
2229
end

test/TestParent.m

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,19 @@ function test_parent(tc, p)
2424
{"a/", "\."}, ...
2525
{"a/b", "a"}, ...
2626
{'a/b/', 'a'}, ...
27+
{'a//b', 'a'}, ...
2728
{"ab/.parent", "ab"}, ...
2829
{"ab/.parent.txt", "ab"}, ...
2930
{"a/b/../.parent.txt", "a/b/\.\."}, ...
30-
{"a/////b////c", "a/b"}, ...
31-
{"c:/", "."}, ...
32-
{"c:\", "."}, ...
33-
{"c:/a/b", "c:/a"}, ...
34-
{"c:\a/b", "c:\\a"}
35-
};
31+
{"a/////b////c", "a/b"}};
3632

3733
if ispc
38-
p{12}{2} = "c:/";
39-
p{13}{2} = "c:/";
40-
p{14}{2} = "c:/a";
41-
p{15}{2} = "c:/a";
42-
p{end+1} = {"c:/a", "c:/"};
34+
p{end+1} = {"c:/", "c:/"};
35+
p{end+1} = {"c:\", "c:/"};
36+
p{end+1} = {"c:/a/b", "c:/a"};
37+
p{end+1} = {"c:\a/b", "c:/a"};
38+
p{end+1} = {"c:/a", "c:/"};
4339
p{end+1} = {"c:", "c:/"};
44-
4540
end
4641

4742
end

0 commit comments

Comments
 (0)