From 88d88af111968b2472dc0ac4b0fc53eb1b928a86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Sat, 26 Mar 2022 11:12:04 +0200 Subject: [PATCH] wip cloneArray may not turn out to be useful though --- lib/Group.zig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/Group.zig b/lib/Group.zig index 92ca99b..d2c115f 100644 --- a/lib/Group.zig +++ b/lib/Group.zig @@ -37,6 +37,25 @@ pub fn someMembers( return bufset; } +// cloneArray clones an array of strings. This may be needed +// to change members to []const []const u8 +fn cloneArray(allocator: Allocator, arr: []const []const u8) error{OutOfMemory}![]const []const u8 { + const total_len = blk: { + var sum: usize = 0; + for (arr) |elem| sum += elem.len; + break :blk sum; + }; + var buf = try allocator.alloc(u8, total_len); + var ret = try allocator.alloc([]const u8, arr.len); + ret.len = arr.len; + for (arr) |elem, i| { + const old_len = buf.len; + mem.copy(u8, buf[old_len..], elem); + ret[i] = buf[old_len .. old_len + elem.len]; + } + return ret; +} + const testing = std.testing; test "Group.clone" {