Function format [src]

To allow std.fmt.format to work with this type. If the absolute value of integer is greater than or equal to pow(2, 64 * @sizeOf(usize) * 8), this function will fail to print the string, printing "(BigInt)" instead of a number. This is because the rendering algorithm requires reversing a string, which requires O(N) memory. See toString and toStringAlloc for a way to print big integers without failure.

Prototype

pub fn format( self: Const, comptime fmt: []const u8, options: std.fmt.FormatOptions, out_stream: anytype, ) !void

Parameters

self: Constfmt: []const u8options: std.fmt.FormatOptions

Source

pub fn format( self: Const, comptime fmt: []const u8, options: std.fmt.FormatOptions, out_stream: anytype, ) !void { _ = options; comptime var base = 10; comptime var case: std.fmt.Case = .lower; if (fmt.len == 0 or comptime mem.eql(u8, fmt, "d")) { base = 10; case = .lower; } else if (comptime mem.eql(u8, fmt, "b")) { base = 2; case = .lower; } else if (comptime mem.eql(u8, fmt, "x")) { base = 16; case = .lower; } else if (comptime mem.eql(u8, fmt, "X")) { base = 16; case = .upper; } else { std.fmt.invalidFmtError(fmt, self); } const available_len = 64; if (self.limbs.len > available_len) return out_stream.writeAll("(BigInt)"); var limbs: [calcToStringLimbsBufferLen(available_len, base)]Limb = undefined; const biggest: Const = .{ .limbs = &([1]Limb{comptime math.maxInt(Limb)} ** available_len), .positive = false, }; var buf: [biggest.sizeInBaseUpperBound(base)]u8 = undefined; const len = self.toString(&buf, base, case, &limbs); return out_stream.writeAll(buf[0..len]); }