authorgravatar for pascal@quies.netPascal S. de Kloe <pascal@quies.net> 2023-09-12 23:24:26+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-09-13 13:52:05+03:00
log4a44b7993573016fb5ccb3398d366a884307bf3a
tree820d3c309866660cd4af647cd3eb0d12e1ebf3db
parent4d29b3967873d6ff7e7426b2ab99c00c9e0fa284

mem: explicit Allocator.Error on concat and join


1 files changed, 6 insertions(+), 6 deletions(-)

lib/std/mem.zig+6-6
......@@ -2948,18 +2948,18 @@ pub fn SplitBackwardsIterator(comptime T: type, comptime delimiter_type: Delimit
29482948
29492949/// Naively combines a series of slices with a separator.
29502950/// Allocates memory for the result, which must be freed by the caller.
2951pub fn join(allocator: Allocator, separator: []const u8, slices: []const []const u8) ![]u8 {
2951pub fn join(allocator: Allocator, separator: []const u8, slices: []const []const u8) Allocator.Error![]u8 {
29522952 return joinMaybeZ(allocator, separator, slices, false);
29532953}
29542954
29552955/// Naively combines a series of slices with a separator and null terminator.
29562956/// Allocates memory for the result, which must be freed by the caller.
2957pub fn joinZ(allocator: Allocator, separator: []const u8, slices: []const []const u8) ![:0]u8 {
2957pub fn joinZ(allocator: Allocator, separator: []const u8, slices: []const []const u8) Allocator.Error![:0]u8 {
29582958 const out = try joinMaybeZ(allocator, separator, slices, true);
29592959 return out[0 .. out.len - 1 :0];
29602960}
29612961
2962fn joinMaybeZ(allocator: Allocator, separator: []const u8, slices: []const []const u8, zero: bool) ![]u8 {
2962fn joinMaybeZ(allocator: Allocator, separator: []const u8, slices: []const []const u8, zero: bool) Allocator.Error![]u8 {
29632963 if (slices.len == 0) return if (zero) try allocator.dupe(u8, &[1]u8{0}) else &[0]u8{};
29642964
29652965 const total_len = blk: {
......@@ -3038,18 +3038,18 @@ test "joinZ" {
30383038}
30393039
30403040/// Copies each T from slices into a new slice that exactly holds all the elements.
3041pub fn concat(allocator: Allocator, comptime T: type, slices: []const []const T) ![]T {
3041pub fn concat(allocator: Allocator, comptime T: type, slices: []const []const T) Allocator.Error![]T {
30423042 return concatMaybeSentinel(allocator, T, slices, null);
30433043}
30443044
30453045/// Copies each T from slices into a new slice that exactly holds all the elements.
3046pub fn concatWithSentinel(allocator: Allocator, comptime T: type, slices: []const []const T, comptime s: T) ![:s]T {
3046pub fn concatWithSentinel(allocator: Allocator, comptime T: type, slices: []const []const T, comptime s: T) Allocator.Error![:s]T {
30473047 const ret = try concatMaybeSentinel(allocator, T, slices, s);
30483048 return ret[0 .. ret.len - 1 :s];
30493049}
30503050
30513051/// Copies each T from slices into a new slice that exactly holds all the elements as well as the sentinel.
3052pub fn concatMaybeSentinel(allocator: Allocator, comptime T: type, slices: []const []const T, comptime s: ?T) ![]T {
3052pub fn concatMaybeSentinel(allocator: Allocator, comptime T: type, slices: []const []const T, comptime s: ?T) Allocator.Error![]T {
30533053 if (slices.len == 0) return if (s) |sentinel| try allocator.dupe(T, &[1]T{sentinel}) else &[0]T{};
30543054
30553055 const total_len = blk: {