test "test_id_list" {
const a = std.testing.allocator;
var idList = IdList.init(a);
defer idList.deinit();
_ = try idList.addId("pippo");
try std.testing.expectEqual(idList.ids.items.len, 1);
}
const IdList = struct {
allocator: std.mem.Allocator,
ids: std.ArrayListUnmanaged([:0]const u8) = .{},
pub fn init(allocator: std.mem.Allocator) IdList {
return .{ .allocator = allocator, }; }
pub fn deinit(self: *IdList) void {
for (self.ids.items) |el| self.allocator.free(el);
return self.ids.deinit(self.allocator); }
pub fn addId(self: *IdList, value: []const u8) !usize {
const newId = self.allocator.allocSentinel(
u8, value.len, 0) catch |err| { return err; };
errdefer self.allocator.free(newId);
for (newId, value) |*target, source|
target.* = if (source == ' ') return error.InvalidId
else source;
try self.ids.append(self.allocator, newId);
return self.ids.items.len - 1;
} };
const std = @import("std");