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