Skip to content

Commit

Permalink
most functions require scalar, non-empty input for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Mar 1, 2024
1 parent 3fc63c3 commit a02a55c
Show file tree
Hide file tree
Showing 29 changed files with 67 additions and 78 deletions.
6 changes: 3 additions & 3 deletions +stdlib/+fileio/absolute_path.m
@@ -1,13 +1,13 @@
function abspath = absolute_path(p)
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getAbsolutePath()
arguments
p string {mustBeScalarOrEmpty}
p (1,1) string
end

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

if isempty(abspath) || abspath == ""
if abspath == ""
return
end

Expand All @@ -21,6 +21,6 @@
abspath = stdlib.fileio.join(pwd, abspath);
end

abspath = stdlib.posix(java.io.File(abspath).getAbsolutePath());
abspath = stdlib.fileio.posix(java.io.File(abspath).getAbsolutePath());

end % function
6 changes: 1 addition & 5 deletions +stdlib/+fileio/canonical.m
@@ -1,16 +1,12 @@
function c = canonical(p)
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getCanonicalPath()
arguments
p string {mustBeScalarOrEmpty}
p (1,1) string
end

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

if isempty(c)
return
end

if ispc && startsWith(c, "\\")
% UNC path is not canonicalized
return
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/+fileio/expanduser.m
@@ -1,7 +1,7 @@
function e = expanduser(p)

arguments
p string {mustBeScalarOrEmpty}
p (1,1) string
end

e = p;
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/+fileio/filename.m
@@ -1,7 +1,7 @@
function p = filename(path)
% FILENAME filename (including suffix) without directory
arguments
path string
path (1,1) string
end

% NOT https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getName()
Expand Down
7 changes: 1 addition & 6 deletions +stdlib/+fileio/is_absolute_path.m
@@ -1,12 +1,7 @@
function isabs = is_absolute_path(apath)
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#isAbsolute()
arguments
apath string {mustBeScalarOrEmpty}
end

if isempty(apath)
isabs = logical.empty();
return
apath (1,1) string
end

% expanduser() here to work like C++ filesystem::path::is_absolute()
Expand Down
7 changes: 1 addition & 6 deletions +stdlib/+fileio/is_exe.m
Expand Up @@ -3,12 +3,7 @@
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#canExecute()

arguments
file string {mustBeScalarOrEmpty}
end

if isempty(file)
ok = logical.empty;
return
file (1,1) string
end

ok = java.io.File(file).canExecute();
Expand Down
7 changes: 1 addition & 6 deletions +stdlib/+fileio/is_readable.m
Expand Up @@ -3,12 +3,7 @@
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isReadable(java.nio.file.Path)

arguments
file string {mustBeScalarOrEmpty}
end

if isempty(file)
ok = logical.empty;
return
file (1,1) string
end


Expand Down
8 changes: 1 addition & 7 deletions +stdlib/+fileio/is_writable.m
Expand Up @@ -3,15 +3,9 @@
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isWritable(java.nio.file.Path)

arguments
file string {mustBeScalarOrEmpty}
file (1,1) string
end

if isempty(file)
ok = logical.empty;
return
end


ok = java.nio.file.Files.isWritable(java.io.File(stdlib.fileio.absolute_path(file)).toPath());

end
4 changes: 2 additions & 2 deletions +stdlib/+fileio/join.m
@@ -1,8 +1,8 @@
function p = join(a, b)
%% JOIN: join two paths with posix file separator
arguments
a string {mustBeScalarOrEmpty}
b string {mustBeScalarOrEmpty}
a (1,1) string
b (1,1) string
end

p = stdlib.fileio.posix(fullfile(a, b));
Expand Down
9 changes: 2 additions & 7 deletions +stdlib/+fileio/normalize.m
Expand Up @@ -2,14 +2,9 @@
% normalize(p) remove redundant elements of path p
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Path.html#normalize()
arguments
p string {mustBeScalarOrEmpty}
p (1,1) string
end

n = p;
if isempty(p)
return
end

n = stdlib.fileio.posix(java.io.File(stdlib.fileio.expanduser(n)).toPath().normalize());
n = stdlib.fileio.posix(java.io.File(stdlib.fileio.expanduser(p)).toPath().normalize());

end
9 changes: 7 additions & 2 deletions +stdlib/+fileio/parent.m
Expand Up @@ -2,10 +2,15 @@
% PARENT parent directory of path
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getParent()
arguments
path string
path (1,1) string
end

p = stdlib.fileio.posix(java.io.File(path).getParent());
jp = java.io.File(path).getParent();
if isempty(jp)
jp = "";
end

p = stdlib.fileio.posix(jp);

% must drop trailing slash - need to as_posix for windows
%p = fileparts(strip(stdlib.fileio.posix(path), 'right', '/'));
Expand Down
6 changes: 1 addition & 5 deletions +stdlib/+fileio/resolve.m
Expand Up @@ -2,16 +2,12 @@
% distinct from canonical(), resolve() always returns absolute path
% non-existant path is made absolute relative to pwd
arguments
p string {mustBeScalarOrEmpty}
p (1,1) string
end

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

if isempty(c)
return
end

if ispc && startsWith(c, "\\")
% UNC path is not canonicalized
return
Expand Down
9 changes: 2 additions & 7 deletions +stdlib/+fileio/samepath.m
@@ -1,13 +1,8 @@
function issame = samepath(path1, path2)
% 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)
arguments
path1 string {mustBeScalarOrEmpty}
path2 string {mustBeScalarOrEmpty}
end

if isempty(path1) || isempty(path2)
issame = logical.empty;
return
path1 (1,1) string
path2 (1,1) string
end

