mirror of
https://github.com/ziglang/zig.git
synced 2026-01-21 06:45:24 +00:00
use std.ascii instead of defining ascii functions in std.fs.path
This commit is contained in:
parent
246a39c10e
commit
0be46866fe
@ -5,6 +5,7 @@ const assert = debug.assert;
|
||||
const testing = std.testing;
|
||||
const mem = std.mem;
|
||||
const fmt = std.fmt;
|
||||
const ascii = std.ascii;
|
||||
const Allocator = mem.Allocator;
|
||||
const math = std.math;
|
||||
const windows = std.os.windows;
|
||||
@ -423,7 +424,7 @@ fn networkShareServersEql(ns1: []const u8, ns2: []const u8) bool {
|
||||
var it2 = mem.tokenize(u8, ns2, &[_]u8{sep2});
|
||||
|
||||
// TODO ASCII is wrong, we actually need full unicode support to compare paths.
|
||||
return asciiEqlIgnoreCase(it1.next().?, it2.next().?);
|
||||
return ascii.eqlIgnoreCase(it1.next().?, it2.next().?);
|
||||
}
|
||||
|
||||
fn 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
|
||||
return true;
|
||||
},
|
||||
WindowsPath.Kind.Drive => {
|
||||
return asciiUpper(p1[0]) == asciiUpper(p2[0]);
|
||||
return ascii.toUpper(p1[0]) == ascii.toUpper(p2[0]);
|
||||
},
|
||||
WindowsPath.Kind.NetworkShare => {
|
||||
const sep1 = p1[0];
|
||||
@ -444,29 +445,11 @@ fn compareDiskDesignators(kind: WindowsPath.Kind, p1: []const u8, p2: []const u8
|
||||
var it2 = mem.tokenize(u8, p2, &[_]u8{sep2});
|
||||
|
||||
// TODO ASCII is wrong, we actually need full unicode support to compare paths.
|
||||
return asciiEqlIgnoreCase(it1.next().?, it2.next().?) and asciiEqlIgnoreCase(it1.next().?, it2.next().?);
|
||||
return ascii.eqlIgnoreCase(it1.next().?, it2.next().?) and ascii.eqlIgnoreCase(it1.next().?, it2.next().?);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn asciiUpper(byte: u8) u8 {
|
||||
return switch (byte) {
|
||||
'a'...'z' => 'A' + (byte - 'a'),
|
||||
else => byte,
|
||||
};
|
||||
}
|
||||
|
||||
fn asciiEqlIgnoreCase(s1: []const u8, s2: []const u8) bool {
|
||||
if (s1.len != s2.len)
|
||||
return false;
|
||||
var i: usize = 0;
|
||||
while (i < s1.len) : (i += 1) {
|
||||
if (asciiUpper(s1[i]) != asciiUpper(s2[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`.
|
||||
pub fn resolve(allocator: Allocator, paths: []const []const u8) ![]u8 {
|
||||
if (native_os == .windows) {
|
||||
@ -506,7 +489,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {
|
||||
}
|
||||
switch (parsed.kind) {
|
||||
WindowsPath.Kind.Drive => {
|
||||
result_drive_buf[0] = asciiUpper(parsed.disk_designator[0]);
|
||||
result_drive_buf[0] = ascii.toUpper(parsed.disk_designator[0]);
|
||||
result_disk_designator = result_drive_buf[0..];
|
||||
have_drive_kind = WindowsPath.Kind.Drive;
|
||||
},
|
||||
@ -590,7 +573,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {
|
||||
result_index += parsed_cwd.disk_designator.len;
|
||||
result_disk_designator = result[0..parsed_cwd.disk_designator.len];
|
||||
if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
|
||||
result[0] = asciiUpper(result[0]);
|
||||
result[0] = ascii.toUpper(result[0]);
|
||||
}
|
||||
have_drive_kind = parsed_cwd.kind;
|
||||
},
|
||||
@ -608,7 +591,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 {
|
||||
const parsed_cwd = windowsParsePath(result[0..result_index]);
|
||||
result_disk_designator = parsed_cwd.disk_designator;
|
||||
if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
|
||||
result[0] = asciiUpper(result[0]);
|
||||
result[0] = ascii.toUpper(result[0]);
|
||||
// Remove the trailing slash if present, eg. if the cwd is a root
|
||||
// directory.
|
||||
if (cwd.len > 0 and cwd[cwd.len - 1] == sep_windows) {
|
||||
@ -741,7 +724,7 @@ test "resolve" {
|
||||
defer testing.allocator.free(cwd);
|
||||
if (native_os == .windows) {
|
||||
if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) {
|
||||
cwd[0] = asciiUpper(cwd[0]);
|
||||
cwd[0] = ascii.toUpper(cwd[0]);
|
||||
}
|
||||
try testResolveWindows(&[_][]const u8{"."}, cwd);
|
||||
} else {
|
||||
@ -768,7 +751,7 @@ test "resolveWindows" {
|
||||
});
|
||||
defer testing.allocator.free(expected);
|
||||
if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
|
||||
expected[0] = asciiUpper(parsed_cwd.disk_designator[0]);
|
||||
expected[0] = ascii.toUpper(parsed_cwd.disk_designator[0]);
|
||||
}
|
||||
try testResolveWindows(&[_][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" }, expected);
|
||||
}
|
||||
@ -779,7 +762,7 @@ test "resolveWindows" {
|
||||
});
|
||||
defer testing.allocator.free(expected);
|
||||
if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
|
||||
expected[0] = asciiUpper(parsed_cwd.disk_designator[0]);
|
||||
expected[0] = ascii.toUpper(parsed_cwd.disk_designator[0]);
|
||||
}
|
||||
try testResolveWindows(&[_][]const u8{ "usr/local", "lib\\zig" }, expected);
|
||||
}
|
||||
@ -1110,7 +1093,7 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) !
|
||||
break :x !networkShareServersEql(parsed_to.disk_designator, parsed_from.disk_designator);
|
||||
},
|
||||
WindowsPath.Kind.Drive => {
|
||||
break :x asciiUpper(parsed_from.disk_designator[0]) != asciiUpper(parsed_to.disk_designator[0]);
|
||||
break :x ascii.toUpper(parsed_from.disk_designator[0]) != ascii.toUpper(parsed_to.disk_designator[0]);
|
||||
},
|
||||
else => unreachable,
|
||||
}
|
||||
@ -1128,7 +1111,7 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) !
|
||||
const to_rest = to_it.rest();
|
||||
if (to_it.next()) |to_component| {
|
||||
// TODO ASCII is wrong, we actually need full unicode support to compare paths.
|
||||
if (asciiEqlIgnoreCase(from_component, to_component))
|
||||
if (ascii.eqlIgnoreCase(from_component, to_component))
|
||||
continue;
|
||||
}
|
||||
var up_count: usize = 1;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user