Skip to content

Commit a2ed3db

Browse files
committed
add root_name(), improve is_absolute implementation
1 parent a3d34ba commit a2ed3db

File tree

13 files changed

+211
-74
lines changed

13 files changed

+211
-74
lines changed

+stdlib/hostname.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
% Ref: https://docs.oracle.com/javase/8/docs/api/java/net/InetAddress.html#getHostName--
55

66
function name = hostname()
7+
8+
if stdlib.isoctave
9+
name = gethostname();
10+
else
711
name = string(java.net.InetAddress.getLocalHost().getHostName());
812
end
13+
14+
end

+stdlib/is_absolute.m

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
%% IS_ABSOLUTE is path absolute?
22
%
3-
% Ref: https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/io/File.html#isAbsolute()
3+
%!assert(is_absolute('', false), false)
4+
%!test
5+
%! if ispc
6+
%! assert(is_absolute('C:\', false), true)
7+
%! assert(is_absolute('C:/', false), true)
8+
%! assert(is_absolute('C:', false), false)
9+
%! assert(is_absolute('C', false), false)
10+
%! else
11+
%! assert(is_absolute('/', false), true)
12+
%! assert(is_absolute('/usr', false), true)
13+
%! assert(is_absolute('usr', false), false)
14+
%! endif
415

516
function isabs = is_absolute(p, use_java)
617
arguments
7-
p (1,1) string
18+
p (1,1)
819
use_java (1,1) logical = false
920
end
1021

1122
if use_java
1223
% java is about 5x to 10x slower than intrinsic
24+
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/io/File.html#isAbsolute()
1325
isabs = java.io.File(p).toPath().isAbsolute();
26+
elseif ischar(p)
27+
L = length(p);
28+
if ispc
29+
isabs = L > 2 && ~isempty(stdlib.root_name(p)) && (p(3) == '\' || p(3) == '/');
30+
else
31+
isabs = L >= 1 && p(1) == '/';
32+
end
1433
else
1534
L = strlength(p);
1635
if ispc
17-
isabs = L >= 2 && isletter(extractBetween(p, 1, 1)) && extractBetween(p, 2, 2) == ":";
36+
isabs = L > 2 && strlength(stdlib.root_name(p)) >= 2 && any(extractBetween(p, 3, 3) == ['/', '\']);
1837
else
1938
isabs = L >= 1 && startsWith(p, "/");
2039
end

+stdlib/is_exe.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
%% IS_EXE is file executable
22
%
3-
% Ref: https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/io/File.html#canExecute()
3+
% false if file does not exist
4+
%
5+
%!assert (is_exe('', false), false)
46

57
function ok = is_exe(file, use_java)
68
arguments
@@ -11,13 +13,14 @@
1113
if use_java
1214
% about the same time as fileattrib
1315
% doesn't need absolute path like other Java functions
16+
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/io/File.html#canExecute()
1417
ok = java.io.File(file).canExecute();
1518

1619
% more complicated
1720
% ok = java.nio.file.Files.isExecutable(java.io.File(stdlib.canonical(file)).toPath());
1821

1922
else
20-
if strlength(file) == 0
23+
if stdlib.len(file) == 0
2124
ok = false;
2225
return
2326
end

+stdlib/is_readable.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
%% IS_READABLE is file readable
22
%
33
% Ref: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isReadable(java.nio.file.Path)
4+
%
5+
%!assert (is_readable('is_readable.m', false))
6+
%!assert (is_readable('', false), false)
47

58
function ok = is_readable(file, use_java)
69
arguments

+stdlib/is_writable.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
%% IS_WRITABLE is path writable
22
%
3-
% Ref: https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/Files.html#isWritable(java.nio.file.Path)
3+
%!assert (is_writable('is_writable.m', false))
4+
%!assert (is_writable('', false), false)
45

56
function ok = is_writable(file, use_java)
67
arguments
@@ -13,6 +14,7 @@
1314
% needs absolute()
1415
file = stdlib.absolute(file, "", false, use_java);
1516

17+
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/Files.html#isWritable(java.nio.file.Path)
1618
ok = java.nio.file.Files.isWritable(java.io.File(file).toPath());
1719
else
1820
[status, v] = fileattrib(file);

+stdlib/iscygwin.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
%% ISCYGWIN Detect if running under Cygwin
22

33
function iscyg = iscygwin()
4+
45
if ispc || ismac
56
iscyg = false;
67
elseif isunix

+stdlib/isoctave.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
%% ISOCTAVE Detects if this is GNU Octave
22

33
function isoct = isoctave()
4-
isoct = exist('OCTAVE_VERSION', 'builtin') == 5;
4+
5+
persistent o
6+
7+
if isempty(o)
8+
o = exist('OCTAVE_VERSION', 'builtin') == 5;
9+
end
10+
11+
isoct = o;
12+
513
end

+stdlib/len.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function L = len(s)
2+
3+
L = [];
4+
5+
if ischar(s)
6+
L = length(s);
7+
elseif isstring(s)
8+
L = strlength(s);
9+
end
10+
11+
end

+stdlib/parent.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
% drop duplicated slashes in the parent path
1717
p = regexprep(p, "//+", "/");
1818

19-
if ispc && any(strlength(p) == [2,3]) && isletter(extractBetween(p, 1, 1)) && extractBetween(p, 2, 2) == ":"
19+
if ispc && any(strlength(p) == [2,3]) && strlength(stdlib.root_name(p))
2020
% 2 or 3 char drive letter
2121
p = stdlib.root(p);
2222
return

+stdlib/posix.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
%% POSIX posix format of path with '/' separator
22
% convert a path to a Posix string path separated with "/" even on Windows.
33
% If Windows path also have escaping "\" this breaks
4+
%
5+
%!assert (posix('/'), '/')
46

57
function r = posix(p)
68
arguments
7-
p string
9+
p (1,1) string
810
end
911

1012
if ispc
11-
r = strrep(p, "\", "/");
13+
r = strrep(p, '\', '/');
1214
else
1315
r = p;
1416
end

0 commit comments

Comments
 (0)