Switch to ArrayHashMap

This commit is contained in:
jaina heartles 2022-11-06 23:59:35 -08:00
parent 2d464f0820
commit d2151ae326
3 changed files with 12 additions and 6 deletions

View file

@ -3,24 +3,24 @@ const std = @import("std");
pub const Fields = struct {
const HashContext = struct {
const hash_seed = 1;
pub fn eql(_: @This(), lhs: []const u8, rhs: []const u8) bool {
pub fn eql(_: @This(), lhs: []const u8, rhs: []const u8, _: usize) bool {
return std.ascii.eqlIgnoreCase(lhs, rhs);
}
pub fn hash(_: @This(), s: []const u8) u64 {
pub fn hash(_: @This(), s: []const u8) u32 {
var h = std.hash.Wyhash.init(hash_seed);
for (s) |ch| {
const c = [1]u8{std.ascii.toLower(ch)};
h.update(&c);
}
return h.final();
return @truncate(u32, h.final());
}
};
const HashMap = std.HashMapUnmanaged(
const HashMap = std.ArrayHashMapUnmanaged(
[]const u8,
[]const u8,
HashContext,
std.hash_map.default_max_load_percentage,
true,
);
unmanaged: HashMap,

View file

@ -38,7 +38,11 @@ fn writeStatusLine(writer: anytype, status: Status) !void {
fn writeFields(writer: anytype, headers: *const Fields) !void {
var iter = headers.iterator();
std.log.debug("{any}", .{headers});
std.log.debug("{any}", .{iter});
while (iter.next()) |header| {
std.log.debug("{any}", .{headers});
std.log.debug("{any}", .{iter});
for (header.value_ptr.*) |ch| {
if (ch == '\r' or ch == '\n') @panic("newlines not yet supported in headers");
}
@ -95,7 +99,6 @@ pub fn ResponseStream(comptime BaseWriter: type) type {
return;
}
std.debug.print("{}\n", .{cursor});
self.writeToBuffer(bytes[cursor .. cursor + remaining_in_chunk]);
cursor += remaining_in_chunk;
try self.flushChunk();

View file

@ -1,4 +1,5 @@
const api = @import("api");
const std = @import("std");
pub const login = struct {
pub const method = .POST;
@ -12,6 +13,8 @@ pub const login = struct {
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const token = try srv.login(req.body.username, req.body.password);
std.log.debug("{any}", .{res.headers});
try res.json(.ok, token);
}
};