authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-23 22:29:12-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-23 22:29:12-05:00
log2dd20aa04a35f46a189d0aaa5d2d628e46a77999
tree4202a06447b391332e0b4df56b55662728eb7844
parent00878a15d703a668970c7869276d33b21464714a
signaturelock-open Commit is signed but in an unrecognized format.

langref: update for sentinel-terminated types


1 files changed, 19 insertions(+), 18 deletions(-)

doc/langref.html.in+19-18
......@@ -549,7 +549,8 @@ pub fn main() void {
549549 String literals are single-item constant {#link|Pointers#} to null-terminated UTF-8 encoded byte arrays.
550550 The type of string literals encodes both the length, and the fact that they are null-terminated,
551551 and thus they can be {#link|coerced|Type Coercion#} to both {#link|Slices#} and
552 {#link|Null-Terminated Pointers#}. Dereferencing string literals converts them to {#link|Arrays#}.
552 {#link|Null-Terminated Pointers|Sentinel-Terminated Pointers#}.
553 Dereferencing string literals converts them to {#link|Arrays#}.
553554 </p>
554555 <p>
555556 Character literals have type {#syntax#}comptime_int{#endsyntax#}, the same as
......@@ -1780,9 +1781,9 @@ test "multidimensional arrays" {
17801781 {#code_end#}
17811782 {#header_close#}
17821783
1783 {#header_open|Null-Terminated Arrays#}
1784 {#header_open|Sentinel-Terminated Arrays#}
17841785 <p>
1785 The syntax {#syntax#}[N]null T{#endsyntax#} describes an array which has a null element at the
1786 The syntax {#syntax#}[N:x]T{#endsyntax#} describes an array which has a sentinel element at the
17861787 index corresponding to {#syntax#}len{#endsyntax#}.
17871788 </p>
17881789 {#code_begin|test|null_terminated_array#}
......@@ -1790,14 +1791,14 @@ const std = @import("std");
17901791const assert = std.debug.assert;
17911792
17921793test "null terminated array" {
1793 const array = [_]u8 null {1, 2, 3, 4};
1794 const array = [_:0]u8 {1, 2, 3, 4};
17941795
1795 assert(@typeOf(array) == [4]null u8);
1796 assert(@typeOf(array) == [4:0]u8);
17961797 assert(array.len == 4);
17971798 assert(slice[4] == 0);
17981799}
17991800 {#code_end#}
1800 {#see_also|Null-Terminated Pointers|Null-Terminated Slices#}
1801 {#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Slices#}
18011802 {#header_close#}
18021803 {#header_close#}
18031804
......@@ -1899,7 +1900,7 @@ test "pointer array access" {
18991900}
19001901 {#code_end#}
19011902 <p>
1902 In Zig, we prefer slices over pointers to null-terminated arrays.
1903 In Zig, we generally prefer {#link|Slices#} rather than {#link|Sentinel-Terminated Pointers#}.
19031904 You can turn an array or pointer into a slice using slice syntax.
19041905 </p>
19051906 <p>
......@@ -2112,17 +2113,17 @@ test "allowzero" {
21122113 {#code_end#}
21132114 {#header_close#}
21142115
2115 {#header_open|Null-Terminated Pointers#}
2116 {#header_open|Sentinel-Terminated Pointers#}
21162117 <p>
2117 The syntax {#syntax#}[*]null T{#endsyntax#} describes a pointer that
2118 has a length determined by a sentinel null value. This provides protection
2118 The syntax {#syntax#}[*:x]T{#endsyntax#} describes a pointer that
2119 has a length determined by a sentinel value. This provides protection
21192120 against buffer overflow and overreads.
21202121 </p>
21212122 {#code_begin|exe_build_err#}
21222123const std = @import("std");
21232124
21242125// This is also available as `std.c.printf`.
2125pub extern "c" fn printf(format: [*]null const u8, ...) c_int;
2126pub extern "c" fn printf(format: [*:0]const u8, ...) c_int;
21262127
21272128pub fn main() anyerror!void {
21282129 _ = printf("Hello, world!\n"); // OK
......@@ -2132,7 +2133,7 @@ pub fn main() anyerror!void {
21322133 _ = printf(&non_null_terminated_msg);
21332134}
21342135 {#code_end#}
2135 {#see_also|Null-Terminated Slices|Null-Terminated Arrays#}
2136 {#see_also|Sentinel-Terminated Slices|Sentinel-Terminated Arrays#}
21362137 {#header_close#}
21372138 {#header_close#}
21382139
......@@ -2218,11 +2219,11 @@ test "slice widening" {
22182219 {#code_end#}
22192220 {#see_also|Pointers|for|Arrays#}
22202221
2221 {#header_open|Null-Terminated Slices#}
2222 {#header_open|Sentinel-Terminated Slices#}
22222223 <p>
2223 The syntax {#syntax#}[]null T{#endsyntax#} is a slice which has a runtime known length
2224 and also guarantees a null value at the element indexed by the length. The type does not
2225 guarantee that there are no null elements before that. Null-terminated slices allow element
2224 The syntax {#syntax#}[:x]T{#endsyntax#} is a slice which has a runtime known length
2225 and also guarantees a sentinel value at the element indexed by the length. The type does not
2226 guarantee that there are no sentinel elements before that. Sentinel-terminated slices allow element
22262227 access to the {#syntax#}len{#endsyntax#} index.
22272228 </p>
22282229 {#code_begin|test|null_terminated_slice#}
......@@ -2230,13 +2231,13 @@ const std = @import("std");
22302231const assert = std.debug.assert;
22312232
22322233test "null terminated slice" {
2233 const slice: []null const u8 = "hello";
2234 const slice: [:0]const u8 = "hello";
22342235
22352236 assert(slice.len == 5);
22362237 assert(slice[5] == 0);
22372238}
22382239 {#code_end#}
2239 {#see_also|Null-Terminated Pointers|Null-Terminated Arrays#}
2240 {#see_also|Sentinel-Terminated Pointers|Sentinel-Terminated Arrays#}
22402241 {#header_close#}
22412242 {#header_close#}
22422243