Skip to content

Commit 717c65e

Browse files
committed
Windows: better handle leading double slash, possible UNC
1 parent cedfdbd commit 717c65e

File tree

6 files changed

+32
-7
lines changed

6 files changed

+32
-7
lines changed

+stdlib/canonical.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
c = p;
2828
end
2929

30-
if ispc && strncmp(c, "\\", 2)
30+
if ispc && (startsWith(c, "\\") || startsWith(c, "//"))
3131
% UNC path is not canonicalized
3232
return
3333
end

+stdlib/drop_slash.m

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
%% DROP_SLASH drop repeated and trailing slash
2+
%
3+
% on Windows, if leading double slash, do not drop
24

35
function d = drop_slash(p)
46
arguments
@@ -7,12 +9,17 @@
79

810
s = stdlib.posix(p);
911

12+
uncslash = ispc && startsWith(s, "//");
13+
1014
% drop repeated slashes inside string
1115
d = regexprep(s, "/+", "/");
1216

1317
L = stdlib.len(d);
1418

15-
if strcmp(d, '/') || ~L
19+
if L < 2
20+
if uncslash
21+
d = "//";
22+
end
1623
return;
1724
end
1825

@@ -26,10 +33,13 @@
2633
end
2734
end
2835

36+
if uncslash
37+
d = strcat("/", d);
38+
end
39+
2940
end
3041

3142
%!assert(drop_slash(''), '')
3243
%!assert(drop_slash('/'), '/')
33-
%!assert(drop_slash('//'), '/')
3444
%!assert(drop_slash('a//b'), 'a/b')
3545
%!assert(drop_slash('a//b/'), 'a/b')

+stdlib/expanduser.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
e = stdlib.drop_slash(p);
1717

1818
L = stdlib.len(e);
19-
if ~L || ~strncmp(e, "~", 1) || (L > 1 && ~strncmp(e, "~/", 2))
19+
if ~L || ~startsWith(e, "~") || (L > 1 && ~startsWith(e, "~/"))
2020
return
2121
end
2222

+stdlib/is_rosetta.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
% uname -m reports "x86_64" from within Matlab on Apple Silicon if using Rosetta
1212

1313
[ret, raw] = system("sysctl -n sysctl.proc_translated");
14-
r = ret == 0 && strncmp(raw, '1', 1);
14+
r = ret == 0 && startsWith(raw, '1');
1515

1616
end
1717

+stdlib/normalize.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
use_java (1,1) logical = false
1515
end
1616

17-
1817
if use_java
1918
o = javaFileObject(p).toPath().normalize();
20-
n = jPosix(o);
19+
n = stdlib.drop_slash(jPosix(o));
2120
else
2221

2322
n = stdlib.posix(p);
2423

24+
uncslash = ispc && startsWith(n, "//");
25+
2526
% use split to remove /../ and /./ and duplicated /
2627
parts = split(n, '/');
2728
i0 = 1;
@@ -59,6 +60,10 @@
5960
end
6061
end
6162

63+
if uncslash
64+
n = strcat("/", n);
65+
end
66+
6267
end
6368

6469

test/TestNormalize.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
properties (TestParameter)
44
use_java = num2cell(unique([stdlib.has_java(), false]))
5+
p
6+
end
57

8+
methods (TestParameterDefinition, Static)
9+
function p = init()
610
p = {
711
{"", "."}, ...
812
{"a/..", "."}, ...
@@ -22,6 +26,12 @@
2226
{"./a/.", "a"}, ...
2327
{"../a", "../a"}
2428
};
29+
30+
if ispc
31+
p{3}{2} = "//a/b";
32+
end
33+
34+
end
2535
end
2636

2737
methods (Test)

0 commit comments

Comments
 (0)