authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2019-08-18 11:17:16+12:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2019-08-19 22:15:15+12:00
log98859c885e8e66518c1081c56d203cdd77c51ce8
tree8dacd213f4665c71ca367bb04179da6580f13cc5
parent6844dafeca42daaf904bc4df0347c6fd625dbcaf

std/fmt.zig: Pass full options struct to all internal functions

The fill specifier is now handled in some cases. The default fill of '0' is now ' ' for integers and non-byte sequences.

3 files changed, 62 insertions(+), 55 deletions(-)

std/fmt.zig+59-52
......@@ -285,7 +285,7 @@ pub fn formatType(
285285 if (comptime std.mem.eql(u8, fmt, "*")) {
286286 try output(context, @typeName(@typeOf(value).Child));
287287 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);
289289 return;
290290 }
291291
......@@ -434,9 +434,9 @@ fn formatValue(
434434 output: fn (@typeOf(context), []const u8) Errors!void,
435435) Errors!void {
436436 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);
438438 } 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);
440440 }
441441
442442 const T = @typeOf(value);
......@@ -469,7 +469,7 @@ pub fn formatIntValue(
469469 uppercase = false;
470470 } else if (comptime std.mem.eql(u8, fmt, "c")) {
471471 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);
473473 } else {
474474 @compileError("Cannot print integer that is larger than 8 bits as a ascii");
475475 }
......@@ -486,7 +486,7 @@ pub fn formatIntValue(
486486 @compileError("Unknown format string: '" ++ fmt ++ "'");
487487 }
488488
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);
490490}
491491
492492fn formatFloatValue(
......@@ -498,9 +498,9 @@ fn formatFloatValue(
498498 output: fn (@typeOf(context), []const u8) Errors!void,
499499) Errors!void {
500500 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);
502502 } 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);
504504 } else {
505505 @compileError("Unknown format string: '" ++ fmt ++ "'");
506506 }
......@@ -517,11 +517,10 @@ pub fn formatText(
517517 if (fmt.len == 0) {
518518 return output(context, bytes);
519519 } 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);
522521 } else if (comptime (std.mem.eql(u8, fmt, "x") or std.mem.eql(u8, fmt, "X"))) {
523522 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);
525524 }
526525 return;
527526 } else {
......@@ -531,6 +530,7 @@ pub fn formatText(
531530
532531pub fn formatAsciiChar(
533532 c: u8,
533 comptime options: FormatOptions,
534534 context: var,
535535 comptime Errors: type,
536536 output: fn (@typeOf(context), []const u8) Errors!void,
......@@ -540,15 +540,16 @@ pub fn formatAsciiChar(
540540
541541pub fn formatBuf(
542542 buf: []const u8,
543 width: usize,
543 comptime options: FormatOptions,
544544 context: var,
545545 comptime Errors: type,
546546 output: fn (@typeOf(context), []const u8) Errors!void,
547547) Errors!void {
548548 try output(context, buf);
549549
550 const width = options.width orelse 0;
550551 var leftover_padding = if (width > buf.len) (width - buf.len) else return;
551 const pad_byte: u8 = ' ';
552 const pad_byte: u8 = options.fill;
552553 while (leftover_padding > 0) : (leftover_padding -= 1) {
553554 try output(context, (*const [1]u8)(&pad_byte)[0..1]);
554555 }
......@@ -559,7 +560,7 @@ pub fn formatBuf(
559560// same type unambiguously.
560561pub fn formatFloatScientific(
561562 value: var,
562 maybe_precision: ?usize,
563 comptime options: FormatOptions,
563564 context: var,
564565 comptime Errors: type,
565566 output: fn (@typeOf(context), []const u8) Errors!void,
......@@ -581,7 +582,7 @@ pub fn formatFloatScientific(
581582 if (x == 0.0) {
582583 try output(context, "0");
583584
584 if (maybe_precision) |precision| {
585 if (options.precision) |precision| {
585586 if (precision != 0) {
586587 try output(context, ".");
587588 var i: usize = 0;
......@@ -600,7 +601,7 @@ pub fn formatFloatScientific(
600601 var buffer: [32]u8 = undefined;
601602 var float_decimal = errol.errol3(x, buffer[0..]);
602603
603 if (maybe_precision) |precision| {
604 if (options.precision) |precision| {
604605 errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Scientific);
605606
606607 try output(context, float_decimal.digits[0..1]);
......@@ -640,13 +641,13 @@ pub fn formatFloatScientific(
640641 if (exp > -10 and exp < 10) {
641642 try output(context, "0");
642643 }
643 try formatInt(exp, 10, false, 0, context, Errors, output);
644 try formatInt(exp, 10, false, FormatOptions{ .width = 0 }, context, Errors, output);
644645 } else {
645646 try output(context, "-");
646647 if (exp > -10 and exp < 10) {
647648 try output(context, "0");
648649 }
649 try formatInt(-exp, 10, false, 0, context, Errors, output);
650 try formatInt(-exp, 10, false, FormatOptions{ .width = 0 }, context, Errors, output);
650651 }
651652}
652653
......@@ -654,7 +655,7 @@ pub fn formatFloatScientific(
654655// By default floats are printed at full precision (no rounding).
655656pub fn formatFloatDecimal(
656657 value: var,
657 maybe_precision: ?usize,
658 comptime options: FormatOptions,
658659 context: var,
659660 comptime Errors: type,
660661 output: fn (@typeOf(context), []const u8) Errors!void,
......@@ -676,7 +677,7 @@ pub fn formatFloatDecimal(
676677 if (x == 0.0) {
677678 try output(context, "0");
678679
679 if (maybe_precision) |precision| {
680 if (options.precision) |precision| {
680681 if (precision != 0) {
681682 try output(context, ".");
682683 var i: usize = 0;
......@@ -697,7 +698,7 @@ pub fn formatFloatDecimal(
697698 var buffer: [32]u8 = undefined;
698699 var float_decimal = errol.errol3(x, buffer[0..]);
699700
700 if (maybe_precision) |precision| {
701 if (options.precision) |precision| {
701702 errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Decimal);
702703
703704 // exp < 0 means the leading is always 0 as errol result is normalized.
......@@ -799,7 +800,7 @@ pub fn formatFloatDecimal(
799800
800801pub fn formatBytes(
801802 value: var,
802 width: ?usize,
803 comptime options: FormatOptions,
803804 comptime radix: usize,
804805 context: var,
805806 comptime Errors: type,
......@@ -823,7 +824,7 @@ pub fn formatBytes(
823824 else => unreachable,
824825 };
825826
826 try formatFloatDecimal(new_value, width, context, Errors, output);
827 try formatFloatDecimal(new_value, options, context, Errors, output);
827828
828829 if (suffix == ' ') {
829830 return output(context, "B");
......@@ -841,7 +842,7 @@ pub fn formatInt(
841842 value: var,
842843 base: u8,
843844 uppercase: bool,
844 width: usize,
845 comptime options: FormatOptions,
845846 context: var,
846847 comptime Errors: type,
847848 output: fn (@typeOf(context), []const u8) Errors!void,
......@@ -853,9 +854,9 @@ pub fn formatInt(
853854 value;
854855
855856 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);
857858 } else {
858 return formatIntUnsigned(int_value, base, uppercase, width, context, Errors, output);
859 return formatIntUnsigned(int_value, base, uppercase, options, context, Errors, output);
859860 }
860861}
861862
......@@ -863,26 +864,30 @@ fn formatIntSigned(
863864 value: var,
864865 base: u8,
865866 uppercase: bool,
866 width: usize,
867 comptime options: FormatOptions,
867868 context: var,
868869 comptime Errors: type,
869870 output: fn (@typeOf(context), []const u8) Errors!void,
870871) 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
871878 const uint = @IntType(false, @typeOf(value).bit_count);
872879 if (value < 0) {
873880 const minus_sign: u8 = '-';
874881 try output(context, (*const [1]u8)(&minus_sign)[0..]);
875882 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);
880886 } else {
881887 const plus_sign: u8 = '+';
882888 try output(context, (*const [1]u8)(&plus_sign)[0..]);
883889 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);
886891 }
887892}
888893
......@@ -890,7 +895,7 @@ fn formatIntUnsigned(
890895 value: var,
891896 base: u8,
892897 uppercase: bool,
893 width: usize,
898 comptime options: FormatOptions,
894899 context: var,
895900 comptime Errors: type,
896901 output: fn (@typeOf(context), []const u8) Errors!void,
......@@ -911,31 +916,32 @@ fn formatIntUnsigned(
911916 }
912917
913918 const digits_buf = buf[index..];
919 const width = options.width orelse 0;
914920 const padding = if (width > digits_buf.len) (width - digits_buf.len) else 0;
915921
916922 if (padding > index) {
917 const zero_byte: u8 = '0';
923 const zero_byte: u8 = options.fill;
918924 var leftover_padding = padding - index;
919925 while (true) {
920926 try output(context, (*const [1]u8)(&zero_byte)[0..]);
921927 leftover_padding -= 1;
922928 if (leftover_padding == 0) break;
923929 }
924 mem.set(u8, buf[0..index], '0');
930 mem.set(u8, buf[0..index], options.fill);
925931 return output(context, buf);
926932 } else {
927933 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);
929935 return output(context, padded_buf);
930936 }
931937}
932938
933pub fn formatIntBuf(out_buf: []u8, value: var, base: u8, uppercase: bool, width: usize) usize {
939pub fn formatIntBuf(out_buf: []u8, value: var, base: u8, uppercase: bool, comptime options: FormatOptions) usize {
934940 var context = FormatIntBuf{
935941 .out_buf = out_buf,
936942 .index = 0,
937943 };
938 formatInt(value, base, uppercase, width, &context, error{}, formatIntCallback) catch unreachable;
944 formatInt(value, base, uppercase, options, &context, error{}, formatIntCallback) catch unreachable;
939945 return context.index;
940946}
941947const FormatIntBuf = struct {
......@@ -1078,23 +1084,23 @@ fn countSize(size: *usize, bytes: []const u8) (error{}!void) {
10781084test "bufPrintInt" {
10791085 var buffer: [100]u8 = undefined;
10801086 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"));
10851091
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"));
10871093
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"));
10911097
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"));
10941100}
10951101
1096fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, width: usize) []u8 {
1097 return buf[0..formatIntBuf(buf, value, base, uppercase, width)];
1102fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, comptime options: FormatOptions) []u8 {
1103 return buf[0..formatIntBuf(buf, value, base, uppercase, options)];
10981104}
10991105
11001106test "parse u64 digit too big" {
......@@ -1152,7 +1158,8 @@ test "int.specifier" {
11521158}
11531159
11541160test "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));
11561163}
11571164
11581165test "buffer" {
......@@ -1227,7 +1234,7 @@ test "cstr" {
12271234
12281235test "filesize" {
12291236 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));
12311238}
12321239
12331240test "struct" {
test/compare_output.zig+1-1
......@@ -124,7 +124,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
124124 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;
125125 \\ stdout.print("Hello, world!\n{d:4} {x:3} {c}\n", u32(12), u16(0x12), u8('a')) catch unreachable;
126126 \\}
127 , "Hello, world!\n0012 012 a\n");
127 , "Hello, world!\n 12 12 a\n");
128128
129129 cases.addC("number literals",
130130 \\const builtin = @import("builtin");
test/stage1/behavior/enum_with_members.zig+2-2
......@@ -8,8 +8,8 @@ const ET = union(enum) {
88
99 pub fn print(a: *const ET, buf: []u8) anyerror!usize {
1010 return switch (a.*) {
11 ET.SINT => |x| fmt.formatIntBuf(buf, x, 10, false, 0),
12 ET.UINT => |x| fmt.formatIntBuf(buf, x, 10, false, 0),
11 ET.SINT => |x| fmt.formatIntBuf(buf, x, 10, false, fmt.FormatOptions{}),
12 ET.UINT => |x| fmt.formatIntBuf(buf, x, 10, false, fmt.FormatOptions{}),
1313 };
1414 }
1515};