Skip to content

Commit a02a55c

Browse files
committed
most functions require scalar, non-empty input for clarity
1 parent 3fc63c3 commit a02a55c

29 files changed

+67
-78
lines changed

+stdlib/+fileio/absolute_path.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
function abspath = absolute_path(p)
22
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getAbsolutePath()
33
arguments
4-
p string {mustBeScalarOrEmpty}
4+
p (1,1) string
55
end
66

77
% have to expand ~ first (like C++ filesystem::path::absolute)
88
abspath = stdlib.fileio.expanduser(p);
99

10-
if isempty(abspath) || abspath == ""
10+
if abspath == ""
1111
return
1212
end
1313

@@ -21,6 +21,6 @@
2121
abspath = stdlib.fileio.join(pwd, abspath);
2222
end
2323

24-
abspath = stdlib.posix(java.io.File(abspath).getAbsolutePath());
24+
abspath = stdlib.fileio.posix(java.io.File(abspath).getAbsolutePath());
2525

2626
end % function

+stdlib/+fileio/canonical.m

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
function c = canonical(p)
22
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getCanonicalPath()
33
arguments
4-
p string {mustBeScalarOrEmpty}
4+
p (1,1) string
55
end
66

77
% have to expand ~ first (like C++ filesystem::path::absolute)
88
c = stdlib.fileio.expanduser(p);
99

10-
if isempty(c)
11-
return
12-
end
13-
1410
if ispc && startsWith(c, "\\")
1511
% UNC path is not canonicalized
1612
return

+stdlib/+fileio/expanduser.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function e = expanduser(p)
22

33
arguments
4-
p string {mustBeScalarOrEmpty}
4+
p (1,1) string
55
end
66

77
e = p;

+stdlib/+fileio/filename.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function p = filename(path)
22
% FILENAME filename (including suffix) without directory
33
arguments
4-
path string
4+
path (1,1) string
55
end
66

77
% NOT https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getName()

+stdlib/+fileio/is_absolute_path.m

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
function isabs = is_absolute_path(apath)
22
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#isAbsolute()
33
arguments
4-
apath string {mustBeScalarOrEmpty}
5-
end
6-
7-
if isempty(apath)
8-
isabs = logical.empty();
9-
return
4+
apath (1,1) string
105
end
116

127
% expanduser() here to work like C++ filesystem::path::is_absolute()

+stdlib/+fileio/is_exe.m

+1-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#canExecute()
44

55
arguments
6-
file string {mustBeScalarOrEmpty}
7-
end
8-
9-
if isempty(file)
10-
ok = logical.empty;
11-
return
6+
file (1,1) string
127
end
138

149
ok = java.io.File(file).canExecute();

+stdlib/+fileio/is_readable.m

+1-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isReadable(java.nio.file.Path)
44

55
arguments
6-
file string {mustBeScalarOrEmpty}
7-
end
8-
9-
if isempty(file)
10-
ok = logical.empty;
11-
return
6+
file (1,1) string
127
end
138

149

+stdlib/+fileio/is_writable.m

+1-7
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@
33
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isWritable(java.nio.file.Path)
44

55
arguments
6-
file string {mustBeScalarOrEmpty}
6+
file (1,1) string
77
end
88

9-
if isempty(file)
10-
ok = logical.empty;
11-
return
12-
end
13-
14-
159
ok = java.nio.file.Files.isWritable(java.io.File(stdlib.fileio.absolute_path(file)).toPath());
1610

1711
end

+stdlib/+fileio/join.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
function p = join(a, b)
22
%% JOIN: join two paths with posix file separator
33
arguments
4-
a string {mustBeScalarOrEmpty}
5-
b string {mustBeScalarOrEmpty}
4+
a (1,1) string
5+
b (1,1) string
66
end
77

88
p = stdlib.fileio.posix(fullfile(a, b));

