mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00
feat: pre-allocate enough space for accept items
This commit is contained in:
parent
5e347ce649
commit
31ba05d6e9
1 changed files with 7 additions and 5 deletions
|
@ -605,10 +605,12 @@ const AcceptHeaderList = std.ArrayList(AcceptItem);
|
||||||
|
|
||||||
/// Parses `Accept:` http header into `list`, ordered from highest q factor to lowest
|
/// Parses `Accept:` http header into `list`, ordered from highest q factor to lowest
|
||||||
pub fn parseAcceptHeaders(self: *const Self, allocator: std.mem.Allocator) !AcceptHeaderList {
|
pub fn parseAcceptHeaders(self: *const Self, allocator: std.mem.Allocator) !AcceptHeaderList {
|
||||||
var list = AcceptHeaderList.init(allocator);
|
const accept_str = self.getHeaderCommon(.accept) orelse return error.NoAcceptHeader;
|
||||||
errdefer list.deinit();
|
|
||||||
|
|
||||||
const accept_str = self.getHeaderCommon(.accept) orelse return error.NoAccept;
|
const comma_count = std.mem.count(u8, accept_str, ",");
|
||||||
|
|
||||||
|
var list = try AcceptHeaderList.initCapacity(allocator, comma_count + 1);
|
||||||
|
errdefer list.deinit();
|
||||||
|
|
||||||
var tok_iter = std.mem.tokenize(u8, accept_str, ", ");
|
var tok_iter = std.mem.tokenize(u8, accept_str, ", ");
|
||||||
while (tok_iter.next()) |tok| {
|
while (tok_iter.next()) |tok| {
|
||||||
|
@ -637,11 +639,11 @@ pub fn parseAcceptHeaders(self: *const Self, allocator: std.mem.Allocator) !Acce
|
||||||
};
|
};
|
||||||
for (list.items, 1..) |item, i| {
|
for (list.items, 1..) |item, i| {
|
||||||
if (AcceptItem.lessThan({}, new_item, item)) {
|
if (AcceptItem.lessThan({}, new_item, item)) {
|
||||||
try list.insert(i, new_item);
|
list.insertAssumeCapacity(i, new_item);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try list.append(new_item);
|
list.appendAssumeCapacity(new_item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
|
|
Loading…
Add table
Reference in a new issue