Skip to content

Commit 0000de4

Browse files
nmichaelsandrewrk
authored andcommitted
Handle {s} format for C strings. (#4219)
* Handle {s} format for C strings. * Fix "cstr" test to actually use c strings.
1 parent 7a1cde7 commit 0000de4

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lib/std/fmt.zig

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

12871284
test "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

12921297
test "filesize" {

0 commit comments

Comments
 (0)