Skip to content

Commit 0fc96d9

Browse files
committed
simplify
1 parent c7686d7 commit 0fc96d9

File tree

5 files changed

+25
-45
lines changed

5 files changed

+25
-45
lines changed

+stdlib/filename.m

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@
1212

1313
p = stdlib.posix(p);
1414

15-
i = strfind(p, "/");
16-
17-
if isempty(i)
18-
f = p;
19-
elseif ischar(p)
20-
f = p(i(end)+1:end);
21-
else
22-
f = extractAfter(p, i(end));
15+
parts = strsplit(p, "/");
16+
17+
f = parts{end};
18+
19+
try %#ok<TRYNC>
20+
f = string(f);
2321
end
2422

2523
end
2624

2725

2826
%!assert (filename('a/b/c.txt'), 'c.txt')
27+
%!assert (filename('a/b/'), '')

+stdlib/is_prefix.m

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,11 @@
1111
pr = stdlib.drop_slash(prefix);
1212
p = stdlib.drop_slash(pth);
1313

14-
if ischar(pr)
15-
w = ~isempty(strfind(p, "..")) || ~isempty(strfind(pr, "..")); %#ok<STREMP>
16-
s = strfind(p, pr) == 1 && (length(p) >= length(pr));
17-
else
18-
w = contains(p, "..") || contains(pr, "..");
19-
s = startsWith(p, pr) && (strlength(p) >= strlength(pr));
20-
end
21-
22-
if ~strcmp(pr, p) && w
23-
warning("is_prefix: %s and/or %s is ambiguous input with '..' consider using stdlib.canonical() first", pr, p)
24-
end
14+
s = startsWith(p, pr) && (stdlib.len(p) >= stdlib.len(pr));
2515

2616
end
2717

2818
%!assert(is_prefix("a", "a"))
2919
%!assert(is_prefix("a", "a/"))
3020
%!assert(is_prefix("a", "a/b"))
21+
%!assert(!is_prefix("a/b/c", "a/b"))

+stdlib/is_subdir.m

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,10 @@
88
dir (1,1) string
99
end
1010

11-
1211
s = stdlib.drop_slash(subdir);
1312
d = stdlib.drop_slash(dir);
1413

15-
if ischar(subdir)
16-
w = ~isempty(strfind(d, "..")) || ~isempty(strfind(s, "..")); %#ok<STREMP,UNRCH>
17-
s = strfind(s, d) == 1 && (length(s) > length(d));
18-
else
19-
w = contains(d, "..") || contains(s, "..");
20-
s = startsWith(s, d) && (strlength(s) > strlength(d));
21-
end
22-
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)
25-
end
14+
s = startsWith(s, d) && (stdlib.len(s) > stdlib.len(d));
2615

2716
end
2817

+stdlib/join.m

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
end
2323

2424
p = b;
25-
if strlength(o)
25+
if stdlib.len(o)
2626
if endsWith(p, "/")
27-
p = p + o;
28-
elseif strlength(p)
29-
p = p + "/" + o;
27+
p = strcat(p, o);
28+
elseif stdlib.len(p)
29+
p = strcat(p, "/", o);
3030
else
3131
p = o;
3232
end
@@ -36,8 +36,8 @@
3636

3737
end
3838

39-
%!assert(join("", "", 1), "")
40-
%!assert(join("", "b", 1), "b")
41-
%!assert(join("a", "", 1), "a")
42-
%!assert(join("a", "b", 1), "a/b")
43-
%!assert(join("a", "/b/c", 1), "/b/c")
39+
%!assert(join("", "",0), "")
40+
%!assert(join("", "b",0), "b")
41+
%!assert(join("a", "",0), "a")
42+
%!assert(join("a", "b",0), "a/b")
43+
%!assert(join("a", "/b/c",0), "/b/c")

test/TestCanonical.m

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
classdef TestCanonical < matlab.unittest.TestCase
22

3-
properties (TestParameter)
4-
p_canonical = ...
5-
{{"", ""}, {"not-exist", "not-exist"}, {"a/../b", "b"}, ...
3+
properties(TestParameter)
4+
use_java = num2cell(unique([stdlib.has_java(), false]))
5+
6+
p = {{"", ""}, {"not-exist", "not-exist"}, {"a/../b", "b"}, ...
67
{"~", stdlib.homedir()}, {"~/", stdlib.homedir()}, ...
78
{"~/..", stdlib.parent(stdlib.homedir())}, ...f
89
{mfilename("fullpath") + ".m/..", stdlib.parent(mfilename("fullpath"))}};
@@ -11,8 +12,8 @@
1112

1213
methods(Test)
1314

14-
function test_canonical(tc, p_canonical)
15-
tc.verifyEqual(stdlib.canonical(p_canonical{1}), p_canonical{2})
15+
function test_canonical(tc, p, use_java)
16+
tc.verifyEqual(stdlib.canonical(p{1}, true, use_java), p{2})
1617
end
1718

1819
function test_static(tc)

0 commit comments

Comments
 (0)