Skip to content

Commit afc951d

Browse files
committed
efficiency, correctness
1 parent 690f956 commit afc951d

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

+stdlib/+native/samepath.m

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@
33
% this canonical string method is less preferred to using device + inode.
44

55
function y = samepath(path1, path2)
6+
arguments
7+
path1 string
8+
path2 string
9+
end
10+
11+
assert(isequal(size(path1), size(path2)), "path1 string array size does not match path2 array size")
12+
13+
y = false(size(path1));
614

7-
i = stdlib.strempty(path1) | stdlib.strempty(path2);
8-
y(i) = false;
15+
y(strlength(path1) | strlength(path2)) = true;
16+
17+
% necessary for Matlab < R2024a
18+
if ~any(y)
19+
return
20+
end
921

10-
c1 = stdlib.canonical(path1(~i), true);
11-
c2 = stdlib.canonical(path2(~i), true);
12-
y(~i) = ~stdlib.strempty(c1) & strcmp(c1, c2);
22+
c1 = stdlib.canonical(path1(y), true);
23+
c2 = stdlib.canonical(path2(y), true);
24+
y(y) = strlength(c1) & strcmp(c1, c2);
1325

1426
end

test/TestSame.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
classdef (SharedTestFixtures={ matlab.unittest.fixtures.PathFixture("..")}, ...
2-
TestTags = {'R2019b', 'impure'}) ...
2+
TestTags = {'impure'}) ...
33
TestSame < matlab.unittest.TestCase
44

55
properties (TestParameter)
@@ -19,7 +19,7 @@
1919
end
2020

2121

22-
methods(Test)
22+
methods(Test, TestTags={'R2019b'})
2323

2424
function test_samepath(tc, p_same, backend)
2525
r = stdlib.samepath(p_same{:}, backend);
@@ -33,15 +33,17 @@ function test_samepath_notexist(tc, backend)
3333
tc.verifyFalse(stdlib.samepath("", "", backend))
3434
tc.verifyFalse(stdlib.samepath(t, t, backend))
3535
end
36+
end
3637

3738

39+
methods (Test, TestTags={'R2024a'})
3840
function test_samepath_array(tc, backend)
3941
in = [string(mfilename), string(mfilename('fullpath'))] + ".m";
4042
r = stdlib.samepath(in, fliplr(in), backend);
4143
tc.verifyClass(r, 'logical')
4244
tc.verifyEqual(r, [true, true])
4345
end
44-
4546
end
4647

48+
4749
end

0 commit comments

Comments
 (0)