Skip to content

Commit 6cc2d39

Browse files
shriteshandrewrk
authored andcommitted
support comptime_int in formatInt{,Value}
1 parent 3cce56a commit 6cc2d39

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

std/fmt.zig

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,7 @@ fn formatValue(
271271
const T = @typeOf(value);
272272
switch (@typeId(T)) {
273273
builtin.TypeId.Float => return formatFloatValue(value, fmt, context, Errors, output),
274-
builtin.TypeId.Int => 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-
},
274+
builtin.TypeId.Int, builtin.TypeId.ComptimeInt => return formatIntValue(value, fmt, context, Errors, output),
279275
else => comptime unreachable,
280276
}
281277
}
@@ -290,13 +286,20 @@ pub fn formatIntValue(
290286
comptime var radix = 10;
291287
comptime var uppercase = false;
292288
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+
293296
if (fmt.len > 0) {
294297
switch (fmt[0]) {
295298
'c' => {
296-
if (@typeOf(value).bit_count <= 8) {
299+
if (@typeOf(int_value).bit_count <= 8) {
297300
if (fmt.len > 1)
298301
@compileError("Unknown format character: " ++ []u8{fmt[1]});
299-
return formatAsciiChar(u8(value), context, Errors, output);
302+
return formatAsciiChar(u8(int_value), context, Errors, output);
300303
}
301304
},
302305
'b' => {
@@ -323,7 +326,7 @@ pub fn formatIntValue(
323326
}
324327
if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable);
325328
}
326-
return formatInt(value, radix, uppercase, width, context, Errors, output);
329+
return formatInt(int_value, radix, uppercase, width, context, Errors, output);
327330
}
328331

329332
fn formatFloatValue(
@@ -686,10 +689,16 @@ pub fn formatInt(
686689
comptime Errors: type,
687690
output: fn (@typeOf(context), []const u8) Errors!void,
688691
) Errors!void {
689-
if (@typeOf(value).is_signed) {
690-
return formatIntSigned(value, base, uppercase, width, context, Errors, output);
692+
const int_value = if (@typeOf(value) == comptime_int) blk: {
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);
691700
} else {
692-
return formatIntUnsigned(value, base, uppercase, width, context, Errors, output);
701+
return formatIntUnsigned(int_value, base, uppercase, width, context, Errors, output);
693702
}
694703
}
695704

@@ -1432,3 +1441,11 @@ test "fmt.hexToBytes" {
14321441
try hexToBytes(pb[0..], test_hex_str);
14331442
try testFmt(test_hex_str, "{X}", pb);
14341443
}
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+
}

0 commit comments

Comments
 (0)