Skip to content

Commit 252169f

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

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,25 @@
88
methods (Test)
99

1010
function test_posix(tc)
11-
import matlab.unittest.constraints.ContainsSubstring
1211

12+
tc.verifyEqual(stdlib.posix(''), '')
1313
tc.verifyEqual(stdlib.posix(""), "")
1414

1515
if ispc
16-
tc.verifyThat(stdlib.posix("c:\foo"), ~ContainsSubstring("\"))
16+
tc.verifyEqual(stdlib.posix("c:\abc"), "c:/abc")
1717
end
1818
end
1919

20+
21+
function test_drop_slash(tc)
22+
tc.verifyEqual(stdlib.drop_slash(''), '')
23+
tc.verifyEqual(stdlib.drop_slash('/'), '/')
24+
tc.verifyEqual(stdlib.drop_slash('a//b'), 'a/b')
25+
tc.verifyEqual(stdlib.drop_slash('a//b/'), 'a/b')
26+
tc.verifyEqual(stdlib.drop_slash("a//b/c"), "a/b/c")
27+
28+
end
29+
2030
function test_root_name(tc, p)
2131
tc.verifyEqual(stdlib.root_name(p{1}), p{2})
2232
end

test/TestParent.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ 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/\.\."}, ...

0 commit comments

Comments
 (0)