| ... | @@ -271,11 +271,7 @@ fn formatValue( | ... | @@ -271,11 +271,7 @@ fn formatValue( |
| 271 | const T = @typeOf(value); | 271 | const T = @typeOf(value); |
| 272 | switch (@typeId(T)) { | 272 | switch (@typeId(T)) { |
| 273 | builtin.TypeId.Float => return formatFloatValue(value, fmt, context, Errors, output), | 273 | builtin.TypeId.Float => return formatFloatValue(value, fmt, context, Errors, output), |
| 274 | builtin.TypeId.Int => return formatIntValue(value, fmt, context, Errors, output), | 274 | builtin.TypeId.Int, builtin.TypeId.ComptimeInt => return formatIntValue(value, fmt, context, Errors, output), |
| 275 | builtin.TypeId.ComptimeInt => { | | |
| 276 | const Int = math.IntFittingRange(value, value); | | |
| 277 | return formatIntValue(Int(value), fmt, context, Errors, output); | | |
| 278 | }, | | |
| 279 | else => comptime unreachable, | 275 | else => comptime unreachable, |
| 280 | } | 276 | } |
| 281 | } | 277 | } |
| ... | @@ -290,13 +286,20 @@ pub fn formatIntValue( | ... | @@ -290,13 +286,20 @@ pub fn formatIntValue( |
| 290 | comptime var radix = 10; | 286 | comptime var radix = 10; |
| 291 | comptime var uppercase = false; | 287 | comptime var uppercase = false; |
| 292 | comptime var width = 0; | 288 | comptime var width = 0; |
| | 289 | |
| | 290 | const int_value = if (@typeOf(value) == comptime_int) blk: { |
| | 291 | const Int = math.IntFittingRange(value, value); |
| | 292 | break :blk Int(value); |
| | 293 | } else |
| | 294 | value; |
| | 295 | |
| 293 | if (fmt.len > 0) { | 296 | if (fmt.len > 0) { |
| 294 | switch (fmt[0]) { | 297 | switch (fmt[0]) { |
| 295 | 'c' => { | 298 | 'c' => { |
| 296 | if (@typeOf(value).bit_count <= 8) { | 299 | if (@typeOf(int_value).bit_count <= 8) { |
| 297 | if (fmt.len > 1) | 300 | if (fmt.len > 1) |
| 298 | @compileError("Unknown format character: " ++ []u8{fmt[1]}); | 301 | @compileError("Unknown format character: " ++ []u8{fmt[1]}); |
| 299 | return formatAsciiChar(u8(value), context, Errors, output); | 302 | return formatAsciiChar(u8(int_value), context, Errors, output); |
| 300 | } | 303 | } |
| 301 | }, | 304 | }, |
| 302 | 'b' => { | 305 | 'b' => { |
| ... | @@ -323,7 +326,7 @@ pub fn formatIntValue( | ... | @@ -323,7 +326,7 @@ pub fn formatIntValue( |
| 323 | } | 326 | } |
| 324 | if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable); | 327 | if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable); |
| 325 | } | 328 | } |
| 326 | return formatInt(value, radix, uppercase, width, context, Errors, output); | 329 | return formatInt(int_value, radix, uppercase, width, context, Errors, output); |
| 327 | } | 330 | } |
| 328 | | 331 | |
| 329 | fn formatFloatValue( | 332 | fn formatFloatValue( |
| ... | @@ -686,10 +689,16 @@ pub fn formatInt( | ... | @@ -686,10 +689,16 @@ pub fn formatInt( |
| 686 | comptime Errors: type, | 689 | comptime Errors: type, |
| 687 | output: fn (@typeOf(context), []const u8) Errors!void, | 690 | output: fn (@typeOf(context), []const u8) Errors!void, |
| 688 | ) Errors!void { | 691 | ) Errors!void { |
| 689 | if (@typeOf(value).is_signed) { | 692 | const int_value = if (@typeOf(value) == comptime_int) blk: { |
| 690 | return formatIntSigned(value, base, uppercase, width, context, Errors, output); | 693 | const Int = math.IntFittingRange(value, value); |
| | 694 | break :blk Int(value); |
| | 695 | } else |
| | 696 | value; |
| | 697 | |
| | 698 | if (@typeOf(int_value).is_signed) { |
| | 699 | return formatIntSigned(int_value, base, uppercase, width, context, Errors, output); |
| 691 | } else { | 700 | } else { |
| 692 | return formatIntUnsigned(value, base, uppercase, width, context, Errors, output); | 701 | return formatIntUnsigned(int_value, base, uppercase, width, context, Errors, output); |
| 693 | } | 702 | } |
| 694 | } | 703 | } |
| 695 | | 704 | |
| ... | @@ -1432,3 +1441,11 @@ test "fmt.hexToBytes" { | ... | @@ -1432,3 +1441,11 @@ test "fmt.hexToBytes" { |
| 1432 | try hexToBytes(pb[0..], test_hex_str); | 1441 | try hexToBytes(pb[0..], test_hex_str); |
| 1433 | try testFmt(test_hex_str, "{X}", pb); | 1442 | try testFmt(test_hex_str, "{X}", pb); |
| 1434 | } | 1443 | } |
| | 1444 | |
| | 1445 | test "fmt.formatIntValue with comptime_int" { |
| | 1446 | const value: comptime_int = 123456789123456789; |
| | 1447 | |
| | 1448 | var buf = try std.Buffer.init(std.debug.global_allocator, ""); |
| | 1449 | try formatIntValue(value, "", &buf, @typeOf(std.Buffer.append).ReturnType.ErrorSet, std.Buffer.append); |
| | 1450 | assert(mem.eql(u8, buf.toSlice(), "123456789123456789")); |
| | 1451 | } |