Merge pull request #2714 from ziglang/fmt-overhaul

Add positional, precision and width support to std.fmt
This commit is contained in:
Marc Tiehuis 2019-06-25 20:15:33 +12:00 committed by GitHub
commit f5af349bd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 403 additions and 368 deletions

View File

@ -998,7 +998,7 @@ fn printCharValues(out: var, bytes: []const u8) !void {
fn printUnderstandableChar(out: var, char: u8) !void { fn printUnderstandableChar(out: var, char: u8) !void {
if (!std.ascii.isPrint(char) or char == ' ') { if (!std.ascii.isPrint(char) or char == ' ') {
std.fmt.format(out.context, anyerror, out.output, "\\x{X2}", char) catch {}; std.fmt.format(out.context, anyerror, out.output, "\\x{X:2}", char) catch {};
} else { } else {
try out.write("'"); try out.write("'");
try out.write([_]u8{printable_char_tab[char]}); try out.write([_]u8{printable_char_tab[char]});

File diff suppressed because it is too large Load Diff

View File

@ -519,6 +519,7 @@ pub const Int = struct {
pub fn format( pub fn format(
self: Int, self: Int,
comptime fmt: []const u8, comptime fmt: []const u8,
comptime options: std.fmt.FormatOptions,
context: var, context: var,
comptime FmtError: type, comptime FmtError: type,
output: fn (@typeOf(context), []const u8) FmtError!void, output: fn (@typeOf(context), []const u8) FmtError!void,

View File

@ -167,7 +167,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: var) !void {
const allocator = builder.allocator; const allocator = builder.allocator;
for (builder.top_level_steps.toSliceConst()) |top_level_step| { for (builder.top_level_steps.toSliceConst()) |top_level_step| {
try out_stream.print(" {s22} {}\n", top_level_step.step.name, top_level_step.description); try out_stream.print(" {s:22} {}\n", top_level_step.step.name, top_level_step.description);
} }
try out_stream.write( try out_stream.write(
@ -188,7 +188,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: var) !void {
for (builder.available_options_list.toSliceConst()) |option| { for (builder.available_options_list.toSliceConst()) |option| {
const name = try fmt.allocPrint(allocator, " -D{}=[{}]", option.name, Builder.typeIdName(option.type_id)); const name = try fmt.allocPrint(allocator, " -D{}=[{}]", option.name, Builder.typeIdName(option.type_id));
defer allocator.free(name); defer allocator.free(name);
try out_stream.print("{s24} {}\n", name, option.description); try out_stream.print("{s:24} {}\n", name, option.description);
} }
} }

View File

@ -122,7 +122,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ \\
\\pub fn main() void { \\pub fn main() void {
\\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream; \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;
\\ stdout.print("Hello, world!\n{d4} {x3} {c}\n", u32(12), u16(0x12), u8('a')) catch unreachable; \\ stdout.print("Hello, world!\n{d:4} {x:3} {c}\n", u32(12), u16(0x12), u8('a')) catch unreachable;
\\} \\}
, "Hello, world!\n0012 012 a\n"); , "Hello, world!\n0012 012 a\n");