| ... | @@ -285,7 +285,7 @@ pub fn formatType( | ... | @@ -285,7 +285,7 @@ pub fn formatType( |
| 285 | if (comptime std.mem.eql(u8, fmt, "*")) { | 285 | if (comptime std.mem.eql(u8, fmt, "*")) { |
| 286 | try output(context, @typeName(@typeOf(value).Child)); | 286 | try output(context, @typeName(@typeOf(value).Child)); |
| 287 | try output(context, "@"); | 287 | try output(context, "@"); |
| 288 | try formatInt(@ptrToInt(value), 16, false, 0, context, Errors, output); | 288 | try formatInt(@ptrToInt(value), 16, false, FormatOptions{}, context, Errors, output); |
| 289 | return; | 289 | return; |
| 290 | } | 290 | } |
| 291 | | 291 | |
| ... | @@ -434,9 +434,9 @@ fn formatValue( | ... | @@ -434,9 +434,9 @@ fn formatValue( |
| 434 | output: fn (@typeOf(context), []const u8) Errors!void, | 434 | output: fn (@typeOf(context), []const u8) Errors!void, |
| 435 | ) Errors!void { | 435 | ) Errors!void { |
| 436 | if (comptime std.mem.eql(u8, fmt, "B")) { | 436 | if (comptime std.mem.eql(u8, fmt, "B")) { |
| 437 | return formatBytes(value, options.width, 1000, context, Errors, output); | 437 | return formatBytes(value, options, 1000, context, Errors, output); |
| 438 | } else if (comptime std.mem.eql(u8, fmt, "Bi")) { | 438 | } else if (comptime std.mem.eql(u8, fmt, "Bi")) { |
| 439 | return formatBytes(value, options.width, 1024, context, Errors, output); | 439 | return formatBytes(value, options, 1024, context, Errors, output); |
| 440 | } | 440 | } |
| 441 | | 441 | |
| 442 | const T = @typeOf(value); | 442 | const T = @typeOf(value); |
| ... | @@ -469,7 +469,7 @@ pub fn formatIntValue( | ... | @@ -469,7 +469,7 @@ pub fn formatIntValue( |
| 469 | uppercase = false; | 469 | uppercase = false; |
| 470 | } else if (comptime std.mem.eql(u8, fmt, "c")) { | 470 | } else if (comptime std.mem.eql(u8, fmt, "c")) { |
| 471 | if (@typeOf(int_value).bit_count <= 8) { | 471 | if (@typeOf(int_value).bit_count <= 8) { |
| 472 | return formatAsciiChar(u8(int_value), context, Errors, output); | 472 | return formatAsciiChar(u8(int_value), options, context, Errors, output); |
| 473 | } else { | 473 | } else { |
| 474 | @compileError("Cannot print integer that is larger than 8 bits as a ascii"); | 474 | @compileError("Cannot print integer that is larger than 8 bits as a ascii"); |
| 475 | } | 475 | } |
| ... | @@ -486,7 +486,7 @@ pub fn formatIntValue( | ... | @@ -486,7 +486,7 @@ pub fn formatIntValue( |
| 486 | @compileError("Unknown format string: '" ++ fmt ++ "'"); | 486 | @compileError("Unknown format string: '" ++ fmt ++ "'"); |
| 487 | } | 487 | } |
| 488 | | 488 | |
| 489 | return formatInt(int_value, radix, uppercase, options.width orelse 0, context, Errors, output); | 489 | return formatInt(int_value, radix, uppercase, options, context, Errors, output); |
| 490 | } | 490 | } |
| 491 | | 491 | |
| 492 | fn formatFloatValue( | 492 | fn formatFloatValue( |
| ... | @@ -498,9 +498,9 @@ fn formatFloatValue( | ... | @@ -498,9 +498,9 @@ fn formatFloatValue( |
| 498 | output: fn (@typeOf(context), []const u8) Errors!void, | 498 | output: fn (@typeOf(context), []const u8) Errors!void, |
| 499 | ) Errors!void { | 499 | ) Errors!void { |
| 500 | if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) { | 500 | if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) { |
| 501 | return formatFloatScientific(value, options.precision, context, Errors, output); | 501 | return formatFloatScientific(value, options, context, Errors, output); |
| 502 | } else if (comptime std.mem.eql(u8, fmt, "d")) { | 502 | } else if (comptime std.mem.eql(u8, fmt, "d")) { |
| 503 | return formatFloatDecimal(value, options.precision, context, Errors, output); | 503 | return formatFloatDecimal(value, options, context, Errors, output); |
| 504 | } else { | 504 | } else { |
| 505 | @compileError("Unknown format string: '" ++ fmt ++ "'"); | 505 | @compileError("Unknown format string: '" ++ fmt ++ "'"); |
| 506 | } | 506 | } |
| ... | @@ -517,11 +517,10 @@ pub fn formatText( | ... | @@ -517,11 +517,10 @@ pub fn formatText( |
| 517 | if (fmt.len == 0) { | 517 | if (fmt.len == 0) { |
| 518 | return output(context, bytes); | 518 | return output(context, bytes); |
| 519 | } else if (comptime std.mem.eql(u8, fmt, "s")) { | 519 | } else if (comptime std.mem.eql(u8, fmt, "s")) { |
| 520 | if (options.width) |w| return formatBuf(bytes, w, context, Errors, output); | 520 | return formatBuf(bytes, options, context, Errors, output); |
| 521 | return formatBuf(bytes, 0, context, Errors, output); | | |
| 522 | } else if (comptime (std.mem.eql(u8, fmt, "x") or std.mem.eql(u8, fmt, "X"))) { | 521 | } else if (comptime (std.mem.eql(u8, fmt, "x") or std.mem.eql(u8, fmt, "X"))) { |
| 523 | for (bytes) |c| { | 522 | for (bytes) |c| { |
| 524 | try formatInt(c, 16, fmt[0] == 'X', 2, context, Errors, output); | 523 | try formatInt(c, 16, fmt[0] == 'X', FormatOptions{ .width = 2, .fill = '0' }, context, Errors, output); |
| 525 | } | 524 | } |
| 526 | return; | 525 | return; |
| 527 | } else { | 526 | } else { |
| ... | @@ -531,6 +530,7 @@ pub fn formatText( | ... | @@ -531,6 +530,7 @@ pub fn formatText( |
| 531 | | 530 | |
| 532 | pub fn formatAsciiChar( | 531 | pub fn formatAsciiChar( |
| 533 | c: u8, | 532 | c: u8, |
| | 533 | comptime options: FormatOptions, |
| 534 | context: var, | 534 | context: var, |
| 535 | comptime Errors: type, | 535 | comptime Errors: type, |
| 536 | output: fn (@typeOf(context), []const u8) Errors!void, | 536 | output: fn (@typeOf(context), []const u8) Errors!void, |
| ... | @@ -540,15 +540,16 @@ pub fn formatAsciiChar( | ... | @@ -540,15 +540,16 @@ pub fn formatAsciiChar( |
| 540 | | 540 | |
| 541 | pub fn formatBuf( | 541 | pub fn formatBuf( |
| 542 | buf: []const u8, | 542 | buf: []const u8, |
| 543 | width: usize, | 543 | comptime options: FormatOptions, |
| 544 | context: var, | 544 | context: var, |
| 545 | comptime Errors: type, | 545 | comptime Errors: type, |
| 546 | output: fn (@typeOf(context), []const u8) Errors!void, | 546 | output: fn (@typeOf(context), []const u8) Errors!void, |
| 547 | ) Errors!void { | 547 | ) Errors!void { |
| 548 | try output(context, buf); | 548 | try output(context, buf); |
| 549 | | 549 | |
| | 550 | const width = options.width orelse 0; |
| 550 | var leftover_padding = if (width > buf.len) (width - buf.len) else return; | 551 | var leftover_padding = if (width > buf.len) (width - buf.len) else return; |
| 551 | const pad_byte: u8 = ' '; | 552 | const pad_byte: u8 = options.fill; |
| 552 | while (leftover_padding > 0) : (leftover_padding -= 1) { | 553 | while (leftover_padding > 0) : (leftover_padding -= 1) { |
| 553 | try output(context, (*const [1]u8)(&pad_byte)[0..1]); | 554 | try output(context, (*const [1]u8)(&pad_byte)[0..1]); |
| 554 | } | 555 | } |
| ... | @@ -559,7 +560,7 @@ pub fn formatBuf( | ... | @@ -559,7 +560,7 @@ pub fn formatBuf( |
| 559 | // same type unambiguously. | 560 | // same type unambiguously. |
| 560 | pub fn formatFloatScientific( | 561 | pub fn formatFloatScientific( |
| 561 | value: var, | 562 | value: var, |
| 562 | maybe_precision: ?usize, | 563 | comptime options: FormatOptions, |
| 563 | context: var, | 564 | context: var, |
| 564 | comptime Errors: type, | 565 | comptime Errors: type, |
| 565 | output: fn (@typeOf(context), []const u8) Errors!void, | 566 | output: fn (@typeOf(context), []const u8) Errors!void, |
| ... | @@ -581,7 +582,7 @@ pub fn formatFloatScientific( | ... | @@ -581,7 +582,7 @@ pub fn formatFloatScientific( |
| 581 | if (x == 0.0) { | 582 | if (x == 0.0) { |
| 582 | try output(context, "0"); | 583 | try output(context, "0"); |
| 583 | | 584 | |
| 584 | if (maybe_precision) |precision| { | 585 | if (options.precision) |precision| { |
| 585 | if (precision != 0) { | 586 | if (precision != 0) { |
| 586 | try output(context, "."); | 587 | try output(context, "."); |
| 587 | var i: usize = 0; | 588 | var i: usize = 0; |
| ... | @@ -600,7 +601,7 @@ pub fn formatFloatScientific( | ... | @@ -600,7 +601,7 @@ pub fn formatFloatScientific( |
| 600 | var buffer: [32]u8 = undefined; | 601 | var buffer: [32]u8 = undefined; |
| 601 | var float_decimal = errol.errol3(x, buffer[0..]); | 602 | var float_decimal = errol.errol3(x, buffer[0..]); |
| 602 | | 603 | |
| 603 | if (maybe_precision) |precision| { | 604 | if (options.precision) |precision| { |
| 604 | errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Scientific); | 605 | errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Scientific); |
| 605 | | 606 | |
| 606 | try output(context, float_decimal.digits[0..1]); | 607 | try output(context, float_decimal.digits[0..1]); |
| ... | @@ -640,13 +641,13 @@ pub fn formatFloatScientific( | ... | @@ -640,13 +641,13 @@ pub fn formatFloatScientific( |
| 640 | if (exp > -10 and exp < 10) { | 641 | if (exp > -10 and exp < 10) { |
| 641 | try output(context, "0"); | 642 | try output(context, "0"); |
| 642 | } | 643 | } |
| 643 | try formatInt(exp, 10, false, 0, context, Errors, output); | 644 | try formatInt(exp, 10, false, FormatOptions{ .width = 0 }, context, Errors, output); |
| 644 | } else { | 645 | } else { |
| 645 | try output(context, "-"); | 646 | try output(context, "-"); |
| 646 | if (exp > -10 and exp < 10) { | 647 | if (exp > -10 and exp < 10) { |
| 647 | try output(context, "0"); | 648 | try output(context, "0"); |
| 648 | } | 649 | } |
| 649 | try formatInt(-exp, 10, false, 0, context, Errors, output); | 650 | try formatInt(-exp, 10, false, FormatOptions{ .width = 0 }, context, Errors, output); |
| 650 | } | 651 | } |
| 651 | } | 652 | } |
| 652 | | 653 | |
| ... | @@ -654,7 +655,7 @@ pub fn formatFloatScientific( | ... | @@ -654,7 +655,7 @@ pub fn formatFloatScientific( |
| 654 | // By default floats are printed at full precision (no rounding). | 655 | // By default floats are printed at full precision (no rounding). |
| 655 | pub fn formatFloatDecimal( | 656 | pub fn formatFloatDecimal( |
| 656 | value: var, | 657 | value: var, |
| 657 | maybe_precision: ?usize, | 658 | comptime options: FormatOptions, |
| 658 | context: var, | 659 | context: var, |
| 659 | comptime Errors: type, | 660 | comptime Errors: type, |
| 660 | output: fn (@typeOf(context), []const u8) Errors!void, | 661 | output: fn (@typeOf(context), []const u8) Errors!void, |
| ... | @@ -676,7 +677,7 @@ pub fn formatFloatDecimal( | ... | @@ -676,7 +677,7 @@ pub fn formatFloatDecimal( |
| 676 | if (x == 0.0) { | 677 | if (x == 0.0) { |
| 677 | try output(context, "0"); | 678 | try output(context, "0"); |
| 678 | | 679 | |
| 679 | if (maybe_precision) |precision| { | 680 | if (options.precision) |precision| { |
| 680 | if (precision != 0) { | 681 | if (precision != 0) { |
| 681 | try output(context, "."); | 682 | try output(context, "."); |
| 682 | var i: usize = 0; | 683 | var i: usize = 0; |
| ... | @@ -697,7 +698,7 @@ pub fn formatFloatDecimal( | ... | @@ -697,7 +698,7 @@ pub fn formatFloatDecimal( |
| 697 | var buffer: [32]u8 = undefined; | 698 | var buffer: [32]u8 = undefined; |
| 698 | var float_decimal = errol.errol3(x, buffer[0..]); | 699 | var float_decimal = errol.errol3(x, buffer[0..]); |
| 699 | | 700 | |
| 700 | if (maybe_precision) |precision| { | 701 | if (options.precision) |precision| { |
| 701 | errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Decimal); | 702 | errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Decimal); |
| 702 | | 703 | |
| 703 | // exp < 0 means the leading is always 0 as errol result is normalized. | 704 | // exp < 0 means the leading is always 0 as errol result is normalized. |
| ... | @@ -799,7 +800,7 @@ pub fn formatFloatDecimal( | ... | @@ -799,7 +800,7 @@ pub fn formatFloatDecimal( |
| 799 | | 800 | |
| 800 | pub fn formatBytes( | 801 | pub fn formatBytes( |
| 801 | value: var, | 802 | value: var, |
| 802 | width: ?usize, | 803 | comptime options: FormatOptions, |
| 803 | comptime radix: usize, | 804 | comptime radix: usize, |
| 804 | context: var, | 805 | context: var, |
| 805 | comptime Errors: type, | 806 | comptime Errors: type, |
| ... | @@ -823,7 +824,7 @@ pub fn formatBytes( | ... | @@ -823,7 +824,7 @@ pub fn formatBytes( |
| 823 | else => unreachable, | 824 | else => unreachable, |
| 824 | }; | 825 | }; |
| 825 | | 826 | |
| 826 | try formatFloatDecimal(new_value, width, context, Errors, output); | 827 | try formatFloatDecimal(new_value, options, context, Errors, output); |
| 827 | | 828 | |
| 828 | if (suffix == ' ') { | 829 | if (suffix == ' ') { |
| 829 | return output(context, "B"); | 830 | return output(context, "B"); |
| ... | @@ -841,7 +842,7 @@ pub fn formatInt( | ... | @@ -841,7 +842,7 @@ pub fn formatInt( |
| 841 | value: var, | 842 | value: var, |
| 842 | base: u8, | 843 | base: u8, |
| 843 | uppercase: bool, | 844 | uppercase: bool, |
| 844 | width: usize, | 845 | comptime options: FormatOptions, |
| 845 | context: var, | 846 | context: var, |
| 846 | comptime Errors: type, | 847 | comptime Errors: type, |
| 847 | output: fn (@typeOf(context), []const u8) Errors!void, | 848 | output: fn (@typeOf(context), []const u8) Errors!void, |
| ... | @@ -853,9 +854,9 @@ pub fn formatInt( | ... | @@ -853,9 +854,9 @@ pub fn formatInt( |
| 853 | value; | 854 | value; |
| 854 | | 855 | |
| 855 | if (@typeOf(int_value).is_signed) { | 856 | if (@typeOf(int_value).is_signed) { |
| 856 | return formatIntSigned(int_value, base, uppercase, width, context, Errors, output); | 857 | return formatIntSigned(int_value, base, uppercase, options, context, Errors, output); |
| 857 | } else { | 858 | } else { |
| 858 | return formatIntUnsigned(int_value, base, uppercase, width, context, Errors, output); | 859 | return formatIntUnsigned(int_value, base, uppercase, options, context, Errors, output); |
| 859 | } | 860 | } |
| 860 | } | 861 | } |
| 861 | | 862 | |
| ... | @@ -863,26 +864,30 @@ fn formatIntSigned( | ... | @@ -863,26 +864,30 @@ fn formatIntSigned( |
| 863 | value: var, | 864 | value: var, |
| 864 | base: u8, | 865 | base: u8, |
| 865 | uppercase: bool, | 866 | uppercase: bool, |
| 866 | width: usize, | 867 | comptime options: FormatOptions, |
| 867 | context: var, | 868 | context: var, |
| 868 | comptime Errors: type, | 869 | comptime Errors: type, |
| 869 | output: fn (@typeOf(context), []const u8) Errors!void, | 870 | output: fn (@typeOf(context), []const u8) Errors!void, |
| 870 | ) Errors!void { | 871 | ) Errors!void { |
| | 872 | const new_options = FormatOptions{ |
| | 873 | .width = if (options.width) |w| (if (w == 0) 0 else w - 1) else null, |
| | 874 | .precision = options.precision, |
| | 875 | .fill = options.fill, |
| | 876 | }; |
| | 877 | |
| 871 | const uint = @IntType(false, @typeOf(value).bit_count); | 878 | const uint = @IntType(false, @typeOf(value).bit_count); |
| 872 | if (value < 0) { | 879 | if (value < 0) { |
| 873 | const minus_sign: u8 = '-'; | 880 | const minus_sign: u8 = '-'; |
| 874 | try output(context, (*const [1]u8)(&minus_sign)[0..]); | 881 | try output(context, (*const [1]u8)(&minus_sign)[0..]); |
| 875 | const new_value = @intCast(uint, -(value + 1)) + 1; | 882 | const new_value = @intCast(uint, -(value + 1)) + 1; |
| 876 | const new_width = if (width == 0) 0 else (width - 1); | 883 | return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); |
| 877 | return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output); | 884 | } else if (options.width == null or options.width.? == 0) { |
| 878 | } else if (width == 0) { | 885 | return formatIntUnsigned(@intCast(uint, value), base, uppercase, options, context, Errors, output); |
| 879 | return formatIntUnsigned(@intCast(uint, value), base, uppercase, width, context, Errors, output); | | |
| 880 | } else { | 886 | } else { |
| 881 | const plus_sign: u8 = '+'; | 887 | const plus_sign: u8 = '+'; |
| 882 | try output(context, (*const [1]u8)(&plus_sign)[0..]); | 888 | try output(context, (*const [1]u8)(&plus_sign)[0..]); |
| 883 | const new_value = @intCast(uint, value); | 889 | const new_value = @intCast(uint, value); |
| 884 | const new_width = if (width == 0) 0 else (width - 1); | 890 | return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); |
| 885 | return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output); | | |
| 886 | } | 891 | } |
| 887 | } | 892 | } |
| 888 | | 893 | |
| ... | @@ -890,7 +895,7 @@ fn formatIntUnsigned( | ... | @@ -890,7 +895,7 @@ fn formatIntUnsigned( |
| 890 | value: var, | 895 | value: var, |
| 891 | base: u8, | 896 | base: u8, |
| 892 | uppercase: bool, | 897 | uppercase: bool, |
| 893 | width: usize, | 898 | comptime options: FormatOptions, |
| 894 | context: var, | 899 | context: var, |
| 895 | comptime Errors: type, | 900 | comptime Errors: type, |
| 896 | output: fn (@typeOf(context), []const u8) Errors!void, | 901 | output: fn (@typeOf(context), []const u8) Errors!void, |
| ... | @@ -911,31 +916,32 @@ fn formatIntUnsigned( | ... | @@ -911,31 +916,32 @@ fn formatIntUnsigned( |
| 911 | } | 916 | } |
| 912 | | 917 | |
| 913 | const digits_buf = buf[index..]; | 918 | const digits_buf = buf[index..]; |
| | 919 | const width = options.width orelse 0; |
| 914 | const padding = if (width > digits_buf.len) (width - digits_buf.len) else 0; | 920 | const padding = if (width > digits_buf.len) (width - digits_buf.len) else 0; |
| 915 | | 921 | |
| 916 | if (padding > index) { | 922 | if (padding > index) { |
| 917 | const zero_byte: u8 = '0'; | 923 | const zero_byte: u8 = options.fill; |
| 918 | var leftover_padding = padding - index; | 924 | var leftover_padding = padding - index; |
| 919 | while (true) { | 925 | while (true) { |
| 920 | try output(context, (*const [1]u8)(&zero_byte)[0..]); | 926 | try output(context, (*const [1]u8)(&zero_byte)[0..]); |
| 921 | leftover_padding -= 1; | 927 | leftover_padding -= 1; |
| 922 | if (leftover_padding == 0) break; | 928 | if (leftover_padding == 0) break; |
| 923 | } | 929 | } |
| 924 | mem.set(u8, buf[0..index], '0'); | 930 | mem.set(u8, buf[0..index], options.fill); |
| 925 | return output(context, buf); | 931 | return output(context, buf); |
| 926 | } else { | 932 | } else { |
| 927 | const padded_buf = buf[index - padding ..]; | 933 | const padded_buf = buf[index - padding ..]; |
| 928 | mem.set(u8, padded_buf[0..padding], '0'); | 934 | mem.set(u8, padded_buf[0..padding], options.fill); |
| 929 | return output(context, padded_buf); | 935 | return output(context, padded_buf); |
| 930 | } | 936 | } |
| 931 | } | 937 | } |
| 932 | | 938 | |
| 933 | pub fn formatIntBuf(out_buf: []u8, value: var, base: u8, uppercase: bool, width: usize) usize { | 939 | pub fn formatIntBuf(out_buf: []u8, value: var, base: u8, uppercase: bool, comptime options: FormatOptions) usize { |
| 934 | var context = FormatIntBuf{ | 940 | var context = FormatIntBuf{ |
| 935 | .out_buf = out_buf, | 941 | .out_buf = out_buf, |
| 936 | .index = 0, | 942 | .index = 0, |
| 937 | }; | 943 | }; |
| 938 | formatInt(value, base, uppercase, width, &context, error{}, formatIntCallback) catch unreachable; | 944 | formatInt(value, base, uppercase, options, &context, error{}, formatIntCallback) catch unreachable; |
| 939 | return context.index; | 945 | return context.index; |
| 940 | } | 946 | } |
| 941 | const FormatIntBuf = struct { | 947 | const FormatIntBuf = struct { |
| ... | @@ -1078,23 +1084,23 @@ fn countSize(size: *usize, bytes: []const u8) (error{}!void) { | ... | @@ -1078,23 +1084,23 @@ fn countSize(size: *usize, bytes: []const u8) (error{}!void) { |
| 1078 | test "bufPrintInt" { | 1084 | test "bufPrintInt" { |
| 1079 | var buffer: [100]u8 = undefined; | 1085 | var buffer: [100]u8 = undefined; |
| 1080 | const buf = buffer[0..]; | 1086 | const buf = buffer[0..]; |
| 1081 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110")); | 1087 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, FormatOptions{}), "-101111000110000101001110")); |
| 1082 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, 0), "-12345678")); | 1088 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, FormatOptions{}), "-12345678")); |
| 1083 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, false, 0), "-bc614e")); | 1089 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, false, FormatOptions{}), "-bc614e")); |
| 1084 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, true, 0), "-BC614E")); | 1090 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, true, FormatOptions{}), "-BC614E")); |
| 1085 | | 1091 | |
| 1086 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(12345678), 10, true, 0), "12345678")); | 1092 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(12345678), 10, true, FormatOptions{}), "12345678")); |
| 1087 | | 1093 | |
| 1088 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(666), 10, false, 6), "000666")); | 1094 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(666), 10, false, FormatOptions{ .width = 6 }), " 666")); |
| 1089 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 6), "001234")); | 1095 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, FormatOptions{ .width = 6 }), " 1234")); |
| 1090 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 1), "1234")); | 1096 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, FormatOptions{ .width = 1 }), "1234")); |
| 1091 | | 1097 | |
| 1092 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(42), 10, false, 3), "+42")); | 1098 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(42), 10, false, FormatOptions{ .width = 3 }), "+42")); |
| 1093 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-42), 10, false, 3), "-42")); | 1099 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-42), 10, false, FormatOptions{ .width = 3 }), "-42")); |
| 1094 | } | 1100 | } |
| 1095 | | 1101 | |
| 1096 | fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, width: usize) []u8 { | 1102 | fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, comptime options: FormatOptions) []u8 { |
| 1097 | return buf[0..formatIntBuf(buf, value, base, uppercase, width)]; | 1103 | return buf[0..formatIntBuf(buf, value, base, uppercase, options)]; |
| 1098 | } | 1104 | } |
| 1099 | | 1105 | |
| 1100 | test "parse u64 digit too big" { | 1106 | test "parse u64 digit too big" { |
| ... | @@ -1152,7 +1158,8 @@ test "int.specifier" { | ... | @@ -1152,7 +1158,8 @@ test "int.specifier" { |
| 1152 | } | 1158 | } |
| 1153 | | 1159 | |
| 1154 | test "int.padded" { | 1160 | test "int.padded" { |
| 1155 | try testFmt("u8: '0001'", "u8: '{:4}'", u8(1)); | 1161 | try testFmt("u8: ' 1'", "u8: '{:4}'", u8(1)); |
| | 1162 | try testFmt("u8: 'xxx1'", "u8: '{:x<4}'", u8(1)); |
| 1156 | } | 1163 | } |
| 1157 | | 1164 | |
| 1158 | test "buffer" { | 1165 | test "buffer" { |
| ... | @@ -1227,7 +1234,7 @@ test "cstr" { | ... | @@ -1227,7 +1234,7 @@ test "cstr" { |
| 1227 | | 1234 | |
| 1228 | test "filesize" { | 1235 | test "filesize" { |
| 1229 | try testFmt("file size: 63MiB\n", "file size: {Bi}\n", usize(63 * 1024 * 1024)); | 1236 | try testFmt("file size: 63MiB\n", "file size: {Bi}\n", usize(63 * 1024 * 1024)); |
| 1230 | try testFmt("file size: 66.06MB\n", "file size: {B:2}\n", usize(63 * 1024 * 1024)); | 1237 | try testFmt("file size: 66.06MB\n", "file size: {B:.2}\n", usize(63 * 1024 * 1024)); |
| 1231 | } | 1238 | } |
| 1232 | | 1239 | |
| 1233 | test "struct" { | 1240 | test "struct" { |