A poolable string builder (aka string buffer) for Zig
0

Configure Feed

Select the types of activity you want to include in your feed.

Properly grow when writing an encoded integer

https://github.com/karlseguin/pg.zig/issues/52

Karl Seguin (Nov 21, 2024, 9:47 AM +0800) 9b17ad57 c9a9e33b

+20 -9
+19 -8
src/buffer.zig
··· 161 161 return self.writeIntT(T, value, .big); 162 162 } 163 163 164 - pub fn writeIntT(self: *Buffer, comptime T: type, value: T, endian: Endian) void { 164 + pub fn writeIntT(self: *Buffer, comptime T: type, value: T, endian: Endian) !void { 165 165 const l = @divExact(@typeInfo(T).int.bits, 8); 166 166 const pos = self.pos; 167 + try self.ensureUnusedCapacity(l); 167 168 writeIntInto(T, self.buf, pos, value, l, endian); 168 169 self.pos = pos + l; 169 170 } ··· 401 402 } 402 403 } 403 404 405 + test "growth with int" { 406 + var w = try Buffer.init(t.allocator, 10); 407 + defer w.deinit(); 408 + 409 + try w.writeU64Big(9000); 410 + try w.writeU64Big(10000); 411 + try t.expectSlice(u8, &.{0, 0, 0, 0, 0, 0, 0x23, 0x28}, w.string()[0..8]); 412 + try t.expectSlice(u8, &.{0, 0, 0, 0, 0, 0, 0x27, 0x10}, w.string()[8..16]); 413 + } 414 + 404 415 test "truncate" { 405 416 var w = try Buffer.init(t.allocator, 10); 406 417 defer w.deinit(); ··· 487 498 var w = try Buffer.init(t.allocator, 20); 488 499 defer w.deinit(); 489 500 try w.writeU64Little(11234567890123456789); 490 - try t.exectSlice(u8, &[_]u8{21, 129, 209, 7, 249, 51, 233, 155}, w.string()); 501 + try t.expectSlice(u8, &[_]u8{21, 129, 209, 7, 249, 51, 233, 155}, w.string()); 491 502 492 503 try w.writeU32Little(3283856184); 493 - try t.exectSlice(u8, &[_]u8{21, 129, 209, 7, 249, 51, 233, 155, 56, 171, 187, 195}, w.string()); 504 + try t.expectSlice(u8, &[_]u8{21, 129, 209, 7, 249, 51, 233, 155, 56, 171, 187, 195}, w.string()); 494 505 495 506 try w.writeU16Little(15000); 496 - try t.exectSlice(u8, &[_]u8{21, 129, 209, 7, 249, 51, 233, 155, 56, 171, 187, 195, 152, 58}, w.string()); 507 + try t.expectSlice(u8, &[_]u8{21, 129, 209, 7, 249, 51, 233, 155, 56, 171, 187, 195, 152, 58}, w.string()); 497 508 } 498 509 499 510 test "write big" { 500 511 var w = try Buffer.init(t.allocator, 20); 501 512 defer w.deinit(); 502 513 try w.writeU64Big(11234567890123456789); 503 - try t.exectSlice(u8, &[_]u8{155, 233, 51, 249, 7, 209, 129, 21}, w.string()); 514 + try t.expectSlice(u8, &[_]u8{155, 233, 51, 249, 7, 209, 129, 21}, w.string()); 504 515 505 516 try w.writeU32Big(3283856184); 506 - try t.exectSlice(u8, &[_]u8{155, 233, 51, 249, 7, 209, 129, 21, 195, 187, 171, 56}, w.string()); 517 + try t.expectSlice(u8, &[_]u8{155, 233, 51, 249, 7, 209, 129, 21, 195, 187, 171, 56}, w.string()); 507 518 508 519 try w.writeU16Big(15000); 509 - try t.exectSlice(u8, &[_]u8{155, 233, 51, 249, 7, 209, 129, 21, 195, 187, 171, 56, 58, 152}, w.string()); 520 + try t.expectSlice(u8, &[_]u8{155, 233, 51, 249, 7, 209, 129, 21, 195, 187, 171, 56, 58, 152}, w.string()); 510 521 } 511 522 512 523 test "skip & view" { ··· 519 530 view.writeU32Big(@intCast(w.len() - 4)); 520 531 521 532 try w.writeByte('\n'); 522 - try t.exectSlice(u8, &[_]u8{0, 0, 0, 13, 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', '!', '\n'}, w.string()); 533 + try t.expectSlice(u8, &[_]u8{0, 0, 0, 13, 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', '!', '\n'}, w.string()); 523 534 } 524 535 525 536 test "writeAt" {
+1 -1
src/t.zig
··· 8 8 try std.testing.expectEqual(@as(@TypeOf(actual), expected), actual); 9 9 } 10 10 pub const expectString = std.testing.expectEqualStrings; 11 - pub const exectSlice = std.testing.expectEqualSlices; 11 + pub const expectSlice = std.testing.expectEqualSlices; 12 12 13 13 pub fn getRandom() std.Random.DefaultPrng { 14 14 var seed: u64 = undefined;