···129129 // created when we try to write more than static.len
130130 dynamic: ?[]u8,
131131132132-133132 pub fn init(allocator: Allocator, size: usize) !Buffer {
134133 const static = try allocator.alloc(u8, size);
135134 return .{
···152151 }
153152 }
154153155155- pub fn reset(self: *Buffer, clear_dynamic: bool) void {
154154+ pub fn reset(self: *Buffer) void {
156155 self._view.pos = 0;
157157- if (clear_dynamic) {
158158- if (self.dynamic) |dyn| {
159159- (self._da orelse self._a).free(dyn);
160160- self.dynamic = null;
161161- self._view.buf = self.static;
162162- }
163163- self._da = null;
156156+ if (self.dynamic) |dyn| {
157157+ (self._da orelse self._a).free(dyn);
158158+ self.dynamic = null;
159159+ self._view.buf = self.static;
164160 }
161161+ self._da = null;
162162+ }
163163+164164+ pub fn resetRetainingCapacity(self: *Buffer) void {
165165+ self._view.pos = 0;
165166 }
166167167168 pub fn len(self: Buffer) usize {
···337338 try t.expectEqual(492, sb.len());
338339 try t.expectString("over 9000!!!!If you were to run this code, you'd almost certainly see a segmentation fault (aka, segfault). We create a Response which involves creating an ArenaAllocator and from that, an Allocator. This allocator is then used to format our string. For the purpose of this example, we create a 2nd response and immediately free it. We need this for the same reason that warning1 in our first example printed an almost ok value: we want to re-initialize the memory in our init function stack.", sb.string());
339340340340- sb.reset(true);
341341+ sb.reset();
341342 }
342343}
343344···366367test "reset without clear" {
367368 var sb = try Buffer.init(t.allocator, 5);
368369 defer sb.deinit();
369369-370370-371370372371 try sb.write("hello world!1");
373372 try t.expectString("hello world!1", sb.string());
374373375375- sb.reset(false);
374374+ sb.resetRetainingCapacity();
376375 try t.expectEqual(0, sb.len());
377376 try t.expectEqual(false, sb.dynamic == null);
378377 try sb.write("over 9000");
···401400 try control.appendSlice(input);
402401 try t.expectString(control.items, sb.string());
403402 }
404404- sb.reset(true);
403403+ sb.reset();
405404 control.clearRetainingCapacity();
406405 _ = arena.reset(.free_all);
407406 }