Skip to content

Commit bbd36fc

Browse files
committed
relative_to: do not walk up
1 parent a83c495 commit bbd36fc

File tree

4 files changed

+17
-24
lines changed

4 files changed

+17
-24
lines changed

+stdlib/+native/relative_to.m

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,21 @@
1313
return
1414
end
1515

16-
tp = strsplit(fullfile(target), filesep);
17-
bp = strsplit(fullfile(base), filesep);
16+
fs = ["/", filesep];
17+
18+
tp = split(fullfile(target), fs);
19+
bp = split(fullfile(base), fs);
1820

1921
% Find the common base portion
2022
n = 0;
2123
while n < length(tp) && n < length(bp) && strcmp(tp{n+1}, bp{n+1})
2224
n = n + 1;
2325
end
2426

25-
% Number of '..' needed
26-
numUp = length(bp) - n;
27-
28-
relParts = [repmat({'..'}, 1, numUp), tp(n+1:end)];
27+
rel = join(tp(n+1:end), filesep);
2928

30-
if isempty(relParts)
31-
rel = '.';
32-
else
33-
rel = fullfile(relParts{:});
34-
35-
if isempty(rel)
36-
rel = '.';
37-
end
29+
if stdlib.strempty(rel)
30+
rel = ".";
3831
end
3932

40-
rel = string(rel);
41-
4233
end

+stdlib/+python/private/pythonException.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function pythonException(e)
22

33
switch e.identifier
44
case 'MATLAB:Python:PyException'
5-
if ~contains(e.message, ["NotADirectoryError", "FileNotFoundError"])
5+
if ~contains(e.message, ["is not in the subpath of", "NotADirectoryError", "FileNotFoundError"])
66
rethrow(e)
77
end
88
otherwise, rethrow(e)

+stdlib/+python/relative_to.m

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
function rel = relative_to(base, other)
2-
arguments
3-
base (1,1) string
4-
other (1,1) string
2+
3+
rel = "";
4+
5+
if stdlib.strempty(base) || stdlib.strempty(other)
6+
return
57
end
68

79
try
8-
rel = string(py.os.path.relpath(other, base));
9-
catch
10-
rel = "";
10+
rel = string(py.str(py.pathlib.Path(other).relative_to(base)));
11+
catch e
12+
pythonException(e)
1113
end
1214

1315
end

+stdlib/strempty.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
if isempty(s)
77
y = true;
88
else
9-
y = strlength(s) == 0;
9+
y = (strlength(s) == 0) | ismissing(s);
1010
end
1111

1212
end

0 commit comments

Comments
 (0)