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(...@@ -431,7 +431,7 @@ pub fn formatType(
431 },431 },
432 else => return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }),432 else => return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }),
433 },433 },
434 .Many => {434 .Many, .C => {
435 if (ptr_info.child == u8) {435 if (ptr_info.child == u8) {
436 if (fmt.len > 0 and fmt[0] == 's') {436 if (fmt.len > 0 and fmt[0] == 's') {
437 const len = mem.len(u8, value);437 const len = mem.len(u8, value);
...@@ -449,9 +449,6 @@ pub fn formatType(...@@ -449,9 +449,6 @@ pub fn formatType(
449 }449 }
450 return format(context, Errors, output, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value.ptr) });450 return format(context, Errors, output, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value.ptr) });
451 },451 },
452 .C => {
453 return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });
454 },
455 },452 },
456 .Array => |info| {453 .Array => |info| {
457 const Slice = @Type(builtin.TypeInfo{454 const Slice = @Type(builtin.TypeInfo{
...@@ -1285,8 +1282,16 @@ test "pointer" {...@@ -1285,8 +1282,16 @@ test "pointer" {
1285}1282}
12861283
1287test "cstr" {1284test "cstr" {
1288 try testFmt("cstr: Test C\n", "cstr: {s}\n", .{"Test C"});1285 try testFmt(
1289 try testFmt("cstr: Test C \n", "cstr: {s:10}\n", .{"Test C"});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 );
1290}1295}
12911296
1292test "filesize" {1297test "filesize" {