fix std.AutoHashMap usage

This commit is contained in:
Luna 2019-08-09 18:40:11 -03:00
parent c0269ead20
commit 0cab58429c
2 changed files with 27 additions and 3 deletions

View file

@ -71,7 +71,7 @@ fn sf_tell(file: *c.SNDFILE) i64 {
pub fn temporaryName(allocator: *std.mem.Allocator) ![]u8 {
const template_start = "/temp/temp_";
const template = "/tmp/temp_XXXXXXXX";
const template = "/tmp/temp_XXXXXXXXXXX";
var nam = try allocator.alloc(u8, template.len);
std.mem.copy(u8, nam, template);

View file

@ -129,7 +129,20 @@ pub const Command = struct {
pub const CommandList = std.ArrayList(*Command);
pub const ArgList = std.ArrayList([]const u8);
pub const KeywordMap = std.AutoHashMap([]const u8, CommandType);
fn eqlu8(a: []const u8, b: []const u8) bool {
return std.mem.eql(u8, a, b);
}
fn hashu8(key: []const u8) u32 {
var hasher = std.hash.Wyhash.init(0);
for (key) |value| {
std.hash.autoHash(&hasher, value);
}
return @truncate(u32, hasher.final());
}
pub const KeywordMap = std.HashMap([]const u8, CommandType, hashu8, eqlu8);
pub const Lang = struct {
allocator: *std.mem.Allocator,
@ -177,8 +190,19 @@ pub const Lang = struct {
_ = try self.keywords.put("rotate", .Rotate);
}
// TODO remove this once AutoHashMap is fixed.
pub fn getCommand(self: *Lang, stmt: []const u8) ?CommandType {
var kv_opt = self.keywords.get(stmt);
if (kv_opt) |kv| {
return kv.value;
} else {
return null;
}
}
// NOTE this is fallback since std.AutoHashMap does not follow
// pointers anymore (in master).
fn oldGetCommand(self: *Lang, stmt: []const u8) ?CommandType {
const commands = [_][]const u8{
"noop",
"load",