authorgravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2018-05-30 10:41:48-05:00
committergravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2018-05-30 10:41:48-05:00
log8938c16f38866a7149c32463dea44f884b265643
tree3e736f106996f90f849e1503e40c8f4292b62178
parent4e1d0a59faea8ca9a9c28a876bc448f1d2f46a17

Formatting


1 files changed, 19 insertions(+), 6 deletions(-)

std/fmt/index.zig+19-6
......@@ -11,7 +11,10 @@ const max_int_digits = 65;
1111/// Renders fmt string with args, calling output with slices of bytes.
1212/// If `output` returns an error, the error is returned from `format` and
1313/// `output` is not called again.
14pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void, comptime fmt: []const u8, args: ...) Errors!void {
14pub fn format(context: var, comptime Errors: type,
15 output: fn(@typeOf(context), []const u8) Errors!void, comptime fmt: []const u8,
16 args: ...) Errors!void
17{
1518 const State = enum {
1619 Start,
1720 OpenBrace,
......@@ -281,7 +284,9 @@ pub fn formatText(bytes: []const u8, comptime fmt: []const u8, context: var,
281284 return output(context, bytes);
282285}
283286
284pub fn formatAsciiChar(c: u8, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void {
287pub fn formatAsciiChar(c: u8, context: var, comptime Errors: type,
288 output: fn(@typeOf(context), []const u8) Errors!void) Errors!void
289{
285290 return output(context, (&c)[0..1]);
286291}
287292
......@@ -300,7 +305,9 @@ pub fn formatBuf(buf: []const u8, width: usize, context: var,
300305// Print a float in scientific notation to the specified precision. Null uses full precision.
301306// It should be the case that every full precision, printed value can be re-parsed back to the
302307// same type unambiguously.
303pub fn formatFloatScientific(value: var, maybe_precision: ?usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void {
308pub fn formatFloatScientific(value: var, maybe_precision: ?usize, context: var,
309 comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void
310{
304311 var x = f64(value);
305312
306313 // Errol doesn't handle these special cases.
......@@ -389,7 +396,9 @@ pub fn formatFloatScientific(value: var, maybe_precision: ?usize, context: var,
389396
390397// Print a float of the format x.yyyyy where the number of y is specified by the precision argument.
391398// By default floats are printed at full precision (no rounding).
392pub fn formatFloatDecimal(value: var, maybe_precision: ?usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void {
399pub fn formatFloatDecimal(value: var, maybe_precision: ?usize, context: var, comptime Errors: type,
400 output: fn(@typeOf(context), []const u8) Errors!void) Errors!void
401{
393402 var x = f64(value);
394403
395404 // Errol doesn't handle these special cases.
......@@ -579,7 +588,9 @@ pub fn formatInt(
579588 }
580589}
581590
582fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void {
591fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize, context: var,
592 comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void
593{
583594 const uint = @IntType(false, @typeOf(value).bit_count);
584595 if (value < 0) {
585596 const minus_sign: u8 = '-';
......@@ -598,7 +609,9 @@ fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize, context:
598609 }
599610}
600611
601fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void {
612fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize, context: var,
613 comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void
614{
602615 // max_int_digits accounts for the minus sign. when printing an unsigned
603616 // number we don't need to do that.
604617 var buf: [max_int_digits - 1]u8 = undefined;