| ... | ... | @@ -20,10 +20,7 @@ const testing = std.testing; |
| 20 | 20 | const mem = std.mem; |
| 21 | 21 | const ascii = std.ascii; |
| 22 | 22 | const Allocator = mem.Allocator; |
| 23 | | const math = std.math; |
| 24 | 23 | const windows = std.os.windows; |
| 25 | | const os = std.os; |
| 26 | | const fs = std.fs; |
| 27 | 24 | const process = std.process; |
| 28 | 25 | const native_os = builtin.target.os.tag; |
| 29 | 26 | |
| ... | ... | @@ -221,7 +218,7 @@ test join { |
| 221 | 218 | try testJoinMaybeZWindows(&[_][]const u8{}, "", zero); |
| 222 | 219 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\a\\b", "c" }, "c:\\a\\b\\c", zero); |
| 223 | 220 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\a\\b", "c" }, "c:\\a\\b\\c", zero); |
| 224 | | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\a\\b\\", "c" }, "c:\\a\\b\\c", zero); |
| 221 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\a\\b\\", "\\c" }, "c:\\a\\b\\c", zero); |
| 225 | 222 | |
| 226 | 223 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\", "a", "b\\", "c" }, "c:\\a\\b\\c", zero); |
| 227 | 224 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\a\\", "b\\", "c" }, "c:\\a\\b\\c", zero); |
| ... | ... | @@ -283,26 +280,16 @@ pub fn isAbsolute(path: []const u8) bool { |
| 283 | 280 | } |
| 284 | 281 | |
| 285 | 282 | fn isAbsoluteWindowsImpl(comptime T: type, path: []const T) bool { |
| 286 | | if (path.len < 1) |
| 287 | | return false; |
| 288 | | |
| 289 | | if (path[0] == '/') |
| 290 | | return true; |
| 291 | | |
| 292 | | if (path[0] == '\\') |
| 293 | | return true; |
| 294 | | |
| 295 | | if (path.len < 3) |
| 296 | | return false; |
| 297 | | |
| 298 | | if (path[1] == ':') { |
| 299 | | if (path[2] == '/') |
| 300 | | return true; |
| 301 | | if (path[2] == '\\') |
| 302 | | return true; |
| 303 | | } |
| 304 | | |
| 305 | | return false; |
| 283 | return switch (windows.getWin32PathType(T, path)) { |
| 284 | // Unambiguously absolute |
| 285 | .drive_absolute, .unc_absolute, .local_device, .root_local_device => true, |
| 286 | // Unambiguously relative |
| 287 | .relative => false, |
| 288 | // Ambiguous, more absolute than relative |
| 289 | .rooted => true, |
| 290 | // Ambiguous, more relative than absolute |
| 291 | .drive_relative => false, |
| 292 | }; |
| 306 | 293 | } |
| 307 | 294 | |
| 308 | 295 | pub fn isAbsoluteWindows(path: []const u8) bool { |
| ... | ... | @@ -347,6 +334,9 @@ test isAbsoluteWindows { |
| 347 | 334 | try testIsAbsoluteWindows("C:\\Users\\", true); |
| 348 | 335 | try testIsAbsoluteWindows("C:cwd/another", false); |
| 349 | 336 | try testIsAbsoluteWindows("C:cwd\\another", false); |
| 337 | try testIsAbsoluteWindows("λ:\\", true); |
| 338 | try testIsAbsoluteWindows("λ:", false); |
| 339 | try testIsAbsoluteWindows("\u{10000}:\\", false); |
| 350 | 340 | try testIsAbsoluteWindows("directory/directory", false); |
| 351 | 341 | try testIsAbsoluteWindows("directory\\directory", false); |
| 352 | 342 | try testIsAbsoluteWindows("/usr/local", true); |
| ... | ... | @@ -362,12 +352,17 @@ test isAbsolutePosix { |
| 362 | 352 | |
| 363 | 353 | fn testIsAbsoluteWindows(path: []const u8, expected_result: bool) !void { |
| 364 | 354 | try testing.expectEqual(expected_result, isAbsoluteWindows(path)); |
| 355 | const path_w = try std.unicode.wtf8ToWtf16LeAllocZ(std.testing.allocator, path); |
| 356 | defer std.testing.allocator.free(path_w); |
| 357 | try testing.expectEqual(expected_result, isAbsoluteWindowsW(path_w)); |
| 358 | try testing.expectEqual(expected_result, isAbsoluteWindowsWtf16(path_w)); |
| 365 | 359 | } |
| 366 | 360 | |
| 367 | 361 | fn testIsAbsolutePosix(path: []const u8, expected_result: bool) !void { |
| 368 | 362 | try testing.expectEqual(expected_result, isAbsolutePosix(path)); |
| 369 | 363 | } |
| 370 | 364 | |
| 365 | /// Deprecated; see `WindowsPath2` |
| 371 | 366 | pub const WindowsPath = struct { |
| 372 | 367 | is_abs: bool, |
| 373 | 368 | kind: Kind, |
| ... | ... | @@ -380,6 +375,7 @@ pub const WindowsPath = struct { |
| 380 | 375 | }; |
| 381 | 376 | }; |
| 382 | 377 | |
| 378 | /// Deprecated; see `parsePathWindows` |
| 383 | 379 | pub fn windowsParsePath(path: []const u8) WindowsPath { |
| 384 | 380 | if (path.len >= 2 and path[1] == ':') { |
| 385 | 381 | return WindowsPath{ |
| ... | ... | @@ -402,26 +398,18 @@ pub fn windowsParsePath(path: []const u8) WindowsPath { |
| 402 | 398 | .disk_designator = &[_]u8{}, |
| 403 | 399 | .is_abs = false, |
| 404 | 400 | }; |
| 405 | | if (path.len < "//a/b".len) { |
| 406 | | return relative_path; |
| 407 | | } |
| 408 | | |
| 409 | | inline for ("/\\") |this_sep| { |
| 410 | | const two_sep = [_]u8{ this_sep, this_sep }; |
| 411 | | if (mem.startsWith(u8, path, &two_sep)) { |
| 412 | | if (path[2] == this_sep) { |
| 413 | | return relative_path; |
| 414 | | } |
| 415 | 401 | |
| 416 | | var it = mem.tokenizeAny(u8, path, "/\\"); |
| 417 | | _ = (it.next() orelse return relative_path); |
| 418 | | _ = (it.next() orelse return relative_path); |
| 419 | | return WindowsPath{ |
| 420 | | .is_abs = isAbsoluteWindows(path), |
| 421 | | .kind = WindowsPath.Kind.NetworkShare, |
| 422 | | .disk_designator = path[0..it.index], |
| 423 | | }; |
| 424 | | } |
| 402 | if (path.len >= 2 and PathType.windows.isSep(u8, path[0]) and PathType.windows.isSep(u8, path[1])) { |
| 403 | const root_end = root_end: { |
| 404 | var server_end = mem.indexOfAnyPos(u8, path, 2, "/\\") orelse break :root_end path.len; |
| 405 | while (server_end < path.len and PathType.windows.isSep(u8, path[server_end])) server_end += 1; |
| 406 | break :root_end mem.indexOfAnyPos(u8, path, server_end, "/\\") orelse path.len; |
| 407 | }; |
| 408 | return WindowsPath{ |
| 409 | .is_abs = true, |
| 410 | .kind = WindowsPath.Kind.NetworkShare, |
| 411 | .disk_designator = path[0..root_end], |
| 412 | }; |
| 425 | 413 | } |
| 426 | 414 | return relative_path; |
| 427 | 415 | } |
| ... | ... | @@ -446,10 +434,22 @@ test windowsParsePath { |
| 446 | 434 | try testing.expect(mem.eql(u8, parsed.disk_designator, "\\\\a/b")); |
| 447 | 435 | } |
| 448 | 436 | { |
| 449 | | const parsed = windowsParsePath("\\\\a\\"); |
| 450 | | try testing.expect(!parsed.is_abs); |
| 451 | | try testing.expect(parsed.kind == WindowsPath.Kind.None); |
| 452 | | try testing.expect(mem.eql(u8, parsed.disk_designator, "")); |
| 437 | const parsed = windowsParsePath("\\/a\\"); |
| 438 | try testing.expect(parsed.is_abs); |
| 439 | try testing.expect(parsed.kind == WindowsPath.Kind.NetworkShare); |
| 440 | try testing.expect(mem.eql(u8, parsed.disk_designator, "\\/a\\")); |
| 441 | } |
| 442 | { |
| 443 | const parsed = windowsParsePath("\\\\a\\\\b"); |
| 444 | try testing.expect(parsed.is_abs); |
| 445 | try testing.expect(parsed.kind == WindowsPath.Kind.NetworkShare); |
| 446 | try testing.expect(mem.eql(u8, parsed.disk_designator, "\\\\a\\\\b")); |
| 447 | } |
| 448 | { |
| 449 | const parsed = windowsParsePath("\\\\a\\\\b\\c"); |
| 450 | try testing.expect(parsed.is_abs); |
| 451 | try testing.expect(parsed.kind == WindowsPath.Kind.NetworkShare); |
| 452 | try testing.expect(mem.eql(u8, parsed.disk_designator, "\\\\a\\\\b")); |
| 453 | 453 | } |
| 454 | 454 | { |
| 455 | 455 | const parsed = windowsParsePath("/usr/local"); |
| ... | ... | @@ -465,6 +465,229 @@ test windowsParsePath { |
| 465 | 465 | } |
| 466 | 466 | } |
| 467 | 467 | |
| 468 | /// On Windows, this calls `parsePathWindows` and on POSIX it calls `parsePathPosix`. |
| 469 | /// |
| 470 | /// Returns a platform-specific struct with two fields: `root` and `kind`. |
| 471 | /// The `root` will be a slice of `path` (`/` for POSIX absolute paths, and things |
| 472 | /// like `C:\`, `\\server\share\`, etc for Windows paths). |
| 473 | /// If the path is of kind `.relative`, then `root` will be zero-length. |
| 474 | pub fn parsePath(path: []const u8) switch (native_os) { |
| 475 | .windows => WindowsPath2(u8), |
| 476 | else => PosixPath, |
| 477 | } { |
| 478 | switch (native_os) { |
| 479 | .windows => return parsePathWindows(u8, path), |
| 480 | else => return parsePathPosix(path), |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | const PosixPath = struct { |
| 485 | kind: enum { relative, absolute }, |
| 486 | root: []const u8, |
| 487 | }; |
| 488 | |
| 489 | pub fn parsePathPosix(path: []const u8) PosixPath { |
| 490 | const abs = isAbsolutePosix(path); |
| 491 | return .{ |
| 492 | .kind = if (abs) .absolute else .relative, |
| 493 | .root = if (abs) path[0..1] else path[0..0], |
| 494 | }; |
| 495 | } |
| 496 | |
| 497 | test parsePathPosix { |
| 498 | { |
| 499 | const parsed = parsePathPosix("a/b"); |
| 500 | try testing.expectEqual(.relative, parsed.kind); |
| 501 | try testing.expectEqualStrings("", parsed.root); |
| 502 | } |
| 503 | { |
| 504 | const parsed = parsePathPosix("/a/b"); |
| 505 | try testing.expectEqual(.absolute, parsed.kind); |
| 506 | try testing.expectEqualStrings("/", parsed.root); |
| 507 | } |
| 508 | { |
| 509 | const parsed = parsePathPosix("///a/b"); |
| 510 | try testing.expectEqual(.absolute, parsed.kind); |
| 511 | try testing.expectEqualStrings("/", parsed.root); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | pub fn WindowsPath2(comptime T: type) type { |
| 516 | return struct { |
| 517 | kind: windows.Win32PathType, |
| 518 | root: []const T, |
| 519 | }; |
| 520 | } |
| 521 | |
| 522 | pub fn parsePathWindows(comptime T: type, path: []const T) WindowsPath2(T) { |
| 523 | const kind = windows.getWin32PathType(T, path); |
| 524 | const root = root: switch (kind) { |
| 525 | .drive_absolute, .drive_relative => { |
| 526 | const drive_letter_len = getDriveLetter(T, path).len; |
| 527 | break :root path[0 .. drive_letter_len + @as(usize, if (kind == .drive_absolute) 2 else 1)]; |
| 528 | }, |
| 529 | .relative => path[0..0], |
| 530 | .local_device => path[0..4], |
| 531 | .root_local_device => path, |
| 532 | .rooted => path[0..1], |
| 533 | .unc_absolute => { |
| 534 | const unc = parseUNC(T, path); |
| 535 | // There may be any number of path separators between the server and the share, |
| 536 | // so take that into account by using pointer math to get the difference. |
| 537 | var root_len = 2 + (unc.share.ptr - unc.server.ptr) + unc.share.len; |
| 538 | if (unc.sep_after_share) root_len += 1; |
| 539 | break :root path[0..root_len]; |
| 540 | }, |
| 541 | }; |
| 542 | return .{ |
| 543 | .kind = kind, |
| 544 | .root = root, |
| 545 | }; |
| 546 | } |
| 547 | |
| 548 | test parsePathWindows { |
| 549 | { |
| 550 | const path = "//a/b"; |
| 551 | const parsed = parsePathWindows(u8, path); |
| 552 | try testing.expectEqual(.unc_absolute, parsed.kind); |
| 553 | try testing.expectEqualStrings("//a/b", parsed.root); |
| 554 | try testWindowsParsePathHarmony(path); |
| 555 | } |
| 556 | { |
| 557 | const path = "\\\\a\\b"; |
| 558 | const parsed = parsePathWindows(u8, path); |
| 559 | try testing.expectEqual(.unc_absolute, parsed.kind); |
| 560 | try testing.expectEqualStrings("\\\\a\\b", parsed.root); |
| 561 | try testWindowsParsePathHarmony(path); |
| 562 | } |
| 563 | { |
| 564 | const path = "\\/a/b/c"; |
| 565 | const parsed = parsePathWindows(u8, path); |
| 566 | try testing.expectEqual(.unc_absolute, parsed.kind); |
| 567 | try testing.expectEqualStrings("\\/a/b/", parsed.root); |
| 568 | try testWindowsParsePathHarmony(path); |
| 569 | } |
| 570 | { |
| 571 | const path = "\\\\a\\"; |
| 572 | const parsed = parsePathWindows(u8, path); |
| 573 | try testing.expectEqual(.unc_absolute, parsed.kind); |
| 574 | try testing.expectEqualStrings("\\\\a\\", parsed.root); |
| 575 | try testWindowsParsePathHarmony(path); |
| 576 | } |
| 577 | { |
| 578 | const path = "\\\\a\\b\\"; |
| 579 | const parsed = parsePathWindows(u8, path); |
| 580 | try testing.expectEqual(.unc_absolute, parsed.kind); |
| 581 | try testing.expectEqualStrings("\\\\a\\b\\", parsed.root); |
| 582 | try testWindowsParsePathHarmony(path); |
| 583 | } |
| 584 | { |
| 585 | const path = "\\\\a\\/b\\/"; |
| 586 | const parsed = parsePathWindows(u8, path); |
| 587 | try testing.expectEqual(.unc_absolute, parsed.kind); |
| 588 | try testing.expectEqualStrings("\\\\a\\/b\\", parsed.root); |
| 589 | try testWindowsParsePathHarmony(path); |
| 590 | } |
| 591 | { |
| 592 | const path = "\\\\кириллица\\ελληνικά\\português"; |
| 593 | const parsed = parsePathWindows(u8, path); |
| 594 | try testing.expectEqual(.unc_absolute, parsed.kind); |
| 595 | try testing.expectEqualStrings("\\\\кириллица\\ελληνικά\\", parsed.root); |
| 596 | try testWindowsParsePathHarmony(path); |
| 597 | } |
| 598 | { |
| 599 | const path = "/usr/local"; |
| 600 | const parsed = parsePathWindows(u8, path); |
| 601 | try testing.expectEqual(.rooted, parsed.kind); |
| 602 | try testing.expectEqualStrings("/", parsed.root); |
| 603 | try testWindowsParsePathHarmony(path); |
| 604 | } |
| 605 | { |
| 606 | const path = "\\\\."; |
| 607 | const parsed = parsePathWindows(u8, path); |
| 608 | try testing.expectEqual(.root_local_device, parsed.kind); |
| 609 | try testing.expectEqualStrings("\\\\.", parsed.root); |
| 610 | try testWindowsParsePathHarmony(path); |
| 611 | } |
| 612 | { |
| 613 | const path = "\\\\.\\a"; |
| 614 | const parsed = parsePathWindows(u8, path); |
| 615 | try testing.expectEqual(.local_device, parsed.kind); |
| 616 | try testing.expectEqualStrings("\\\\.\\", parsed.root); |
| 617 | try testWindowsParsePathHarmony(path); |
| 618 | } |
| 619 | { |
| 620 | const path = "c:../"; |
| 621 | const parsed = parsePathWindows(u8, path); |
| 622 | try testing.expectEqual(.drive_relative, parsed.kind); |
| 623 | try testing.expectEqualStrings("c:", parsed.root); |
| 624 | try testWindowsParsePathHarmony(path); |
| 625 | } |
| 626 | { |
| 627 | const path = "C:\\../"; |
| 628 | const parsed = parsePathWindows(u8, path); |
| 629 | try testing.expectEqual(.drive_absolute, parsed.kind); |
| 630 | try testing.expectEqualStrings("C:\\", parsed.root); |
| 631 | try testWindowsParsePathHarmony(path); |
| 632 | } |
| 633 | { |
| 634 | // Non-ASCII code point that is encoded as one WTF-16 code unit is considered a valid drive letter |
| 635 | const path = "€:\\"; |
| 636 | const parsed = parsePathWindows(u8, path); |
| 637 | try testing.expectEqual(.drive_absolute, parsed.kind); |
| 638 | try testing.expectEqualStrings("€:\\", parsed.root); |
| 639 | try testWindowsParsePathHarmony(path); |
| 640 | } |
| 641 | { |
| 642 | const path = "€:"; |
| 643 | const parsed = parsePathWindows(u8, path); |
| 644 | try testing.expectEqual(.drive_relative, parsed.kind); |
| 645 | try testing.expectEqualStrings("€:", parsed.root); |
| 646 | try testWindowsParsePathHarmony(path); |
| 647 | } |
| 648 | { |
| 649 | // But code points that are encoded as two WTF-16 code units are not |
| 650 | const path = "\u{10000}:\\"; |
| 651 | const parsed = parsePathWindows(u8, path); |
| 652 | try testing.expectEqual(.relative, parsed.kind); |
| 653 | try testing.expectEqualStrings("", parsed.root); |
| 654 | try testWindowsParsePathHarmony(path); |
| 655 | } |
| 656 | { |
| 657 | const path = "\u{10000}:"; |
| 658 | const parsed = parsePathWindows(u8, path); |
| 659 | try testing.expectEqual(.relative, parsed.kind); |
| 660 | try testing.expectEqualStrings("", parsed.root); |
| 661 | try testWindowsParsePathHarmony(path); |
| 662 | } |
| 663 | { |
| 664 | // Paths are assumed to be in the Win32 namespace, so while this is |
| 665 | // likely a NT namespace path, it's treated as a rooted path. |
| 666 | const path = "\\??\\foo"; |
| 667 | const parsed = parsePathWindows(u8, path); |
| 668 | try testing.expectEqual(.rooted, parsed.kind); |
| 669 | try testing.expectEqualStrings("\\", parsed.root); |
| 670 | try testWindowsParsePathHarmony(path); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | fn testWindowsParsePathHarmony(wtf8: []const u8) !void { |
| 675 | var wtf16_buf: [256]u16 = undefined; |
| 676 | const wtf16_len = try std.unicode.wtf8ToWtf16Le(&wtf16_buf, wtf8); |
| 677 | const wtf16 = wtf16_buf[0..wtf16_len]; |
| 678 | |
| 679 | const wtf8_parsed = parsePathWindows(u8, wtf8); |
| 680 | const wtf16_parsed = parsePathWindows(u16, wtf16); |
| 681 | |
| 682 | var wtf8_buf: [256]u8 = undefined; |
| 683 | const wtf16_root_as_wtf8_len = std.unicode.wtf16LeToWtf8(&wtf8_buf, wtf16_parsed.root); |
| 684 | const wtf16_root_as_wtf8 = wtf8_buf[0..wtf16_root_as_wtf8_len]; |
| 685 | |
| 686 | try std.testing.expectEqual(wtf8_parsed.kind, wtf16_parsed.kind); |
| 687 | try std.testing.expectEqualStrings(wtf8_parsed.root, wtf16_root_as_wtf8); |
| 688 | } |
| 689 | |
| 690 | /// Deprecated; use `parsePath` |
| 468 | 691 | pub fn diskDesignator(path: []const u8) []const u8 { |
| 469 | 692 | if (native_os == .windows) { |
| 470 | 693 | return diskDesignatorWindows(path); |
| ... | ... | @@ -473,41 +696,172 @@ pub fn diskDesignator(path: []const u8) []const u8 { |
| 473 | 696 | } |
| 474 | 697 | } |
| 475 | 698 | |
| 699 | /// Deprecated; use `parsePathWindows` |
| 476 | 700 | pub fn diskDesignatorWindows(path: []const u8) []const u8 { |
| 477 | 701 | return windowsParsePath(path).disk_designator; |
| 478 | 702 | } |
| 479 | 703 | |
| 480 | | fn networkShareServersEql(ns1: []const u8, ns2: []const u8) bool { |
| 481 | | const sep1 = ns1[0]; |
| 482 | | const sep2 = ns2[0]; |
| 704 | fn WindowsUNC(comptime T: type) type { |
| 705 | return struct { |
| 706 | server: []const T, |
| 707 | sep_after_server: bool, |
| 708 | share: []const T, |
| 709 | sep_after_share: bool, |
| 710 | }; |
| 711 | } |
| 483 | 712 | |
| 484 | | var it1 = mem.tokenizeScalar(u8, ns1, sep1); |
| 485 | | var it2 = mem.tokenizeScalar(u8, ns2, sep2); |
| 713 | /// Asserts that `path` starts with two path separators |
| 714 | fn parseUNC(comptime T: type, path: []const T) WindowsUNC(T) { |
| 715 | assert(path.len >= 2 and PathType.windows.isSep(T, path[0]) and PathType.windows.isSep(T, path[1])); |
| 716 | const any_sep = switch (T) { |
| 717 | u8 => "/\\", |
| 718 | u16 => std.unicode.wtf8ToWtf16LeStringLiteral("/\\"), |
| 719 | else => @compileError("only u8 (WTF-8) and u16 (WTF-16LE) are supported"), |
| 720 | }; |
| 721 | // For the server, the first path separator after the initial two is always |
| 722 | // the terminator of the server name, even if that means the server name is |
| 723 | // zero-length. |
| 724 | const server_end = mem.indexOfAnyPos(T, path, 2, any_sep) orelse return .{ |
| 725 | .server = path[2..path.len], |
| 726 | .sep_after_server = false, |
| 727 | .share = path[path.len..path.len], |
| 728 | .sep_after_share = false, |
| 729 | }; |
| 730 | // For the share, there can be any number of path separators between the server |
| 731 | // and the share, so we want to skip over all of them instead of just looking for |
| 732 | // the first one. |
| 733 | var it = std.mem.tokenizeAny(T, path[server_end + 1 ..], any_sep); |
| 734 | const share = it.next() orelse return .{ |
| 735 | .server = path[2..server_end], |
| 736 | .sep_after_server = true, |
| 737 | .share = path[server_end + 1 .. server_end + 1], |
| 738 | .sep_after_share = false, |
| 739 | }; |
| 740 | return .{ |
| 741 | .server = path[2..server_end], |
| 742 | .sep_after_server = true, |
| 743 | .share = share, |
| 744 | .sep_after_share = it.index != it.buffer.len, |
| 745 | }; |
| 746 | } |
| 486 | 747 | |
| 487 | | return windows.eqlIgnoreCaseWtf8(it1.next().?, it2.next().?); |
| 748 | test parseUNC { |
| 749 | { |
| 750 | const unc = parseUNC(u8, "//"); |
| 751 | try std.testing.expectEqualStrings("", unc.server); |
| 752 | try std.testing.expect(!unc.sep_after_server); |
| 753 | try std.testing.expectEqualStrings("", unc.share); |
| 754 | try std.testing.expect(!unc.sep_after_share); |
| 755 | } |
| 756 | { |
| 757 | const unc = parseUNC(u8, "\\\\s"); |
| 758 | try std.testing.expectEqualStrings("s", unc.server); |
| 759 | try std.testing.expect(!unc.sep_after_server); |
| 760 | try std.testing.expectEqualStrings("", unc.share); |
| 761 | try std.testing.expect(!unc.sep_after_share); |
| 762 | } |
| 763 | { |
| 764 | const unc = parseUNC(u8, "\\\\s/"); |
| 765 | try std.testing.expectEqualStrings("s", unc.server); |
| 766 | try std.testing.expect(unc.sep_after_server); |
| 767 | try std.testing.expectEqualStrings("", unc.share); |
| 768 | try std.testing.expect(!unc.sep_after_share); |
| 769 | } |
| 770 | { |
| 771 | const unc = parseUNC(u8, "\\/server\\share"); |
| 772 | try std.testing.expectEqualStrings("server", unc.server); |
| 773 | try std.testing.expect(unc.sep_after_server); |
| 774 | try std.testing.expectEqualStrings("share", unc.share); |
| 775 | try std.testing.expect(!unc.sep_after_share); |
| 776 | } |
| 777 | { |
| 778 | const unc = parseUNC(u8, "/\\server\\share/"); |
| 779 | try std.testing.expectEqualStrings("server", unc.server); |
| 780 | try std.testing.expect(unc.sep_after_server); |
| 781 | try std.testing.expectEqualStrings("share", unc.share); |
| 782 | try std.testing.expect(unc.sep_after_share); |
| 783 | } |
| 784 | { |
| 785 | const unc = parseUNC(u8, "\\\\server/\\share\\/"); |
| 786 | try std.testing.expectEqualStrings("server", unc.server); |
| 787 | try std.testing.expect(unc.sep_after_server); |
| 788 | try std.testing.expectEqualStrings("share", unc.share); |
| 789 | try std.testing.expect(unc.sep_after_share); |
| 790 | } |
| 791 | { |
| 792 | const unc = parseUNC(u8, "\\\\server\\/\\\\"); |
| 793 | try std.testing.expectEqualStrings("server", unc.server); |
| 794 | try std.testing.expect(unc.sep_after_server); |
| 795 | try std.testing.expectEqualStrings("", unc.share); |
| 796 | try std.testing.expect(!unc.sep_after_share); |
| 797 | } |
| 488 | 798 | } |
| 489 | 799 | |
| 490 | | fn compareDiskDesignators(kind: WindowsPath.Kind, p1: []const u8, p2: []const u8) bool { |
| 800 | const DiskDesignatorKind = enum { drive, unc }; |
| 801 | |
| 802 | /// `p1` and `p2` are both assumed to be the `kind` provided. |
| 803 | fn compareDiskDesignators(comptime T: type, kind: DiskDesignatorKind, p1: []const T, p2: []const T) bool { |
| 804 | const eql = switch (T) { |
| 805 | u8 => windows.eqlIgnoreCaseWtf8, |
| 806 | u16 => windows.eqlIgnoreCaseWtf16, |
| 807 | else => @compileError("only u8 (WTF-8) and u16 (WTF-16LE) is supported"), |
| 808 | }; |
| 491 | 809 | switch (kind) { |
| 492 | | WindowsPath.Kind.None => { |
| 493 | | assert(p1.len == 0); |
| 494 | | assert(p2.len == 0); |
| 495 | | return true; |
| 496 | | }, |
| 497 | | WindowsPath.Kind.Drive => { |
| 498 | | return ascii.toUpper(p1[0]) == ascii.toUpper(p2[0]); |
| 810 | .drive => { |
| 811 | const drive_letter1 = getDriveLetter(T, p1); |
| 812 | const drive_letter2 = getDriveLetter(T, p2); |
| 813 | |
| 814 | return eql(drive_letter1, drive_letter2); |
| 499 | 815 | }, |
| 500 | | WindowsPath.Kind.NetworkShare => { |
| 501 | | var it1 = mem.tokenizeAny(u8, p1, "/\\"); |
| 502 | | var it2 = mem.tokenizeAny(u8, p2, "/\\"); |
| 816 | .unc => { |
| 817 | var unc1 = parseUNC(T, p1); |
| 818 | var unc2 = parseUNC(T, p2); |
| 503 | 819 | |
| 504 | | return windows.eqlIgnoreCaseWtf8(it1.next().?, it2.next().?) and windows.eqlIgnoreCaseWtf8(it1.next().?, it2.next().?); |
| 820 | return eql(unc1.server, unc2.server) and |
| 821 | eql(unc1.share, unc2.share); |
| 505 | 822 | }, |
| 506 | 823 | } |
| 507 | 824 | } |
| 508 | 825 | |
| 826 | /// `path` is assumed to be drive-relative or drive-absolute. |
| 827 | fn getDriveLetter(comptime T: type, path: []const T) []const T { |
| 828 | const len: usize = switch (T) { |
| 829 | // getWin32PathType will only return .drive_absolute/.drive_relative when there is |
| 830 | // (1) a valid code point, and (2) a code point < U+10000, so we only need to |
| 831 | // get the length determined by the first byte. |
| 832 | u8 => std.unicode.utf8ByteSequenceLength(path[0]) catch unreachable, |
| 833 | u16 => 1, |
| 834 | else => @compileError("unsupported type: " ++ @typeName(T)), |
| 835 | }; |
| 836 | return path[0..len]; |
| 837 | } |
| 838 | |
| 839 | test compareDiskDesignators { |
| 840 | try testCompareDiskDesignators(true, .drive, "c:", "C:\\"); |
| 841 | try testCompareDiskDesignators(true, .drive, "C:\\", "C:"); |
| 842 | try testCompareDiskDesignators(false, .drive, "C:\\", "D:\\"); |
| 843 | // Case-insensitivity technically applies to non-ASCII drive letters |
| 844 | try testCompareDiskDesignators(true, .drive, "λ:\\", "Λ:"); |
| 845 | |
| 846 | try testCompareDiskDesignators(true, .unc, "\\\\server", "//server//"); |
| 847 | try testCompareDiskDesignators(true, .unc, "\\\\server\\\\share", "/\\server/share"); |
| 848 | try testCompareDiskDesignators(true, .unc, "\\\\server\\\\share", "/\\server/share\\\\foo"); |
| 849 | try testCompareDiskDesignators(false, .unc, "\\\\server\\sharefoo", "/\\server/share\\foo"); |
| 850 | try testCompareDiskDesignators(false, .unc, "\\\\serverfoo\\\\share", "//server/share"); |
| 851 | try testCompareDiskDesignators(false, .unc, "\\\\server\\", "//server/share"); |
| 852 | } |
| 853 | |
| 854 | fn testCompareDiskDesignators(expected_result: bool, kind: DiskDesignatorKind, p1: []const u8, p2: []const u8) !void { |
| 855 | var wtf16_buf1: [256]u16 = undefined; |
| 856 | const w1_len = try std.unicode.wtf8ToWtf16Le(&wtf16_buf1, p1); |
| 857 | var wtf16_buf2: [256]u16 = undefined; |
| 858 | const w2_len = try std.unicode.wtf8ToWtf16Le(&wtf16_buf2, p2); |
| 859 | try std.testing.expectEqual(expected_result, compareDiskDesignators(u8, kind, p1, p2)); |
| 860 | try std.testing.expectEqual(expected_result, compareDiskDesignators(u16, kind, wtf16_buf1[0..w1_len], wtf16_buf2[0..w2_len])); |
| 861 | } |
| 862 | |
| 509 | 863 | /// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`. |
| 510 | | pub fn resolve(allocator: Allocator, paths: []const []const u8) ![]u8 { |
| 864 | pub fn resolve(allocator: Allocator, paths: []const []const u8) Allocator.Error![]u8 { |
| 511 | 865 | if (native_os == .windows) { |
| 512 | 866 | return resolveWindows(allocator, paths); |
| 513 | 867 | } else { |
| ... | ... | @@ -516,184 +870,232 @@ pub fn resolve(allocator: Allocator, paths: []const []const u8) ![]u8 { |
| 516 | 870 | } |
| 517 | 871 | |
| 518 | 872 | /// This function is like a series of `cd` statements executed one after another. |
| 519 | | /// It resolves "." and "..", but will not convert relative path to absolute path, use std.fs.Dir.realpath instead. |
| 520 | | /// The result does not have a trailing path separator. |
| 521 | | /// Each drive has its own current working directory. |
| 873 | /// It resolves "." and ".." to the best of its ability, but will not convert relative paths to |
| 874 | /// an absolute path, use std.fs.Dir.realpath instead. |
| 875 | /// ".." components may persist in the resolved path if the resolved path is relative or drive-relative. |
| 522 | 876 | /// Path separators are canonicalized to '\\' and drives are canonicalized to capital letters. |
| 877 | /// |
| 878 | /// The result will not have a trailing path separator, except for the following scenarios: |
| 879 | /// - The resolved path is drive-absolute with no components (e.g. `C:\`). |
| 880 | /// - The resolved path is a UNC path with only a server name, and the input path contained a trailing separator |
| 881 | /// (e.g. `\\server\`). |
| 882 | /// - The resolved path is a UNC path with no components after the share name, and the input path contained a |
| 883 | /// trailing separator (e.g. `\\server\share\`). |
| 884 | /// |
| 885 | /// Each drive has its own current working directory, which is only resolved via the paths provided. |
| 886 | /// In the scenario that the resolved path contains a drive-relative path that can't be resolved using the paths alone, |
| 887 | /// the result will be a drive-relative path. |
| 888 | /// Similarly, in the scenario that the resolved path contains a rooted path that can't be resolved using the paths alone, |
| 889 | /// the result will be a rooted path. |
| 890 | /// |
| 523 | 891 | /// Note: all usage of this function should be audited due to the existence of symlinks. |
| 524 | 892 | /// Without performing actual syscalls, resolving `..` could be incorrect. |
| 525 | 893 | /// This API may break in the future: https://github.com/ziglang/zig/issues/13613 |
| 526 | | pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 { |
| 527 | | assert(paths.len > 0); |
| 528 | | |
| 529 | | // determine which disk designator we will result with, if any |
| 530 | | var result_drive_buf = "_:".*; |
| 531 | | var disk_designator: []const u8 = ""; |
| 532 | | var drive_kind = WindowsPath.Kind.None; |
| 533 | | var have_abs_path = false; |
| 534 | | var first_index: usize = 0; |
| 535 | | for (paths, 0..) |p, i| { |
| 536 | | const parsed = windowsParsePath(p); |
| 537 | | if (parsed.is_abs) { |
| 538 | | have_abs_path = true; |
| 539 | | first_index = i; |
| 894 | pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) Allocator.Error![]u8 { |
| 895 | // Avoid heap allocation when paths.len is <= @bitSizeOf(usize) * 2 |
| 896 | // (we use `* 3` because stackFallback uses 1 usize as a length) |
| 897 | var bit_set_allocator_state = std.heap.stackFallback(@sizeOf(usize) * 3, allocator); |
| 898 | const bit_set_allocator = bit_set_allocator_state.get(); |
| 899 | var relevant_paths = try std.bit_set.DynamicBitSetUnmanaged.initEmpty(bit_set_allocator, paths.len); |
| 900 | defer relevant_paths.deinit(bit_set_allocator); |
| 901 | |
| 902 | // Iterate the paths backwards, marking the relevant paths along the way. |
| 903 | // This also allows us to break from the loop whenever any earlier paths are known to be irrelevant. |
| 904 | var first_path_i: usize = paths.len; |
| 905 | const effective_root_path: WindowsPath2(u8) = root: { |
| 906 | var last_effective_root_path: WindowsPath2(u8) = .{ .kind = .relative, .root = "" }; |
| 907 | var last_rooted_path_i: ?usize = null; |
| 908 | var last_drive_relative_path_i: usize = undefined; |
| 909 | while (first_path_i > 0) { |
| 910 | first_path_i -= 1; |
| 911 | const parsed = parsePathWindows(u8, paths[first_path_i]); |
| 912 | switch (parsed.kind) { |
| 913 | .unc_absolute, .root_local_device, .local_device => { |
| 914 | switch (last_effective_root_path.kind) { |
| 915 | .rooted => {}, |
| 916 | .drive_relative => continue, |
| 917 | else => { |
| 918 | relevant_paths.set(first_path_i); |
| 919 | }, |
| 920 | } |
| 921 | break :root parsed; |
| 922 | }, |
| 923 | .drive_relative, .drive_absolute => { |
| 924 | switch (last_effective_root_path.kind) { |
| 925 | .drive_relative => if (!compareDiskDesignators(u8, .drive, parsed.root, last_effective_root_path.root)) { |
| 926 | continue; |
| 927 | } else if (last_rooted_path_i != null) { |
| 928 | break :root .{ .kind = .drive_absolute, .root = parsed.root }; |
| 929 | }, |
| 930 | .relative => last_effective_root_path = parsed, |
| 931 | .rooted => { |
| 932 | // This is the end of the line, since the rooted path will always be relative |
| 933 | // to this drive letter, and even if the current path is drive-relative, the |
| 934 | // rooted-ness makes that irrelevant. |
| 935 | // |
| 936 | // Therefore, force the kind of the effective root to be drive-absolute in order to |
| 937 | // properly resolve a rooted path against a drive-relative one, as the result should |
| 938 | // always be drive-absolute. |
| 939 | break :root .{ .kind = .drive_absolute, .root = parsed.root }; |
| 940 | }, |
| 941 | .drive_absolute, .unc_absolute, .root_local_device, .local_device => unreachable, |
| 942 | } |
| 943 | relevant_paths.set(first_path_i); |
| 944 | last_drive_relative_path_i = first_path_i; |
| 945 | if (parsed.kind == .drive_absolute) { |
| 946 | break :root parsed; |
| 947 | } |
| 948 | }, |
| 949 | .relative => { |
| 950 | switch (last_effective_root_path.kind) { |
| 951 | .rooted => continue, |
| 952 | .relative => last_effective_root_path = parsed, |
| 953 | else => {}, |
| 954 | } |
| 955 | relevant_paths.set(first_path_i); |
| 956 | }, |
| 957 | .rooted => { |
| 958 | switch (last_effective_root_path.kind) { |
| 959 | .drive_relative => {}, |
| 960 | .relative => last_effective_root_path = parsed, |
| 961 | .rooted => continue, |
| 962 | .drive_absolute, .unc_absolute, .root_local_device, .local_device => unreachable, |
| 963 | } |
| 964 | if (last_rooted_path_i == null) { |
| 965 | last_rooted_path_i = first_path_i; |
| 966 | relevant_paths.set(first_path_i); |
| 967 | } |
| 968 | }, |
| 969 | } |
| 540 | 970 | } |
| 541 | | switch (parsed.kind) { |
| 542 | | .Drive => { |
| 543 | | result_drive_buf[0] = ascii.toUpper(parsed.disk_designator[0]); |
| 544 | | disk_designator = result_drive_buf[0..]; |
| 545 | | drive_kind = WindowsPath.Kind.Drive; |
| 546 | | }, |
| 547 | | .NetworkShare => { |
| 548 | | disk_designator = parsed.disk_designator; |
| 549 | | drive_kind = WindowsPath.Kind.NetworkShare; |
| 550 | | }, |
| 551 | | .None => {}, |
| 971 | // After iterating, if the pending effective root is drive-relative then that means |
| 972 | // nothing has led to forcing a drive-absolute root (a path that allows resolving the |
| 973 | // drive-specific CWD would cause an early break), so we now need to ignore all paths |
| 974 | // before the most recent drive-relative one. For example, if we're resolving |
| 975 | // { "\\rooted", "relative", "C:drive-relative" } |
| 976 | // then the `\rooted` and `relative` needs to be ignored since we can't |
| 977 | // know what the rooted path is rooted against as that'd require knowing the CWD. |
| 978 | if (last_effective_root_path.kind == .drive_relative) { |
| 979 | for (0..last_drive_relative_path_i) |i| { |
| 980 | relevant_paths.unset(i); |
| 981 | } |
| 552 | 982 | } |
| 553 | | } |
| 983 | break :root last_effective_root_path; |
| 984 | }; |
| 554 | 985 | |
| 555 | | // if we will result with a disk designator, loop again to determine |
| 556 | | // which is the last time the disk designator is absolutely specified, if any |
| 557 | | // and count up the max bytes for paths related to this disk designator |
| 558 | | if (drive_kind != WindowsPath.Kind.None) { |
| 559 | | have_abs_path = false; |
| 560 | | first_index = 0; |
| 561 | | var correct_disk_designator = false; |
| 562 | | |
| 563 | | for (paths, 0..) |p, i| { |
| 564 | | const parsed = windowsParsePath(p); |
| 565 | | if (parsed.kind != WindowsPath.Kind.None) { |
| 566 | | if (parsed.kind == drive_kind) { |
| 567 | | correct_disk_designator = compareDiskDesignators(drive_kind, disk_designator, parsed.disk_designator); |
| 568 | | } else { |
| 569 | | continue; |
| 570 | | } |
| 986 | var result: std.ArrayList(u8) = .empty; |
| 987 | defer result.deinit(allocator); |
| 988 | |
| 989 | var want_path_sep_between_root_and_component = false; |
| 990 | switch (effective_root_path.kind) { |
| 991 | .root_local_device, .local_device => { |
| 992 | try result.ensureUnusedCapacity(allocator, 3); |
| 993 | result.appendSliceAssumeCapacity("\\\\"); |
| 994 | result.appendAssumeCapacity(effective_root_path.root[2]); // . or ? |
| 995 | want_path_sep_between_root_and_component = true; |
| 996 | }, |
| 997 | .drive_absolute, .drive_relative => { |
| 998 | try result.ensureUnusedCapacity(allocator, effective_root_path.root.len); |
| 999 | result.appendAssumeCapacity(std.ascii.toUpper(effective_root_path.root[0])); |
| 1000 | result.appendAssumeCapacity(':'); |
| 1001 | if (effective_root_path.kind == .drive_absolute) { |
| 1002 | result.appendAssumeCapacity('\\'); |
| 571 | 1003 | } |
| 572 | | if (!correct_disk_designator) { |
| 573 | | continue; |
| 1004 | }, |
| 1005 | .unc_absolute => { |
| 1006 | const unc = parseUNC(u8, effective_root_path.root); |
| 1007 | |
| 1008 | const root_len = len: { |
| 1009 | var len: usize = 2 + unc.server.len + unc.share.len; |
| 1010 | if (unc.sep_after_server) len += 1; |
| 1011 | if (unc.sep_after_share) len += 1; |
| 1012 | break :len len; |
| 1013 | }; |
| 1014 | try result.ensureUnusedCapacity(allocator, root_len); |
| 1015 | result.appendSliceAssumeCapacity("\\\\"); |
| 1016 | if (unc.server.len > 0 or unc.sep_after_server) { |
| 1017 | result.appendSliceAssumeCapacity(unc.server); |
| 1018 | if (unc.sep_after_server) |
| 1019 | result.appendAssumeCapacity('\\') |
| 1020 | else |
| 1021 | want_path_sep_between_root_and_component = true; |
| 574 | 1022 | } |
| 575 | | if (parsed.is_abs) { |
| 576 | | first_index = i; |
| 577 | | have_abs_path = true; |
| 1023 | if (unc.share.len > 0) { |
| 1024 | result.appendSliceAssumeCapacity(unc.share); |
| 1025 | if (unc.sep_after_share) |
| 1026 | result.appendAssumeCapacity('\\') |
| 1027 | else |
| 1028 | want_path_sep_between_root_and_component = true; |
| 578 | 1029 | } |
| 579 | | } |
| 1030 | }, |
| 1031 | .rooted => { |
| 1032 | try result.append(allocator, '\\'); |
| 1033 | }, |
| 1034 | .relative => {}, |
| 580 | 1035 | } |
| 581 | 1036 | |
| 582 | | // Allocate result and fill in the disk designator. |
| 583 | | var result = std.array_list.Managed(u8).init(allocator); |
| 584 | | defer result.deinit(); |
| 585 | | |
| 586 | | const disk_designator_len: usize = l: { |
| 587 | | if (!have_abs_path) break :l 0; |
| 588 | | switch (drive_kind) { |
| 589 | | .Drive => { |
| 590 | | try result.appendSlice(disk_designator); |
| 591 | | break :l disk_designator.len; |
| 592 | | }, |
| 593 | | .NetworkShare => { |
| 594 | | var it = mem.tokenizeAny(u8, paths[first_index], "/\\"); |
| 595 | | const server_name = it.next().?; |
| 596 | | const other_name = it.next().?; |
| 597 | | |
| 598 | | try result.ensureUnusedCapacity(2 + 1 + server_name.len + other_name.len); |
| 599 | | result.appendSliceAssumeCapacity("\\\\"); |
| 600 | | result.appendSliceAssumeCapacity(server_name); |
| 601 | | result.appendAssumeCapacity('\\'); |
| 602 | | result.appendSliceAssumeCapacity(other_name); |
| 603 | | |
| 604 | | break :l result.items.len; |
| 605 | | }, |
| 606 | | .None => { |
| 607 | | break :l 1; |
| 608 | | }, |
| 609 | | } |
| 610 | | }; |
| 611 | | |
| 612 | | var correct_disk_designator = true; |
| 1037 | const root_len = result.items.len; |
| 613 | 1038 | var negative_count: usize = 0; |
| 1039 | for (paths[first_path_i..], first_path_i..) |path, i| { |
| 1040 | if (!relevant_paths.isSet(i)) continue; |
| 614 | 1041 | |
| 615 | | for (paths[first_index..]) |p| { |
| 616 | | const parsed = windowsParsePath(p); |
| 617 | | |
| 618 | | if (parsed.kind != .None) { |
| 619 | | if (parsed.kind == drive_kind) { |
| 620 | | const dd = result.items[0..disk_designator_len]; |
| 621 | | correct_disk_designator = compareDiskDesignators(drive_kind, dd, parsed.disk_designator); |
| 622 | | } else { |
| 623 | | continue; |
| 624 | | } |
| 625 | | } |
| 626 | | if (!correct_disk_designator) { |
| 627 | | continue; |
| 628 | | } |
| 629 | | var it = mem.tokenizeAny(u8, p[parsed.disk_designator.len..], "/\\"); |
| 1042 | const parsed = parsePathWindows(u8, path); |
| 1043 | const skip_len = parsed.root.len; |
| 1044 | var it = mem.tokenizeAny(u8, path[skip_len..], "/\\"); |
| 630 | 1045 | while (it.next()) |component| { |
| 631 | 1046 | if (mem.eql(u8, component, ".")) { |
| 632 | 1047 | continue; |
| 633 | 1048 | } else if (mem.eql(u8, component, "..")) { |
| 634 | | if (result.items.len == 0) { |
| 1049 | if (result.items.len == 0 or (result.items.len == root_len and effective_root_path.kind == .drive_relative)) { |
| 635 | 1050 | negative_count += 1; |
| 636 | 1051 | continue; |
| 637 | 1052 | } |
| 638 | 1053 | while (true) { |
| 639 | | if (result.items.len == disk_designator_len) { |
| 1054 | if (result.items.len == root_len) { |
| 640 | 1055 | break; |
| 641 | 1056 | } |
| 642 | | const end_with_sep = switch (result.items[result.items.len - 1]) { |
| 643 | | '\\', '/' => true, |
| 644 | | else => false, |
| 645 | | }; |
| 1057 | const end_with_sep = PathType.windows.isSep(u8, result.items[result.items.len - 1]); |
| 646 | 1058 | result.items.len -= 1; |
| 647 | | if (end_with_sep or result.items.len == 0) break; |
| 1059 | if (end_with_sep) break; |
| 648 | 1060 | } |
| 649 | | } else if (!have_abs_path and result.items.len == 0) { |
| 650 | | try result.appendSlice(component); |
| 1061 | } else if (result.items.len == root_len and !want_path_sep_between_root_and_component) { |
| 1062 | try result.appendSlice(allocator, component); |
| 651 | 1063 | } else { |
| 652 | | try result.ensureUnusedCapacity(1 + component.len); |
| 1064 | try result.ensureUnusedCapacity(allocator, 1 + component.len); |
| 653 | 1065 | result.appendAssumeCapacity('\\'); |
| 654 | 1066 | result.appendSliceAssumeCapacity(component); |
| 655 | 1067 | } |
| 656 | 1068 | } |
| 657 | 1069 | } |
| 658 | 1070 | |
| 659 | | if (disk_designator_len != 0 and result.items.len == disk_designator_len) { |
| 660 | | try result.append('\\'); |
| 661 | | return result.toOwnedSlice(); |
| 1071 | if (root_len != 0 and result.items.len == root_len and negative_count == 0) { |
| 1072 | return result.toOwnedSlice(allocator); |
| 662 | 1073 | } |
| 663 | 1074 | |
| 664 | | if (result.items.len == 0) { |
| 1075 | if (result.items.len == root_len) { |
| 665 | 1076 | if (negative_count == 0) { |
| 666 | 1077 | return allocator.dupe(u8, "."); |
| 667 | | } else { |
| 668 | | const real_result = try allocator.alloc(u8, 3 * negative_count - 1); |
| 669 | | var count = negative_count - 1; |
| 670 | | var i: usize = 0; |
| 671 | | while (count > 0) : (count -= 1) { |
| 672 | | real_result[i..][0..3].* = "..\\".*; |
| 673 | | i += 3; |
| 674 | | } |
| 675 | | real_result[i..][0..2].* = "..".*; |
| 676 | | return real_result; |
| 677 | 1078 | } |
| 678 | | } |
| 679 | 1079 | |
| 680 | | if (negative_count == 0) { |
| 681 | | return result.toOwnedSlice(); |
| 1080 | try result.ensureTotalCapacityPrecise(allocator, 3 * negative_count - 1); |
| 1081 | for (0..negative_count - 1) |_| { |
| 1082 | result.appendSliceAssumeCapacity("..\\"); |
| 1083 | } |
| 1084 | result.appendSliceAssumeCapacity(".."); |
| 682 | 1085 | } else { |
| 683 | | const real_result = try allocator.alloc(u8, 3 * negative_count + result.items.len); |
| 684 | | var count = negative_count; |
| 685 | | var i: usize = 0; |
| 686 | | while (count > 0) : (count -= 1) { |
| 687 | | real_result[i..][0..3].* = "..\\".*; |
| 688 | | i += 3; |
| 1086 | const dest = try result.addManyAt(allocator, root_len, 3 * negative_count); |
| 1087 | for (0..negative_count) |i| { |
| 1088 | dest[i * 3 ..][0..3].* = "..\\".*; |
| 689 | 1089 | } |
| 690 | | @memcpy(real_result[i..][0..result.items.len], result.items); |
| 691 | | return real_result; |
| 692 | 1090 | } |
| 1091 | |
| 1092 | return result.toOwnedSlice(allocator); |
| 693 | 1093 | } |
| 694 | 1094 | |
| 695 | 1095 | /// This function is like a series of `cd` statements executed one after another. |
| 696 | | /// It resolves "." and "..", but will not convert relative path to absolute path, use std.fs.Dir.realpath instead. |
| 1096 | /// It resolves "." and ".." to the best of its ability, but will not convert relative paths to |
| 1097 | /// an absolute path, use std.fs.Dir.realpath instead. |
| 1098 | /// ".." components may persist in the resolved path if the resolved path is relative. |
| 697 | 1099 | /// The result does not have a trailing path separator. |
| 698 | 1100 | /// This function does not perform any syscalls. Executing this series of path |
| 699 | 1101 | /// lookups on the actual filesystem may produce different results due to |
| ... | ... | @@ -772,10 +1174,14 @@ pub fn resolvePosix(allocator: Allocator, paths: []const []const u8) Allocator.E |
| 772 | 1174 | } |
| 773 | 1175 | |
| 774 | 1176 | test resolve { |
| 1177 | try testResolveWindows(&[_][]const u8{ "a", "..\\..\\.." }, "..\\.."); |
| 1178 | try testResolveWindows(&[_][]const u8{ "..", "", "..\\..\\foo" }, "..\\..\\..\\foo"); |
| 775 | 1179 | try testResolveWindows(&[_][]const u8{ "a\\b\\c\\", "..\\..\\.." }, "."); |
| 776 | 1180 | try testResolveWindows(&[_][]const u8{"."}, "."); |
| 777 | 1181 | try testResolveWindows(&[_][]const u8{""}, "."); |
| 778 | 1182 | |
| 1183 | try testResolvePosix(&[_][]const u8{ "a", "../../.." }, "../.."); |
| 1184 | try testResolvePosix(&[_][]const u8{ "..", "", "../../foo" }, "../../../foo"); |
| 779 | 1185 | try testResolvePosix(&[_][]const u8{ "a/b/c/", "../../.." }, "."); |
| 780 | 1186 | try testResolvePosix(&[_][]const u8{"."}, "."); |
| 781 | 1187 | try testResolvePosix(&[_][]const u8{""}, "."); |
| ... | ... | @@ -792,22 +1198,81 @@ test resolveWindows { |
| 792 | 1198 | ); |
| 793 | 1199 | |
| 794 | 1200 | try testResolveWindows(&[_][]const u8{ "c:\\a\\b\\c", "/hi", "ok" }, "C:\\hi\\ok"); |
| 1201 | try testResolveWindows(&[_][]const u8{ "c:\\a\\b\\c\\", ".\\..\\foo" }, "C:\\a\\b\\foo"); |
| 795 | 1202 | try testResolveWindows(&[_][]const u8{ "c:/blah\\blah", "d:/games", "c:../a" }, "C:\\blah\\a"); |
| 796 | 1203 | try testResolveWindows(&[_][]const u8{ "c:/blah\\blah", "d:/games", "C:../a" }, "C:\\blah\\a"); |
| 797 | 1204 | try testResolveWindows(&[_][]const u8{ "c:/ignore", "d:\\a/b\\c/d", "\\e.exe" }, "D:\\e.exe"); |
| 798 | 1205 | try testResolveWindows(&[_][]const u8{ "c:/ignore", "c:/some/file" }, "C:\\some\\file"); |
| 799 | | try testResolveWindows(&[_][]const u8{ "d:/ignore", "d:some/dir//" }, "D:\\ignore\\some\\dir"); |
| 1206 | // The first path "sets" the CWD, so the drive-relative path is then relative to that. |
| 1207 | try testResolveWindows(&[_][]const u8{ "d:/foo", "d:some/dir//", "D:another" }, "D:\\foo\\some\\dir\\another"); |
| 800 | 1208 | try testResolveWindows(&[_][]const u8{ "//server/share", "..", "relative\\" }, "\\\\server\\share\\relative"); |
| 801 | 1209 | try testResolveWindows(&[_][]const u8{ "\\\\server/share", "..", "relative\\" }, "\\\\server\\share\\relative"); |
| 802 | | try testResolveWindows(&[_][]const u8{ "c:/", "//" }, "C:\\"); |
| 803 | | try testResolveWindows(&[_][]const u8{ "c:/", "//dir" }, "C:\\dir"); |
| 804 | | try testResolveWindows(&[_][]const u8{ "c:/", "//server/share" }, "\\\\server\\share\\"); |
| 805 | | try testResolveWindows(&[_][]const u8{ "c:/", "//server//share" }, "\\\\server\\share\\"); |
| 806 | | try testResolveWindows(&[_][]const u8{ "c:/", "///some//dir" }, "C:\\some\\dir"); |
| 1210 | try testResolveWindows(&[_][]const u8{ "\\\\server/share/ignore", "//server/share/bar" }, "\\\\server\\share\\bar"); |
| 1211 | try testResolveWindows(&[_][]const u8{ "\\/server\\share/", "..", "relative" }, "\\\\server\\share\\relative"); |
| 1212 | try testResolveWindows(&[_][]const u8{ "\\\\server\\share", "C:drive-relative" }, "C:drive-relative"); |
| 1213 | try testResolveWindows(&[_][]const u8{ "c:/", "//" }, "\\\\"); |
| 1214 | try testResolveWindows(&[_][]const u8{ "c:/", "//server" }, "\\\\server"); |
| 1215 | try testResolveWindows(&[_][]const u8{ "c:/", "//server/share" }, "\\\\server\\share"); |
| 1216 | try testResolveWindows(&[_][]const u8{ "c:/", "//server//share////" }, "\\\\server\\share\\"); |
| 1217 | try testResolveWindows(&[_][]const u8{ "c:/", "///some//dir" }, "\\\\\\some\\dir"); |
| 1218 | try testResolveWindows(&[_][]const u8{ "c:foo", "bar" }, "C:foo\\bar"); |
| 807 | 1219 | try testResolveWindows(&[_][]const u8{ "C:\\foo\\tmp.3\\", "..\\tmp.3\\cycles\\root.js" }, "C:\\foo\\tmp.3\\cycles\\root.js"); |
| 1220 | // Drive-relative stays drive-relative if there's nothing to provide the drive-specific CWD |
| 1221 | try testResolveWindows(&[_][]const u8{ "relative", "d:foo" }, "D:foo"); |
| 1222 | try testResolveWindows(&[_][]const u8{ "../..\\..", "d:foo" }, "D:foo"); |
| 1223 | try testResolveWindows(&[_][]const u8{ "../..\\..", "\\rooted", "d:foo" }, "D:foo"); |
| 1224 | try testResolveWindows(&[_][]const u8{ "C:\\foo", "../..\\..", "\\rooted", "d:foo" }, "D:foo"); |
| 1225 | try testResolveWindows(&[_][]const u8{ "D:relevant", "../..\\..", "d:foo" }, "D:..\\..\\foo"); |
| 1226 | try testResolveWindows(&[_][]const u8{ "D:relevant", "../..\\..", "\\\\.\\ignored", "C:\\ignored", "C:ignored", "\\\\ignored", "d:foo" }, "D:..\\..\\foo"); |
| 1227 | try testResolveWindows(&[_][]const u8{ "ignored", "\\\\.\\ignored", "C:\\ignored", "C:ignored", "\\\\ignored", "d:foo" }, "D:foo"); |
| 1228 | // Rooted paths remain rooted if there's no absolute path available to resolve the "root" |
| 1229 | try testResolveWindows(&[_][]const u8{ "/foo", "bar" }, "\\foo\\bar"); |
| 1230 | // Rooted against a UNC path |
| 1231 | try testResolveWindows(&[_][]const u8{ "//server/share/ignore", "/foo", "bar" }, "\\\\server\\share\\foo\\bar"); |
| 1232 | try testResolveWindows(&[_][]const u8{ "//server/share/", "/foo" }, "\\\\server\\share\\foo"); |
| 1233 | try testResolveWindows(&[_][]const u8{ "//server/share", "/foo" }, "\\\\server\\share\\foo"); |
| 1234 | try testResolveWindows(&[_][]const u8{ "//server/", "/foo" }, "\\\\server\\foo"); |
| 1235 | try testResolveWindows(&[_][]const u8{ "//server", "/foo" }, "\\\\server\\foo"); |
| 1236 | try testResolveWindows(&[_][]const u8{ "//", "/foo" }, "\\\\foo"); |
| 1237 | // Rooted against a drive-relative path |
| 1238 | try testResolveWindows(&[_][]const u8{ "C:", "/foo", "bar" }, "C:\\foo\\bar"); |
| 1239 | try testResolveWindows(&[_][]const u8{ "C:\\ignore", "C:", "/foo", "bar" }, "C:\\foo\\bar"); |
| 1240 | try testResolveWindows(&[_][]const u8{ "C:\\ignore", "\\foo", "C:bar" }, "C:\\foo\\bar"); |
| 1241 | // Only the last rooted path is relevant |
| 1242 | try testResolveWindows(&[_][]const u8{ "\\ignore", "\\foo" }, "\\foo"); |
| 1243 | try testResolveWindows(&[_][]const u8{ "c:ignore", "ignore", "\\ignore", "\\foo" }, "C:\\foo"); |
| 1244 | // Rooted is only relevant to a drive-relative if there's a previous drive-* path |
| 1245 | try testResolveWindows(&[_][]const u8{ "\\ignore", "C:foo" }, "C:foo"); |
| 1246 | try testResolveWindows(&[_][]const u8{ "\\ignore", "\\ignore2", "C:foo" }, "C:foo"); |
| 1247 | try testResolveWindows(&[_][]const u8{ "c:ignore", "\\ignore", "\\rooted", "C:foo" }, "C:\\rooted\\foo"); |
| 1248 | try testResolveWindows(&[_][]const u8{ "c:\\ignore", "\\ignore", "\\rooted", "C:foo" }, "C:\\rooted\\foo"); |
| 1249 | try testResolveWindows(&[_][]const u8{ "d:\\ignore", "\\ignore", "\\ignore2", "C:foo" }, "C:foo"); |
| 1250 | // Root local device paths |
| 1251 | try testResolveWindows(&[_][]const u8{"\\/."}, "\\\\."); |
| 1252 | try testResolveWindows(&[_][]const u8{ "\\/.", "C:drive-relative" }, "C:drive-relative"); |
| 1253 | try testResolveWindows(&[_][]const u8{"/\\?"}, "\\\\?"); |
| 1254 | try testResolveWindows(&[_][]const u8{ "ignore", "c:\\ignore", "\\\\.", "foo" }, "\\\\.\\foo"); |
| 1255 | try testResolveWindows(&[_][]const u8{ "ignore", "c:\\ignore", "\\\\?", "foo" }, "\\\\?\\foo"); |
| 1256 | try testResolveWindows(&[_][]const u8{ "ignore", "c:\\ignore", "//.", "ignore", "\\foo" }, "\\\\.\\foo"); |
| 1257 | try testResolveWindows(&[_][]const u8{ "ignore", "c:\\ignore", "\\\\?", "ignore", "\\foo" }, "\\\\?\\foo"); |
| 808 | 1258 | |
| 809 | 1259 | // Keep relative paths relative. |
| 810 | 1260 | try testResolveWindows(&[_][]const u8{"a/b"}, "a\\b"); |
| 1261 | try testResolveWindows(&[_][]const u8{".."}, ".."); |
| 1262 | try testResolveWindows(&[_][]const u8{"../.."}, "..\\.."); |
| 1263 | try testResolveWindows(&[_][]const u8{ "C:foo", "../.." }, "C:.."); |
| 1264 | try testResolveWindows(&[_][]const u8{ "d:foo", "../..\\.." }, "D:..\\.."); |
| 1265 | |
| 1266 | // Local device paths treat the \\.\ or \\?\ as the "root", everything afterwards is treated as a regular component. |
| 1267 | try testResolveWindows(&[_][]const u8{ "\\\\?\\C:\\foo", "../bar", "baz" }, "\\\\?\\C:\\bar\\baz"); |
| 1268 | try testResolveWindows(&[_][]const u8{ "\\\\.\\C:/foo", "../../../../bar", "baz" }, "\\\\.\\bar\\baz"); |
| 1269 | try testResolveWindows(&[_][]const u8{ "//./C:/foo", "../../../../bar", "baz" }, "\\\\.\\bar\\baz"); |
| 1270 | try testResolveWindows(&[_][]const u8{ "\\\\.\\foo", ".." }, "\\\\."); |
| 1271 | try testResolveWindows(&[_][]const u8{ "\\\\.\\foo", "..\\.." }, "\\\\."); |
| 1272 | |
| 1273 | // Paths are assumed to be Win32, so paths that are likely NT paths are treated as a rooted path. |
| 1274 | try testResolveWindows(&[_][]const u8{ "\\??\\C:\\foo", "/bar", "baz" }, "\\bar\\baz"); |
| 1275 | try testResolveWindows(&[_][]const u8{ "C:\\", "\\??\\C:\\foo", "bar" }, "C:\\??\\C:\\foo\\bar"); |
| 811 | 1276 | } |
| 812 | 1277 | |
| 813 | 1278 | test resolvePosix { |
| ... | ... | @@ -855,63 +1320,18 @@ pub fn dirname(path: []const u8) ?[]const u8 { |
| 855 | 1320 | } |
| 856 | 1321 | |
| 857 | 1322 | pub fn dirnameWindows(path: []const u8) ?[]const u8 { |
| 858 | | if (path.len == 0) |
| 859 | | return null; |
| 860 | | |
| 861 | | const root_slice = diskDesignatorWindows(path); |
| 862 | | if (path.len == root_slice.len) |
| 863 | | return null; |
| 864 | | |
| 865 | | const have_root_slash = path.len > root_slice.len and (path[root_slice.len] == '/' or path[root_slice.len] == '\\'); |
| 866 | | |
| 867 | | var end_index: usize = path.len - 1; |
| 868 | | |
| 869 | | while (path[end_index] == '/' or path[end_index] == '\\') { |
| 870 | | if (end_index == 0) |
| 871 | | return null; |
| 872 | | end_index -= 1; |
| 873 | | } |
| 874 | | |
| 875 | | while (path[end_index] != '/' and path[end_index] != '\\') { |
| 876 | | if (end_index == 0) |
| 877 | | return null; |
| 878 | | end_index -= 1; |
| 879 | | } |
| 880 | | |
| 881 | | if (have_root_slash and end_index == root_slice.len) { |
| 882 | | end_index += 1; |
| 883 | | } |
| 884 | | |
| 885 | | if (end_index == 0) |
| 886 | | return null; |
| 887 | | |
| 888 | | return path[0..end_index]; |
| 1323 | return dirnameInner(.windows, path); |
| 889 | 1324 | } |
| 890 | 1325 | |
| 891 | 1326 | pub fn dirnamePosix(path: []const u8) ?[]const u8 { |
| 892 | | if (path.len == 0) |
| 893 | | return null; |
| 894 | | |
| 895 | | var end_index: usize = path.len - 1; |
| 896 | | while (path[end_index] == '/') { |
| 897 | | if (end_index == 0) |
| 898 | | return null; |
| 899 | | end_index -= 1; |
| 900 | | } |
| 901 | | |
| 902 | | while (path[end_index] != '/') { |
| 903 | | if (end_index == 0) |
| 904 | | return null; |
| 905 | | end_index -= 1; |
| 906 | | } |
| 907 | | |
| 908 | | if (end_index == 0 and path[0] == '/') |
| 909 | | return path[0..1]; |
| 910 | | |
| 911 | | if (end_index == 0) |
| 912 | | return null; |
| 1327 | return dirnameInner(.posix, path); |
| 1328 | } |
| 913 | 1329 | |
| 914 | | return path[0..end_index]; |
| 1330 | fn dirnameInner(comptime path_type: PathType, path: []const u8) ?[]const u8 { |
| 1331 | var it = ComponentIterator(path_type, u8).init(path); |
| 1332 | _ = it.last() orelse return null; |
| 1333 | const up = it.previous() orelse return it.root(); |
| 1334 | return up.path; |
| 915 | 1335 | } |
| 916 | 1336 | |
| 917 | 1337 | test dirnamePosix { |
| ... | ... | @@ -930,11 +1350,12 @@ test dirnamePosix { |
| 930 | 1350 | |
| 931 | 1351 | test dirnameWindows { |
| 932 | 1352 | try testDirnameWindows("c:\\", null); |
| 1353 | try testDirnameWindows("c:\\\\", null); |
| 933 | 1354 | try testDirnameWindows("c:\\foo", "c:\\"); |
| 934 | | try testDirnameWindows("c:\\foo\\", "c:\\"); |
| 1355 | try testDirnameWindows("c:\\\\foo\\", "c:\\"); |
| 935 | 1356 | try testDirnameWindows("c:\\foo\\bar", "c:\\foo"); |
| 936 | 1357 | try testDirnameWindows("c:\\foo\\bar\\", "c:\\foo"); |
| 937 | | try testDirnameWindows("c:\\foo\\bar\\baz", "c:\\foo\\bar"); |
| 1358 | try testDirnameWindows("c:\\\\foo\\bar\\baz", "c:\\\\foo\\bar"); |
| 938 | 1359 | try testDirnameWindows("\\", null); |
| 939 | 1360 | try testDirnameWindows("\\foo", "\\"); |
| 940 | 1361 | try testDirnameWindows("\\foo\\", "\\"); |
| ... | ... | @@ -942,19 +1363,30 @@ test dirnameWindows { |
| 942 | 1363 | try testDirnameWindows("\\foo\\bar\\", "\\foo"); |
| 943 | 1364 | try testDirnameWindows("\\foo\\bar\\baz", "\\foo\\bar"); |
| 944 | 1365 | try testDirnameWindows("c:", null); |
| 945 | | try testDirnameWindows("c:foo", null); |
| 946 | | try testDirnameWindows("c:foo\\", null); |
| 1366 | try testDirnameWindows("c:foo", "c:"); |
| 1367 | try testDirnameWindows("c:foo\\", "c:"); |
| 947 | 1368 | try testDirnameWindows("c:foo\\bar", "c:foo"); |
| 948 | 1369 | try testDirnameWindows("c:foo\\bar\\", "c:foo"); |
| 949 | 1370 | try testDirnameWindows("c:foo\\bar\\baz", "c:foo\\bar"); |
| 950 | 1371 | try testDirnameWindows("file:stream", null); |
| 951 | 1372 | try testDirnameWindows("dir\\file:stream", "dir"); |
| 952 | 1373 | try testDirnameWindows("\\\\unc\\share", null); |
| 1374 | try testDirnameWindows("\\\\unc\\share\\\\", null); |
| 953 | 1375 | try testDirnameWindows("\\\\unc\\share\\foo", "\\\\unc\\share\\"); |
| 954 | 1376 | try testDirnameWindows("\\\\unc\\share\\foo\\", "\\\\unc\\share\\"); |
| 955 | 1377 | try testDirnameWindows("\\\\unc\\share\\foo\\bar", "\\\\unc\\share\\foo"); |
| 956 | 1378 | try testDirnameWindows("\\\\unc\\share\\foo\\bar\\", "\\\\unc\\share\\foo"); |
| 957 | 1379 | try testDirnameWindows("\\\\unc\\share\\foo\\bar\\baz", "\\\\unc\\share\\foo\\bar"); |
| 1380 | try testDirnameWindows("\\\\.", null); |
| 1381 | try testDirnameWindows("\\\\.\\", null); |
| 1382 | try testDirnameWindows("\\\\.\\device", "\\\\.\\"); |
| 1383 | try testDirnameWindows("\\\\.\\device\\", "\\\\.\\"); |
| 1384 | try testDirnameWindows("\\\\.\\device\\foo", "\\\\.\\device"); |
| 1385 | try testDirnameWindows("\\\\?", null); |
| 1386 | try testDirnameWindows("\\\\?\\", null); |
| 1387 | try testDirnameWindows("\\\\?\\device", "\\\\?\\"); |
| 1388 | try testDirnameWindows("\\\\?\\device\\", "\\\\?\\"); |
| 1389 | try testDirnameWindows("\\\\?\\device\\foo", "\\\\?\\device"); |
| 958 | 1390 | try testDirnameWindows("/a/b/", "/a"); |
| 959 | 1391 | try testDirnameWindows("/a/b", "/a"); |
| 960 | 1392 | try testDirnameWindows("/a", "/"); |
| ... | ... | @@ -974,7 +1406,7 @@ fn testDirnamePosix(input: []const u8, expected_output: ?[]const u8) !void { |
| 974 | 1406 | |
| 975 | 1407 | fn testDirnameWindows(input: []const u8, expected_output: ?[]const u8) !void { |
| 976 | 1408 | if (dirnameWindows(input)) |output| { |
| 977 | | try testing.expect(mem.eql(u8, output, expected_output.?)); |
| 1409 | try testing.expectEqualStrings(expected_output.?, output); |
| 978 | 1410 | } else { |
| 979 | 1411 | try testing.expect(expected_output == null); |
| 980 | 1412 | } |
| ... | ... | @@ -989,56 +1421,17 @@ pub fn basename(path: []const u8) []const u8 { |
| 989 | 1421 | } |
| 990 | 1422 | |
| 991 | 1423 | pub fn basenamePosix(path: []const u8) []const u8 { |
| 992 | | if (path.len == 0) |
| 993 | | return &[_]u8{}; |
| 994 | | |
| 995 | | var end_index: usize = path.len - 1; |
| 996 | | while (path[end_index] == '/') { |
| 997 | | if (end_index == 0) |
| 998 | | return &[_]u8{}; |
| 999 | | end_index -= 1; |
| 1000 | | } |
| 1001 | | var start_index: usize = end_index; |
| 1002 | | end_index += 1; |
| 1003 | | while (path[start_index] != '/') { |
| 1004 | | if (start_index == 0) |
| 1005 | | return path[0..end_index]; |
| 1006 | | start_index -= 1; |
| 1007 | | } |
| 1008 | | |
| 1009 | | return path[start_index + 1 .. end_index]; |
| 1424 | return basenameInner(.posix, path); |
| 1010 | 1425 | } |
| 1011 | 1426 | |
| 1012 | 1427 | pub fn basenameWindows(path: []const u8) []const u8 { |
| 1013 | | if (path.len == 0) |
| 1014 | | return &[_]u8{}; |
| 1015 | | |
| 1016 | | var end_index: usize = path.len - 1; |
| 1017 | | while (true) { |
| 1018 | | const byte = path[end_index]; |
| 1019 | | if (byte == '/' or byte == '\\') { |
| 1020 | | if (end_index == 0) |
| 1021 | | return &[_]u8{}; |
| 1022 | | end_index -= 1; |
| 1023 | | continue; |
| 1024 | | } |
| 1025 | | if (byte == ':' and end_index == 1) { |
| 1026 | | return &[_]u8{}; |
| 1027 | | } |
| 1028 | | break; |
| 1029 | | } |
| 1030 | | |
| 1031 | | var start_index: usize = end_index; |
| 1032 | | end_index += 1; |
| 1033 | | while (path[start_index] != '/' and path[start_index] != '\\' and |
| 1034 | | !(path[start_index] == ':' and start_index == 1)) |
| 1035 | | { |
| 1036 | | if (start_index == 0) |
| 1037 | | return path[0..end_index]; |
| 1038 | | start_index -= 1; |
| 1039 | | } |
| 1428 | return basenameInner(.windows, path); |
| 1429 | } |
| 1040 | 1430 | |
| 1041 | | return path[start_index + 1 .. end_index]; |
| 1431 | fn basenameInner(comptime path_type: PathType, path: []const u8) []const u8 { |
| 1432 | var it = ComponentIterator(path_type, u8).init(path); |
| 1433 | const last = it.last() orelse return &[_]u8{}; |
| 1434 | return last.name; |
| 1042 | 1435 | } |
| 1043 | 1436 | |
| 1044 | 1437 | test basename { |
| ... | ... | @@ -1053,7 +1446,9 @@ test basename { |
| 1053 | 1446 | try testBasename("/aaa/", "aaa"); |
| 1054 | 1447 | try testBasename("/aaa/b", "b"); |
| 1055 | 1448 | try testBasename("/a/b", "b"); |
| 1056 | | try testBasename("//a", "a"); |
| 1449 | |
| 1450 | // For Windows, this is a UNC path that only has a server name component. |
| 1451 | try testBasename("//a", if (native_os == .windows) "" else "a"); |
| 1057 | 1452 | |
| 1058 | 1453 | try testBasenamePosix("\\dir\\basename.ext", "\\dir\\basename.ext"); |
| 1059 | 1454 | try testBasenamePosix("\\basename.ext", "\\basename.ext"); |
| ... | ... | @@ -1076,6 +1471,12 @@ test basename { |
| 1076 | 1471 | try testBasenameWindows("C:basename.ext", "basename.ext"); |
| 1077 | 1472 | try testBasenameWindows("C:basename.ext\\", "basename.ext"); |
| 1078 | 1473 | try testBasenameWindows("C:basename.ext\\\\", "basename.ext"); |
| 1474 | try testBasenameWindows("\\\\.", ""); |
| 1475 | try testBasenameWindows("\\\\.\\", ""); |
| 1476 | try testBasenameWindows("\\\\.\\basename.ext", "basename.ext"); |
| 1477 | try testBasenameWindows("\\\\?", ""); |
| 1478 | try testBasenameWindows("\\\\?\\", ""); |
| 1479 | try testBasenameWindows("\\\\?\\basename.ext", "basename.ext"); |
| 1079 | 1480 | try testBasenameWindows("C:foo", "foo"); |
| 1080 | 1481 | try testBasenameWindows("file:stream", "file:stream"); |
| 1081 | 1482 | } |
| ... | ... | @@ -1092,11 +1493,15 @@ fn testBasenameWindows(input: []const u8, expected_output: []const u8) !void { |
| 1092 | 1493 | try testing.expectEqualSlices(u8, expected_output, basenameWindows(input)); |
| 1093 | 1494 | } |
| 1094 | 1495 | |
| 1496 | pub const RelativeError = std.process.GetCwdAllocError; |
| 1497 | |
| 1095 | 1498 | /// Returns the relative path from `from` to `to`. If `from` and `to` each |
| 1096 | 1499 | /// resolve to the same path (after calling `resolve` on each), a zero-length |
| 1097 | 1500 | /// string is returned. |
| 1098 | | /// On Windows this canonicalizes the drive to a capital letter and paths to `\\`. |
| 1099 | | pub fn relative(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 { |
| 1501 | /// On Windows, the result is not guaranteed to be relative, as the paths may be |
| 1502 | /// on different volumes. In that case, the result will be the canonicalized absolute |
| 1503 | /// path of `to`. |
| 1504 | pub fn relative(allocator: Allocator, from: []const u8, to: []const u8) RelativeError![]u8 { |
| 1100 | 1505 | if (native_os == .windows) { |
| 1101 | 1506 | return relativeWindows(allocator, from, to); |
| 1102 | 1507 | } else { |
| ... | ... | @@ -1105,30 +1510,53 @@ pub fn relative(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 { |
| 1105 | 1510 | } |
| 1106 | 1511 | |
| 1107 | 1512 | pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 { |
| 1108 | | const cwd = try process.getCwdAlloc(allocator); |
| 1109 | | defer allocator.free(cwd); |
| 1110 | | const resolved_from = try resolveWindows(allocator, &[_][]const u8{ cwd, from }); |
| 1111 | | defer allocator.free(resolved_from); |
| 1513 | if (native_os != .windows) @compileError("this function relies on Windows-specific semantics"); |
| 1514 | |
| 1515 | const parsed_from = parsePathWindows(u8, from); |
| 1516 | const parsed_to = parsePathWindows(u8, to); |
| 1112 | 1517 | |
| 1518 | const result_is_always_to = x: { |
| 1519 | if (parsed_from.kind != parsed_to.kind) { |
| 1520 | break :x false; |
| 1521 | } |
| 1522 | switch (parsed_from.kind) { |
| 1523 | .drive_relative, .drive_absolute => { |
| 1524 | break :x !compareDiskDesignators(u8, .drive, parsed_from.root, parsed_to.root); |
| 1525 | }, |
| 1526 | .unc_absolute => { |
| 1527 | break :x !compareDiskDesignators(u8, .unc, parsed_from.root, parsed_to.root); |
| 1528 | }, |
| 1529 | .relative, .rooted, .local_device => break :x false, |
| 1530 | .root_local_device => break :x true, |
| 1531 | } |
| 1532 | }; |
| 1533 | |
| 1534 | if (result_is_always_to) { |
| 1535 | return windowsResolveAgainstCwd(allocator, to, parsed_to); |
| 1536 | } |
| 1537 | |
| 1538 | const resolved_from = try windowsResolveAgainstCwd(allocator, from, parsed_from); |
| 1539 | defer allocator.free(resolved_from); |
| 1113 | 1540 | var clean_up_resolved_to = true; |
| 1114 | | const resolved_to = try resolveWindows(allocator, &[_][]const u8{ cwd, to }); |
| 1541 | const resolved_to = try windowsResolveAgainstCwd(allocator, to, parsed_to); |
| 1115 | 1542 | defer if (clean_up_resolved_to) allocator.free(resolved_to); |
| 1116 | 1543 | |
| 1117 | | const parsed_from = windowsParsePath(resolved_from); |
| 1118 | | const parsed_to = windowsParsePath(resolved_to); |
| 1544 | const parsed_resolved_from = parsePathWindows(u8, resolved_from); |
| 1545 | const parsed_resolved_to = parsePathWindows(u8, resolved_to); |
| 1546 | |
| 1119 | 1547 | const result_is_to = x: { |
| 1120 | | if (parsed_from.kind != parsed_to.kind) { |
| 1548 | if (parsed_resolved_from.kind != parsed_resolved_to.kind) { |
| 1121 | 1549 | break :x true; |
| 1122 | | } else switch (parsed_from.kind) { |
| 1123 | | .NetworkShare => { |
| 1124 | | break :x !networkShareServersEql(parsed_to.disk_designator, parsed_from.disk_designator); |
| 1125 | | }, |
| 1126 | | .Drive => { |
| 1127 | | break :x ascii.toUpper(parsed_from.disk_designator[0]) != ascii.toUpper(parsed_to.disk_designator[0]); |
| 1550 | } |
| 1551 | switch (parsed_resolved_from.kind) { |
| 1552 | .drive_absolute, .drive_relative => { |
| 1553 | break :x !compareDiskDesignators(u8, .drive, parsed_resolved_from.root, parsed_resolved_to.root); |
| 1128 | 1554 | }, |
| 1129 | | .None => { |
| 1130 | | break :x false; |
| 1555 | .unc_absolute => { |
| 1556 | break :x !compareDiskDesignators(u8, .unc, parsed_resolved_from.root, parsed_resolved_to.root); |
| 1131 | 1557 | }, |
| 1558 | .relative, .rooted, .local_device => break :x false, |
| 1559 | .root_local_device => break :x true, |
| 1132 | 1560 | } |
| 1133 | 1561 | }; |
| 1134 | 1562 | |
| ... | ... | @@ -1137,8 +1565,8 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ! |
| 1137 | 1565 | return resolved_to; |
| 1138 | 1566 | } |
| 1139 | 1567 | |
| 1140 | | var from_it = mem.tokenizeAny(u8, resolved_from, "/\\"); |
| 1141 | | var to_it = mem.tokenizeAny(u8, resolved_to, "/\\"); |
| 1568 | var from_it = mem.tokenizeAny(u8, resolved_from[parsed_resolved_from.root.len..], "/\\"); |
| 1569 | var to_it = mem.tokenizeAny(u8, resolved_to[parsed_resolved_to.root.len..], "/\\"); |
| 1142 | 1570 | while (true) { |
| 1143 | 1571 | const from_component = from_it.next() orelse return allocator.dupe(u8, to_it.rest()); |
| 1144 | 1572 | const to_rest = to_it.rest(); |
| ... | ... | @@ -1170,11 +1598,101 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ! |
| 1170 | 1598 | |
| 1171 | 1599 | return allocator.realloc(result, result_index); |
| 1172 | 1600 | } |
| 1173 | | |
| 1174 | 1601 | return [_]u8{}; |
| 1175 | 1602 | } |
| 1176 | 1603 | |
| 1604 | fn windowsResolveAgainstCwd(allocator: Allocator, path: []const u8, parsed: WindowsPath2(u8)) ![]u8 { |
| 1605 | // Space for 256 WTF-16 code units; potentially 3 WTF-8 bytes per WTF-16 code unit |
| 1606 | var temp_allocator_state = std.heap.stackFallback(256 * 3, allocator); |
| 1607 | return switch (parsed.kind) { |
| 1608 | .drive_absolute, |
| 1609 | .unc_absolute, |
| 1610 | .root_local_device, |
| 1611 | .local_device, |
| 1612 | => try resolveWindows(allocator, &.{path}), |
| 1613 | .relative => blk: { |
| 1614 | const temp_allocator = temp_allocator_state.get(); |
| 1615 | |
| 1616 | const peb_cwd = windows.peb().ProcessParameters.CurrentDirectory.DosPath; |
| 1617 | const cwd_w = (peb_cwd.Buffer.?)[0 .. peb_cwd.Length / 2]; |
| 1618 | |
| 1619 | const wtf8_len = std.unicode.calcWtf8Len(cwd_w); |
| 1620 | const wtf8_buf = try temp_allocator.alloc(u8, wtf8_len); |
| 1621 | defer temp_allocator.free(wtf8_buf); |
| 1622 | assert(std.unicode.wtf16LeToWtf8(wtf8_buf, cwd_w) == wtf8_len); |
| 1623 | |
| 1624 | break :blk try resolveWindows(allocator, &.{ wtf8_buf, path }); |
| 1625 | }, |
| 1626 | .rooted => blk: { |
| 1627 | const peb_cwd = windows.peb().ProcessParameters.CurrentDirectory.DosPath; |
| 1628 | const cwd_w = (peb_cwd.Buffer.?)[0 .. peb_cwd.Length / 2]; |
| 1629 | const parsed_cwd = parsePathWindows(u16, cwd_w); |
| 1630 | switch (parsed_cwd.kind) { |
| 1631 | .drive_absolute => { |
| 1632 | var drive_buf = "_:\\".*; |
| 1633 | drive_buf[0] = @truncate(cwd_w[0]); |
| 1634 | break :blk try resolveWindows(allocator, &.{ &drive_buf, path }); |
| 1635 | }, |
| 1636 | .unc_absolute => { |
| 1637 | const temp_allocator = temp_allocator_state.get(); |
| 1638 | var root_buf = try temp_allocator.alloc(u8, parsed_cwd.root.len * 3); |
| 1639 | defer temp_allocator.free(root_buf); |
| 1640 | |
| 1641 | const wtf8_len = std.unicode.wtf16LeToWtf8(root_buf, parsed_cwd.root); |
| 1642 | const root = root_buf[0..wtf8_len]; |
| 1643 | break :blk try resolveWindows(allocator, &.{ root, path }); |
| 1644 | }, |
| 1645 | // Effectively a malformed CWD, give up and just return a normalized path |
| 1646 | else => break :blk try resolveWindows(allocator, &.{path}), |
| 1647 | } |
| 1648 | }, |
| 1649 | .drive_relative => blk: { |
| 1650 | const temp_allocator = temp_allocator_state.get(); |
| 1651 | const drive_cwd = drive_cwd: { |
| 1652 | const peb_cwd = windows.peb().ProcessParameters.CurrentDirectory.DosPath; |
| 1653 | const cwd_w = (peb_cwd.Buffer.?)[0 .. peb_cwd.Length / 2]; |
| 1654 | const parsed_cwd = parsePathWindows(u16, cwd_w); |
| 1655 | |
| 1656 | if (parsed_cwd.kind == .drive_absolute) { |
| 1657 | const drive_letter_w = parsed_cwd.root[0]; |
| 1658 | const drive_letters_match = drive_letter_w <= 0x7F and |
| 1659 | ascii.toUpper(@intCast(drive_letter_w)) == ascii.toUpper(parsed.root[0]); |
| 1660 | if (drive_letters_match) { |
| 1661 | const wtf8_len = std.unicode.calcWtf8Len(cwd_w); |
| 1662 | const wtf8_buf = try temp_allocator.alloc(u8, wtf8_len); |
| 1663 | assert(std.unicode.wtf16LeToWtf8(wtf8_buf, cwd_w) == wtf8_len); |
| 1664 | break :drive_cwd wtf8_buf[0..]; |
| 1665 | } |
| 1666 | |
| 1667 | // Per-drive CWD's are stored in special semi-hidden environment variables |
| 1668 | // of the format `=<drive-letter>:`, e.g. `=C:`. This type of CWD is |
| 1669 | // purely a shell concept, so there's no guarantee that it'll be set |
| 1670 | // or that it'll even be accurate. |
| 1671 | var key_buf = std.unicode.wtf8ToWtf16LeStringLiteral("=_:").*; |
| 1672 | key_buf[1] = parsed.root[0]; |
| 1673 | if (std.process.getenvW(&key_buf)) |drive_cwd_w| { |
| 1674 | const wtf8_len = std.unicode.calcWtf8Len(drive_cwd_w); |
| 1675 | const wtf8_buf = try temp_allocator.alloc(u8, wtf8_len); |
| 1676 | assert(std.unicode.wtf16LeToWtf8(wtf8_buf, drive_cwd_w) == wtf8_len); |
| 1677 | break :drive_cwd wtf8_buf[0..]; |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | const drive_buf = try temp_allocator.alloc(u8, 3); |
| 1682 | drive_buf[0] = parsed.root[0]; |
| 1683 | drive_buf[1] = ':'; |
| 1684 | drive_buf[2] = '\\'; |
| 1685 | break :drive_cwd drive_buf; |
| 1686 | }; |
| 1687 | defer temp_allocator.free(drive_cwd); |
| 1688 | break :blk try resolveWindows(allocator, &.{ drive_cwd, path }); |
| 1689 | }, |
| 1690 | }; |
| 1691 | } |
| 1692 | |
| 1177 | 1693 | pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 { |
| 1694 | if (native_os == .windows) @compileError("this function relies on semantics that do not apply to Windows"); |
| 1695 | |
| 1178 | 1696 | const cwd = try process.getCwdAlloc(allocator); |
| 1179 | 1697 | defer allocator.free(cwd); |
| 1180 | 1698 | const resolved_from = try resolvePosix(allocator, &[_][]const u8{ cwd, from }); |
| ... | ... | @@ -1217,51 +1735,59 @@ pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![] |
| 1217 | 1735 | } |
| 1218 | 1736 | |
| 1219 | 1737 | test relative { |
| 1220 | | try testRelativeWindows("c:/blah\\blah", "d:/games", "D:\\games"); |
| 1221 | | try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa", ".."); |
| 1222 | | try testRelativeWindows("c:/aaaa/bbbb", "c:/cccc", "..\\..\\cccc"); |
| 1223 | | try testRelativeWindows("c:/aaaa/bbbb", "C:/aaaa/bbbb", ""); |
| 1224 | | try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa/cccc", "..\\cccc"); |
| 1225 | | try testRelativeWindows("c:/aaaa/", "c:/aaaa/cccc", "cccc"); |
| 1226 | | try testRelativeWindows("c:/", "c:\\aaaa\\bbbb", "aaaa\\bbbb"); |
| 1227 | | try testRelativeWindows("c:/aaaa/bbbb", "d:\\", "D:\\"); |
| 1228 | | try testRelativeWindows("c:/AaAa/bbbb", "c:/aaaa/bbbb", ""); |
| 1229 | | try testRelativeWindows("c:/aaaaa/", "c:/aaaa/cccc", "..\\aaaa\\cccc"); |
| 1230 | | try testRelativeWindows("C:\\foo\\bar\\baz\\quux", "C:\\", "..\\..\\..\\.."); |
| 1231 | | try testRelativeWindows("C:\\foo\\test", "C:\\foo\\test\\bar\\package.json", "bar\\package.json"); |
| 1232 | | try testRelativeWindows("C:\\foo\\bar\\baz-quux", "C:\\foo\\bar\\baz", "..\\baz"); |
| 1233 | | try testRelativeWindows("C:\\foo\\bar\\baz", "C:\\foo\\bar\\baz-quux", "..\\baz-quux"); |
| 1234 | | try testRelativeWindows("\\\\foo\\bar", "\\\\foo\\bar\\baz", "baz"); |
| 1235 | | try testRelativeWindows("\\\\foo\\bar\\baz", "\\\\foo\\bar", ".."); |
| 1236 | | try testRelativeWindows("\\\\foo\\bar\\baz-quux", "\\\\foo\\bar\\baz", "..\\baz"); |
| 1237 | | try testRelativeWindows("\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz-quux", "..\\baz-quux"); |
| 1238 | | try testRelativeWindows("C:\\baz-quux", "C:\\baz", "..\\baz"); |
| 1239 | | try testRelativeWindows("C:\\baz", "C:\\baz-quux", "..\\baz-quux"); |
| 1240 | | try testRelativeWindows("\\\\foo\\baz-quux", "\\\\foo\\baz", "..\\baz"); |
| 1241 | | try testRelativeWindows("\\\\foo\\baz", "\\\\foo\\baz-quux", "..\\baz-quux"); |
| 1242 | | try testRelativeWindows("C:\\baz", "\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz"); |
| 1243 | | try testRelativeWindows("\\\\foo\\bar\\baz", "C:\\baz", "C:\\baz"); |
| 1244 | | |
| 1245 | | try testRelativeWindows("a/b/c", "a\\b", ".."); |
| 1246 | | try testRelativeWindows("a/b/c", "a", "..\\.."); |
| 1247 | | try testRelativeWindows("a/b/c", "a\\b\\c\\d", "d"); |
| 1248 | | |
| 1249 | | try testRelativeWindows("\\\\FOO\\bar\\baz", "\\\\foo\\BAR\\BAZ", ""); |
| 1250 | | // Unicode-aware case-insensitive path comparison |
| 1251 | | try testRelativeWindows("\\\\кириллица\\ελληνικά\\português", "\\\\КИРИЛЛИЦА\\ΕΛΛΗΝΙΚΆ\\PORTUGUÊS", ""); |
| 1252 | | |
| 1253 | | try testRelativePosix("/var/lib", "/var", ".."); |
| 1254 | | try testRelativePosix("/var/lib", "/bin", "../../bin"); |
| 1255 | | try testRelativePosix("/var/lib", "/var/lib", ""); |
| 1256 | | try testRelativePosix("/var/lib", "/var/apache", "../apache"); |
| 1257 | | try testRelativePosix("/var/", "/var/lib", "lib"); |
| 1258 | | try testRelativePosix("/", "/var/lib", "var/lib"); |
| 1259 | | try testRelativePosix("/foo/test", "/foo/test/bar/package.json", "bar/package.json"); |
| 1260 | | try testRelativePosix("/Users/a/web/b/test/mails", "/Users/a/web/b", "../.."); |
| 1261 | | try testRelativePosix("/foo/bar/baz-quux", "/foo/bar/baz", "../baz"); |
| 1262 | | try testRelativePosix("/foo/bar/baz", "/foo/bar/baz-quux", "../baz-quux"); |
| 1263 | | try testRelativePosix("/baz-quux", "/baz", "../baz"); |
| 1264 | | try testRelativePosix("/baz", "/baz-quux", "../baz-quux"); |
| 1738 | if (native_os == .windows) { |
| 1739 | try testRelativeWindows("c:/blah\\blah", "d:/games", "D:\\games"); |
| 1740 | try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa", ".."); |
| 1741 | try testRelativeWindows("c:/aaaa/bbbb", "c:/cccc", "..\\..\\cccc"); |
| 1742 | try testRelativeWindows("c:/aaaa/bbbb", "C:/aaaa/bbbb", ""); |
| 1743 | try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa/cccc", "..\\cccc"); |
| 1744 | try testRelativeWindows("c:/aaaa/", "c:/aaaa/cccc", "cccc"); |
| 1745 | try testRelativeWindows("c:/", "c:\\aaaa\\bbbb", "aaaa\\bbbb"); |
| 1746 | try testRelativeWindows("c:/aaaa/bbbb", "d:\\", "D:\\"); |
| 1747 | try testRelativeWindows("c:/AaAa/bbbb", "c:/aaaa/bbbb", ""); |
| 1748 | try testRelativeWindows("c:/aaaaa/", "c:/aaaa/cccc", "..\\aaaa\\cccc"); |
| 1749 | try testRelativeWindows("C:\\foo\\bar\\baz\\quux", "C:\\", "..\\..\\..\\.."); |
| 1750 | try testRelativeWindows("C:\\foo\\test", "C:\\foo\\test\\bar\\package.json", "bar\\package.json"); |
| 1751 | try testRelativeWindows("C:\\foo\\bar\\baz-quux", "C:\\foo\\bar\\baz", "..\\baz"); |
| 1752 | try testRelativeWindows("C:\\foo\\bar\\baz", "C:\\foo\\bar\\baz-quux", "..\\baz-quux"); |
| 1753 | try testRelativeWindows("\\\\foo\\bar", "\\\\foo\\bar\\baz", "baz"); |
| 1754 | try testRelativeWindows("\\\\foo\\bar\\baz", "\\\\foo\\bar", ".."); |
| 1755 | try testRelativeWindows("\\\\foo\\bar\\baz-quux", "\\\\foo\\bar\\baz", "..\\baz"); |
| 1756 | try testRelativeWindows("\\\\foo/bar\\baz-quux", "//foo\\bar/baz", "..\\baz"); |
| 1757 | try testRelativeWindows("\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz-quux", "..\\baz-quux"); |
| 1758 | try testRelativeWindows("C:\\baz-quux", "C:\\baz", "..\\baz"); |
| 1759 | try testRelativeWindows("C:\\baz", "C:\\baz-quux", "..\\baz-quux"); |
| 1760 | try testRelativeWindows("\\\\foo\\baz-quux", "\\\\foo\\baz", "\\\\foo\\baz"); |
| 1761 | try testRelativeWindows("\\\\foo\\baz", "\\\\foo\\baz-quux", "\\\\foo\\baz-quux"); |
| 1762 | try testRelativeWindows("C:\\baz", "\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz"); |
| 1763 | try testRelativeWindows("\\\\foo\\bar\\baz", "C:\\baz", "C:\\baz"); |
| 1764 | |
| 1765 | try testRelativeWindows("c:blah\\blah", "c:foo", "..\\..\\foo"); |
| 1766 | try testRelativeWindows("c:foo", "c:foo\\bar", "bar"); |
| 1767 | try testRelativeWindows("\\blah\\blah", "\\foo", "..\\..\\foo"); |
| 1768 | try testRelativeWindows("\\foo", "\\foo\\bar", "bar"); |
| 1769 | |
| 1770 | try testRelativeWindows("a/b/c", "a\\b", ".."); |
| 1771 | try testRelativeWindows("a/b/c", "a", "..\\.."); |
| 1772 | try testRelativeWindows("a/b/c", "a\\b\\c\\d", "d"); |
| 1773 | |
| 1774 | try testRelativeWindows("\\\\FOO\\bar\\baz", "\\\\foo\\BAR\\BAZ", ""); |
| 1775 | // Unicode-aware case-insensitive path comparison |
| 1776 | try testRelativeWindows("\\\\кириллица\\ελληνικά\\português", "\\\\КИРИЛЛИЦА\\ΕΛΛΗΝΙΚΆ\\PORTUGUÊS", ""); |
| 1777 | } else { |
| 1778 | try testRelativePosix("/var/lib", "/var", ".."); |
| 1779 | try testRelativePosix("/var/lib", "/bin", "../../bin"); |
| 1780 | try testRelativePosix("/var/lib", "/var/lib", ""); |
| 1781 | try testRelativePosix("/var/lib", "/var/apache", "../apache"); |
| 1782 | try testRelativePosix("/var/", "/var/lib", "lib"); |
| 1783 | try testRelativePosix("/", "/var/lib", "var/lib"); |
| 1784 | try testRelativePosix("/foo/test", "/foo/test/bar/package.json", "bar/package.json"); |
| 1785 | try testRelativePosix("/Users/a/web/b/test/mails", "/Users/a/web/b", "../.."); |
| 1786 | try testRelativePosix("/foo/bar/baz-quux", "/foo/bar/baz", "../baz"); |
| 1787 | try testRelativePosix("/foo/bar/baz", "/foo/bar/baz-quux", "../baz-quux"); |
| 1788 | try testRelativePosix("/baz-quux", "/baz", "../baz"); |
| 1789 | try testRelativePosix("/baz", "/baz-quux", "../baz-quux"); |
| 1790 | } |
| 1265 | 1791 | } |
| 1266 | 1792 | |
| 1267 | 1793 | fn testRelativePosix(from: []const u8, to: []const u8, expected_output: []const u8) !void { |
| ... | ... | @@ -1391,7 +1917,10 @@ test stem { |
| 1391 | 1917 | pub fn ComponentIterator(comptime path_type: PathType, comptime T: type) type { |
| 1392 | 1918 | return struct { |
| 1393 | 1919 | path: []const T, |
| 1394 | | root_end_index: usize = 0, |
| 1920 | /// Length of the root with at most one trailing path separator included (e.g. `C:/`). |
| 1921 | root_len: usize, |
| 1922 | /// Length of the root with all trailing path separators included (e.g. `C://///`). |
| 1923 | root_end_index: usize, |
| 1395 | 1924 | start_index: usize = 0, |
| 1396 | 1925 | end_index: usize = 0, |
| 1397 | 1926 | |
| ... | ... | @@ -1406,100 +1935,45 @@ pub fn ComponentIterator(comptime path_type: PathType, comptime T: type) type { |
| 1406 | 1935 | path: []const T, |
| 1407 | 1936 | }; |
| 1408 | 1937 | |
| 1409 | | const InitError = switch (path_type) { |
| 1410 | | .windows => error{BadPathName}, |
| 1411 | | else => error{}, |
| 1412 | | }; |
| 1413 | | |
| 1414 | 1938 | /// After `init`, `next` will return the first component after the root |
| 1415 | 1939 | /// (there is no need to call `first` after `init`). |
| 1416 | 1940 | /// To iterate backwards (from the end of the path to the beginning), call `last` |
| 1417 | 1941 | /// after `init` and then iterate via `previous` calls. |
| 1418 | | /// For Windows paths, `error.BadPathName` is returned if the `path` has an explicit |
| 1419 | | /// namespace prefix (`\\.\`, `\\?\`, or `\??\`) or if it is a UNC path with more |
| 1420 | | /// than two path separators at the beginning. |
| 1421 | | pub fn init(path: []const T) InitError!Self { |
| 1422 | | const root_end_index: usize = switch (path_type) { |
| 1942 | /// For Windows paths, paths are assumed to be in the Win32 namespace. |
| 1943 | pub fn init(path: []const T) Self { |
| 1944 | const root_len: usize = switch (path_type) { |
| 1423 | 1945 | .posix, .uefi => posix: { |
| 1424 | 1946 | // Root on UEFI and POSIX only differs by the path separator |
| 1425 | | var root_end_index: usize = 0; |
| 1426 | | while (true) : (root_end_index += 1) { |
| 1427 | | if (root_end_index >= path.len or !path_type.isSep(T, path[root_end_index])) { |
| 1428 | | break; |
| 1429 | | } |
| 1430 | | } |
| 1431 | | break :posix root_end_index; |
| 1947 | break :posix if (path.len > 0 and path_type.isSep(T, path[0])) 1 else 0; |
| 1432 | 1948 | }, |
| 1433 | 1949 | .windows => windows: { |
| 1434 | | // Namespaces other than the Win32 file namespace are tricky |
| 1435 | | // and basically impossible to determine a 'root' for, since it's |
| 1436 | | // possible to construct an effectively arbitrarily long 'root', |
| 1437 | | // e.g. `\\.\GLOBALROOT\??\UNC\localhost\C$\foo` is a |
| 1438 | | // possible path that would be effectively equivalent to |
| 1439 | | // `C:\foo`, and the `GLOBALROOT\??\` part can also be recursive, |
| 1440 | | // so `GLOBALROOT\??\GLOBALROOT\??\...` would work for any number |
| 1441 | | // of repetitions. Therefore, paths with an explicit namespace prefix |
| 1442 | | // (\\.\, \??\, \\?\) are not allowed here. |
| 1443 | | if (std.os.windows.getNamespacePrefix(T, path) != .none) { |
| 1444 | | return error.BadPathName; |
| 1445 | | } |
| 1446 | | const windows_path_type = std.os.windows.getUnprefixedPathType(T, path); |
| 1447 | | break :windows switch (windows_path_type) { |
| 1448 | | .relative => 0, |
| 1449 | | .root_local_device => path.len, |
| 1450 | | .rooted => 1, |
| 1451 | | .unc_absolute => unc: { |
| 1452 | | var end_index: usize = 2; |
| 1453 | | // Any extra separators between the first two and the server name are not allowed |
| 1454 | | // and will always lead to STATUS_OBJECT_PATH_INVALID if it is attempted |
| 1455 | | // to be used. |
| 1456 | | if (end_index < path.len and path_type.isSep(T, path[end_index])) { |
| 1457 | | return error.BadPathName; |
| 1458 | | } |
| 1459 | | // Server |
| 1460 | | while (end_index < path.len and !path_type.isSep(T, path[end_index])) { |
| 1461 | | end_index += 1; |
| 1462 | | } |
| 1463 | | // Slash(es) after server |
| 1464 | | while (end_index < path.len and path_type.isSep(T, path[end_index])) { |
| 1465 | | end_index += 1; |
| 1466 | | } |
| 1467 | | // Share |
| 1468 | | while (end_index < path.len and !path_type.isSep(T, path[end_index])) { |
| 1469 | | end_index += 1; |
| 1470 | | } |
| 1471 | | // Slash(es) after share |
| 1472 | | while (end_index < path.len and path_type.isSep(T, path[end_index])) { |
| 1473 | | end_index += 1; |
| 1474 | | } |
| 1475 | | break :unc end_index; |
| 1476 | | }, |
| 1477 | | .drive_absolute => drive: { |
| 1478 | | var end_index: usize = 3; |
| 1479 | | while (end_index < path.len and path_type.isSep(T, path[end_index])) { |
| 1480 | | end_index += 1; |
| 1481 | | } |
| 1482 | | break :drive end_index; |
| 1483 | | }, |
| 1484 | | .drive_relative => 2, |
| 1485 | | }; |
| 1950 | break :windows parsePathWindows(T, path).root.len; |
| 1486 | 1951 | }, |
| 1487 | 1952 | }; |
| 1953 | // If there are repeated path separators directly after the root, |
| 1954 | // keep track of that info so that they don't have to be dealt with when |
| 1955 | // iterating components. |
| 1956 | var root_end_index = root_len; |
| 1957 | for (path[root_len..]) |c| { |
| 1958 | if (!path_type.isSep(T, c)) break; |
| 1959 | root_end_index += 1; |
| 1960 | } |
| 1488 | 1961 | return .{ |
| 1489 | 1962 | .path = path, |
| 1963 | .root_len = root_len, |
| 1490 | 1964 | .root_end_index = root_end_index, |
| 1491 | 1965 | .start_index = root_end_index, |
| 1492 | 1966 | .end_index = root_end_index, |
| 1493 | 1967 | }; |
| 1494 | 1968 | } |
| 1495 | 1969 | |
| 1496 | | /// Returns the root of the path if it is an absolute path, or null otherwise. |
| 1970 | /// Returns the root of the path if it is not a relative path, or null otherwise. |
| 1497 | 1971 | /// For POSIX paths, this will be `/`. |
| 1498 | 1972 | /// For Windows paths, this will be something like `C:\`, `\\server\share\`, etc. |
| 1499 | 1973 | /// For UEFI paths, this will be `\`. |
| 1500 | 1974 | pub fn root(self: Self) ?[]const T { |
| 1501 | 1975 | if (self.root_end_index == 0) return null; |
| 1502 | | return self.path[0..self.root_end_index]; |
| 1976 | return self.path[0..self.root_len]; |
| 1503 | 1977 | } |
| 1504 | 1978 | |
| 1505 | 1979 | /// Returns the first component (from the beginning of the path). |
| ... | ... | @@ -1614,7 +2088,7 @@ pub const NativeComponentIterator = ComponentIterator(switch (native_os) { |
| 1614 | 2088 | else => .posix, |
| 1615 | 2089 | }, u8); |
| 1616 | 2090 | |
| 1617 | | pub fn componentIterator(path: []const u8) !NativeComponentIterator { |
| 2091 | pub fn componentIterator(path: []const u8) NativeComponentIterator { |
| 1618 | 2092 | return NativeComponentIterator.init(path); |
| 1619 | 2093 | } |
| 1620 | 2094 | |
| ... | ... | @@ -1622,8 +2096,9 @@ test "ComponentIterator posix" { |
| 1622 | 2096 | const PosixComponentIterator = ComponentIterator(.posix, u8); |
| 1623 | 2097 | { |
| 1624 | 2098 | const path = "a/b/c/"; |
| 1625 | | var it = try PosixComponentIterator.init(path); |
| 1626 | | try std.testing.expectEqual(@as(usize, 0), it.root_end_index); |
| 2099 | var it = PosixComponentIterator.init(path); |
| 2100 | try std.testing.expectEqual(0, it.root_len); |
| 2101 | try std.testing.expectEqual(0, it.root_end_index); |
| 1627 | 2102 | try std.testing.expect(null == it.root()); |
| 1628 | 2103 | { |
| 1629 | 2104 | try std.testing.expect(null == it.previous()); |
| ... | ... | @@ -1669,8 +2144,9 @@ test "ComponentIterator posix" { |
| 1669 | 2144 | |
| 1670 | 2145 | { |
| 1671 | 2146 | const path = "/a/b/c/"; |
| 1672 | | var it = try PosixComponentIterator.init(path); |
| 1673 | | try std.testing.expectEqual(@as(usize, 1), it.root_end_index); |
| 2147 | var it = PosixComponentIterator.init(path); |
| 2148 | try std.testing.expectEqual(1, it.root_len); |
| 2149 | try std.testing.expectEqual(1, it.root_end_index); |
| 1674 | 2150 | try std.testing.expectEqualStrings("/", it.root().?); |
| 1675 | 2151 | { |
| 1676 | 2152 | try std.testing.expect(null == it.previous()); |
| ... | ... | @@ -1714,10 +2190,59 @@ test "ComponentIterator posix" { |
| 1714 | 2190 | } |
| 1715 | 2191 | } |
| 1716 | 2192 | |
| 2193 | { |
| 2194 | const path = "////a///b///c////"; |
| 2195 | var it = PosixComponentIterator.init(path); |
| 2196 | try std.testing.expectEqual(1, it.root_len); |
| 2197 | try std.testing.expectEqual(4, it.root_end_index); |
| 2198 | try std.testing.expectEqualStrings("/", it.root().?); |
| 2199 | { |
| 2200 | try std.testing.expect(null == it.previous()); |
| 2201 | |
| 2202 | const first_via_next = it.next().?; |
| 2203 | try std.testing.expectEqualStrings("a", first_via_next.name); |
| 2204 | try std.testing.expectEqualStrings("////a", first_via_next.path); |
| 2205 | |
| 2206 | const first = it.first().?; |
| 2207 | try std.testing.expectEqualStrings("a", first.name); |
| 2208 | try std.testing.expectEqualStrings("////a", first.path); |
| 2209 | |
| 2210 | try std.testing.expect(null == it.previous()); |
| 2211 | |
| 2212 | const second = it.next().?; |
| 2213 | try std.testing.expectEqualStrings("b", second.name); |
| 2214 | try std.testing.expectEqualStrings("////a///b", second.path); |
| 2215 | |
| 2216 | const third = it.next().?; |
| 2217 | try std.testing.expectEqualStrings("c", third.name); |
| 2218 | try std.testing.expectEqualStrings("////a///b///c", third.path); |
| 2219 | |
| 2220 | try std.testing.expect(null == it.next()); |
| 2221 | } |
| 2222 | { |
| 2223 | const last = it.last().?; |
| 2224 | try std.testing.expectEqualStrings("c", last.name); |
| 2225 | try std.testing.expectEqualStrings("////a///b///c", last.path); |
| 2226 | |
| 2227 | try std.testing.expect(null == it.next()); |
| 2228 | |
| 2229 | const second_to_last = it.previous().?; |
| 2230 | try std.testing.expectEqualStrings("b", second_to_last.name); |
| 2231 | try std.testing.expectEqualStrings("////a///b", second_to_last.path); |
| 2232 | |
| 2233 | const third_to_last = it.previous().?; |
| 2234 | try std.testing.expectEqualStrings("a", third_to_last.name); |
| 2235 | try std.testing.expectEqualStrings("////a", third_to_last.path); |
| 2236 | |
| 2237 | try std.testing.expect(null == it.previous()); |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 1717 | 2241 | { |
| 1718 | 2242 | const path = "/"; |
| 1719 | | var it = try PosixComponentIterator.init(path); |
| 1720 | | try std.testing.expectEqual(@as(usize, 1), it.root_end_index); |
| 2243 | var it = PosixComponentIterator.init(path); |
| 2244 | try std.testing.expectEqual(1, it.root_len); |
| 2245 | try std.testing.expectEqual(1, it.root_end_index); |
| 1721 | 2246 | try std.testing.expectEqualStrings("/", it.root().?); |
| 1722 | 2247 | |
| 1723 | 2248 | try std.testing.expect(null == it.first()); |
| ... | ... | @@ -1733,8 +2258,9 @@ test "ComponentIterator posix" { |
| 1733 | 2258 | |
| 1734 | 2259 | { |
| 1735 | 2260 | const path = ""; |
| 1736 | | var it = try PosixComponentIterator.init(path); |
| 1737 | | try std.testing.expectEqual(@as(usize, 0), it.root_end_index); |
| 2261 | var it = PosixComponentIterator.init(path); |
| 2262 | try std.testing.expectEqual(0, it.root_len); |
| 2263 | try std.testing.expectEqual(0, it.root_end_index); |
| 1738 | 2264 | try std.testing.expect(null == it.root()); |
| 1739 | 2265 | |
| 1740 | 2266 | try std.testing.expect(null == it.first()); |
| ... | ... | @@ -1753,8 +2279,9 @@ test "ComponentIterator windows" { |
| 1753 | 2279 | const WindowsComponentIterator = ComponentIterator(.windows, u8); |
| 1754 | 2280 | { |
| 1755 | 2281 | const path = "a/b\\c//"; |
| 1756 | | var it = try WindowsComponentIterator.init(path); |
| 1757 | | try std.testing.expectEqual(@as(usize, 0), it.root_end_index); |
| 2282 | var it = WindowsComponentIterator.init(path); |
| 2283 | try std.testing.expectEqual(0, it.root_len); |
| 2284 | try std.testing.expectEqual(0, it.root_end_index); |
| 1758 | 2285 | try std.testing.expect(null == it.root()); |
| 1759 | 2286 | { |
| 1760 | 2287 | try std.testing.expect(null == it.previous()); |
| ... | ... | @@ -1800,8 +2327,9 @@ test "ComponentIterator windows" { |
| 1800 | 2327 | |
| 1801 | 2328 | { |
| 1802 | 2329 | const path = "C:\\a/b/c/"; |
| 1803 | | var it = try WindowsComponentIterator.init(path); |
| 1804 | | try std.testing.expectEqual(@as(usize, 3), it.root_end_index); |
| 2330 | var it = WindowsComponentIterator.init(path); |
| 2331 | try std.testing.expectEqual(3, it.root_len); |
| 2332 | try std.testing.expectEqual(3, it.root_end_index); |
| 1805 | 2333 | try std.testing.expectEqualStrings("C:\\", it.root().?); |
| 1806 | 2334 | { |
| 1807 | 2335 | const first = it.first().?; |
| ... | ... | @@ -1835,10 +2363,49 @@ test "ComponentIterator windows" { |
| 1835 | 2363 | } |
| 1836 | 2364 | } |
| 1837 | 2365 | |
| 2366 | { |
| 2367 | const path = "C:\\\\//a/\\/\\b///c////"; |
| 2368 | var it = WindowsComponentIterator.init(path); |
| 2369 | try std.testing.expectEqual(3, it.root_len); |
| 2370 | try std.testing.expectEqual(6, it.root_end_index); |
| 2371 | try std.testing.expectEqualStrings("C:\\", it.root().?); |
| 2372 | { |
| 2373 | const first = it.first().?; |
| 2374 | try std.testing.expectEqualStrings("a", first.name); |
| 2375 | try std.testing.expectEqualStrings("C:\\\\//a", first.path); |
| 2376 | |
| 2377 | const second = it.next().?; |
| 2378 | try std.testing.expectEqualStrings("b", second.name); |
| 2379 | try std.testing.expectEqualStrings("C:\\\\//a/\\/\\b", second.path); |
| 2380 | |
| 2381 | const third = it.next().?; |
| 2382 | try std.testing.expectEqualStrings("c", third.name); |
| 2383 | try std.testing.expectEqualStrings("C:\\\\//a/\\/\\b///c", third.path); |
| 2384 | |
| 2385 | try std.testing.expect(null == it.next()); |
| 2386 | } |
| 2387 | { |
| 2388 | const last = it.last().?; |
| 2389 | try std.testing.expectEqualStrings("c", last.name); |
| 2390 | try std.testing.expectEqualStrings("C:\\\\//a/\\/\\b///c", last.path); |
| 2391 | |
| 2392 | const second_to_last = it.previous().?; |
| 2393 | try std.testing.expectEqualStrings("b", second_to_last.name); |
| 2394 | try std.testing.expectEqualStrings("C:\\\\//a/\\/\\b", second_to_last.path); |
| 2395 | |
| 2396 | const third_to_last = it.previous().?; |
| 2397 | try std.testing.expectEqualStrings("a", third_to_last.name); |
| 2398 | try std.testing.expectEqualStrings("C:\\\\//a", third_to_last.path); |
| 2399 | |
| 2400 | try std.testing.expect(null == it.previous()); |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 1838 | 2404 | { |
| 1839 | 2405 | const path = "/"; |
| 1840 | | var it = try WindowsComponentIterator.init(path); |
| 1841 | | try std.testing.expectEqual(@as(usize, 1), it.root_end_index); |
| 2406 | var it = WindowsComponentIterator.init(path); |
| 2407 | try std.testing.expectEqual(1, it.root_len); |
| 2408 | try std.testing.expectEqual(1, it.root_end_index); |
| 1842 | 2409 | try std.testing.expectEqualStrings("/", it.root().?); |
| 1843 | 2410 | |
| 1844 | 2411 | try std.testing.expect(null == it.first()); |
| ... | ... | @@ -1854,8 +2421,9 @@ test "ComponentIterator windows" { |
| 1854 | 2421 | |
| 1855 | 2422 | { |
| 1856 | 2423 | const path = ""; |
| 1857 | | var it = try WindowsComponentIterator.init(path); |
| 1858 | | try std.testing.expectEqual(@as(usize, 0), it.root_end_index); |
| 2424 | var it = WindowsComponentIterator.init(path); |
| 2425 | try std.testing.expectEqual(0, it.root_len); |
| 2426 | try std.testing.expectEqual(0, it.root_end_index); |
| 1859 | 2427 | try std.testing.expect(null == it.root()); |
| 1860 | 2428 | |
| 1861 | 2429 | try std.testing.expect(null == it.first()); |
| ... | ... | @@ -1880,8 +2448,9 @@ test "ComponentIterator windows WTF-16" { |
| 1880 | 2448 | const L = std.unicode.utf8ToUtf16LeStringLiteral; |
| 1881 | 2449 | |
| 1882 | 2450 | const path = L("C:\\a/b/c/"); |
| 1883 | | var it = try WindowsComponentIterator.init(path); |
| 1884 | | try std.testing.expectEqual(@as(usize, 3), it.root_end_index); |
| 2451 | var it = WindowsComponentIterator.init(path); |
| 2452 | try std.testing.expectEqual(3, it.root_len); |
| 2453 | try std.testing.expectEqual(3, it.root_end_index); |
| 1885 | 2454 | try std.testing.expectEqualSlices(u16, L("C:\\"), it.root().?); |
| 1886 | 2455 | { |
| 1887 | 2456 | const first = it.first().?; |
| ... | ... | @@ -1918,55 +2487,60 @@ test "ComponentIterator windows WTF-16" { |
| 1918 | 2487 | test "ComponentIterator roots" { |
| 1919 | 2488 | // UEFI |
| 1920 | 2489 | { |
| 1921 | | var it = try ComponentIterator(.uefi, u8).init("\\\\a"); |
| 1922 | | try std.testing.expectEqualStrings("\\\\", it.root().?); |
| 2490 | var it = ComponentIterator(.uefi, u8).init("\\\\a"); |
| 2491 | try std.testing.expectEqualStrings("\\", it.root().?); |
| 1923 | 2492 | |
| 1924 | | it = try ComponentIterator(.uefi, u8).init("//a"); |
| 2493 | it = ComponentIterator(.uefi, u8).init("//a"); |
| 1925 | 2494 | try std.testing.expect(null == it.root()); |
| 1926 | 2495 | } |
| 1927 | 2496 | // POSIX |
| 1928 | 2497 | { |
| 1929 | | var it = try ComponentIterator(.posix, u8).init("//a"); |
| 1930 | | try std.testing.expectEqualStrings("//", it.root().?); |
| 2498 | var it = ComponentIterator(.posix, u8).init("//a"); |
| 2499 | try std.testing.expectEqualStrings("/", it.root().?); |
| 1931 | 2500 | |
| 1932 | | it = try ComponentIterator(.posix, u8).init("\\\\a"); |
| 2501 | it = ComponentIterator(.posix, u8).init("\\\\a"); |
| 1933 | 2502 | try std.testing.expect(null == it.root()); |
| 1934 | 2503 | } |
| 1935 | 2504 | // Windows |
| 1936 | 2505 | { |
| 1937 | 2506 | // Drive relative |
| 1938 | | var it = try ComponentIterator(.windows, u8).init("C:a"); |
| 2507 | var it = ComponentIterator(.windows, u8).init("C:a"); |
| 1939 | 2508 | try std.testing.expectEqualStrings("C:", it.root().?); |
| 1940 | 2509 | |
| 1941 | 2510 | // Drive absolute |
| 1942 | | it = try ComponentIterator(.windows, u8).init("C://a"); |
| 1943 | | try std.testing.expectEqualStrings("C://", it.root().?); |
| 1944 | | it = try ComponentIterator(.windows, u8).init("C:\\a"); |
| 2511 | it = ComponentIterator(.windows, u8).init("C:/a"); |
| 2512 | try std.testing.expectEqualStrings("C:/", it.root().?); |
| 2513 | it = ComponentIterator(.windows, u8).init("C:\\a"); |
| 1945 | 2514 | try std.testing.expectEqualStrings("C:\\", it.root().?); |
| 2515 | it = ComponentIterator(.windows, u8).init("C:///a"); |
| 2516 | try std.testing.expectEqualStrings("C:/", it.root().?); |
| 1946 | 2517 | |
| 1947 | 2518 | // Rooted |
| 1948 | | it = try ComponentIterator(.windows, u8).init("\\a"); |
| 2519 | it = ComponentIterator(.windows, u8).init("\\a"); |
| 1949 | 2520 | try std.testing.expectEqualStrings("\\", it.root().?); |
| 1950 | | it = try ComponentIterator(.windows, u8).init("/a"); |
| 2521 | it = ComponentIterator(.windows, u8).init("/a"); |
| 1951 | 2522 | try std.testing.expectEqualStrings("/", it.root().?); |
| 1952 | 2523 | |
| 1953 | 2524 | // Root local device |
| 1954 | | it = try ComponentIterator(.windows, u8).init("\\\\."); |
| 2525 | it = ComponentIterator(.windows, u8).init("\\\\."); |
| 1955 | 2526 | try std.testing.expectEqualStrings("\\\\.", it.root().?); |
| 1956 | | it = try ComponentIterator(.windows, u8).init("//?"); |
| 2527 | it = ComponentIterator(.windows, u8).init("//?"); |
| 1957 | 2528 | try std.testing.expectEqualStrings("//?", it.root().?); |
| 1958 | 2529 | |
| 1959 | 2530 | // UNC absolute |
| 1960 | | it = try ComponentIterator(.windows, u8).init("//"); |
| 2531 | it = ComponentIterator(.windows, u8).init("//"); |
| 1961 | 2532 | try std.testing.expectEqualStrings("//", it.root().?); |
| 1962 | | it = try ComponentIterator(.windows, u8).init("\\\\a"); |
| 2533 | it = ComponentIterator(.windows, u8).init("\\\\a"); |
| 1963 | 2534 | try std.testing.expectEqualStrings("\\\\a", it.root().?); |
| 1964 | | it = try ComponentIterator(.windows, u8).init("\\\\a\\b\\\\c"); |
| 1965 | | try std.testing.expectEqualStrings("\\\\a\\b\\\\", it.root().?); |
| 1966 | | it = try ComponentIterator(.windows, u8).init("//a"); |
| 2535 | it = ComponentIterator(.windows, u8).init("\\\\a\\b\\\\c"); |
| 2536 | try std.testing.expectEqualStrings("\\\\a\\b\\", it.root().?); |
| 2537 | it = ComponentIterator(.windows, u8).init("//a"); |
| 1967 | 2538 | try std.testing.expectEqualStrings("//a", it.root().?); |
| 1968 | | it = try ComponentIterator(.windows, u8).init("//a/b//c"); |
| 1969 | | try std.testing.expectEqualStrings("//a/b//", it.root().?); |
| 2539 | it = ComponentIterator(.windows, u8).init("//a/b//c"); |
| 2540 | try std.testing.expectEqualStrings("//a/b/", it.root().?); |
| 2541 | // Malformed UNC path with empty server name |
| 2542 | it = ComponentIterator(.windows, u8).init("\\\\\\a\\b\\c"); |
| 2543 | try std.testing.expectEqualStrings("\\\\\\a\\", it.root().?); |
| 1970 | 2544 | } |
| 1971 | 2545 | } |
| 1972 | 2546 | |