| ... | @@ -133,8 +133,7 @@ pub const Headers = struct { | ... | @@ -133,8 +133,7 @@ pub const Headers = struct { |
| 133 | self.index.deinit(); | 133 | self.index.deinit(); |
| 134 | } | 134 | } |
| 135 | { | 135 | { |
| 136 | var it = self.data.iterator(); | 136 | for (self.data.toSliceConst()) |entry| { |
| 137 | while (it.next()) |entry| { | | |
| 138 | entry.deinit(); | 137 | entry.deinit(); |
| 139 | } | 138 | } |
| 140 | self.data.deinit(); | 139 | self.data.deinit(); |
| ... | @@ -144,27 +143,20 @@ pub const Headers = struct { | ... | @@ -144,27 +143,20 @@ pub const Headers = struct { |
| 144 | pub fn clone(self: Self, allocator: *Allocator) !Self { | 143 | pub fn clone(self: Self, allocator: *Allocator) !Self { |
| 145 | var other = Headers.init(allocator); | 144 | var other = Headers.init(allocator); |
| 146 | errdefer other.deinit(); | 145 | errdefer other.deinit(); |
| 147 | try other.data.ensureCapacity(self.data.count()); | 146 | try other.data.ensureCapacity(self.data.len); |
| 148 | try other.index.initCapacity(self.index.entries.len); | 147 | try other.index.initCapacity(self.index.entries.len); |
| 149 | var it = self.data.iterator(); | 148 | for (self.data.toSliceConst()) |entry| { |
| 150 | while (it.next()) |entry| { | | |
| 151 | try other.append(entry.name, entry.value, entry.never_index); | 149 | try other.append(entry.name, entry.value, entry.never_index); |
| 152 | } | 150 | } |
| 153 | return other; | 151 | return other; |
| 154 | } | 152 | } |
| 155 | | 153 | |
| 156 | pub fn count(self: Self) usize { | 154 | pub fn toSlice(self: Self) []const HeaderEntry { |
| 157 | return self.data.count(); | 155 | return self.data.toSliceConst(); |
| 158 | } | | |
| 159 | | | |
| 160 | pub const Iterator = HeaderList.Iterator; | | |
| 161 | | | |
| 162 | pub fn iterator(self: Self) Iterator { | | |
| 163 | return self.data.iterator(); | | |
| 164 | } | 156 | } |
| 165 | | 157 | |
| 166 | pub fn append(self: *Self, name: []const u8, value: []const u8, never_index: ?bool) !void { | 158 | pub fn append(self: *Self, name: []const u8, value: []const u8, never_index: ?bool) !void { |
| 167 | const n = self.data.count() + 1; | 159 | const n = self.data.len + 1; |
| 168 | try self.data.ensureCapacity(n); | 160 | try self.data.ensureCapacity(n); |
| 169 | var entry: HeaderEntry = undefined; | 161 | var entry: HeaderEntry = undefined; |
| 170 | if (self.index.get(name)) |kv| { | 162 | if (self.index.get(name)) |kv| { |
| ... | @@ -190,7 +182,7 @@ pub const Headers = struct { | ... | @@ -190,7 +182,7 @@ pub const Headers = struct { |
| 190 | pub fn upsert(self: *Self, name: []const u8, value: []const u8, never_index: ?bool) !void { | 182 | pub fn upsert(self: *Self, name: []const u8, value: []const u8, never_index: ?bool) !void { |
| 191 | if (self.index.get(name)) |kv| { | 183 | if (self.index.get(name)) |kv| { |
| 192 | const dex = kv.value; | 184 | const dex = kv.value; |
| 193 | if (dex.count() != 1) | 185 | if (dex.len != 1) |
| 194 | return error.CannotUpsertMultiValuedField; | 186 | return error.CannotUpsertMultiValuedField; |
| 195 | var e = &self.data.at(dex.at(0)); | 187 | var e = &self.data.at(dex.at(0)); |
| 196 | try e.modify(value, never_index); | 188 | try e.modify(value, never_index); |
| ... | @@ -209,7 +201,7 @@ pub const Headers = struct { | ... | @@ -209,7 +201,7 @@ pub const Headers = struct { |
| 209 | if (self.index.remove(name)) |kv| { | 201 | if (self.index.remove(name)) |kv| { |
| 210 | var dex = &kv.value; | 202 | var dex = &kv.value; |
| 211 | // iterate backwards | 203 | // iterate backwards |
| 212 | var i = dex.count(); | 204 | var i = dex.len; |
| 213 | while (i > 0) { | 205 | while (i > 0) { |
| 214 | i -= 1; | 206 | i -= 1; |
| 215 | const data_index = dex.at(i); | 207 | const data_index = dex.at(i); |
| ... | @@ -232,18 +224,18 @@ pub const Headers = struct { | ... | @@ -232,18 +224,18 @@ pub const Headers = struct { |
| 232 | const removed = self.data.orderedRemove(i); | 224 | const removed = self.data.orderedRemove(i); |
| 233 | const kv = self.index.get(removed.name).?; | 225 | const kv = self.index.get(removed.name).?; |
| 234 | var dex = &kv.value; | 226 | var dex = &kv.value; |
| 235 | if (dex.count() == 1) { | 227 | if (dex.len == 1) { |
| 236 | // was last item; delete the index | 228 | // was last item; delete the index |
| 237 | _ = self.index.remove(kv.key); | 229 | _ = self.index.remove(kv.key); |
| 238 | dex.deinit(); | 230 | dex.deinit(); |
| 239 | removed.deinit(); | 231 | removed.deinit(); |
| 240 | self.allocator.free(kv.key); | 232 | self.allocator.free(kv.key); |
| 241 | } else { | 233 | } else { |
| 242 | dex.shrink(dex.count() - 1); | 234 | dex.shrink(dex.len - 1); |
| 243 | removed.deinit(); | 235 | removed.deinit(); |
| 244 | } | 236 | } |
| 245 | // if it was the last item; no need to rebuild index | 237 | // if it was the last item; no need to rebuild index |
| 246 | if (i != self.data.count()) { | 238 | if (i != self.data.len) { |
| 247 | self.rebuild_index(); | 239 | self.rebuild_index(); |
| 248 | } | 240 | } |
| 249 | } | 241 | } |
| ... | @@ -254,18 +246,18 @@ pub const Headers = struct { | ... | @@ -254,18 +246,18 @@ pub const Headers = struct { |
| 254 | const removed = self.data.swapRemove(i); | 246 | const removed = self.data.swapRemove(i); |
| 255 | const kv = self.index.get(removed.name).?; | 247 | const kv = self.index.get(removed.name).?; |
| 256 | var dex = &kv.value; | 248 | var dex = &kv.value; |
| 257 | if (dex.count() == 1) { | 249 | if (dex.len == 1) { |
| 258 | // was last item; delete the index | 250 | // was last item; delete the index |
| 259 | _ = self.index.remove(kv.key); | 251 | _ = self.index.remove(kv.key); |
| 260 | dex.deinit(); | 252 | dex.deinit(); |
| 261 | removed.deinit(); | 253 | removed.deinit(); |
| 262 | self.allocator.free(kv.key); | 254 | self.allocator.free(kv.key); |
| 263 | } else { | 255 | } else { |
| 264 | dex.shrink(dex.count() - 1); | 256 | dex.shrink(dex.len - 1); |
| 265 | removed.deinit(); | 257 | removed.deinit(); |
| 266 | } | 258 | } |
| 267 | // if it was the last item; no need to rebuild index | 259 | // if it was the last item; no need to rebuild index |
| 268 | if (i != self.data.count()) { | 260 | if (i != self.data.len) { |
| 269 | self.rebuild_index(); | 261 | self.rebuild_index(); |
| 270 | } | 262 | } |
| 271 | } | 263 | } |
| ... | @@ -289,10 +281,9 @@ pub const Headers = struct { | ... | @@ -289,10 +281,9 @@ pub const Headers = struct { |
| 289 | pub fn get(self: Self, allocator: *Allocator, name: []const u8) !?[]const HeaderEntry { | 281 | pub fn get(self: Self, allocator: *Allocator, name: []const u8) !?[]const HeaderEntry { |
| 290 | const dex = self.getIndices(name) orelse return null; | 282 | const dex = self.getIndices(name) orelse return null; |
| 291 | | 283 | |
| 292 | const buf = try allocator.alloc(HeaderEntry, dex.count()); | 284 | const buf = try allocator.alloc(HeaderEntry, dex.len); |
| 293 | var it = dex.iterator(); | | |
| 294 | var n: usize = 0; | 285 | var n: usize = 0; |
| 295 | while (it.next()) |idx| { | 286 | for (dex.toSliceConst()) |idx| { |
| 296 | buf[n] = self.data.at(idx); | 287 | buf[n] = self.data.at(idx); |
| 297 | n += 1; | 288 | n += 1; |
| 298 | } | 289 | } |
| ... | @@ -314,9 +305,8 @@ pub const Headers = struct { | ... | @@ -314,9 +305,8 @@ pub const Headers = struct { |
| 314 | | 305 | |
| 315 | // adapted from mem.join | 306 | // adapted from mem.join |
| 316 | const total_len = blk: { | 307 | const total_len = blk: { |
| 317 | var sum: usize = dex.count() - 1; // space for separator(s) | 308 | var sum: usize = dex.len - 1; // space for separator(s) |
| 318 | var it = dex.iterator(); | 309 | for (dex.toSliceConst()) |idx| |
| 319 | while (it.next()) |idx| | | |
| 320 | sum += self.data.at(idx).value.len; | 310 | sum += self.data.at(idx).value.len; |
| 321 | break :blk sum; | 311 | break :blk sum; |
| 322 | }; | 312 | }; |
| ... | @@ -348,10 +338,9 @@ pub const Headers = struct { | ... | @@ -348,10 +338,9 @@ pub const Headers = struct { |
| 348 | } | 338 | } |
| 349 | } | 339 | } |
| 350 | { // fill up indexes again; we know capacity is fine from before | 340 | { // fill up indexes again; we know capacity is fine from before |
| 351 | var it = self.data.iterator(); | 341 | for (self.data.toSliceConst()) |entry, i| { |
| 352 | while (it.next()) |entry| { | | |
| 353 | var dex = &self.index.get(entry.name).?.value; | 342 | var dex = &self.index.get(entry.name).?.value; |
| 354 | dex.appendAssumeCapacity(it.count); | 343 | dex.appendAssumeCapacity(i); |
| 355 | } | 344 | } |
| 356 | } | 345 | } |
| 357 | } | 346 | } |
| ... | @@ -369,8 +358,7 @@ pub const Headers = struct { | ... | @@ -369,8 +358,7 @@ pub const Headers = struct { |
| 369 | comptime Errors: type, | 358 | comptime Errors: type, |
| 370 | output: fn (@TypeOf(context), []const u8) Errors!void, | 359 | output: fn (@TypeOf(context), []const u8) Errors!void, |
| 371 | ) Errors!void { | 360 | ) Errors!void { |
| 372 | var it = self.iterator(); | 361 | for (self.toSlice()) |entry| { |
| 373 | while (it.next()) |entry| { | | |
| 374 | try output(context, entry.name); | 362 | try output(context, entry.name); |
| 375 | try output(context, ": "); | 363 | try output(context, ": "); |
| 376 | try output(context, entry.value); | 364 | try output(context, entry.value); |
| ... | @@ -386,8 +374,7 @@ test "Headers.iterator" { | ... | @@ -386,8 +374,7 @@ test "Headers.iterator" { |
| 386 | try h.append("cookie", "somevalue", null); | 374 | try h.append("cookie", "somevalue", null); |
| 387 | | 375 | |
| 388 | var count: i32 = 0; | 376 | var count: i32 = 0; |
| 389 | var it = h.iterator(); | 377 | for (h.toSlice()) |e| { |
| 390 | while (it.next()) |e| { | | |
| 391 | if (count == 0) { | 378 | if (count == 0) { |
| 392 | testing.expectEqualSlices(u8, "foo", e.name); | 379 | testing.expectEqualSlices(u8, "foo", e.name); |
| 393 | testing.expectEqualSlices(u8, "bar", e.value); | 380 | testing.expectEqualSlices(u8, "bar", e.value); |
| ... | @@ -420,10 +407,10 @@ test "Headers.delete" { | ... | @@ -420,10 +407,10 @@ test "Headers.delete" { |
| 420 | try h.append("cookie", "somevalue", null); | 407 | try h.append("cookie", "somevalue", null); |
| 421 | | 408 | |
| 422 | testing.expectEqual(false, h.delete("not-present")); | 409 | testing.expectEqual(false, h.delete("not-present")); |
| 423 | testing.expectEqual(@as(usize, 3), h.count()); | 410 | testing.expectEqual(@as(usize, 3), h.toSlice().len); |
| 424 | | 411 | |
| 425 | testing.expectEqual(true, h.delete("foo")); | 412 | testing.expectEqual(true, h.delete("foo")); |
| 426 | testing.expectEqual(@as(usize, 2), h.count()); | 413 | testing.expectEqual(@as(usize, 2), h.toSlice().len); |
| 427 | { | 414 | { |
| 428 | const e = h.at(0); | 415 | const e = h.at(0); |
| 429 | testing.expectEqualSlices(u8, "baz", e.name); | 416 | testing.expectEqualSlices(u8, "baz", e.name); |
| ... | @@ -448,7 +435,7 @@ test "Headers.orderedRemove" { | ... | @@ -448,7 +435,7 @@ test "Headers.orderedRemove" { |
| 448 | try h.append("cookie", "somevalue", null); | 435 | try h.append("cookie", "somevalue", null); |
| 449 | | 436 | |
| 450 | h.orderedRemove(0); | 437 | h.orderedRemove(0); |
| 451 | testing.expectEqual(@as(usize, 2), h.count()); | 438 | testing.expectEqual(@as(usize, 2), h.toSlice().len); |
| 452 | { | 439 | { |
| 453 | const e = h.at(0); | 440 | const e = h.at(0); |
| 454 | testing.expectEqualSlices(u8, "baz", e.name); | 441 | testing.expectEqualSlices(u8, "baz", e.name); |
| ... | @@ -471,7 +458,7 @@ test "Headers.swapRemove" { | ... | @@ -471,7 +458,7 @@ test "Headers.swapRemove" { |
| 471 | try h.append("cookie", "somevalue", null); | 458 | try h.append("cookie", "somevalue", null); |
| 472 | | 459 | |
| 473 | h.swapRemove(0); | 460 | h.swapRemove(0); |
| 474 | testing.expectEqual(@as(usize, 2), h.count()); | 461 | testing.expectEqual(@as(usize, 2), h.toSlice().len); |
| 475 | { | 462 | { |
| 476 | const e = h.at(0); | 463 | const e = h.at(0); |
| 477 | testing.expectEqualSlices(u8, "cookie", e.name); | 464 | testing.expectEqualSlices(u8, "cookie", e.name); |