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;...@@ -11,7 +11,10 @@ const max_int_digits = 65;
11/// Renders fmt string with args, calling output with slices of bytes.11/// Renders fmt string with args, calling output with slices of bytes.
12/// If `output` returns an error, the error is returned from `format` and12/// If `output` returns an error, the error is returned from `format` and
13/// `output` is not called again.13/// `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{
15 const State = enum {18 const State = enum {
16 Start,19 Start,
17 OpenBrace,20 OpenBrace,
...@@ -281,7 +284,9 @@ pub fn formatText(bytes: []const u8, comptime fmt: []const u8, context: var,...@@ -281,7 +284,9 @@ pub fn formatText(bytes: []const u8, comptime fmt: []const u8, context: var,
281 return output(context, bytes);284 return output(context, bytes);
282}285}
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{
285 return output(context, (&c)[0..1]);290 return output(context, (&c)[0..1]);
286}291}
287292
...@@ -300,7 +305,9 @@ pub fn formatBuf(buf: []const u8, width: usize, context: var,...@@ -300,7 +305,9 @@ pub fn formatBuf(buf: []const u8, width: usize, context: var,
300// Print a float in scientific notation to the specified precision. Null uses full precision.305// Print a float in scientific notation to the specified precision. Null uses full precision.
301// It should be the case that every full precision, printed value can be re-parsed back to the306// It should be the case that every full precision, printed value can be re-parsed back to the
302// same type unambiguously.307// 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{
304 var x = f64(value);311 var x = f64(value);
305312
306 // Errol doesn't handle these special cases.313 // Errol doesn't handle these special cases.
...@@ -389,7 +396,9 @@ pub fn formatFloatScientific(value: var, maybe_precision: ?usize, context: var,...@@ -389,7 +396,9 @@ pub fn formatFloatScientific(value: var, maybe_precision: ?usize, context: var,
389396
390// Print a float of the format x.yyyyy where the number of y is specified by the precision argument.397// Print a float of the format x.yyyyy where the number of y is specified by the precision argument.
391// By default floats are printed at full precision (no rounding).398// 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{
393 var x = f64(value);402 var x = f64(value);
394403
395 // Errol doesn't handle these special cases.404 // Errol doesn't handle these special cases.
...@@ -579,7 +588,9 @@ pub fn formatInt(...@@ -579,7 +588,9 @@ pub fn formatInt(
579 }588 }
580}589}
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{
583 const uint = @IntType(false, @typeOf(value).bit_count);594 const uint = @IntType(false, @typeOf(value).bit_count);
584 if (value < 0) {595 if (value < 0) {
585 const minus_sign: u8 = '-';596 const minus_sign: u8 = '-';
...@@ -598,7 +609,9 @@ fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize, context:...@@ -598,7 +609,9 @@ fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize, context:
598 }609 }
599}610}
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{
602 // max_int_digits accounts for the minus sign. when printing an unsigned615 // max_int_digits accounts for the minus sign. when printing an unsigned
603 // number we don't need to do that.616 // number we don't need to do that.
604 var buf: [max_int_digits - 1]u8 = undefined;617 var buf: [max_int_digits - 1]u8 = undefined;