mirror of
https://github.com/ziglang/zig.git
synced 2024-11-30 09:02:32 +00:00
mem: explicit Allocator.Error on concat and join
This commit is contained in:
parent
4d29b39678
commit
4a44b79935
@ -2948,18 +2948,18 @@ pub fn SplitBackwardsIterator(comptime T: type, comptime delimiter_type: Delimit
|
||||
|
||||
/// Naively combines a series of slices with a separator.
|
||||
/// Allocates memory for the result, which must be freed by the caller.
|
||||
pub fn join(allocator: Allocator, separator: []const u8, slices: []const []const u8) ![]u8 {
|
||||
pub fn join(allocator: Allocator, separator: []const u8, slices: []const []const u8) Allocator.Error![]u8 {
|
||||
return joinMaybeZ(allocator, separator, slices, false);
|
||||
}
|
||||
|
||||
/// Naively combines a series of slices with a separator and null terminator.
|
||||
/// Allocates memory for the result, which must be freed by the caller.
|
||||
pub fn joinZ(allocator: Allocator, separator: []const u8, slices: []const []const u8) ![:0]u8 {
|
||||
pub fn joinZ(allocator: Allocator, separator: []const u8, slices: []const []const u8) Allocator.Error![:0]u8 {
|
||||
const out = try joinMaybeZ(allocator, separator, slices, true);
|
||||
return out[0 .. out.len - 1 :0];
|
||||
}
|
||||
|
||||
fn joinMaybeZ(allocator: Allocator, separator: []const u8, slices: []const []const u8, zero: bool) ![]u8 {
|
||||
fn joinMaybeZ(allocator: Allocator, separator: []const u8, slices: []const []const u8, zero: bool) Allocator.Error![]u8 {
|
||||
if (slices.len == 0) return if (zero) try allocator.dupe(u8, &[1]u8{0}) else &[0]u8{};
|
||||
|
||||
const total_len = blk: {
|
||||
@ -3038,18 +3038,18 @@ test "joinZ" {
|
||||
}
|
||||
|
||||
/// Copies each T from slices into a new slice that exactly holds all the elements.
|
||||
pub fn concat(allocator: Allocator, comptime T: type, slices: []const []const T) ![]T {
|
||||
pub fn concat(allocator: Allocator, comptime T: type, slices: []const []const T) Allocator.Error![]T {
|
||||
return concatMaybeSentinel(allocator, T, slices, null);
|
||||
}
|
||||
|
||||
/// Copies each T from slices into a new slice that exactly holds all the elements.
|
||||
pub fn concatWithSentinel(allocator: Allocator, comptime T: type, slices: []const []const T, comptime s: T) ![:s]T {
|
||||
pub fn concatWithSentinel(allocator: Allocator, comptime T: type, slices: []const []const T, comptime s: T) Allocator.Error![:s]T {
|
||||
const ret = try concatMaybeSentinel(allocator, T, slices, s);
|
||||
return ret[0 .. ret.len - 1 :s];
|
||||
}
|
||||
|
||||
/// Copies each T from slices into a new slice that exactly holds all the elements as well as the sentinel.
|
||||
pub fn concatMaybeSentinel(allocator: Allocator, comptime T: type, slices: []const []const T, comptime s: ?T) ![]T {
|
||||
pub 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: {
|
||||
|
Loading…
Reference in New Issue
Block a user