···11-# StringBuilder For Zig
11+# String Builder / Buffer For Zig
2233Behaves a lot like a `std.ArrayList(u8)` but with a cleaner interface and pooling capabilities.
4455```zig
66-const StringBuilder = @import("string_builder").StringBuilder;
66+const Buffer = @import("buffer").Buffer;
7788// Starts off with a static buffer of 100 bytes
99// If you go over this, memory will be dynamically allocated
···1111// those 100 bytes (aka the static portion of the buffer) are
1212// re-used when pooling is used
13131414-var sb = try new StringBuilder(allocator, 100);
1414+var buf = try new Buffer(allocator, 100);
15151616-try sb.writeByte('o');
1717-try sb.write("ver 9000!1");
1818-sb.truncate(1);
1616+try buf.writeByte('o');
1717+try buf.write("ver 9000!1");
1818+buf.truncate(1);
19192020-sb.len(); // 10
2121-sb.string(); // "over 9000!"
2020+buf.len(); // 10
2121+buf.string(); // "over 9000!"
2222```
23232424-You can call `sb.writer()` to get an `std.io.Writer`.
2424+You can call `buf.writer()` to get an `std.io.Writer`.
25252626## Pooling
2727···2929const Pool = @import("string_builder").Pool;
303031313232-// Creates pool of 100 StringBuilders, each configured with a static buffer
3232+// Creates pool of 100 Buffers, each configured with a static buffer
3333// of 10000 bytes
3434var pool = try Pool.init(allocator, 100, 10000);
3535-var sb = try pool.acquire();
3636-defer pool.release(sb);
3535+var buf = try pool.acquire();
3636+defer pool.release(buf);
3737```
38383939-The `Pool` is thread-safe. The `StringBuilder` is not thread safe.
3939+The `Pool` is thread-safe. The `Buffer` is not thread safe.
40404141For a more advanced use case, `pool.acquireWithAllocator(std.mem.Allocator)` can be used. This has a specific purpose: to allocate the static buffer upfront using [probably] a general purpose allocator, but for any dynamic allocation to happen with a [probably] arena allocator. This is meant for the case where an arena allocator is available which outlives the checked out buffer. In such cases, using the arena allocator for any potential dynamic allocation by the buffer will offer better performance.