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 {...@@ -64,7 +64,9 @@ fn peekIsAlign(comptime fmt: []const u8) bool {
64/// - `x` and `X`:64/// - `x` and `X`:
65/// - format the non-numeric value as a string of bytes in hexadecimal notation ("binary dump") in either lower case or upper case65/// - format the non-numeric value as a string of bytes in hexadecimal notation ("binary dump") in either lower case or upper case
66/// - output numeric value in hexadecimal notation66/// - output numeric value in hexadecimal notation
67/// - `s`: print a pointer-to-many as a c-string, use zero-termination67/// - `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
68/// - `z`: escape the string with @"" syntax if it is not a valid Zig identifier.70/// - `z`: escape the string with @"" syntax if it is not a valid Zig identifier.
69/// - `Z`: print the string escaping non-printable characters using Zig escape sequences.71/// - `Z`: print the string escaping non-printable characters using Zig escape sequences.
70/// - `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.72/// - `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" {...@@ -1383,6 +1385,10 @@ test "slice" {
1383 const value = @intToPtr([*]align(1) const []const u8, 0xdeadbeef)[runtime_zero..runtime_zero];1385 const value = @intToPtr([*]align(1) const []const u8, 0xdeadbeef)[runtime_zero..runtime_zero];
1384 try testFmt("slice: []const u8@deadbeef\n", "slice: {}\n", .{value});1386 try testFmt("slice: []const u8@deadbeef\n", "slice: {}\n", .{value});
1385 }1387 }
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
1387 try testFmt("buf: Test\n", "buf: {s:5}\n", .{"Test"});1393 try testFmt("buf: Test\n", "buf: {s:5}\n", .{"Test"});
1388 try testFmt("buf: Test\n Other text", "buf: {s}\n Other text", .{"Test"});1394 try testFmt("buf: Test\n Other text", "buf: {s}\n Other text", .{"Test"});