From f08c6e4fe6ad63145ef69377f36f34e9f04cadec Mon Sep 17 00:00:00 2001 From: Sahnvour Date: Tue, 3 Sep 2019 23:53:05 +0200 Subject: [PATCH] changing occurrences of HashMap with []const u8 as keys for StringHashMap --- doc/docgen.zig | 4 ++-- src-self-hosted/arg.zig | 4 ++-- src-self-hosted/compilation.zig | 2 +- src-self-hosted/decl.zig | 2 +- src-self-hosted/main.zig | 2 +- src-self-hosted/package.zig | 2 +- src-self-hosted/stage1.zig | 4 ++-- std/buf_map.zig | 4 ++-- std/buf_set.zig | 4 ++-- std/build.zig | 6 +++--- std/json.zig | 4 ++-- std/mem.zig | 37 +++++++++++---------------------- 12 files changed, 31 insertions(+), 44 deletions(-) diff --git a/doc/docgen.zig b/doc/docgen.zig index 458b97d2c0..a2b4e8501c 100644 --- a/doc/docgen.zig +++ b/doc/docgen.zig @@ -307,7 +307,7 @@ const Node = union(enum) { const Toc = struct { nodes: []Node, toc: []u8, - urls: std.HashMap([]const u8, Token, mem.hash_slice_u8, mem.eql_slice_u8), + urls: std.StringHashMap(Token), }; const Action = enum { @@ -316,7 +316,7 @@ const Action = enum { }; fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc { - var urls = std.HashMap([]const u8, Token, mem.hash_slice_u8, mem.eql_slice_u8).init(allocator); + var urls = std.StringHashMap(Token).init(allocator); errdefer urls.deinit(); var header_stack_size: usize = 0; diff --git a/src-self-hosted/arg.zig b/src-self-hosted/arg.zig index 8f95e2d7ef..8b0ea5ea61 100644 --- a/src-self-hosted/arg.zig +++ b/src-self-hosted/arg.zig @@ -5,7 +5,7 @@ const mem = std.mem; const Allocator = mem.Allocator; const ArrayList = std.ArrayList; -const HashMap = std.HashMap; +const StringHashMap = std.StringHashMap; fn trimStart(slice: []const u8, ch: u8) []const u8 { var i: usize = 0; @@ -73,7 +73,7 @@ fn readFlagArguments(allocator: *Allocator, args: []const []const u8, required: } } -const HashMapFlags = HashMap([]const u8, FlagArg, std.hash.Fnv1a_32.hash, mem.eql_slice_u8); +const HashMapFlags = StringHashMap(FlagArg); // A store for querying found flags and positional arguments. pub const Args = struct { diff --git a/src-self-hosted/compilation.zig b/src-self-hosted/compilation.zig index a64e52a2b6..1e71a5e561 100644 --- a/src-self-hosted/compilation.zig +++ b/src-self-hosted/compilation.zig @@ -249,7 +249,7 @@ pub const Compilation = struct { const ArrayTypeTable = std.HashMap(*const Type.Array.Key, *Type.Array, Type.Array.Key.hash, Type.Array.Key.eql); const PtrTypeTable = std.HashMap(*const Type.Pointer.Key, *Type.Pointer, Type.Pointer.Key.hash, Type.Pointer.Key.eql); const FnTypeTable = std.HashMap(*const Type.Fn.Key, *Type.Fn, Type.Fn.Key.hash, Type.Fn.Key.eql); - const TypeTable = std.HashMap([]const u8, *Type, mem.hash_slice_u8, mem.eql_slice_u8); + const TypeTable = std.StringHashMap(*Type); const CompileErrList = std.ArrayList(*Msg); diff --git a/src-self-hosted/decl.zig b/src-self-hosted/decl.zig index 25fcf195d1..1af06dea39 100644 --- a/src-self-hosted/decl.zig +++ b/src-self-hosted/decl.zig @@ -20,7 +20,7 @@ pub const Decl = struct { // TODO when we destroy the decl, deref the tree scope tree_scope: *Scope.AstTree, - pub const Table = std.HashMap([]const u8, *Decl, mem.hash_slice_u8, mem.eql_slice_u8); + pub const Table = std.StringHashMap(*Decl); pub fn cast(base: *Decl, comptime T: type) ?*T { if (base.id != @field(Id, @typeName(T))) return null; diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index 5136b32735..52eda5824a 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -541,7 +541,7 @@ const Fmt = struct { color: errmsg.Color, loop: *event.Loop, - const SeenMap = std.HashMap([]const u8, void, mem.hash_slice_u8, mem.eql_slice_u8); + const SeenMap = std.StringHashMap(void); }; fn parseLibcPaths(allocator: *Allocator, libc: *LibCInstallation, libc_paths_file: []const u8) void { diff --git a/src-self-hosted/package.zig b/src-self-hosted/package.zig index 0d31731b55..c8d46c7719 100644 --- a/src-self-hosted/package.zig +++ b/src-self-hosted/package.zig @@ -10,7 +10,7 @@ pub const Package = struct { /// relative to root_src_dir table: Table, - pub const Table = std.HashMap([]const u8, *Package, mem.hash_slice_u8, mem.eql_slice_u8); + pub const Table = std.StringHashMap(*Package); /// makes internal copies of root_src_dir and root_src_path /// allocator should be an arena allocator because Package never frees anything diff --git a/src-self-hosted/stage1.zig b/src-self-hosted/stage1.zig index b8f13b5d03..2f48f2f450 100644 --- a/src-self-hosted/stage1.zig +++ b/src-self-hosted/stage1.zig @@ -343,7 +343,7 @@ const Fmt = struct { color: errmsg.Color, allocator: *mem.Allocator, - const SeenMap = std.HashMap([]const u8, void, mem.hash_slice_u8, mem.eql_slice_u8); + const SeenMap = std.StringHashMap(void); }; fn printErrMsgToFile( @@ -376,7 +376,7 @@ fn printErrMsgToFile( const text = text_buf.toOwnedSlice(); const stream = &file.outStream().stream; - try stream.print( "{}:{}:{}: error: {}\n", path, start_loc.line + 1, start_loc.column + 1, text); + try stream.print("{}:{}:{}: error: {}\n", path, start_loc.line + 1, start_loc.column + 1, text); if (!color_on) return; diff --git a/std/buf_map.zig b/std/buf_map.zig index 4079d41caf..d7aa314157 100644 --- a/std/buf_map.zig +++ b/std/buf_map.zig @@ -1,5 +1,5 @@ const std = @import("std.zig"); -const HashMap = std.HashMap; +const StringHashMap = std.StringHashMap; const mem = std.mem; const Allocator = mem.Allocator; const testing = std.testing; @@ -9,7 +9,7 @@ const testing = std.testing; pub const BufMap = struct { hash_map: BufMapHashMap, - const BufMapHashMap = HashMap([]const u8, []const u8, mem.hash_slice_u8, mem.eql_slice_u8); + const BufMapHashMap = StringHashMap([]const u8); pub fn init(allocator: *Allocator) BufMap { var self = BufMap{ .hash_map = BufMapHashMap.init(allocator) }; diff --git a/std/buf_set.zig b/std/buf_set.zig index 33e66a64e8..1a321e89c9 100644 --- a/std/buf_set.zig +++ b/std/buf_set.zig @@ -1,5 +1,5 @@ const std = @import("std.zig"); -const HashMap = @import("hash_map.zig").HashMap; +const StringHashMap = std.StringHashMap; const mem = @import("mem.zig"); const Allocator = mem.Allocator; const testing = std.testing; @@ -7,7 +7,7 @@ const testing = std.testing; pub const BufSet = struct { hash_map: BufSetHashMap, - const BufSetHashMap = HashMap([]const u8, void, mem.hash_slice_u8, mem.eql_slice_u8); + const BufSetHashMap = StringHashMap(void); pub fn init(a: *Allocator) BufSet { var self = BufSet{ .hash_map = BufSetHashMap.init(a) }; diff --git a/std/build.zig b/std/build.zig index 27715663f7..0049dcd035 100644 --- a/std/build.zig +++ b/std/build.zig @@ -7,7 +7,7 @@ const debug = std.debug; const assert = debug.assert; const warn = std.debug.warn; const ArrayList = std.ArrayList; -const HashMap = std.HashMap; +const StringHashMap = std.StringHashMap; const Allocator = mem.Allocator; const process = std.process; const BufSet = std.BufSet; @@ -60,8 +60,8 @@ pub const Builder = struct { C11, }; - const UserInputOptionsMap = HashMap([]const u8, UserInputOption, mem.hash_slice_u8, mem.eql_slice_u8); - const AvailableOptionsMap = HashMap([]const u8, AvailableOption, mem.hash_slice_u8, mem.eql_slice_u8); + const UserInputOptionsMap = StringHashMap(UserInputOption); + const AvailableOptionsMap = StringHashMap(AvailableOption); const AvailableOption = struct { name: []const u8, diff --git a/std/json.zig b/std/json.zig index 8324c59e32..f562a672f8 100644 --- a/std/json.zig +++ b/std/json.zig @@ -989,7 +989,7 @@ test "json.validate" { const Allocator = std.mem.Allocator; const ArenaAllocator = std.heap.ArenaAllocator; const ArrayList = std.ArrayList; -const HashMap = std.HashMap; +const StringHashMap = std.StringHashMap; pub const ValueTree = struct { arena: ArenaAllocator, @@ -1000,7 +1000,7 @@ pub const ValueTree = struct { } }; -pub const ObjectMap = HashMap([]const u8, Value, mem.hash_slice_u8, mem.eql_slice_u8); +pub const ObjectMap = StringHashMap(Value); pub const Value = union(enum) { Null, diff --git a/std/mem.zig b/std/mem.zig index 61dc5c7a30..62a1ae886a 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -738,47 +738,34 @@ test "writeIntBig and writeIntLittle" { var buf9: [9]u8 = undefined; writeIntBig(u0, &buf0, 0x0); - testing.expect(eql_slice_u8(buf0[0..], [_]u8{})); + testing.expect(eql(u8, buf0[0..], [_]u8{})); writeIntLittle(u0, &buf0, 0x0); - testing.expect(eql_slice_u8(buf0[0..], [_]u8{})); + testing.expect(eql(u8, buf0[0..], [_]u8{})); writeIntBig(u8, &buf1, 0x12); - testing.expect(eql_slice_u8(buf1[0..], [_]u8{0x12})); + testing.expect(eql(u8, buf1[0..], [_]u8{0x12})); writeIntLittle(u8, &buf1, 0x34); - testing.expect(eql_slice_u8(buf1[0..], [_]u8{0x34})); + testing.expect(eql(u8, buf1[0..], [_]u8{0x34})); writeIntBig(u16, &buf2, 0x1234); - testing.expect(eql_slice_u8(buf2[0..], [_]u8{ 0x12, 0x34 })); + testing.expect(eql(u8, buf2[0..], [_]u8{ 0x12, 0x34 })); writeIntLittle(u16, &buf2, 0x5678); - testing.expect(eql_slice_u8(buf2[0..], [_]u8{ 0x78, 0x56 })); + testing.expect(eql(u8, buf2[0..], [_]u8{ 0x78, 0x56 })); writeIntBig(u72, &buf9, 0x123456789abcdef024); - testing.expect(eql_slice_u8(buf9[0..], [_]u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 })); + testing.expect(eql(u8, buf9[0..], [_]u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 })); writeIntLittle(u72, &buf9, 0xfedcba9876543210ec); - testing.expect(eql_slice_u8(buf9[0..], [_]u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe })); + testing.expect(eql(u8, buf9[0..], [_]u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe })); writeIntBig(i8, &buf1, -1); - testing.expect(eql_slice_u8(buf1[0..], [_]u8{0xff})); + testing.expect(eql(u8, buf1[0..], [_]u8{0xff})); writeIntLittle(i8, &buf1, -2); - testing.expect(eql_slice_u8(buf1[0..], [_]u8{0xfe})); + testing.expect(eql(u8, buf1[0..], [_]u8{0xfe})); writeIntBig(i16, &buf2, -3); - testing.expect(eql_slice_u8(buf2[0..], [_]u8{ 0xff, 0xfd })); + testing.expect(eql(u8, buf2[0..], [_]u8{ 0xff, 0xfd })); writeIntLittle(i16, &buf2, -4); - testing.expect(eql_slice_u8(buf2[0..], [_]u8{ 0xfc, 0xff })); -} - -pub fn hash_slice_u8(k: []const u8) u32 { - // FNV 32-bit hash - var h: u32 = 2166136261; - for (k) |b| { - h = (h ^ b) *% 16777619; - } - return h; -} - -pub fn eql_slice_u8(a: []const u8, b: []const u8) bool { - return eql(u8, a, b); + testing.expect(eql(u8, buf2[0..], [_]u8{ 0xfc, 0xff })); } /// Returns an iterator that iterates over the slices of `buffer` that are not