Function printInt [src]
Prototype
pub inline fn printInt( w: *Writer, value: anytype, base: u8, case: std.fmt.Case, options: std.fmt.Options, ) Error!void
Parameters
w: *Writer
base: u8
case: std.fmt.Case
options: std.fmt.Options
Possible Errors
See the Writer
implementation for detailed diagnostics.
Example
test printInt {
try testPrintIntCase("-1", @as(i1, -1), 10, .lower, .{});
try testPrintIntCase("-101111000110000101001110", @as(i32, -12345678), 2, .lower, .{});
try testPrintIntCase("-12345678", @as(i32, -12345678), 10, .lower, .{});
try testPrintIntCase("-bc614e", @as(i32, -12345678), 16, .lower, .{});
try testPrintIntCase("-BC614E", @as(i32, -12345678), 16, .upper, .{});
try testPrintIntCase("12345678", @as(u32, 12345678), 10, .upper, .{});
try testPrintIntCase(" 666", @as(u32, 666), 10, .lower, .{ .width = 6 });
try testPrintIntCase(" 1234", @as(u32, 0x1234), 16, .lower, .{ .width = 6 });
try testPrintIntCase("1234", @as(u32, 0x1234), 16, .lower, .{ .width = 1 });
try testPrintIntCase("+42", @as(i32, 42), 10, .lower, .{ .width = 3 });
try testPrintIntCase("-42", @as(i32, -42), 10, .lower, .{ .width = 3 });
try testPrintIntCase("123456789123456789", @as(comptime_int, 123456789123456789), 10, .lower, .{});
}
Source
pub inline fn printInt(
w: *Writer,
value: anytype,
base: u8,
case: std.fmt.Case,
options: std.fmt.Options,
) Error!void {
switch (@TypeOf(value)) {
isize, usize => {},
comptime_int => {
if (comptime std.math.cast(usize, value)) |x| return printIntAny(w, x, base, case, options);
if (comptime std.math.cast(isize, value)) |x| return printIntAny(w, x, base, case, options);
const Int = std.math.IntFittingRange(value, value);
return printIntAny(w, @as(Int, value), base, case, options);
},
else => switch (@typeInfo(@TypeOf(value)).int.signedness) {
.signed => if (std.math.cast(isize, value)) |x| return printIntAny(w, x, base, case, options),
.unsigned => if (std.math.cast(usize, value)) |x| return printIntAny(w, x, base, case, options),
},
}
return printIntAny(w, value, base, case, options);
}