pub fn concatMaybeSentinel(allocator: Allocator, comptime T: type, slices: []const []const T, comptime s: ?T) Allocator.Error![]T allocator: AllocatorT: typeslices: []const []const Ts: ?T3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653pub fn concatMaybeSentinel(allocator: Allocator, comptime T: type, slices: []const []const T, comptime s: ?T) Allocator.Error![]T {
if (slices.len == 0) return if (s) |sentinel| try allocator.dupe(T, &[1]T{sentinel}) else &[0]T{};
const total_len = blk: {
var sum: usize = 0;
for (slices) |slice| {
sum += slice.len;
}
if (s) |_| {
sum += 1;
}
break :blk sum;
};
const buf = try allocator.alloc(T, total_len);
errdefer allocator.free(buf);
var buf_index: usize = 0;
for (slices) |slice| {
@memcpy(buf[buf_index .. buf_index + slice.len], slice);
buf_index += slice.len;
}
if (s) |sentinel| {
buf[buf.len - 1] = sentinel;
}
// No need for shrink since buf is exactly the correct size.
return buf;
}
Copies each T from slices into a new slice that exactly holds all the elements as well as the sentinel.