| ... | ... | @@ -12,13 +12,13 @@ const fs = std.fs; |
| 12 | 12 | const process = std.process; |
| 13 | 13 | const native_os = builtin.target.os.tag; |
| 14 | 14 | |
| 15 | | pub const sep_windows = '\\'; |
| 15 | pub const sep_windows_uefi = '\\'; |
| 16 | 16 | pub const sep_posix = '/'; |
| 17 | | pub const sep = if (native_os == .windows) sep_windows else sep_posix; |
| 17 | pub const sep = if (native_os == .windows or native_os == .uefi) sep_windows_uefi else sep_posix; |
| 18 | 18 | |
| 19 | | pub const sep_str_windows = "\\"; |
| 19 | pub const sep_str_windows_uefi = "\\"; |
| 20 | 20 | pub const sep_str_posix = "/"; |
| 21 | | pub const sep_str = if (native_os == .windows) sep_str_windows else sep_str_posix; |
| 21 | pub const sep_str = if (native_os == .windows or native_os == .uefi) sep_str_windows_uefi else sep_str_posix; |
| 22 | 22 | |
| 23 | 23 | pub const delimiter_windows = ';'; |
| 24 | 24 | pub const delimiter_posix = ':'; |
| ... | ... | @@ -26,11 +26,11 @@ pub const delimiter = if (native_os == .windows) delimiter_windows else delimite |
| 26 | 26 | |
| 27 | 27 | /// Returns if the given byte is a valid path separator |
| 28 | 28 | pub fn isSep(byte: u8) bool { |
| 29 | | if (native_os == .windows) { |
| 30 | | return byte == '/' or byte == '\\'; |
| 31 | | } else { |
| 32 | | return byte == '/'; |
| 33 | | } |
| 29 | return switch (native_os) { |
| 30 | .windows => byte == '/' or byte == '\\', |
| 31 | .uefi => byte == '\\', |
| 32 | else => byte == '/', |
| 33 | }; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /// This is different from mem.join in that the separator will not be repeated if |
| ... | ... | @@ -110,13 +110,24 @@ pub fn joinZ(allocator: Allocator, paths: []const []const u8) ![:0]u8 { |
| 110 | 110 | return out[0 .. out.len - 1 :0]; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | fn testJoinMaybeZUefi(paths: []const []const u8, expected: []const u8, zero: bool) !void { |
| 114 | const uefiIsSep = struct { |
| 115 | fn isSep(byte: u8) bool { |
| 116 | return byte == '\\'; |
| 117 | } |
| 118 | }.isSep; |
| 119 | const actual = try joinSepMaybeZ(testing.allocator, sep_windows_uefi, uefiIsSep, paths, zero); |
| 120 | defer testing.allocator.free(actual); |
| 121 | try testing.expectEqualSlices(u8, expected, if (zero) actual[0 .. actual.len - 1 :0] else actual); |
| 122 | } |
| 123 | |
| 113 | 124 | fn testJoinMaybeZWindows(paths: []const []const u8, expected: []const u8, zero: bool) !void { |
| 114 | 125 | const windowsIsSep = struct { |
| 115 | 126 | fn isSep(byte: u8) bool { |
| 116 | 127 | return byte == '/' or byte == '\\'; |
| 117 | 128 | } |
| 118 | 129 | }.isSep; |
| 119 | | const actual = try joinSepMaybeZ(testing.allocator, sep_windows, windowsIsSep, paths, zero); |
| 130 | const actual = try joinSepMaybeZ(testing.allocator, sep_windows_uefi, windowsIsSep, paths, zero); |
| 120 | 131 | defer testing.allocator.free(actual); |
| 121 | 132 | try testing.expectEqualSlices(u8, expected, if (zero) actual[0 .. actual.len - 1 :0] else actual); |
| 122 | 133 | } |
| ... | ... | @@ -158,6 +169,11 @@ test "join" { |
| 158 | 169 | zero, |
| 159 | 170 | ); |
| 160 | 171 | |
| 172 | try testJoinMaybeZUefi(&[_][]const u8{ "EFI", "Boot", "bootx64.efi" }, "EFI\\Boot\\bootx64.efi", zero); |
| 173 | try testJoinMaybeZUefi(&[_][]const u8{ "EFI\\Boot", "bootx64.efi" }, "EFI\\Boot\\bootx64.efi", zero); |
| 174 | try testJoinMaybeZUefi(&[_][]const u8{ "EFI\\", "\\Boot", "bootx64.efi" }, "EFI\\Boot\\bootx64.efi", zero); |
| 175 | try testJoinMaybeZUefi(&[_][]const u8{ "EFI\\", "\\Boot\\", "\\bootx64.efi" }, "EFI\\Boot\\bootx64.efi", zero); |
| 176 | |
| 161 | 177 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\", "a", "b/", "c" }, "c:\\a\\b/c", zero); |
| 162 | 178 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\a/", "b\\", "/c" }, "c:\\a/b\\c", zero); |
| 163 | 179 | |
| ... | ... | @@ -588,7 +604,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 { |
| 588 | 604 | result[0] = asciiUpper(result[0]); |
| 589 | 605 | // Remove the trailing slash if present, eg. if the cwd is a root |
| 590 | 606 | // directory. |
| 591 | | if (cwd.len > 0 and cwd[cwd.len - 1] == sep_windows) { |
| 607 | if (cwd.len > 0 and cwd[cwd.len - 1] == sep_windows_uefi) { |
| 592 | 608 | result_index -= 1; |
| 593 | 609 | } |
| 594 | 610 | } |
| ... | ... | @@ -625,7 +641,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 { |
| 625 | 641 | break; |
| 626 | 642 | } |
| 627 | 643 | } else { |
| 628 | | result[result_index] = sep_windows; |
| 644 | result[result_index] = sep_windows_uefi; |
| 629 | 645 | result_index += 1; |
| 630 | 646 | mem.copy(u8, result[result_index..], component); |
| 631 | 647 | result_index += component.len; |