2022-03-25 14:31:56 +02:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
const mem = std.mem;
|
|
|
|
const Allocator = mem.Allocator;
|
|
|
|
|
|
|
|
const Group = @This();
|
|
|
|
|
|
|
|
gid: u32,
|
2022-04-03 14:48:53 +03:00
|
|
|
name: [:0]const u8,
|
|
|
|
//
|
|
|
|
members: [:null]const ?[:0]const u8,
|
2022-03-25 14:31:56 +02:00
|
|
|
|
|
|
|
pub fn clone(self: *const Group, allocator: Allocator) Allocator.Error!Group {
|
2022-04-03 14:48:53 +03:00
|
|
|
var members = try allocator.allocSentinel(?[:0]const u8, self.members.len, null);
|
2022-03-26 12:06:07 +02:00
|
|
|
errdefer allocator.free(members);
|
|
|
|
members.len = self.members.len;
|
|
|
|
|
|
|
|
for (self.members) |member, i| {
|
2022-04-03 14:48:53 +03:00
|
|
|
const member_name = member.?;
|
|
|
|
members[i] = try allocator.dupeZ(u8, member_name);
|
2022-03-26 12:06:07 +02:00
|
|
|
}
|
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
return Group{
|
|
|
|
.gid = self.gid,
|
2022-04-03 14:48:53 +03:00
|
|
|
.name = try allocator.dupeZ(u8, self.name),
|
2022-03-26 12:06:07 +02:00
|
|
|
.members = members,
|
2022-03-25 14:31:56 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deinit(self: *Group, allocator: Allocator) void {
|
|
|
|
allocator.free(self.name);
|
2022-04-03 14:48:53 +03:00
|
|
|
for (self.members) |member| {
|
|
|
|
allocator.free(member.?);
|
|
|
|
}
|
2022-03-26 12:06:07 +02:00
|
|
|
allocator.free(self.members);
|
2022-03-25 14:31:56 +02:00
|
|
|
self.* = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const testing = std.testing;
|
|
|
|
|
|
|
|
test "Group.clone" {
|
2022-03-26 12:06:07 +02:00
|
|
|
// TODO: how to do this on stack?
|
2022-04-03 14:48:53 +03:00
|
|
|
var member1 = try testing.allocator.dupeZ(u8, "member1");
|
2022-03-26 12:06:07 +02:00
|
|
|
defer testing.allocator.free(member1);
|
2022-03-25 14:31:56 +02:00
|
|
|
|
2022-03-26 12:06:07 +02:00
|
|
|
var group = Group{
|
|
|
|
.gid = 1,
|
|
|
|
.name = "foo",
|
2022-04-03 14:48:53 +03:00
|
|
|
.members = &[_:null]?[:0]const u8{ member1, "member2", null },
|
2022-03-26 12:06:07 +02:00
|
|
|
};
|
2022-03-25 14:31:56 +02:00
|
|
|
|
2022-03-26 12:06:07 +02:00
|
|
|
var cloned = try group.clone(testing.allocator);
|
|
|
|
defer cloned.deinit(testing.allocator);
|
2022-03-25 14:31:56 +02:00
|
|
|
|
2022-03-26 12:06:07 +02:00
|
|
|
group.gid = 2;
|
|
|
|
group.name = "bar";
|
|
|
|
member1[0] = 'x';
|
|
|
|
try testing.expectEqual(cloned.gid, 1);
|
|
|
|
try testing.expectEqualSlices(u8, cloned.name, "foo");
|
2022-04-03 14:48:53 +03:00
|
|
|
try testing.expectEqualSlices(u8, cloned.members[0].?, "member1");
|
2022-03-25 14:31:56 +02:00
|
|
|
}
|