Fixes fmt padding for some types

This commit is contained in:
data-man 2020-05-13 18:07:52 +05:00 committed by Andrew Kelley
parent e05923f34b
commit 6647c3f054

View File

@ -334,16 +334,16 @@ pub fn formatType(
return formatValue(value, fmt, options, out_stream);
},
.Void => {
return out_stream.writeAll("void");
return formatBuf("void", options, out_stream);
},
.Bool => {
return out_stream.writeAll(if (value) "true" else "false");
return formatBuf(if (value) "true" else "false", options, out_stream);
},
.Optional => {
if (value) |payload| {
return formatType(payload, fmt, options, out_stream, max_depth);
} else {
return out_stream.writeAll("null");
return formatBuf("null", options, out_stream);
}
},
.ErrorUnion => {
@ -495,7 +495,7 @@ fn formatValue(
switch (@typeInfo(T)) {
.Float => return formatFloatValue(value, fmt, options, out_stream),
.Int, .ComptimeInt => return formatIntValue(value, fmt, options, out_stream),
.Bool => return out_stream.writeAll(if (value) "true" else "false"),
.Bool => return formatBuf(if (value) "true" else "false", options, out_stream),
else => comptime unreachable,
}
}
@ -561,9 +561,7 @@ pub fn formatText(
options: FormatOptions,
out_stream: var,
) !void {
if (fmt.len == 0) {
return out_stream.writeAll(bytes);
} else if (comptime std.mem.eql(u8, fmt, "s")) {
if (comptime std.mem.eql(u8, fmt, "s") or (fmt.len == 0)) {
return formatBuf(bytes, options, out_stream);
} else if (comptime (std.mem.eql(u8, fmt, "x") or std.mem.eql(u8, fmt, "X"))) {
for (bytes) |c| {
@ -588,13 +586,30 @@ pub fn formatBuf(
options: FormatOptions,
out_stream: var,
) !void {
try out_stream.writeAll(buf);
const width = options.width orelse 0;
var leftover_padding = if (width > buf.len) (width - buf.len) else return;
const width = options.width orelse buf.len;
const alignment = options.alignment orelse .Left;
var padding = if (width > buf.len) (width - buf.len) else 0;
const pad_byte = [1]u8{options.fill};
while (leftover_padding > 0) : (leftover_padding -= 1) {
try out_stream.writeAll(&pad_byte);
switch (alignment) {
.Left => {
try out_stream.writeAll(buf);
while (padding > 0) : (padding -= 1) {
try out_stream.writeAll(&pad_byte);
}
},
.Center => {
const padl = padding / 2;
var i: usize = 0;
while (i < padl) : (i += 1) try out_stream.writeAll(&pad_byte);
try out_stream.writeAll(buf);
while (i < padding) : (i += 1) try out_stream.writeAll(&pad_byte);
},
.Right => {
while (padding > 0) : (padding -= 1) {
try out_stream.writeAll(&pad_byte);
}
try out_stream.writeAll(buf);
},
}
}
@ -1686,3 +1701,16 @@ test "vector" {
test "enum-literal" {
try testFmt(".hello_world", "{}", .{.hello_world});
}
test "padding" {
try testFmt("Simple", "{}", .{"Simple"});
try testFmt("true ", "{:10}", .{true});
try testFmt(" true", "{:>10}", .{true});
try testFmt("======true", "{:=>10}", .{true});
try testFmt("true======", "{:=<10}", .{true});
try testFmt(" true ", "{:^10}", .{true});
try testFmt("===true===", "{:=^10}", .{true});
try testFmt("Minimum width", "{:18} width", .{"Minimum"});
try testFmt("==================Filled", "{:=>24}", .{"Filled"});
try testFmt(" Centered ", "{:^24}", .{"Centered"});
}