+stdlib/+fileio/normalize.m

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22
% normalize(p) remove redundant elements of path p
33
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Path.html#normalize()
44
arguments
5-
p string {mustBeScalarOrEmpty}
5+
p (1,1) string
66
end
77

8-
n = p;
9-
if isempty(p)
10-
return
11-
end
12-
13-
n = stdlib.fileio.posix(java.io.File(stdlib.fileio.expanduser(n)).toPath().normalize());
8+
n = stdlib.fileio.posix(java.io.File(stdlib.fileio.expanduser(p)).toPath().normalize());
149

1510
end

+stdlib/+fileio/parent.m

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
% PARENT parent directory of path
33
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getParent()
44
arguments
5-
path string
5+
path (1,1) string
66
end
77

8-
p = stdlib.fileio.posix(java.io.File(path).getParent());
8+
jp = java.io.File(path).getParent();
9+
if isempty(jp)
10+
jp = "";
11+
end
12+
13+
p = stdlib.fileio.posix(jp);
914

1015
% must drop trailing slash - need to as_posix for windows
1116
%p = fileparts(strip(stdlib.fileio.posix(path), 'right', '/'));

+stdlib/+fileio/resolve.m

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22
% distinct from canonical(), resolve() always returns absolute path
33
% non-existant path is made absolute relative to pwd
44
arguments
5-
p string {mustBeScalarOrEmpty}
5+
p (1,1) string
66
end
77

88
% have to expand ~ first (like C++ filesystem::path::absolute)
99
c = stdlib.fileio.expanduser(p);
1010

11-
if isempty(c)
12-
return
13-
end
14-
1511
if ispc && startsWith(c, "\\")
1612
% UNC path is not canonicalized
1713
return

+stdlib/+fileio/samepath.m

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
function issame = samepath(path1, path2)
22
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isSameFile(java.nio.file.Path,java.nio.file.Path)
33
arguments
4-
path1 string {mustBeScalarOrEmpty}
5-
path2 string {mustBeScalarOrEmpty}
6-
end
7-
8-
if isempty(path1) || isempty(path2)
9-
issame = logical.empty;
10-
return
4+
path1 (1,1) string
5+
path2 (1,1) string
116
end
127

138
% java.nio.file.Files needs CANONICAL -- not just absolute path

+stdlib/+fileio/with_suffix.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function filename = with_suffix(filename, suffix)
22

33
arguments
4-
filename string {mustBeScalarOrEmpty}
4+
filename (1,1) string
55
suffix (1,1) string
66
end
77

+stdlib/absolute_path.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
% * a: absolute path, if determined
1313

1414
arguments
15-
p string {mustBeScalarOrEmpty}
15+
p (1,1) string
1616
end
1717

1818
a = stdlib.fileio.absolute_path(p);

+stdlib/canonical.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
% * c: canonical path, if determined
1616

1717
arguments
18-
p string {mustBeScalarOrEmpty}
18+
p (1,1) string
1919
end
2020

2121
c = stdlib.fileio.canonical(p);

+stdlib/expanduser.m

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
%% expanduser(path)
33
% expands tilde ~ into user home directory
44
%
5-
% Useful for Matlab functions like h5read() and some Computer Vision toolbox functions
6-
% that can't handle ~
5+
% Useful for Matlab functions that can't handle ~
76
%
87
%%% Inputs
98
% * p: path to expand, if tilde present
109
%%% Outputs
1110
% * expanded: expanded path
1211

1312
arguments
14-
p string
13+
p (1,1) string
1514
end
1615

1716
expanded = stdlib.fileio.expanduser(p);

+stdlib/extract_zstd.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function extract_zstd(archive, out_dir)
77

88
arguments
99
archive (1,1) string {mustBeFile}
10-
out_dir (1,1) string
10+
out_dir (1,1) string {mustBeFolder}
1111
end
1212

1313

+stdlib/filename.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function p = filename(path)
22
% FILENAME filename (including suffix) without directory
33
arguments
4-
path string
4+
path (1,1) string
55
end
66

