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...@@ -2948,18 +2948,18 @@ pub fn SplitBackwardsIterator(comptime T: type, comptime delimiter_type: Delimit
29482948
2949/// Naively combines a series of slices with a separator.2949/// Naively combines a series of slices with a separator.
2950/// Allocates memory for the result, which must be freed by the caller.2950/// 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 {
2952 return joinMaybeZ(allocator, separator, slices, false);2952 return joinMaybeZ(allocator, separator, slices, false);
2953}2953}
29542954
2955/// Naively combines a series of slices with a separator and null terminator.2955/// Naively combines a series of slices with a separator and null terminator.
2956/// Allocates memory for the result, which must be freed by the caller.2956/// 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 {
2958 const out = try joinMaybeZ(allocator, separator, slices, true);2958 const out = try joinMaybeZ(allocator, separator, slices, true);
2959 return out[0 .. out.len - 1 :0];2959 return out[0 .. out.len - 1 :0];
2960}2960}
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 {
2963 if (slices.len == 0) return if (zero) try allocator.dupe(u8, &[1]u8{0}) else &[0]u8{};2963 if (slices.len == 0) return if (zero) try allocator.dupe(u8, &[1]u8{0}) else &[0]u8{};
29642964
2965 const total_len = blk: {2965 const total_len = blk: {
...@@ -3038,18 +3038,18 @@ test "joinZ" {...@@ -3038,18 +3038,18 @@ test "joinZ" {
3038}3038}
30393039
3040/// Copies each T from slices into a new slice that exactly holds all the elements.3040/// 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 {
3042 return concatMaybeSentinel(allocator, T, slices, null);3042 return concatMaybeSentinel(allocator, T, slices, null);
3043}3043}
30443044
3045/// Copies each T from slices into a new slice that exactly holds all the elements.3045/// 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 {
3047 const ret = try concatMaybeSentinel(allocator, T, slices, s);3047 const ret = try concatMaybeSentinel(allocator, T, slices, s);
3048 return ret[0 .. ret.len - 1 :s];3048 return ret[0 .. ret.len - 1 :s];
3049}3049}
30503050
3051/// Copies each T from slices into a new slice that exactly holds all the elements as well as the sentinel.3051/// 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 {
3053 if (slices.len == 0) return if (s) |sentinel| try allocator.dupe(T, &[1]T{sentinel}) else &[0]T{};3053 if (slices.len == 0) return if (s) |sentinel| try allocator.dupe(T, &[1]T{sentinel}) else &[0]T{};
30543054
3055 const total_len = blk: {3055 const total_len = blk: {