Skip to content

Commit 2a6f539

Browse files
committed
absolute: improve efficiency
1 parent 514c948 commit 2a6f539

File tree

8 files changed

+39
-25
lines changed

8 files changed

+39
-25
lines changed

+stdlib/absolute.m

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626

2727
cwd = stdlib.posix(pwd());
2828

29-
if strlength(p) == 0 && strlength(base) == 0
29+
Lb = stdlib.len(base);
30+
31+
if stdlib.len(p) == 0 && Lb == 0
3032
c = cwd;
3133
return
3234
end
@@ -37,24 +39,36 @@
3739
c = p;
3840
end
3941

40-
if stdlib.is_absolute(c)
42+
if stdlib.is_absolute(c, use_java)
4143
c = stdlib.posix(c);
4244
else
4345
% .getAbsolutePath(), .toAbsolutePath()
4446
% default is Documents/Matlab, which is probably not wanted.
45-
if strlength(base) == 0
46-
c = cwd + "/" + c;
47+
if Lb == 0
48+
if ischar(cwd)
49+
c = strcat(cwd, '/', c);
50+
else
51+
c = cwd + "/" + c;
52+
end
4753
else
4854
d = stdlib.absolute(base, "", expand_tilde, use_java);
49-
if isempty(c) || strlength(c) == 0
55+
if isempty(c) || stdlib.len(c) == 0
5056
c = d;
5157
else
52-
c = d + "/" + c;
58+
if ischar(d)
59+
c = strcat(d, '/', c);
60+
else
61+
c = d + "/" + c;
62+
end
5363
end
5464
end
5565
end
5666

5767
% not needed:
5868
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getAbsolutePath()
5969

60-
end % function
70+
end
71+
72+
73+
%!assert(absolute('', '', 0,0), posix(pwd))
74+
%!assert(absolute('a/b', '', 0,0), posix(fullfile(pwd, 'a/b')))

+stdlib/h5create_group.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
if isa(file, 'H5ML.id')
1717
fid = file;
1818
else
19-
% avoid confusing creating file ./~/foo.h5
19+
% avoid confusing creating file ./~/a.h5
2020
file = stdlib.expanduser(file);
2121
dcpl = 'H5P_DEFAULT';
2222
if isfile(file)

+stdlib/h5save.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function h5save(filename, varname, A, opts)
3232
mustBeReal(A)
3333
end
3434

35-
% avoid confusing creating file ./~/foo.h5
35+
% avoid confusing creating file ./~/a.h5
3636
filename = stdlib.expanduser(filename);
3737

3838
% coerce if needed

+stdlib/ncsave.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function ncsave(filename, varname, A, opts)
2424
mustBeReal(A)
2525
end
2626

27-
% avoid creating confusing file ./~/foo.nc
27+
% avoid creating confusing file ./~/a.nc
2828
filename = stdlib.expanduser(filename);
2929

3030
% coerce if needed

+stdlib/parent.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
%!assert(parent('a//b'), 'a')
5555
%!test
5656
%! if ispc
57-
%! assert(parent('c:/foo'), 'c:/')
58-
%! assert(parent('c:\foo\'), 'c:/')
57+
%! assert(parent('c:/a'), 'c:/')
58+
%! assert(parent('c:\a\'), 'c:/')
5959
%! assert(parent('c:\'), 'c:/')
6060
%! assert(parent('c:'), 'c:/')
6161
%! end

+stdlib/with_suffix.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535

3636
%!assert(with_suffix("", ""), "")
37-
%!assert(with_suffix("foo.h5", ".nc"), "foo.nc")
38-
%!assert(with_suffix("foo", ".nc"), "foo.nc")
39-
%!assert(with_suffix("foo.h5", ""), "foo")
40-
%!assert(with_suffix("foo", ""), "foo")
37+
%!assert(with_suffix("a.h5", ".nc"), "a.nc")
38+
%!assert(with_suffix("a", ".nc"), "a.nc")
39+
%!assert(with_suffix("a.h5", ""), "a")
40+
%!assert(with_suffix("a", ""), "a")

test/TestFileImpure.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
cwd = fileparts(mfilename("fullpath"));
2929
top = fullfile(cwd, "..");
3030
addpath(top)
31-
p_expand = {{"", ""}, {"~abc", "~abc"}, {"~", stdlib.homedir()}, {"~/foo", stdlib.homedir() + "/foo"}, {'~/////c', stdlib.homedir() + "/c"}};
31+
p_expand = {{"", ""}, {"~abc", "~abc"}, {"~", stdlib.homedir()}, {"~/c", stdlib.homedir() + "/c"}, {'~/////c', stdlib.homedir() + "/c"}};
3232
end
3333
end
3434

test/TestFilePure.m

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
p_relative_to
99
p_proximate_to
1010
p_is_absolute
11-
in_filename = {"", "/foo/bar/baz", "/foo/bar/baz/", "foo/bar/baz.txt", "foo/bar/baz.txt.gz"}
12-
ref_filename = {"", "baz", "", "baz.txt", "baz.txt.gz"}
11+
in_filename = {"", "/a/b/c", "/a/b/c/", "a/b/c.txt", "a/b/c.txt.gz"}
12+
ref_filename = {"", "c", "", "c.txt", "c.txt.gz"}
1313
dir_is_subdir
1414
sub_is_subdir
1515
ref_is_subdir
@@ -30,7 +30,7 @@
3030
{"ab/cd", "/ef", "/ef"} ...
3131
};
3232

33-
in_suffix = {"", "/foo/bar/baz", "/foo/bar/baz/", "foo/bar/baz.txt", "foo/bar/baz.txt.gz", ".stat", ".stat.txt"}
33+
in_suffix = {"", "/a/b/c", "/a/b/c/", "a/b/c.txt", "a/b/c.txt.gz", ".stat", ".stat.txt"}
3434
ref_suffix = {"", "", "", ".txt", ".gz", ".stat", ".txt"}
3535

3636
in_norm = {"", "a/..", "//a/b/", "/a/b/", "a/b/", "a/../c", "a/b/../c", "a/b/../../c", "a/b/../../c/..", ...
@@ -226,13 +226,13 @@ function test_stem(tc)
226226

227227
tc.verifyEqual(stdlib.stem(""), "")
228228

229-
tc.verifyEqual(stdlib.stem("/foo/bar/baz"), "baz")
230-
tc.verifyEqual(stdlib.stem("/foo/bar/baz/"), "")
229+
tc.verifyEqual(stdlib.stem("/a/b/c"), "c")
230+
tc.verifyEqual(stdlib.stem("/a/b/c/"), "")
231231

232-
tc.verifyEqual(stdlib.stem("foo/bar/baz/"), "")
232+
tc.verifyEqual(stdlib.stem("a/b/c/"), "")
233233

234-
tc.verifyEqual(stdlib.stem("foo/bar/baz.txt"), "baz")
235-
tc.verifyEqual(stdlib.stem("foo/bar/baz.txt.gz"), "baz.txt")
234+
tc.verifyEqual(stdlib.stem("a/b/c.txt"), "c")
235+
tc.verifyEqual(stdlib.stem("a/b/c.txt.gz"), "c.txt")
236236

237237
end
238238

0 commit comments

Comments
 (0)