authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-08-24 20:06:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-09-04 17:41:27-07:00
log91c5eaeab9e2c5d71bda4614c8748d6ebf715835
treef6aa8dc3dfc48a814c494471bc0d5585bc385929
parentfe409bad656a866ea17de13595a11fbbe2eea225

std.mem: add copySentinel and lessThanZ functions


1 files changed, 26 insertions(+), 0 deletions(-)

lib/std/mem.zig+26
...@@ -269,6 +269,18 @@ pub fn copyBackwards(comptime T: type, dest: []T, source: []const T) void {...@@ -269,6 +269,18 @@ 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
273/// or equal to source len. Returns number of elements copied.
274pub fn copySentinel(comptime T: type, comptime s: T, dest: []T, source: [*:s]const T) usize {
275 const i = findSentinel(T, s, source);
276 @memcpy(dest[0..i], source[0..i]);
277 return i;
278}
279
280test copySentinel {
281 @panic("TODO");
282}
283
272/// Generally, Zig users are encouraged to explicitly initialize all fields of a struct explicitly rather than using this function.284/// Generally, Zig users are encouraged to explicitly initialize all fields of a struct explicitly rather than using this function.
273/// However, it is recognized that there are sometimes use cases for initializing all fields to a "zero" value. For example, when285/// However, it is recognized that there are sometimes use cases for initializing all fields to a "zero" value. For example, when
274/// interfacing with a C API where this practice is more common and relied upon. If you are performing code review and see this286/// interfacing with a C API where this practice is more common and relied upon. If you are performing code review and see this
...@@ -733,6 +745,20 @@ test lessThan {...@@ -733,6 +745,20 @@ test lessThan {
733 try testing.expect(lessThan(u8, "", "a"));745 try testing.expect(lessThan(u8, "", "a"));
734}746}
735747
748/// Returns `true` if `lhs < rhs`; `false` otherwise, operating on
749/// null-terminated arrays.
750pub fn lessThanZ(comptime T: type, lhs: [*:0]const T, rhs: [*:0]const T) bool {
751 return orderZ(T, lhs, rhs) == .lt;
752}
753
754test lessThanZ {
755 try testing.expect(lessThanZ(u8, "abcd", "bee"));
756 try testing.expect(!lessThanZ(u8, "abc", "abc"));
757 try testing.expect(lessThanZ(u8, "abc", "abc0"));
758 try testing.expect(!lessThanZ(u8, "", ""));
759 try testing.expect(lessThanZ(u8, "", "a"));
760}
761
736const use_vectors = switch (builtin.zig_backend) {762const use_vectors = switch (builtin.zig_backend) {
737 // These backends don't support vectors yet.763 // These backends don't support vectors yet.
738 .stage2_aarch64,764 .stage2_aarch64,