aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorMichael Byrne <michael@michaelbyrne.io>2021-12-04 12:37:48 +1100
committerGitHub <noreply@github.com>2021-12-03 20:37:48 -0500
commit7e2fae10c9ce320d7a407856cbb935f9a95c5443 (patch)
tree70801d3a5436eea5c13390556d60c2a7f510b27d /doc
parent2a0adef583d96d87fdca1bc536269931486e4349 (diff)
downloadzig-7e2fae10c9ce320d7a407856cbb935f9a95c5443.tar.gz
zig-7e2fae10c9ce320d7a407856cbb935f9a95c5443.zip
Add documentation for sentinel-terminated slicing (#10010)
closes #9680
Diffstat (limited to 'doc')
-rw-r--r--doc/langref.html.in39
1 files changed, 39 insertions, 0 deletions
diff --git a/doc/langref.html.in b/doc/langref.html.in
index db68f8eb08..29e9e0cc1d 100644
--- a/doc/langref.html.in
+++ b/doc/langref.html.in
@@ -2916,6 +2916,45 @@ test "null terminated slice" {
try expect(slice[5] == 0);
}
{#code_end#}
+ <p>
+ Sentinel-terminated slices can also be created using a variation of the slice syntax
+ {#syntax#}data[start..end :x]{#endsyntax#}, where {#syntax#}data{#endsyntax#} is a many-item pointer,
+ array or slice and {#syntax#}x{#endsyntax#} is the sentinel value.
+ </p>
+ {#code_begin|test|null_terminated_slicing#}
+const std = @import("std");
+const expect = std.testing.expect;
+
+test "null terminated slicing" {
+ var array = [_]u8{ 3, 2, 1, 0, 3, 2, 1, 0 };
+ var runtime_length: usize = 3;
+ const slice = array[0..runtime_length :0];
+
+ try expect(@TypeOf(slice) == [:0]u8);
+ try expect(slice.len == 3);
+}
+ {#code_end#}
+ <p>
+ Sentinel-terminated slicing asserts that the element in the sentinel position of the backing data is
+ actually the sentinel value. If this is not the case, safety-protected {#link|Undefined Behavior#} results.
+ </p>
+ {#code_begin|test_safety|sentinel mismatch#}
+const std = @import("std");
+const expect = std.testing.expect;
+
+test "sentinel mismatch" {
+ var array = [_]u8{ 3, 2, 1, 0 };
+
+ // Creating a sentinel-terminated slice from the array with a length of 2
+ // will result in the value `1` occupying the sentinel element position.
+ // This does not match the indicated sentinel value of `0` and will lead
+ // to a runtime panic.
+ var runtime_length: usize = 2;
+ const slice = array[0..runtime_length :0];
+
+ _ = slice;
+}
+ {#code_end#}
{#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Arrays#}
{#header_close#}
{#header_close#}