Skip to content

Commit cd406f1

Browse files
committed
relative_to subfunction
1 parent 13721a4 commit cd406f1

File tree

5 files changed

+42
-51
lines changed

5 files changed

+42
-51
lines changed

+stdlib/+dotnet/relative_to.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function rel = relative_to(base, other)
2+
3+
if strempty(other)
4+
rel = base;
5+
return
6+
end
7+
if strempty(base)
8+
rel = other;
9+
return
10+
end
11+
12+
base = fullfile(base);
13+
other = fullfile(other);
14+
15+
if stdlib.is_absolute(base) && ~(startsWith(base, other) || startsWith(other, base))
16+
rel = "";
17+
else
18+
% https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getrelativepath
19+
rel = string(System.IO.Path.GetRelativePath(base, other));
20+
end
21+
22+
end

+stdlib/+python/relative_to.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function rel = relative_to(base, other)
2+
3+
try
4+
rel = string(py.os.path.relpath(other, base));
5+
catch e
6+
if e.identifier == "MATLAB:Python:PyException" && startsWith(e.message, 'Python Error: ValueError')
7+
rel = "";
8+
else
9+
rethrow(e)
10+
end
11+
end
12+
13+
end

+stdlib/proximate_to.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,4 @@
1717
rel = other;
1818
end
1919

20-
2120
end
22-
23-
%!testif 0

+stdlib/python_version.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
force_old (1,1) logical = false
1818
end
1919

20-
persistent v_
20+
persistent stdlib_py_version
2121

22-
if ~isempty(v_)
23-
v = v_;
22+
if ~isempty(stdlib_py_version)
23+
v = stdlib_py_version;
2424
return
2525
end
2626

@@ -32,13 +32,13 @@
3232
end
3333

3434
% we use a separate function because the JIT compiler in Matlab < R2022a
35-
% breaks try-catch for any py.* command
35+
% breaks for any py.* command when pyenv() is not correctly configured
3636

3737
v = stdlib.python.version();
3838

3939
% cache the result
4040
if ~isempty(v)
41-
v_ = v;
41+
stdlib_py_version = v;
4242
end
4343

4444
end

+stdlib/relative_to.m

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,11 @@
2323
end
2424

2525
if stdlib.has_python()
26-
rel = relative_to_python(base, other);
26+
rel = stdlib.python.relative_to(base, other);
2727
elseif stdlib.dotnet_api() >= 5
28-
rel = relative_to_dotnet(base, other);
28+
rel = stdlib.dotnet.relative_to(base, other);
2929
else
3030
error('no supported relative path method found, please install .NET or "buildtool mex"')
3131
end
3232

3333
end
34-
35-
36-
function rel = relative_to_dotnet(base, other)
37-
38-
if strempty(other)
39-
rel = base;
40-
return
41-
end
42-
if strempty(base)
43-
rel = other;
44-
return
45-
end
46-
47-
base = fullfile(base);
48-
other = fullfile(other);
49-
50-
if stdlib.is_absolute(base) && ~(startsWith(base, other) || startsWith(other, base))
51-
rel = "";
52-
else
53-
% https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getrelativepath
54-
rel = string(System.IO.Path.GetRelativePath(base, other));
55-
end
56-
57-
end
58-
59-
60-
function rel = relative_to_python(base, other)
61-
62-
try
63-
rel = string(py.os.path.relpath(other, base));
64-
catch e
65-
if e.identifier == "MATLAB:Python:PyException" && startsWith(e.message, 'Python Error: ValueError')
66-
rel = "";
67-
else
68-
rethrow(e)
69-
end
70-
end
71-
72-
end
73-
74-
%!testif 0

0 commit comments

Comments
 (0)