···5050 }
5151 }
52525353- pub fn reset(self: *StringBuilder) void {
5353+ pub fn reset(self: *StringBuilder, clear_dynamic: bool) void {
5454 self.pos = 0;
5555- if (self.dynamic) |dyn| {
5656- (self._da orelse self._a).free(dyn);
5757- self.dynamic = null;
5858- self.buf = self.static;
5555+ if (clear_dynamic) {
5656+ if (self.dynamic) |dyn| {
5757+ (self._da orelse self._a).free(dyn);
5858+ self.dynamic = null;
5959+ self.buf = self.static;
6060+ }
6161+ self._da = null;
5962 }
6060- self._da = null;
6163 }
62646365 pub fn len(self: StringBuilder) usize {
···201203 try t.expectEqual(492, sb.len());
202204 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());
203205204204- sb.reset();
206206+ sb.reset(true);
205207 }
206208}
207209···227229 try t.expectString("hello w", sb.string());
228230}
229231232232+test "reset without clear" {
233233+ var sb = try StringBuilder.init(t.allocator, 5);
234234+ defer sb.deinit();
235235+236236+237237+238238+ try sb.write("hello world!1");
239239+ try t.expectString("hello world!1", sb.string());
240240+241241+ sb.reset(false);
242242+ try t.expectEqual(0, sb.len());
243243+ try t.expectEqual(false, sb.dynamic == null);
244244+ try sb.write("over 9000");
245245+ try sb.write("over 9000");
246246+}
247247+230248test "fuzz" {
231249 var control = std.ArrayList(u8).init(t.allocator);
232250 defer control.deinit();
···249267 try control.appendSlice(input);
250268 try t.expectString(control.items, sb.string());
251269 }
252252- sb.reset();
270270+ sb.reset(true);
253271 control.clearRetainingCapacity();
254272 _ = arena.reset(.free_all);
255273 }