| ... | ... | @@ -943,19 +943,17 @@ fn formatIntSigned( |
| 943 | 943 | .precision = options.precision, |
| 944 | 944 | .fill = options.fill, |
| 945 | 945 | }; |
| 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); |
| 948 | 948 | 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); |
| 952 | 951 | return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); |
| 953 | 952 | } 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); |
| 955 | 954 | } 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); |
| 959 | 957 | return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); |
| 960 | 958 | } |
| 961 | 959 | } |
| ... | ... | @@ -1165,19 +1163,22 @@ pub fn allocPrint0(allocator: *mem.Allocator, comptime fmt: []const u8, args: va |
| 1165 | 1163 | test "bufPrintInt" { |
| 1166 | 1164 | var buffer: [100]u8 = undefined; |
| 1167 | 1165 | 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")); |
| 1172 | 1166 | |
| 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{})); |
| 1174 | 1175 | |
| 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 })); |
| 1178 | 1179 | |
| 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 })); |
| 1181 | 1182 | } |
| 1182 | 1183 | |
| 1183 | 1184 | fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, options: FormatOptions) []u8 { |