diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-06-19 19:01:28 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-06-19 19:01:28 -0400 |
| commit | 04c25efe112e374facaf1bc8b58bbdb6999a39e3 (patch) | |
| tree | 66a70b185ad8e44a69c8be27dd7dbc23425b040c /std/mem.zig | |
| parent | 4ffab5b85f03f63a7e724698482f8497cacc7212 (diff) | |
| parent | 381c6a38b145665a22440f7aa816f0ddd9b70ee5 (diff) | |
| download | zig-04c25efe112e374facaf1bc8b58bbdb6999a39e3.tar.gz zig-04c25efe112e374facaf1bc8b58bbdb6999a39e3.zip | |
Merge remote-tracking branch 'origin/master' into copy-elision-3
Diffstat (limited to 'std/mem.zig')
| -rw-r--r-- | std/mem.zig | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/std/mem.zig b/std/mem.zig index 49ea48a890..7ecd483020 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -996,6 +996,43 @@ test "mem.join" { testing.expect(eql(u8, try join(a, ",", [_][]const u8{ "a", "", "b", "", "c" }), "a,,b,,c")); } +/// 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 { + if (slices.len == 0) return (([*]T)(undefined))[0..0]; + + const total_len = blk: { + var sum: usize = 0; + for (slices) |slice| { + sum += slice.len; + } + break :blk sum; + }; + + const buf = try allocator.alloc(T, total_len); + errdefer allocator.free(buf); + + var buf_index: usize = 0; + for (slices) |slice| { + copy(T, buf[buf_index..], slice); + buf_index += slice.len; + } + + // No need for shrink since buf is exactly the correct size. + return buf; +} + +test "concat" { + var buf: [1024]u8 = undefined; + const a = &std.heap.FixedBufferAllocator.init(&buf).allocator; + testing.expect(eql(u8, try concat(a, u8, [_][]const u8{ "abc", "def", "ghi" }), "abcdefghi")); + testing.expect(eql(u32, try concat(a, u32, [_][]const u32{ + [_]u32{ 0, 1 }, + [_]u32{ 2, 3, 4 }, + [_]u32{}, + [_]u32{5}, + }), [_]u32{ 0, 1, 2, 3, 4, 5 })); +} + test "testStringEquality" { testing.expect(eql(u8, "abcd", "abcd")); testing.expect(!eql(u8, "abcdef", "abZdef")); |
