authorgravatar for shritesh@shritesh.comShritesh Bhattarai <shritesh@shritesh.com> 2019-04-04 20:36:31-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-05 11:11:04-04:00
log6cc2d3938e81f1c5db9cf94db90fda11b026422a
tree8d4171fa74513793d9d5200ad60616f216182ae6
parent3cce56af99657e85f50b0fd3d7bdb539794cef59

support comptime_int in formatInt{,Value}


1 files changed, 28 insertions(+), 11 deletions(-)

std/fmt.zig+28-11
......@@ -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
329332fn 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
1445test "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}