77
p = stdlib.fileio.filename(path);

+stdlib/is_absolute_path.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
% os.path.isabs("/foo/../bar") == True
77

88
arguments
9-
apath string {mustBeScalarOrEmpty}
9+
apath (1,1) string
1010
end
1111

1212
isabs = stdlib.fileio.is_absolute_path(apath);

+stdlib/is_exe.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
% * ok: boolean logical
99

1010
arguments
11-
file string {mustBeScalarOrEmpty}
11+
file (1,1) string
1212
end
1313

1414
ok = stdlib.fileio.is_exe(file);

+stdlib/join.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
function p = join(a, b)
22
%% JOIN: join two paths with posix file separator
33
arguments
4-
a string {mustBeScalarOrEmpty}
5-
b string {mustBeScalarOrEmpty}
4+
a (1,1) string
5+
b (1,1) string
66
end
77

88
p = stdlib.fileio.join(a, b);

+stdlib/normalize.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
% * c: normalized path
99

1010
arguments
11-
p string {mustBeScalarOrEmpty}
11+
p (1,1) string
1212
end
1313

1414
c = stdlib.fileio.normalize(p);

+stdlib/parent.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function p = parent(path)
22
% PARENT parent of path
33
arguments
4-
path string
4+
path (1,1) string
55
end
66

77
p = stdlib.fileio.parent(path);

+stdlib/posix.m

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
function ppath = posix(file)
22
%% posix(file)
3-
% convert a path or sequence of path components to a Posix path separated
4-
% with "/" even on Windows.
3+
% convert a path to a Posix path separated with "/" even on Windows.
54
% If Windows path also have escaping "\" this breaks
65
arguments
76
file string

+stdlib/resolve.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
% * c: resolved path
1616

1717
arguments
18-
p string {mustBeScalarOrEmpty}
18+
p (1,1) string
1919
end
2020

2121
c = stdlib.fileio.resolve(p);

+stdlib/samepath.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
%%% Outputs
88
% issame: logical
99
arguments
10-
path1 string {mustBeScalarOrEmpty}
11-
path2 string {mustBeScalarOrEmpty}
10+
path1 (1,1) string
11+
path2 (1,1) string
1212
end
1313

1414
issame = stdlib.fileio.samepath(path1, path2);

+stdlib/with_suffix.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
%%% Outputs
88
% * filename: modified filename
99
arguments
10-
filename string
10+
filename (1,1) string
1111
suffix (1,1) string
1212
end
1313

test/TestFilePure.m

+25
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,31 @@ function test_is_absolute_path(tc)
9494
tc.verifyFalse(stdlib.is_absolute_path("c"))
9595
end
9696

97+
function test_absolute_path(tc)
98+
import matlab.unittest.constraints.StartsWithSubstring
99+
100+
tc.verifyEqual(stdlib.absolute_path(""), "")
101+
102+
pabs = stdlib.absolute_path('2foo');
103+
pabs2 = stdlib.absolute_path('4foo');
104+
tc.verifyThat(pabs, ~StartsWithSubstring("2"))
105+
tc.verifyTrue(strncmp(pabs, pabs2, 2))
106+
107+
par1 = stdlib.absolute_path("../2foo");
108+
tc.verifyNotEmpty(par1)
109+
110+
par2 = stdlib.absolute_path("../4foo");
111+
tc.verifyTrue(strncmp(par2, pabs2, 2))
112+
113+
pt1 = stdlib.absolute_path("bar/../2foo");
114+
tc.verifyNotEmpty(pt1)
115+
116+
va = stdlib.absolute_path("2foo");
117+
vb = stdlib.absolute_path("4foo");
118+
tc.verifyThat(va, ~StartsWithSubstring("2"))
119+
tc.verifyTrue(strncmp(va, vb, 2))
120+
121+
end
97122

98123
function test_normalize(tc)
99124

0 commit comments

Comments
 (0)