fix std.fs.path.resolveWindows on UNC paths with mixed path separators

This commit is contained in:
Techatrix 2025-10-24 02:38:34 +02:00 committed by Alex Rønne Petersen
parent 433846100b
commit bd1e960bc0

View File

@ -413,7 +413,7 @@ pub fn windowsParsePath(path: []const u8) WindowsPath {
return relative_path; return relative_path;
} }
var it = mem.tokenizeScalar(u8, path, this_sep); var it = mem.tokenizeAny(u8, path, "/\\");
_ = (it.next() orelse return relative_path); _ = (it.next() orelse return relative_path);
_ = (it.next() orelse return relative_path); _ = (it.next() orelse return relative_path);
return WindowsPath{ return WindowsPath{
@ -439,6 +439,12 @@ test windowsParsePath {
try testing.expect(parsed.kind == WindowsPath.Kind.NetworkShare); try testing.expect(parsed.kind == WindowsPath.Kind.NetworkShare);
try testing.expect(mem.eql(u8, parsed.disk_designator, "\\\\a\\b")); try testing.expect(mem.eql(u8, parsed.disk_designator, "\\\\a\\b"));
} }
{
const parsed = windowsParsePath("\\\\a/b");
try testing.expect(parsed.is_abs);
try testing.expect(parsed.kind == WindowsPath.Kind.NetworkShare);
try testing.expect(mem.eql(u8, parsed.disk_designator, "\\\\a/b"));
}
{ {
const parsed = windowsParsePath("\\\\a\\"); const parsed = windowsParsePath("\\\\a\\");
try testing.expect(!parsed.is_abs); try testing.expect(!parsed.is_abs);
@ -492,11 +498,8 @@ fn compareDiskDesignators(kind: WindowsPath.Kind, p1: []const u8, p2: []const u8
return ascii.toUpper(p1[0]) == ascii.toUpper(p2[0]); return ascii.toUpper(p1[0]) == ascii.toUpper(p2[0]);
}, },
WindowsPath.Kind.NetworkShare => { WindowsPath.Kind.NetworkShare => {
const sep1 = p1[0]; var it1 = mem.tokenizeAny(u8, p1, "/\\");
const sep2 = p2[0]; var it2 = mem.tokenizeAny(u8, p2, "/\\");
var it1 = mem.tokenizeScalar(u8, p1, sep1);
var it2 = mem.tokenizeScalar(u8, p2, sep2);
return windows.eqlIgnoreCaseWtf8(it1.next().?, it2.next().?) and windows.eqlIgnoreCaseWtf8(it1.next().?, it2.next().?); return windows.eqlIgnoreCaseWtf8(it1.next().?, it2.next().?) and windows.eqlIgnoreCaseWtf8(it1.next().?, it2.next().?);
}, },
@ -795,6 +798,7 @@ test resolveWindows {
try testResolveWindows(&[_][]const u8{ "c:/ignore", "c:/some/file" }, "C:\\some\\file"); try testResolveWindows(&[_][]const u8{ "c:/ignore", "c:/some/file" }, "C:\\some\\file");
try testResolveWindows(&[_][]const u8{ "d:/ignore", "d:some/dir//" }, "D:\\ignore\\some\\dir"); try testResolveWindows(&[_][]const u8{ "d:/ignore", "d:some/dir//" }, "D:\\ignore\\some\\dir");
try testResolveWindows(&[_][]const u8{ "//server/share", "..", "relative\\" }, "\\\\server\\share\\relative"); try testResolveWindows(&[_][]const u8{ "//server/share", "..", "relative\\" }, "\\\\server\\share\\relative");
try testResolveWindows(&[_][]const u8{ "\\\\server/share", "..", "relative\\" }, "\\\\server\\share\\relative");
try testResolveWindows(&[_][]const u8{ "c:/", "//" }, "C:\\"); try testResolveWindows(&[_][]const u8{ "c:/", "//" }, "C:\\");
try testResolveWindows(&[_][]const u8{ "c:/", "//dir" }, "C:\\dir"); try testResolveWindows(&[_][]const u8{ "c:/", "//dir" }, "C:\\dir");
try testResolveWindows(&[_][]const u8{ "c:/", "//server/share" }, "\\\\server\\share\\"); try testResolveWindows(&[_][]const u8{ "c:/", "//server/share" }, "\\\\server\\share\\");