| ... | @@ -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 | } |
| 271 | | 271 | |
| 272 | /// Copy `source` into `dest`. Asserts no overlap. Asserts `dest.len` greater or equal to source len. Returns | 272 | /// 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. |
| 274 | pub fn copySentinel(comptime T: type, comptime s: T, dest: []T, source: [*:s]const T) usize { | 276 | pub 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 | } |
| 286 | | 288 | |
| | 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. |
| | 293 | pub 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 | |
| | 299 | test 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, when | 306 | /// 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 this | 307 | /// interfacing with a C API where this practice is more common and relied upon. If you are performing code review and see this |