authorgravatar for noiryuh@proton.menoiryuh <noiryuh@proton.me> 2022-09-16 11:24:41+07:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-23 12:19:09+03:00
log0be46866fe6ece0680ceef95fd15362d15825bce
tree21d5e08ad497076777c204f1fbeef4e87672484a
parent246a39c10e94653d9d0f15971fb9c2e52fe8e1d1

use `std.ascii` instead of defining ascii functions in `std.fs.path`


1 files changed, 12 insertions(+), 29 deletions(-)

lib/std/fs/path.zig+12-29
...@@ -5,6 +5,7 @@ const assert = debug.assert;...@@ -5,6 +5,7 @@ const assert = debug.assert;
5const testing = std.testing;5const testing = std.testing;
6const mem = std.mem;6const mem = std.mem;
7const fmt = std.fmt;7const fmt = std.fmt;
8const ascii = std.ascii;
8const Allocator = mem.Allocator;9const Allocator = mem.Allocator;
9const math = std.math;10const math = std.math;
10const windows = std.os.windows;11const windows = std.os.windows;
...@@ -423,7 +424,7 @@ fn networkShareServersEql(ns1: []const u8, ns2: []const u8) bool {...@@ -423,7 +424,7 @@ fn networkShareServersEql(ns1: []const u8, ns2: []const u8) bool {
423 var it2 = mem.tokenize(u8, ns2, &[_]u8{sep2});424 var it2 = mem.tokenize(u8, ns2, &[_]u8{sep2});
424425
425 // TODO ASCII is wrong, we actually need full unicode support to compare paths.426 // TODO ASCII is wrong, we actually need full unicode support to compare paths.
426 return asciiEqlIgnoreCase(it1.next().?, it2.next().?);427 return ascii.eqlIgnoreCase(it1.next().?, it2.next().?);
427}428}
428429
429fn compareDiskDesignators(kind: WindowsPath.Kind, p1: []const u8, p2: []const u8) bool {430fn compareDiskDesignators(kind: WindowsPath.Kind, p1: []const u8, p2: []const u8) bool {
...@@ -434,7 +435,7 @@ fn compareDiskDesignators(kind: WindowsPath.Kind, p1: []const u8, p2: []const u8...@@ -434,7 +435,7 @@ fn compareDiskDesignators(kind: WindowsPath.Kind, p1: []const u8, p2: []const u8
434 return true;435 return true;
435 },436 },
436 WindowsPath.Kind.Drive => {437 WindowsPath.Kind.Drive => {
437 return asciiUpper(p1[0]) == asciiUpper(p2[0]);438 return ascii.toUpper(p1[0]) == ascii.toUpper(p2[0]);
438 },439 },
439 WindowsPath.Kind.NetworkShare => {440 WindowsPath.Kind.NetworkShare => {
440 const sep1 = p1[0];441 const sep1 = p1[0];
...@@ -444,29 +445,11 @@ fn compareDiskDesignators(kind: WindowsPath.Kind, p1: []const u8, p2: []const u8...@@ -444,29 +445,11 @@ fn compareDiskDesignators(kind: WindowsPath.Kind, p1: []const u8, p2: []const u8
444 var it2 = mem.tokenize(u8, p2, &[_]u8{sep2});445 var it2 = mem.tokenize(u8, p2, &[_]u8{sep2});
445446
446 // TODO ASCII is wrong, we actually need full unicode support to compare paths.447 // TODO ASCII is wrong, we actually need full unicode support to compare paths.
447 return asciiEqlIgnoreCase(it1.next().?, it2.next().?) and asciiEqlIgnoreCase(it1.next().?, it2.next().?);448 return ascii.eqlIgnoreCase(it1.next().?, it2.next().?) and ascii.eqlIgnoreCase(it1.next().?, it2.next().?);
448 },449 },
449 }450 }
450}451}
451452
452fn asciiUpper(byte: u8) u8 {
453 return switch (byte) {
454 'a'...'z' => 'A' + (byte - 'a'),
455 else => byte,
456 };
457}
458
459fn asciiEqlIgnoreCase(s1: []const u8, s2: []const u8) bool {
460 if (s1.len != s2.len)
461 return false;
462 var i: usize = 0;
463 while (i < s1.len) : (i += 1) {
464 if (asciiUpper(s1[i]) != asciiUpper(s2[i]))
465 return false;
466 }
467 return true;
468}
469
470/// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`.453/// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`.
471pub fn resolve(allocator: Allocator, paths: []const []const u8) ![]u8 {454pub fn resolve(allocator: Allocator, paths: []const []const u8) ![]u8 {
472 if (native_os == .windows) {455 if (native_os == .windows) {
...@@ -506,7 +489,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {...@@ -506,7 +489,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {
506 }489 }
507 switch (parsed.kind) {490 switch (parsed.kind) {
508 WindowsPath.Kind.Drive => {491 WindowsPath.Kind.Drive => {
509 result_drive_buf[0] = asciiUpper(parsed.disk_designator[0]);492 result_drive_buf[0] = ascii.toUpper(parsed.disk_designator[0]);
510 result_disk_designator = result_drive_buf[0..];493 result_disk_designator = result_drive_buf[0..];
511 have_drive_kind = WindowsPath.Kind.Drive;494 have_drive_kind = WindowsPath.Kind.Drive;
512 },495 },
...@@ -590,7 +573,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {...@@ -590,7 +573,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {
590 result_index += parsed_cwd.disk_designator.len;573 result_index += parsed_cwd.disk_designator.len;
591 result_disk_designator = result[0..parsed_cwd.disk_designator.len];574 result_disk_designator = result[0..parsed_cwd.disk_designator.len];
592 if (parsed_cwd.kind == WindowsPath.Kind.Drive) {575 if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
593 result[0] = asciiUpper(result[0]);576 result[0] = ascii.toUpper(result[0]);
594 }577 }
595 have_drive_kind = parsed_cwd.kind;578 have_drive_kind = parsed_cwd.kind;
596 },579 },
...@@ -608,7 +591,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {...@@ -608,7 +591,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {
608 const parsed_cwd = windowsParsePath(result[0..result_index]);591 const parsed_cwd = windowsParsePath(result[0..result_index]);
609 result_disk_designator = parsed_cwd.disk_designator;592 result_disk_designator = parsed_cwd.disk_designator;
610 if (parsed_cwd.kind == WindowsPath.Kind.Drive) {593 if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
611 result[0] = asciiUpper(result[0]);594 result[0] = ascii.toUpper(result[0]);
612 // Remove the trailing slash if present, eg. if the cwd is a root595 // Remove the trailing slash if present, eg. if the cwd is a root
613 // directory.596 // directory.
614 if (cwd.len > 0 and cwd[cwd.len - 1] == sep_windows) {597 if (cwd.len > 0 and cwd[cwd.len - 1] == sep_windows) {
...@@ -741,7 +724,7 @@ test "resolve" {...@@ -741,7 +724,7 @@ test "resolve" {
741 defer testing.allocator.free(cwd);724 defer testing.allocator.free(cwd);
742 if (native_os == .windows) {725 if (native_os == .windows) {
743 if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) {726 if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) {
744 cwd[0] = asciiUpper(cwd[0]);727 cwd[0] = ascii.toUpper(cwd[0]);
745 }728 }
746 try testResolveWindows(&[_][]const u8{"."}, cwd);729 try testResolveWindows(&[_][]const u8{"."}, cwd);
747 } else {730 } else {
...@@ -768,7 +751,7 @@ test "resolveWindows" {...@@ -768,7 +751,7 @@ test "resolveWindows" {
768 });751 });
769 defer testing.allocator.free(expected);752 defer testing.allocator.free(expected);
770 if (parsed_cwd.kind == WindowsPath.Kind.Drive) {753 if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
771 expected[0] = asciiUpper(parsed_cwd.disk_designator[0]);754 expected[0] = ascii.toUpper(parsed_cwd.disk_designator[0]);
772 }755 }
773 try testResolveWindows(&[_][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" }, expected);756 try testResolveWindows(&[_][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" }, expected);
774 }757 }
...@@ -779,7 +762,7 @@ test "resolveWindows" {...@@ -779,7 +762,7 @@ test "resolveWindows" {
779 });762 });
780 defer testing.allocator.free(expected);763 defer testing.allocator.free(expected);
781 if (parsed_cwd.kind == WindowsPath.Kind.Drive) {764 if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
782 expected[0] = asciiUpper(parsed_cwd.disk_designator[0]);765 expected[0] = ascii.toUpper(parsed_cwd.disk_designator[0]);
783 }766 }
784 try testResolveWindows(&[_][]const u8{ "usr/local", "lib\\zig" }, expected);767 try testResolveWindows(&[_][]const u8{ "usr/local", "lib\\zig" }, expected);
785 }768 }
...@@ -1110,7 +1093,7 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) !...@@ -1110,7 +1093,7 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) !
1110 break :x !networkShareServersEql(parsed_to.disk_designator, parsed_from.disk_designator);1093 break :x !networkShareServersEql(parsed_to.disk_designator, parsed_from.disk_designator);
1111 },1094 },
1112 WindowsPath.Kind.Drive => {1095 WindowsPath.Kind.Drive => {
1113 break :x asciiUpper(parsed_from.disk_designator[0]) != asciiUpper(parsed_to.disk_designator[0]);1096 break :x ascii.toUpper(parsed_from.disk_designator[0]) != ascii.toUpper(parsed_to.disk_designator[0]);
1114 },1097 },
1115 else => unreachable,1098 else => unreachable,
1116 }1099 }
...@@ -1128,7 +1111,7 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) !...@@ -1128,7 +1111,7 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) !
1128 const to_rest = to_it.rest();1111 const to_rest = to_it.rest();
1129 if (to_it.next()) |to_component| {1112 if (to_it.next()) |to_component| {
1130 // TODO ASCII is wrong, we actually need full unicode support to compare paths.1113 // TODO ASCII is wrong, we actually need full unicode support to compare paths.
1131 if (asciiEqlIgnoreCase(from_component, to_component))1114 if (ascii.eqlIgnoreCase(from_component, to_component))
1132 continue;1115 continue;
1133 }1116 }
1134 var up_count: usize = 1;1117 var up_count: usize = 1;