authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-01 18:30:09-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-11-01 18:30:09-05:00
logaf60931a488ac84030900d14c2ae7d17e0c84891
tree375d92224fda985b8b882bc6e286088c20090cea
parent445d808bae4b9a2ff1c7a56a4ebff64ae848f46c
parent788900c35c99361e4ab0e2b5dd38cfa0737700d7
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6888 from jcmoyer/issues/6874

Update std.fmt docs and add test for null terminated slices with embedded null bytes

1 files changed, 7 insertions(+), 1 deletions(-)

lib/std/fmt.zig+7-1
......@@ -64,7 +64,9 @@ fn peekIsAlign(comptime fmt: []const u8) bool {
6464/// - `x` and `X`:
6565/// - format the non-numeric value as a string of bytes in hexadecimal notation ("binary dump") in either lower case or upper case
6666/// - output numeric value in hexadecimal notation
67/// - `s`: print a pointer-to-many as a c-string, use zero-termination
67/// - `s`:
68/// - for pointer-to-many and C pointers of u8, print as a C-string using zero-termination
69/// - for slices of u8, print the entire slice as a string without zero-termination
6870/// - `z`: escape the string with @"" syntax if it is not a valid Zig identifier.
6971/// - `Z`: print the string escaping non-printable characters using Zig escape sequences.
7072/// - `B` and `Bi`: output a memory size in either metric (1000) or power-of-two (1024) based notation. works for both float and integer values.
......@@ -1383,6 +1385,10 @@ test "slice" {
13831385 const value = @intToPtr([*]align(1) const []const u8, 0xdeadbeef)[runtime_zero..runtime_zero];
13841386 try testFmt("slice: []const u8@deadbeef\n", "slice: {}\n", .{value});
13851387 }
1388 {
1389 const null_term_slice: [:0]const u8 = "\x00hello\x00";
1390 try testFmt("buf: \x00hello\x00\n", "buf: {s}\n", .{null_term_slice});
1391 }
13861392
13871393 try testFmt("buf: Test\n", "buf: {s:5}\n", .{"Test"});
13881394 try testFmt("buf: Test\n Other text", "buf: {s}\n Other text", .{"Test"});