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;
55const testing = std.testing;
66const mem = std.mem;
77const fmt = std.fmt;
8const ascii = std.ascii;
89const Allocator = mem.Allocator;
910const math = std.math;
1011const windows = std.os.windows;
......@@ -423,7 +424,7 @@ fn networkShareServersEql(ns1: []const u8, ns2: []const u8) bool {
423424 var it2 = mem.tokenize(u8, ns2, &[_]u8{sep2});
424425
425426 // 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().?);
427428}
428429
429430fn 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
434435 return true;
435436 },
436437 WindowsPath.Kind.Drive => {
437 return asciiUpper(p1[0]) == asciiUpper(p2[0]);
438 return ascii.toUpper(p1[0]) == ascii.toUpper(p2[0]);
438439 },
439440 WindowsPath.Kind.NetworkShare => {
440441 const sep1 = p1[0];
......@@ -444,29 +445,11 @@ fn compareDiskDesignators(kind: WindowsPath.Kind, p1: []const u8, p2: []const u8
444445 var it2 = mem.tokenize(u8, p2, &[_]u8{sep2});
445446
446447 // 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().?);
448449 },
449450 }
450451}
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
470453/// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`.
471454pub fn resolve(allocator: Allocator, paths: []const []const u8) ![]u8 {
472455 if (native_os == .windows) {
......@@ -506,7 +489,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {
506489 }
507490 switch (parsed.kind) {
508491 WindowsPath.Kind.Drive => {
509 result_drive_buf[0] = asciiUpper(parsed.disk_designator[0]);
492 result_drive_buf[0] = ascii.toUpper(parsed.disk_designator[0]);
510493 result_disk_designator = result_drive_buf[0..];
511494 have_drive_kind = WindowsPath.Kind.Drive;
512495 },
......@@ -590,7 +573,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {
590573 result_index += parsed_cwd.disk_designator.len;
591574 result_disk_designator = result[0..parsed_cwd.disk_designator.len];
592575 if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
593 result[0] = asciiUpper(result[0]);
576 result[0] = ascii.toUpper(result[0]);
594577 }
595578 have_drive_kind = parsed_cwd.kind;
596579 },
......@@ -608,7 +591,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {
608591 const parsed_cwd = windowsParsePath(result[0..result_index]);
609592 result_disk_designator = parsed_cwd.disk_designator;
610593 if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
611 result[0] = asciiUpper(result[0]);
594 result[0] = ascii.toUpper(result[0]);
612595 // Remove the trailing slash if present, eg. if the cwd is a root
613596 // directory.
614597 if (cwd.len > 0 and cwd[cwd.len - 1] == sep_windows) {
......@@ -741,7 +724,7 @@ test "resolve" {
741724 defer testing.allocator.free(cwd);
742725 if (native_os == .windows) {
743726 if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) {
744 cwd[0] = asciiUpper(cwd[0]);
727 cwd[0] = ascii.toUpper(cwd[0]);
745728 }
746729 try testResolveWindows(&[_][]const u8{"."}, cwd);
747730 } else {
......@@ -768,7 +751,7 @@ test "resolveWindows" {
768751 });
769752 defer testing.allocator.free(expected);
770753 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]);
772755 }
773756 try testResolveWindows(&[_][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" }, expected);
774757 }
......@@ -779,7 +762,7 @@ test "resolveWindows" {
779762 });
780763 defer testing.allocator.free(expected);
781764 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]);
783766 }
784767 try testResolveWindows(&[_][]const u8{ "usr/local", "lib\\zig" }, expected);
785768 }
......@@ -1110,7 +1093,7 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) !
11101093 break :x !networkShareServersEql(parsed_to.disk_designator, parsed_from.disk_designator);
11111094 },
11121095 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]);
11141097 },
11151098 else => unreachable,
11161099 }
......@@ -1128,7 +1111,7 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) !
11281111 const to_rest = to_it.rest();
11291112 if (to_it.next()) |to_component| {
11301113 // 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))
11321115 continue;
11331116 }
11341117 var up_count: usize = 1;