Skip to content

Commit 454e0ee

Browse files
committed
canonical: simplify with builtin
1 parent 188b8c9 commit 454e0ee

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

+stdlib/canonical.m

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,16 @@
1616
p {mustBeTextScalar}
1717
end
1818

19-
c = "";
20-
21-
if ~strlength(p) || (ispc() && (startsWith(p, {'\\', '//'})))
22-
% UNC path is not canonicalized
23-
return
24-
end
25-
26-
if stdlib.isoctave()
27-
% empty if any component of path does not exist
28-
c = canonicalize_file_name(p);
19+
if strempty(p)
20+
c = '';
2921
else
30-
% errors if any component of path does not exist.
31-
% disp("builtin")
32-
try %#ok<TRYNC>
33-
c = builtin('_canonicalizepath', p);
34-
end
35-
end
36-
37-
c = stdlib.posix(c);
22+
[s, r] = fileattrib(p);
3823

39-
if ~strlength(c)
40-
c = stdlib.normalize(p);
24+
if s == 1
25+
c = stdlib.posix(r.Name);
26+
else
27+
c = stdlib.normalize(p);
28+
end
4129
end
4230

4331
if isstring(p)
@@ -47,4 +35,4 @@
4735
end
4836

4937
%!assert(canonical(""), "")
50-
%!assert(canonical("~"), homedir())
38+
%!assert(canonical("."), pwd())

+stdlib/private/strempty.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
%% STREMPTY is the char / string empty
2+
3+
function y = strempty(s)
4+
arguments
5+
s {mustBeTextScalar}
6+
end
7+
8+
y = strlength(s) == 0;
9+
10+
end

test/TestCanonical.m

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

33
properties(TestParameter)
4-
p = {{'', ""}, ...
4+
p = {{'', ''}, ...
55
{"", ""}, ...
66
{"not-exist", "not-exist"}, ...
77
{"a/../b", "b"}, ...
8-
{mfilename("fullpath") + ".m/..", string(stdlib.parent(mfilename("fullpath")))}, ...
8+
{strcat(mfilename("fullpath"), '.m/..'), stdlib.parent(mfilename("fullpath"))}, ...
99
{"not-exist/a/..", "not-exist"}, ...
1010
{"./not-exist", "not-exist"}, ...
1111
{"../not-exist", "../not-exist"}

0 commit comments

Comments
 (0)