Skip to content

Commit fc3210e

Browse files
committed
improve path_tail and add tests
1 parent e07094f commit fc3210e

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

+stdlib/+fileio/path_tail.m

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,12 @@
22
% get last part of directory path
33
% if filename, return filename with suffix
44
arguments
5-
apath string
5+
apath (1,:) string
66
end
77

8-
k = strlength(apath) == 0;
9-
final(k) = "";
8+
import stdlib.fileio.absolute_path
109

11-
final(~k) = extractAfter(apath(~k), strlength(apath(~k))-1);
12-
13-
i = final == "/" | final == "\";
14-
15-
apath(i) = extractBefore(apath(i), strlength(apath(i)));
16-
17-
[~, name, ext] = fileparts(apath);
10+
[~, name, ext] = fileparts(absolute_path(apath));
1811

1912
last = append(name, ext);
2013

+stdlib/TestFileio.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ function test_posix(tc)
2525
tc.verifyEmpty(posix(string.empty))
2626
end
2727

28+
function test_path_tail(tc)
29+
30+
import stdlib.fileio.path_tail
31+
32+
tc.verifyEqual(path_tail("/foo/bar/baz"), "baz")
33+
tc.verifyEqual(path_tail("/foo/bar/baz/"), "baz")
34+
tc.verifyEqual(path_tail("/foo/bar/baz/."), "baz")
35+
tc.verifyEqual(path_tail("/foo/bar/baz/.."), "bar")
36+
tc.verifyEqual(path_tail("/foo/bar/baz.txt"), "baz.txt")
37+
38+
end
39+
2840
function test_is_absolute_path(tc)
2941

3042
import stdlib.fileio.is_absolute_path

0 commit comments

Comments
 (0)