Skip to content

Commit ace18cb

Browse files
committed
file_size: speed, simplify
1 parent 121b218 commit ace18cb

File tree

3 files changed

+6
-27
lines changed

3 files changed

+6
-27
lines changed

+stdlib/file_size.m

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,19 @@
33
%%% Inputs
44
% * p: path to file
55
%%% Outputs
6-
% * s: size in bytes; NaN if file does not exist
6+
% * s: size in bytes; empty if file does not exist
77

88
function s = file_size(p)
99
arguments
10-
p string
10+
p (1,1) string
1111
end
1212

13-
s = NaN(size(p));
13+
s = [];
1414

15-
i = isfile(p);
15+
d = dir(p);
1616

17-
if ~any(i)
18-
return
19-
elseif isscalar(p)
20-
d = dir(p);
21-
else
22-
d = arrayfun(@dir, p(i));
17+
if isscalar(d) && ~d.isdir
18+
s = d.bytes;
2319
end
2420

25-
i = i & ~isempty(d);
26-
s(i) = [d.bytes];
27-
2821
end

example/bench_file_size.m

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/TestFileImpure.m

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ function test_file_size(tc, p_file_size)
1616
tc.verifyGreaterThan(s, 0)
1717
end
1818

19-
function test_file_size_array(tc)
20-
s = stdlib.file_size([mfilename("fullpath") + ".m", "not-exist", fullfile(fileparts(pwd()), "buildfile.m"), tempname()]);
21-
tc.verifyTrue(all([s(1) > 0, isnan(s(2)), s(3) > 0, isnan(s(4))]))
22-
end
23-
2419

2520
function test_null_file(tc)
2621
if ispc()

0 commit comments

Comments
 (0)