Function formatIntValue [src]

Prototype

pub fn formatIntValue( value: anytype, comptime fmt: []const u8, options: FormatOptions, writer: anytype, ) !void

Parameters

fmt: []const u8options: FormatOptions

Source

pub fn formatIntValue( value: anytype, comptime fmt: []const u8, options: FormatOptions, writer: anytype, ) !void { comptime var base = 10; comptime var case: Case = .lower; const int_value = if (@TypeOf(value) == comptime_int) blk: { const Int = math.IntFittingRange(value, value); break :blk @as(Int, value); } else value; if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "d")) { base = 10; case = .lower; } else if (comptime std.mem.eql(u8, fmt, "c")) { if (@typeInfo(@TypeOf(int_value)).int.bits <= 8) { return formatAsciiChar(@as(u8, int_value), options, writer); } else { @compileError("cannot print integer that is larger than 8 bits as an ASCII character"); } } else if (comptime std.mem.eql(u8, fmt, "u")) { if (@typeInfo(@TypeOf(int_value)).int.bits <= 21) { return formatUnicodeCodepoint(@as(u21, int_value), options, writer); } else { @compileError("cannot print integer that is larger than 21 bits as an UTF-8 sequence"); } } else if (comptime std.mem.eql(u8, fmt, "b")) { base = 2; case = .lower; } else if (comptime std.mem.eql(u8, fmt, "x")) { base = 16; case = .lower; } else if (comptime std.mem.eql(u8, fmt, "X")) { base = 16; case = .upper; } else if (comptime std.mem.eql(u8, fmt, "o")) { base = 8; case = .lower; } else { invalidFmtError(fmt, value); } return formatInt(int_value, base, case, options, writer); }