% java.nio.file.Files needs CANONICAL -- not just absolute path
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/+fileio/with_suffix.m
@@ -1,7 +1,7 @@
function filename = with_suffix(filename, suffix)

arguments
filename string {mustBeScalarOrEmpty}
filename (1,1) string
suffix (1,1) string
end

Expand Down
2 changes: 1 addition & 1 deletion +stdlib/absolute_path.m
Expand Up @@ -12,7 +12,7 @@
% * a: absolute path, if determined

arguments
p string {mustBeScalarOrEmpty}
p (1,1) string
end

a = stdlib.fileio.absolute_path(p);
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/canonical.m
Expand Up @@ -15,7 +15,7 @@
% * c: canonical path, if determined

arguments
p string {mustBeScalarOrEmpty}
p (1,1) string
end

c = stdlib.fileio.canonical(p);
Expand Down
5 changes: 2 additions & 3 deletions +stdlib/expanduser.m
Expand Up @@ -2,16 +2,15 @@
%% expanduser(path)
% expands tilde ~ into user home directory
%
% Useful for Matlab functions like h5read() and some Computer Vision toolbox functions
% that can't handle ~
% Useful for Matlab functions that can't handle ~
%
%%% Inputs
% * p: path to expand, if tilde present
%%% Outputs
% * expanded: expanded path

arguments
p string
p (1,1) string
end

expanded = stdlib.fileio.expanduser(p);
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/extract_zstd.m
Expand Up @@ -7,7 +7,7 @@ function extract_zstd(archive, out_dir)

arguments
archive (1,1) string {mustBeFile}
out_dir (1,1) string
out_dir (1,1) string {mustBeFolder}
end


Expand Down
2 changes: 1 addition & 1 deletion +stdlib/filename.m
@@ -1,7 +1,7 @@
function p = filename(path)
% FILENAME filename (including suffix) without directory
arguments
path string
path (1,1) string
end

p = stdlib.fileio.filename(path);
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/is_absolute_path.m
Expand Up @@ -6,7 +6,7 @@
% os.path.isabs("/foo/../bar") == True

arguments
apath string {mustBeScalarOrEmpty}
apath (1,1) string
end

isabs = stdlib.fileio.is_absolute_path(apath);
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/is_exe.m
Expand Up @@ -8,7 +8,7 @@
% * ok: boolean logical

arguments
file string {mustBeScalarOrEmpty}
file (1,1) string
end

ok = stdlib.fileio.is_exe(file);
Expand Down
4 changes: 2 additions & 2 deletions +stdlib/join.m
@@ -1,8 +1,8 @@
function p = join(a, b)
%% JOIN: join two paths with posix file separator
arguments
a string {mustBeScalarOrEmpty}
b string {mustBeScalarOrEmpty}
a (1,1) string
b (1,1) string
end

p = stdlib.fileio.join(a, b);
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/normalize.m
Expand Up @@ -8,7 +8,7 @@
% * c: normalized path

arguments
p string {mustBeScalarOrEmpty}
p (1,1) string
end

c = stdlib.fileio.normalize(p);
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/parent.m
@@ -1,7 +1,7 @@
function p = parent(path)
% PARENT parent of path
arguments
path string
path (1,1) string
end

p = stdlib.fileio.parent(path);
Expand Down
3 changes: 1 addition & 2 deletions +stdlib/posix.m
@@ -1,7 +1,6 @@
function ppath = posix(file)
%% posix(file)
% convert a path or sequence of path components to a Posix path separated
% with "/" even on Windows.
% convert a path to a Posix path separated with "/" even on Windows.
% If Windows path also have escaping "\" this breaks
arguments
file string
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/resolve.m
Expand Up @@ -15,7 +15,7 @@
% * c: resolved path

arguments
p string {mustBeScalarOrEmpty}
p (1,1) string
end

c = stdlib.fileio.resolve(p);
Expand Down
4 changes: 2 additions & 2 deletions +stdlib/samepath.m
Expand Up @@ -7,8 +7,8 @@
%%% Outputs
% issame: logical
arguments
path1 string {mustBeScalarOrEmpty}
path2 string {mustBeScalarOrEmpty}
path1 (1,1) string
path2 (1,1) string
end

issame = stdlib.fileio.samepath(path1, path2);
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/with_suffix.m
Expand Up @@ -7,7 +7,7 @@
%%% Outputs
% * filename: modified filename
arguments
filename string
filename (1,1) string
suffix (1,1) string
end

Expand Down
25 changes: 25 additions & 0 deletions test/TestFilePure.m
Expand Up @@ -94,6 +94,31 @@ function test_is_absolute_path(tc)
tc.verifyFalse(stdlib.is_absolute_path("c"))
end

function test_absolute_path(tc)
import matlab.unittest.constraints.StartsWithSubstring

tc.verifyEqual(stdlib.absolute_path(""), "")

pabs = stdlib.absolute_path('2foo');
pabs2 = stdlib.absolute_path('4foo');
tc.verifyThat(pabs, ~StartsWithSubstring("2"))
tc.verifyTrue(strncmp(pabs, pabs2, 2))

par1 = stdlib.absolute_path("../2foo");
tc.verifyNotEmpty(par1)

par2 = stdlib.absolute_path("../4foo");
tc.verifyTrue(strncmp(par2, pabs2, 2))

pt1 = stdlib.absolute_path("bar/../2foo");
tc.verifyNotEmpty(pt1)

va = stdlib.absolute_path("2foo");
vb = stdlib.absolute_path("4foo");
tc.verifyThat(va, ~StartsWithSubstring("2"))
tc.verifyTrue(strncmp(va, vb, 2))

end

function test_normalize(tc)

Expand Down

0 comments on commit a02a55c

Please sign in to comment.