authorgravatar for nathan@nmichaels.orgNathan Michaels <nathan@nmichaels.org> 2020-01-20 12:23:43-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-20 12:23:43-05:00
log0000de4fee601047f662dc33fe890eb8831d9787
tree74912dd3934522f802037fd94cfa3ca208d9e08c
parent7a1cde7206263c8bb3265c225ed4213d1b7bdb58

Handle {s} format for C strings. (#4219)

* Handle {s} format for C strings. * Fix "cstr" test to actually use c strings.

1 files changed, 11 insertions(+), 6 deletions(-)

lib/std/fmt.zig+11-6
......@@ -431,7 +431,7 @@ pub fn formatType(
431431 },
432432 else => return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }),
433433 },
434 .Many => {
434 .Many, .C => {
435435 if (ptr_info.child == u8) {
436436 if (fmt.len > 0 and fmt[0] == 's') {
437437 const len = mem.len(u8, value);
......@@ -449,9 +449,6 @@ pub fn formatType(
449449 }
450450 return format(context, Errors, output, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value.ptr) });
451451 },
452 .C => {
453 return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });
454 },
455452 },
456453 .Array => |info| {
457454 const Slice = @Type(builtin.TypeInfo{
......@@ -1285,8 +1282,16 @@ test "pointer" {
12851282}
12861283
12871284test "cstr" {
1288 try testFmt("cstr: Test C\n", "cstr: {s}\n", .{"Test C"});
1289 try testFmt("cstr: Test C \n", "cstr: {s:10}\n", .{"Test C"});
1285 try testFmt(
1286 "cstr: Test C\n",
1287 "cstr: {s}\n",
1288 .{@ptrCast([*c]const u8, "Test C")},
1289 );
1290 try testFmt(
1291 "cstr: Test C \n",
1292 "cstr: {s:10}\n",
1293 .{@ptrCast([*c]const u8, "Test C")},
1294 );
12901295}
12911296
12921297test "filesize" {