authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-05 10:33:02-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-03-05 10:33:02-05:00
logd7dc7d7a500541cf2ac0d3cfc28511b2fe7e88d4
tree6cc1c5a5571a6a593e86220c8dd877bd5318b308
parent378bf1c3b71fdcc823fb9a5382400ec0a659fc00
parente9c3b65bf4c7dc6c5fc07b9bcec195a5fe709db8
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4633 from daurnimator/4632-i1

Fix formatting of i1 values

2 files changed, 47 insertions(+), 41 deletions(-)

lib/std/fmt.zig+20-19
......@@ -943,19 +943,17 @@ fn formatIntSigned(
943943 .precision = options.precision,
944944 .fill = options.fill,
945945 };
946
947 const uint = std.meta.IntType(false, @TypeOf(value).bit_count);
946 const bit_count = @typeInfo(@TypeOf(value)).Int.bits;
947 const Uint = std.meta.IntType(false, bit_count);
948948 if (value < 0) {
949 const minus_sign: u8 = '-';
950 try output(context, @as(*const [1]u8, &minus_sign)[0..]);
951 const new_value = @intCast(uint, -(value + 1)) + 1;
949 try output(context, "-");
950 const new_value = math.absCast(value);
952951 return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output);
953952 } else if (options.width == null or options.width.? == 0) {
954 return formatIntUnsigned(@intCast(uint, value), base, uppercase, options, context, Errors, output);
953 return formatIntUnsigned(@intCast(Uint, value), base, uppercase, options, context, Errors, output);
955954 } else {
956 const plus_sign: u8 = '+';
957 try output(context, @as(*const [1]u8, &plus_sign)[0..]);
958 const new_value = @intCast(uint, value);
955 try output(context, "+");
956 const new_value = @intCast(Uint, value);
959957 return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output);
960958 }
961959}
......@@ -1165,19 +1163,22 @@ pub fn allocPrint0(allocator: *mem.Allocator, comptime fmt: []const u8, args: va
11651163test "bufPrintInt" {
11661164 var buffer: [100]u8 = undefined;
11671165 const buf = buffer[0..];
1168 std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 2, false, FormatOptions{}), "-101111000110000101001110"));
1169 std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 10, false, FormatOptions{}), "-12345678"));
1170 std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 16, false, FormatOptions{}), "-bc614e"));
1171 std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -12345678), 16, true, FormatOptions{}), "-BC614E"));
11721166
1173 std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 12345678), 10, true, FormatOptions{}), "12345678"));
1167 std.testing.expectEqualSlices(u8, "-1", bufPrintIntToSlice(buf, @as(i1, -1), 10, false, FormatOptions{}));
1168
1169 std.testing.expectEqualSlices(u8, "-101111000110000101001110", bufPrintIntToSlice(buf, @as(i32, -12345678), 2, false, FormatOptions{}));
1170 std.testing.expectEqualSlices(u8, "-12345678", bufPrintIntToSlice(buf, @as(i32, -12345678), 10, false, FormatOptions{}));
1171 std.testing.expectEqualSlices(u8, "-bc614e", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, false, FormatOptions{}));
1172 std.testing.expectEqualSlices(u8, "-BC614E", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, true, FormatOptions{}));
1173
1174 std.testing.expectEqualSlices(u8, "12345678", bufPrintIntToSlice(buf, @as(u32, 12345678), 10, true, FormatOptions{}));
11741175
1175 std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 666), 10, false, FormatOptions{ .width = 6 }), " 666"));
1176 std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 6 }), " 1234"));
1177 std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 1 }), "1234"));
1176 std.testing.expectEqualSlices(u8, " 666", bufPrintIntToSlice(buf, @as(u32, 666), 10, false, FormatOptions{ .width = 6 }));
1177 std.testing.expectEqualSlices(u8, " 1234", bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 6 }));
1178 std.testing.expectEqualSlices(u8, "1234", bufPrintIntToSlice(buf, @as(u32, 0x1234), 16, false, FormatOptions{ .width = 1 }));
11781179
1179 std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, 42), 10, false, FormatOptions{ .width = 3 }), "+42"));
1180 std.testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, @as(i32, -42), 10, false, FormatOptions{ .width = 3 }), "-42"));
1180 std.testing.expectEqualSlices(u8, "+42", bufPrintIntToSlice(buf, @as(i32, 42), 10, false, FormatOptions{ .width = 3 }));
1181 std.testing.expectEqualSlices(u8, "-42", bufPrintIntToSlice(buf, @as(i32, -42), 10, false, FormatOptions{ .width = 3 }));
11811182}
11821183
11831184fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, options: FormatOptions) []u8 {
lib/std/math.zig+27-22
......@@ -670,33 +670,38 @@ fn testRem() void {
670670
671671/// Returns the absolute value of the integer parameter.
672672/// Result is an unsigned integer.
673pub fn absCast(x: var) t: {
674 if (@TypeOf(x) == comptime_int) {
675 break :t comptime_int;
676 } else {
677 break :t std.meta.IntType(false, @TypeOf(x).bit_count);
673pub fn absCast(x: var) switch(@typeInfo(@TypeOf(x))) {
674 .ComptimeInt => comptime_int,
675 .Int => |intInfo| std.meta.IntType(false, intInfo.bits),
676 else => @compileError("absCast only accepts integers"),
678677 }
679} {
680 if (@TypeOf(x) == comptime_int) {
681 return if (x < 0) -x else x;
678{
679 switch(@typeInfo(@TypeOf(x))) {
680 .ComptimeInt => {
681 if (x < 0) {
682 return -x;
683 } else {
684 return x;
685 }
686 },
687 .Int => |intInfo| {
688 const Uint = std.meta.IntType(false, intInfo.bits);
689 if (x < 0) {
690 return ~@bitCast(Uint, x +% -1);
691 } else {
692 return @intCast(Uint, x);
693 }
694 },
695 else => unreachable,
682696 }
683 const uint = std.meta.IntType(false, @TypeOf(x).bit_count);
684 if (x >= 0) return @intCast(uint, x);
685
686 return @intCast(uint, -(x + 1)) + 1;
687697}
688698
689699test "math.absCast" {
690 testing.expect(absCast(@as(i32, -999)) == 999);
691 testing.expect(@TypeOf(absCast(@as(i32, -999))) == u32);
692
693 testing.expect(absCast(@as(i32, 999)) == 999);
694 testing.expect(@TypeOf(absCast(@as(i32, 999))) == u32);
695
696 testing.expect(absCast(@as(i32, minInt(i32))) == -minInt(i32));
697 testing.expect(@TypeOf(absCast(@as(i32, minInt(i32)))) == u32);
698
699 testing.expect(absCast(-999) == 999);
700 testing.expectEqual(@as(u1, 1), absCast(@as(i1, -1)));
701 testing.expectEqual(@as(u32, 999), absCast(@as(i32, -999)));
702 testing.expectEqual(@as(u32, 999), absCast(@as(i32, 999)));
703 testing.expectEqual(@as(u32, -minInt(i32)), absCast(@as(i32, minInt(i32))));
704 testing.expectEqual(999, absCast(-999));
700705}
701706
702707/// Returns the negation of the integer parameter.