From 71f69190ef7a79650647cf5422633813756fbe48 Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Fri, 4 Feb 2022 23:42:10 -0700 Subject: [PATCH] some fixes to the EnvMap HashContext --- lib/std/build/RunStep.zig | 2 +- lib/std/process.zig | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/std/build/RunStep.zig b/lib/std/build/RunStep.zig index 1e4cca5167..e8cf87a441 100644 --- a/lib/std/build/RunStep.zig +++ b/lib/std/build/RunStep.zig @@ -99,7 +99,7 @@ pub fn clearEnvironment(self: *RunStep) void { pub fn addPathDir(self: *RunStep, search_path: []const u8) void { const env_map = self.getEnvMap(); - var key: []const u8 = "PATH"; + const key = "PATH"; var prev_path = env_map.get(key); if (prev_path) |pp| { diff --git a/lib/std/process.zig b/lib/std/process.zig index 26cde9db51..b460264a8c 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -62,18 +62,20 @@ pub const EnvMap = struct { std.hash_map.default_max_load_percentage, ); + pub const Size = HashMap.Size; + pub const EnvNameHashContext = struct { fn upcase(c: u21) u21 { if (c <= std.math.maxInt(u16)) - return std.os.windows.ntdll.RtlUpcaseUnicodeChar(c); + return std.os.windows.ntdll.RtlUpcaseUnicodeChar(@intCast(u16, c)); return c; } pub fn hash(self: @This(), s: []const u8) u64 { _ = self; if (builtin.os.tag == .windows) { - const h = std.hash.Wyhash.init(0); - var it = std.unicode.Utf8View(s).iterator(); + var h = std.hash.Wyhash.init(0); + var it = std.unicode.Utf8View.initUnchecked(s).iterator(); while (it.nextCodepoint()) |cp| { const cp_upper = upcase(cp); h.update(&[_]u8{ @@ -90,15 +92,15 @@ pub const EnvMap = struct { pub fn eql(self: @This(), a: []const u8, b: []const u8) bool { _ = self; if (builtin.os.tag == .windows) { - var it_a = std.unicode.Utf8View(a).iterator(); - var it_b = std.unicode.Utf8View(b).iterator(); + var it_a = std.unicode.Utf8View.initUnchecked(a).iterator(); + var it_b = std.unicode.Utf8View.initUnchecked(b).iterator(); while (true) { const c_a = it_a.nextCodepoint() orelse break; const c_b = it_b.nextCodepoint() orelse return false; if (upcase(c_a) != upcase(c_b)) return false; } - if (it_b.nextCodepoint()) return false; + if (it_b.nextCodepoint()) |_| return false; } return std.hash_map.eqlString(a, b); } @@ -220,7 +222,7 @@ test "EnvMap" { var it = env.iterator(); var count: EnvMap.Size = 0; while (it.next()) |entry| { - const is_an_expected_name = std.mem.eql(u8, "SOMETHING_NEW", entry.name) or std.mem.eql(u8, "SOMETHING_NEW_AND_LONGER", entry.name); + const is_an_expected_name = std.mem.eql(u8, "SOMETHING_NEW", entry.key_ptr.*) or std.mem.eql(u8, "SOMETHING_NEW_AND_LONGER", entry.key_ptr.*); try testing.expect(is_an_expected_name); count += 1; }