String Builder
Efficient string building with dynamic allocation
Description
A string builder for efficient string construction in Zig. Uses dynamic allocation to build strings incrementally without repeated allocations.
Features
- Dynamic growth
- Efficient appending
- Memory management
- Format support
Code
const std = @import("std");pub const StringBuilder = struct { list: std.ArrayList(u8), pub fn init(allocator: std.mem.Allocator) StringBuilder { return StringBuilder{ .list = std.ArrayList(u8).init(allocator), }; } pub fn initWithCapacity(allocator: std.mem.Allocator, capacity: usize) StringBuilder { var sb = StringBuilder{ .list = std.ArrayList(u8).init(allocator), }; sb.list.ensureTotalCapacity(capacity) catch {}; return sb; } pub fn append(self: *StringBuilder, str: []const u8) !void { try self.list.appendSlice(str); } pub fn appendChar(self: *StringBuilder, c: u8) !void { try self.list.append(c); } pub fn appendFormat(self: *StringBuilder, comptime fmt: []const u8, args: anytype) !void { try std.fmt.format(self.list.writer(), fmt, args); } pub fn appendLine(self: *StringBuilder, str: []const u8) !void { try self.append(str); try self.appendChar('\n'); } pub fn build(self: *StringBuilder) ![]u8 { return try self.list.toOwnedSlice(); } pub fn deinit(self: *StringBuilder) void { self.list.deinit(); } pub fn len(self: *const StringBuilder) usize { return self.list.items.len; } pub fn clear(self: *StringBuilder) void { self.list.clearAndFree(); }};
const std = @import("std");const StringBuilder = @import("string_builder.zig").StringBuilder;pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const allocator = gpa.allocator(); var sb = StringBuilder.initWithCapacity(allocator, 100); defer sb.deinit(); try sb.append("Hello"); try sb.append(" "); try sb.append("World"); try sb.appendChar('!'); try sb.appendLine("This is a line"); const result = try sb.build(); defer allocator.free(result); std.debug.print("{s}\n", .{result});}
Comments
No comments yet. Be the first to comment!
Please login to leave a comment.