authorgravatar for datamanrb@gmail.comdata-man <datamanrb@gmail.com> 2020-05-13 18:07:52+05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-16 12:45:43-04:00
log6647c3f05428234749bd13b8d0ea1736a03a6a04
tree3283f12f4002f53cd83b9aab5873605accb5ff2b
parente05923f34b566a47d81ee955ec170a855ac99a1d

Fixes fmt padding for some types


1 files changed, 41 insertions(+), 13 deletions(-)

lib/std/fmt.zig+41-13
...@@ -334,16 +334,16 @@ pub fn formatType(...@@ -334,16 +334,16 @@ pub fn formatType(
334 return formatValue(value, fmt, options, out_stream);334 return formatValue(value, fmt, options, out_stream);
335 },335 },
336 .Void => {336 .Void => {
337 return out_stream.writeAll("void");337 return formatBuf("void", options, out_stream);
338 },338 },
339 .Bool => {339 .Bool => {
340 return out_stream.writeAll(if (value) "true" else "false");340 return formatBuf(if (value) "true" else "false", options, out_stream);
341 },341 },
342 .Optional => {342 .Optional => {
343 if (value) |payload| {343 if (value) |payload| {
344 return formatType(payload, fmt, options, out_stream, max_depth);344 return formatType(payload, fmt, options, out_stream, max_depth);
345 } else {345 } else {
346 return out_stream.writeAll("null");346 return formatBuf("null", options, out_stream);
347 }347 }
348 },348 },
349 .ErrorUnion => {349 .ErrorUnion => {
...@@ -495,7 +495,7 @@ fn formatValue(...@@ -495,7 +495,7 @@ fn formatValue(
495 switch (@typeInfo(T)) {495 switch (@typeInfo(T)) {
496 .Float => return formatFloatValue(value, fmt, options, out_stream),496 .Float => return formatFloatValue(value, fmt, options, out_stream),
497 .Int, .ComptimeInt => return formatIntValue(value, fmt, options, out_stream),497 .Int, .ComptimeInt => return formatIntValue(value, fmt, options, out_stream),
498 .Bool => return out_stream.writeAll(if (value) "true" else "false"),498 .Bool => return formatBuf(if (value) "true" else "false", options, out_stream),
499 else => comptime unreachable,499 else => comptime unreachable,
500 }500 }
501}501}
...@@ -561,9 +561,7 @@ pub fn formatText(...@@ -561,9 +561,7 @@ pub fn formatText(
561 options: FormatOptions,561 options: FormatOptions,
562 out_stream: var,562 out_stream: var,
563) !void {563) !void {
564 if (fmt.len == 0) {564 if (comptime std.mem.eql(u8, fmt, "s") or (fmt.len == 0)) {
565 return out_stream.writeAll(bytes);
566 } else if (comptime std.mem.eql(u8, fmt, "s")) {
567 return formatBuf(bytes, options, out_stream);565 return formatBuf(bytes, options, out_stream);
568 } else if (comptime (std.mem.eql(u8, fmt, "x") or std.mem.eql(u8, fmt, "X"))) {566 } else if (comptime (std.mem.eql(u8, fmt, "x") or std.mem.eql(u8, fmt, "X"))) {
569 for (bytes) |c| {567 for (bytes) |c| {
...@@ -588,13 +586,30 @@ pub fn formatBuf(...@@ -588,13 +586,30 @@ pub fn formatBuf(
588 options: FormatOptions,586 options: FormatOptions,
589 out_stream: var,587 out_stream: var,
590) !void {588) !void {
591 try out_stream.writeAll(buf);589 const width = options.width orelse buf.len;
592590 const alignment = options.alignment orelse .Left;
593 const width = options.width orelse 0;591 var padding = if (width > buf.len) (width - buf.len) else 0;
594 var leftover_padding = if (width > buf.len) (width - buf.len) else return;
595 const pad_byte = [1]u8{options.fill};592 const pad_byte = [1]u8{options.fill};
596 while (leftover_padding > 0) : (leftover_padding -= 1) {593 switch (alignment) {
597 try out_stream.writeAll(&pad_byte);594 .Left => {
595 try out_stream.writeAll(buf);
596 while (padding > 0) : (padding -= 1) {
597 try out_stream.writeAll(&pad_byte);
598 }
599 },
600 .Center => {
601 const padl = padding / 2;
602 var i: usize = 0;
603 while (i < padl) : (i += 1) try out_stream.writeAll(&pad_byte);
604 try out_stream.writeAll(buf);
605 while (i < padding) : (i += 1) try out_stream.writeAll(&pad_byte);
606 },
607 .Right => {
608 while (padding > 0) : (padding -= 1) {
609 try out_stream.writeAll(&pad_byte);
610 }
611 try out_stream.writeAll(buf);
612 },
598 }613 }
599}614}
600615
...@@ -1686,3 +1701,16 @@ test "vector" {...@@ -1686,3 +1701,16 @@ test "vector" {
1686test "enum-literal" {1701test "enum-literal" {
1687 try testFmt(".hello_world", "{}", .{.hello_world});1702 try testFmt(".hello_world", "{}", .{.hello_world});
1688}1703}
1704
1705test "padding" {
1706 try testFmt("Simple", "{}", .{"Simple"});
1707 try testFmt("true ", "{:10}", .{true});
1708 try testFmt(" true", "{:>10}", .{true});
1709 try testFmt("======true", "{:=>10}", .{true});
1710 try testFmt("true======", "{:=<10}", .{true});
1711 try testFmt(" true ", "{:^10}", .{true});
1712 try testFmt("===true===", "{:=^10}", .{true});
1713 try testFmt("Minimum width", "{:18} width", .{"Minimum"});
1714 try testFmt("==================Filled", "{:=>24}", .{"Filled"});
1715 try testFmt(" Centered ", "{:^24}", .{"Centered"});
1716}