| ... | ... | @@ -285,7 +285,7 @@ pub fn formatType( |
| 285 | 285 | if (comptime std.mem.eql(u8, fmt, "*")) { |
| 286 | 286 | try output(context, @typeName(@typeOf(value).Child)); |
| 287 | 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 | 289 | return; |
| 290 | 290 | } |
| 291 | 291 | |
| ... | ... | @@ -434,9 +434,9 @@ fn formatValue( |
| 434 | 434 | output: fn (@typeOf(context), []const u8) Errors!void, |
| 435 | 435 | ) Errors!void { |
| 436 | 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 | 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 | 442 | const T = @typeOf(value); |
| ... | ... | @@ -469,7 +469,7 @@ pub fn formatIntValue( |
| 469 | 469 | uppercase = false; |
| 470 | 470 | } else if (comptime std.mem.eql(u8, fmt, "c")) { |
| 471 | 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 | 473 | } else { |
| 474 | 474 | @compileError("Cannot print integer that is larger than 8 bits as a ascii"); |
| 475 | 475 | } |
| ... | ... | @@ -486,7 +486,7 @@ pub fn formatIntValue( |
| 486 | 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 | 492 | fn formatFloatValue( |
| ... | ... | @@ -498,9 +498,9 @@ fn formatFloatValue( |
| 498 | 498 | output: fn (@typeOf(context), []const u8) Errors!void, |
| 499 | 499 | ) Errors!void { |
| 500 | 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 | 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 | 504 | } else { |
| 505 | 505 | @compileError("Unknown format string: '" ++ fmt ++ "'"); |
| 506 | 506 | } |
| ... | ... | @@ -517,11 +517,10 @@ pub fn formatText( |
| 517 | 517 | if (fmt.len == 0) { |
| 518 | 518 | return output(context, bytes); |
| 519 | 519 | } else if (comptime std.mem.eql(u8, fmt, "s")) { |
| 520 | | if (options.width) |w| return formatBuf(bytes, w, context, Errors, output); |
| 521 | | return formatBuf(bytes, 0, context, Errors, output); |
| 520 | return formatBuf(bytes, options, context, Errors, output); |
| 522 | 521 | } else if (comptime (std.mem.eql(u8, fmt, "x") or std.mem.eql(u8, fmt, "X"))) { |
| 523 | 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 | 525 | return; |
| 527 | 526 | } else { |
| ... | ... | @@ -531,6 +530,7 @@ pub fn formatText( |
| 531 | 530 | |
| 532 | 531 | pub fn formatAsciiChar( |
| 533 | 532 | c: u8, |
| 533 | comptime options: FormatOptions, |
| 534 | 534 | context: var, |
| 535 | 535 | comptime Errors: type, |
| 536 | 536 | output: fn (@typeOf(context), []const u8) Errors!void, |
| ... | ... | @@ -540,15 +540,16 @@ pub fn formatAsciiChar( |
| 540 | 540 | |
| 541 | 541 | pub fn formatBuf( |
| 542 | 542 | buf: []const u8, |
| 543 | | width: usize, |
| 543 | comptime options: FormatOptions, |
| 544 | 544 | context: var, |
| 545 | 545 | comptime Errors: type, |
| 546 | 546 | output: fn (@typeOf(context), []const u8) Errors!void, |
| 547 | 547 | ) Errors!void { |
| 548 | 548 | try output(context, buf); |
| 549 | 549 | |
| 550 | const width = options.width orelse 0; |
| 550 | 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 | 553 | while (leftover_padding > 0) : (leftover_padding -= 1) { |
| 553 | 554 | try output(context, (*const [1]u8)(&pad_byte)[0..1]); |
| 554 | 555 | } |
| ... | ... | @@ -559,7 +560,7 @@ pub fn formatBuf( |
| 559 | 560 | // same type unambiguously. |
| 560 | 561 | pub fn formatFloatScientific( |
| 561 | 562 | value: var, |
| 562 | | maybe_precision: ?usize, |
| 563 | comptime options: FormatOptions, |
| 563 | 564 | context: var, |
| 564 | 565 | comptime Errors: type, |
| 565 | 566 | output: fn (@typeOf(context), []const u8) Errors!void, |
| ... | ... | @@ -581,7 +582,7 @@ pub fn formatFloatScientific( |
| 581 | 582 | if (x == 0.0) { |
| 582 | 583 | try output(context, "0"); |
| 583 | 584 | |
| 584 | | if (maybe_precision) |precision| { |
| 585 | if (options.precision) |precision| { |
| 585 | 586 | if (precision != 0) { |
| 586 | 587 | try output(context, "."); |
| 587 | 588 | var i: usize = 0; |
| ... | ... | @@ -600,7 +601,7 @@ pub fn formatFloatScientific( |
| 600 | 601 | var buffer: [32]u8 = undefined; |
| 601 | 602 | var float_decimal = errol.errol3(x, buffer[0..]); |
| 602 | 603 | |
| 603 | | if (maybe_precision) |precision| { |
| 604 | if (options.precision) |precision| { |
| 604 | 605 | errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Scientific); |
| 605 | 606 | |
| 606 | 607 | try output(context, float_decimal.digits[0..1]); |
| ... | ... | @@ -640,13 +641,13 @@ pub fn formatFloatScientific( |
| 640 | 641 | if (exp > -10 and exp < 10) { |
| 641 | 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 | 645 | } else { |
| 645 | 646 | try output(context, "-"); |
| 646 | 647 | if (exp > -10 and exp < 10) { |
| 647 | 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 | 655 | // By default floats are printed at full precision (no rounding). |
| 655 | 656 | pub fn formatFloatDecimal( |
| 656 | 657 | value: var, |
| 657 | | maybe_precision: ?usize, |
| 658 | comptime options: FormatOptions, |
| 658 | 659 | context: var, |
| 659 | 660 | comptime Errors: type, |
| 660 | 661 | output: fn (@typeOf(context), []const u8) Errors!void, |
| ... | ... | @@ -676,7 +677,7 @@ pub fn formatFloatDecimal( |
| 676 | 677 | if (x == 0.0) { |
| 677 | 678 | try output(context, "0"); |
| 678 | 679 | |
| 679 | | if (maybe_precision) |precision| { |
| 680 | if (options.precision) |precision| { |
| 680 | 681 | if (precision != 0) { |
| 681 | 682 | try output(context, "."); |
| 682 | 683 | var i: usize = 0; |
| ... | ... | @@ -697,7 +698,7 @@ pub fn formatFloatDecimal( |
| 697 | 698 | var buffer: [32]u8 = undefined; |
| 698 | 699 | var float_decimal = errol.errol3(x, buffer[0..]); |
| 699 | 700 | |
| 700 | | if (maybe_precision) |precision| { |
| 701 | if (options.precision) |precision| { |
| 701 | 702 | errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Decimal); |
| 702 | 703 | |
| 703 | 704 | // exp < 0 means the leading is always 0 as errol result is normalized. |
| ... | ... | @@ -799,7 +800,7 @@ pub fn formatFloatDecimal( |
| 799 | 800 | |
| 800 | 801 | pub fn formatBytes( |
| 801 | 802 | value: var, |
| 802 | | width: ?usize, |
| 803 | comptime options: FormatOptions, |
| 803 | 804 | comptime radix: usize, |
| 804 | 805 | context: var, |
| 805 | 806 | comptime Errors: type, |
| ... | ... | @@ -823,7 +824,7 @@ pub fn formatBytes( |
| 823 | 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 | 829 | if (suffix == ' ') { |
| 829 | 830 | return output(context, "B"); |
| ... | ... | @@ -841,7 +842,7 @@ pub fn formatInt( |
| 841 | 842 | value: var, |
| 842 | 843 | base: u8, |
| 843 | 844 | uppercase: bool, |
| 844 | | width: usize, |
| 845 | comptime options: FormatOptions, |
| 845 | 846 | context: var, |
| 846 | 847 | comptime Errors: type, |
| 847 | 848 | output: fn (@typeOf(context), []const u8) Errors!void, |
| ... | ... | @@ -853,9 +854,9 @@ pub fn formatInt( |
| 853 | 854 | value; |
| 854 | 855 | |
| 855 | 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 | 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 | 864 | value: var, |
| 864 | 865 | base: u8, |
| 865 | 866 | uppercase: bool, |
| 866 | | width: usize, |
| 867 | comptime options: FormatOptions, |
| 867 | 868 | context: var, |
| 868 | 869 | comptime Errors: type, |
| 869 | 870 | output: fn (@typeOf(context), []const u8) Errors!void, |
| 870 | 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 | 878 | const uint = @IntType(false, @typeOf(value).bit_count); |
| 872 | 879 | if (value < 0) { |
| 873 | 880 | const minus_sign: u8 = '-'; |
| 874 | 881 | try output(context, (*const [1]u8)(&minus_sign)[0..]); |
| 875 | 882 | const new_value = @intCast(uint, -(value + 1)) + 1; |
| 876 | | const new_width = if (width == 0) 0 else (width - 1); |
| 877 | | return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output); |
| 878 | | } else if (width == 0) { |
| 879 | | return formatIntUnsigned(@intCast(uint, value), base, uppercase, width, context, Errors, output); |
| 883 | return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); |
| 884 | } else if (options.width == null or options.width.? == 0) { |
| 885 | return formatIntUnsigned(@intCast(uint, value), base, uppercase, options, context, Errors, output); |
| 880 | 886 | } else { |
| 881 | 887 | const plus_sign: u8 = '+'; |
| 882 | 888 | try output(context, (*const [1]u8)(&plus_sign)[0..]); |
| 883 | 889 | const new_value = @intCast(uint, value); |
| 884 | | const new_width = if (width == 0) 0 else (width - 1); |
| 885 | | return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output); |
| 890 | return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); |
| 886 | 891 | } |
| 887 | 892 | } |
| 888 | 893 | |
| ... | ... | @@ -890,7 +895,7 @@ fn formatIntUnsigned( |
| 890 | 895 | value: var, |
| 891 | 896 | base: u8, |
| 892 | 897 | uppercase: bool, |
| 893 | | width: usize, |
| 898 | comptime options: FormatOptions, |
| 894 | 899 | context: var, |
| 895 | 900 | comptime Errors: type, |
| 896 | 901 | output: fn (@typeOf(context), []const u8) Errors!void, |
| ... | ... | @@ -911,31 +916,32 @@ fn formatIntUnsigned( |
| 911 | 916 | } |
| 912 | 917 | |
| 913 | 918 | const digits_buf = buf[index..]; |
| 919 | const width = options.width orelse 0; |
| 914 | 920 | const padding = if (width > digits_buf.len) (width - digits_buf.len) else 0; |
| 915 | 921 | |
| 916 | 922 | if (padding > index) { |
| 917 | | const zero_byte: u8 = '0'; |
| 923 | const zero_byte: u8 = options.fill; |
| 918 | 924 | var leftover_padding = padding - index; |
| 919 | 925 | while (true) { |
| 920 | 926 | try output(context, (*const [1]u8)(&zero_byte)[0..]); |
| 921 | 927 | leftover_padding -= 1; |
| 922 | 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 | 931 | return output(context, buf); |
| 926 | 932 | } else { |
| 927 | 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 | 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 | 940 | var context = FormatIntBuf{ |
| 935 | 941 | .out_buf = out_buf, |
| 936 | 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 | 945 | return context.index; |
| 940 | 946 | } |
| 941 | 947 | const FormatIntBuf = struct { |
| ... | ... | @@ -1078,23 +1084,23 @@ fn countSize(size: *usize, bytes: []const u8) (error{}!void) { |
| 1078 | 1084 | test "bufPrintInt" { |
| 1079 | 1085 | var buffer: [100]u8 = undefined; |
| 1080 | 1086 | const buf = buffer[0..]; |
| 1081 | | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110")); |
| 1082 | | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, 0), "-12345678")); |
| 1083 | | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, false, 0), "-bc614e")); |
| 1084 | | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, true, 0), "-BC614E")); |
| 1087 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, FormatOptions{}), "-101111000110000101001110")); |
| 1088 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, FormatOptions{}), "-12345678")); |
| 1089 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, false, FormatOptions{}), "-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")); |
| 1089 | | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 6), "001234")); |
| 1090 | | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 1), "1234")); |
| 1094 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(666), 10, false, FormatOptions{ .width = 6 }), " 666")); |
| 1095 | testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, FormatOptions{ .width = 6 }), " 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")); |
| 1093 | | 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")); |
| 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 { |
| 1097 | | return buf[0..formatIntBuf(buf, value, base, uppercase, width)]; |
| 1102 | fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, comptime options: FormatOptions) []u8 { |
| 1103 | return buf[0..formatIntBuf(buf, value, base, uppercase, options)]; |
| 1098 | 1104 | } |
| 1099 | 1105 | |
| 1100 | 1106 | test "parse u64 digit too big" { |
| ... | ... | @@ -1152,7 +1158,8 @@ test "int.specifier" { |
| 1152 | 1158 | } |
| 1153 | 1159 | |
| 1154 | 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 | 1165 | test "buffer" { |
| ... | ... | @@ -1227,7 +1234,7 @@ test "cstr" { |
| 1227 | 1234 | |
| 1228 | 1235 | test "filesize" { |
| 1229 | 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 | 1240 | test "struct" { |