std.math.big.Int: don't rely on the allocator when printing
parent
d58233b361
commit
1eda2ada9a
|
@ -143,12 +143,15 @@ pub const Int = struct {
|
|||
/// Clones an Int and returns a new Int with the same value. The new Int is a deep copy and
|
||||
/// can be modified separately from the original.
|
||||
pub fn clone(other: Int) !Int {
|
||||
other.assertWritable();
|
||||
return other.clone2(other.allocator.?);
|
||||
}
|
||||
|
||||
pub fn clone2(other: Int, allocator: *Allocator) !Int {
|
||||
return Int{
|
||||
.allocator = other.allocator,
|
||||
.allocator = allocator,
|
||||
.metadata = other.metadata,
|
||||
.limbs = block: {
|
||||
var limbs = try other.allocator.?.alloc(Limb, other.len());
|
||||
var limbs = try allocator.alloc(Limb, other.len());
|
||||
mem.copy(Limb, limbs[0..], other.limbs[0..other.len()]);
|
||||
break :block limbs;
|
||||
},
|
||||
|
@ -470,8 +473,8 @@ pub const Int = struct {
|
|||
break;
|
||||
}
|
||||
}
|
||||
} // Non power-of-two: batch divisions per word size.
|
||||
else {
|
||||
} else {
|
||||
// Non power-of-two: batch divisions per word size.
|
||||
const digits_per_limb = math.log(Limb, base, maxInt(Limb));
|
||||
var limb_base: Limb = 1;
|
||||
var j: usize = 0;
|
||||
|
@ -479,7 +482,7 @@ pub const Int = struct {
|
|||
limb_base *= base;
|
||||
}
|
||||
|
||||
var q = try self.clone();
|
||||
var q = try self.clone2(allocator);
|
||||
defer q.deinit();
|
||||
q.abs();
|
||||
var r = try Int.init(allocator);
|
||||
|
@ -522,15 +525,13 @@ pub const Int = struct {
|
|||
|
||||
/// To allow `std.fmt.printf` to work with Int.
|
||||
/// TODO make this non-allocating
|
||||
/// TODO support read-only fixed integers
|
||||
pub fn format(
|
||||
self: Int,
|
||||
comptime fmt: []const u8,
|
||||
options: std.fmt.FormatOptions,
|
||||
out_stream: var,
|
||||
) !void {
|
||||
self.assertWritable();
|
||||
// TODO support read-only fixed integers
|
||||
|
||||
comptime var radix = 10;
|
||||
comptime var uppercase = false;
|
||||
|
||||
|
@ -550,8 +551,9 @@ pub const Int = struct {
|
|||
@compileError("Unknown format string: '" ++ fmt ++ "'");
|
||||
}
|
||||
|
||||
const str = self.toString(self.allocator.?, radix, uppercase) catch @panic("TODO make this non allocating");
|
||||
defer self.allocator.?.free(str);
|
||||
var buf: [4096]u8 = undefined;
|
||||
var fba = std.heap.FixedBufferAllocator.init(&buf);
|
||||
const str = self.toString(&fba.allocator, radix, uppercase) catch @panic("TODO make this non allocating");
|
||||
return out_stream.writeAll(str);
|
||||
}
|
||||
|
||||
|
|
|
@ -714,7 +714,9 @@ pub fn main() anyerror!void {
|
|||
var new_zir_module = try text.emit_zir(allocator, analyzed_module);
|
||||
defer new_zir_module.deinit(allocator);
|
||||
|
||||
new_zir_module.dump();
|
||||
var bos = std.io.bufferedOutStream(std.io.getStdOut().outStream());
|
||||
try new_zir_module.writeToStream(allocator, bos.outStream());
|
||||
try bos.flush();
|
||||
}
|
||||
|
||||
fn findLineColumn(source: []const u8, byte_offset: usize) struct { line: usize, column: usize } {
|
||||
|
|
Loading…
Reference in New Issue