| ... | @@ -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` and | 12 | /// If `output` returns an error, the error is returned from `format` and |
| 13 | /// `output` is not called again. | 13 | /// `output` is not called again. |
| 14 | pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void, comptime fmt: []const u8, args: ...) Errors!void { | 14 | pub 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 | } |
| 283 | | 286 | |
| 284 | pub fn formatAsciiChar(c: u8, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void { | 287 | pub 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 | } |
| 287 | | 292 | |
| ... | @@ -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 the | 306 | // 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. |
| 303 | pub fn formatFloatScientific(value: var, maybe_precision: ?usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void { | 308 | pub 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); |
| 305 | | 312 | |
| 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, |
| 389 | | 396 | |
| 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). |
| 392 | pub fn formatFloatDecimal(value: var, maybe_precision: ?usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void { | 399 | pub 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); |
| 394 | | 403 | |
| 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 | } |
| 581 | | 590 | |
| 582 | fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void { | 591 | fn 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 | } |
| 600 | | 611 | |
| 601 | fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void { | 612 | fn 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 unsigned | 615 | // 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; |