authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-09-07 16:43:44-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-09-07 16:44:06-07:00
log06d068bf7b84b8fd4e28edf3daffb3fca9079000
treebcb83a76d9255840282055843396a3285761b68a
parent41905730c036dd69e2d5d4ed8d87320c609b4531

std.mem: introduce copySentinelInclusive


1 files changed, 20 insertions(+), 2 deletions(-)

lib/std/mem.zig+20-2
...@@ -269,8 +269,10 @@ pub fn copyBackwards(comptime T: type, dest: []T, source: []const T) void {...@@ -269,8 +269,10 @@ pub fn copyBackwards(comptime T: type, dest: []T, source: []const T) void {
269 }269 }
270}270}
271271
272/// Copy `source` into `dest`. Asserts no overlap. Asserts `dest.len` greater or equal to source len. Returns272/// Copy `source` into `dest`, excluding the sentinel.
273/// number of elements copied. Sentinel is not copied.273///
274/// Asserts no overlap. Asserts `dest.len` greater or equal to source len. Returns number of elements copied,
275/// not counting the sentinel.
274pub fn copySentinel(comptime T: type, comptime s: T, dest: []T, source: [*:s]const T) usize {276pub fn copySentinel(comptime T: type, comptime s: T, dest: []T, source: [*:s]const T) usize {
275 const i = findSentinel(T, s, source);277 const i = findSentinel(T, s, source);
276 @memcpy(dest[0..i], source[0..i]);278 @memcpy(dest[0..i], source[0..i]);
...@@ -284,6 +286,22 @@ test copySentinel {...@@ -284,6 +286,22 @@ test copySentinel {
284 try testing.expectEqual(n, 10);286 try testing.expectEqual(n, 10);
285}287}
286288
289/// Copy `source` into `dest`, including the sentinel.
290///
291/// Asserts no overlap. Asserts `dest.len` greater or equal to source len, including room for the sentinel.
292/// Returns number of elements copied, including the sentinel.
293pub fn copySentinelInclusive(comptime T: type, comptime s: T, dest: []T, source: [*:s]const T) usize {
294 const i = findSentinel(T, s, source) + 1;
295 @memcpy(dest[0..i], source[0..i]);
296 return i;
297}
298
299test copySentinelInclusive {
300 var dst: [12]u8 = @splat(0xff);
301 const n = copySentinelInclusive(u8, 0, &dst, "hello this\x00is my null-terminated string");
302 try testing.expectEqualStrings("hello this\x00\xff", &dst);
303 try testing.expectEqual(n, 11);
304}
287/// Generally, Zig users are encouraged to explicitly initialize all fields of a struct explicitly rather than using this function.305/// Generally, Zig users are encouraged to explicitly initialize all fields of a struct explicitly rather than using this function.
288/// However, it is recognized that there are sometimes use cases for initializing all fields to a "zero" value. For example, when306/// However, it is recognized that there are sometimes use cases for initializing all fields to a "zero" value. For example, when
289/// interfacing with a C API where this practice is more common and relied upon. If you are performing code review and see this307/// interfacing with a C API where this practice is more common and relied upon. If you are performing code review and see this