| ... | @@ -943,19 +943,17 @@ fn formatIntSigned( | ... | @@ -943,19 +943,17 @@ fn formatIntSigned( |
| 943 | .precision = options.precision, | 943 | .precision = options.precision, |
| 944 | .fill = options.fill, | 944 | .fill = options.fill, |
| 945 | }; | 945 | }; |
| 946 | | 946 | const bit_count = @typeInfo(@TypeOf(value)).Int.bits; |
| 947 | const uint = std.meta.IntType(false, @TypeOf(value).bit_count); | 947 | const Uint = std.meta.IntType(false, bit_count); |
| 948 | if (value < 0) { | 948 | if (value < 0) { |
| 949 | const minus_sign: u8 = '-'; | 949 | try output(context, "-"); |
| 950 | try output(context, @as(*const [1]u8, &minus_sign)[0..]); | 950 | const new_value = ~@bitCast(Uint, value +% -1); |
| 951 | const new_value = @intCast(uint, -(value + 1)) + 1; | | |
| 952 | return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); | 951 | return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); |
| 953 | } else if (options.width == null or options.width.? == 0) { | 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 | } else { | 954 | } else { |
| 956 | const plus_sign: u8 = '+'; | 955 | try output(context, "+"); |
| 957 | try output(context, @as(*const [1]u8, &plus_sign)[0..]); | 956 | const new_value = @intCast(Uint, value); |
| 958 | const new_value = @intCast(uint, value); | | |
| 959 | return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); | 957 | return formatIntUnsigned(new_value, base, uppercase, new_options, context, Errors, output); |
| 960 | } | 958 | } |
| 961 | } | 959 | } |
| ... | @@ -1166,6 +1164,8 @@ test "bufPrintInt" { | ... | @@ -1166,6 +1164,8 @@ test "bufPrintInt" { |
| 1166 | var buffer: [100]u8 = undefined; | 1164 | var buffer: [100]u8 = undefined; |
| 1167 | const buf = buffer[0..]; | 1165 | const buf = buffer[0..]; |
| 1168 | | 1166 | |
| | 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{})); | 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{})); | 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{})); | 1171 | std.testing.expectEqualSlices(u8, "-bc614e", bufPrintIntToSlice(buf, @as(i32, -12345678), 16, false, FormatOptions{})); |