std.math.big.Int: don't rely on the allocator when printing
This commit is contained in:
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
|
/// 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.
|
/// can be modified separately from the original.
|
||||||
pub fn clone(other: Int) !Int {
|
pub fn clone(other: Int) !Int {
|
||||||
other.assertWritable();
|
return other.clone2(other.allocator.?);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clone2(other: Int, allocator: *Allocator) !Int {
|
||||||
return Int{
|
return Int{
|
||||||
.allocator = other.allocator,
|
.allocator = allocator,
|
||||||
.metadata = other.metadata,
|
.metadata = other.metadata,
|
||||||
.limbs = block: {
|
.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()]);
|
mem.copy(Limb, limbs[0..], other.limbs[0..other.len()]);
|
||||||
break :block limbs;
|
break :block limbs;
|
||||||
},
|
},
|
||||||
@ -470,8 +473,8 @@ pub const Int = struct {
|
|||||||
break;
|
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));
|
const digits_per_limb = math.log(Limb, base, maxInt(Limb));
|
||||||
var limb_base: Limb = 1;
|
var limb_base: Limb = 1;
|
||||||
var j: usize = 0;
|
var j: usize = 0;
|
||||||
@ -479,7 +482,7 @@ pub const Int = struct {
|
|||||||
limb_base *= base;
|
limb_base *= base;
|
||||||
}
|
}
|
||||||
|
|
||||||
var q = try self.clone();
|
var q = try self.clone2(allocator);
|
||||||
defer q.deinit();
|
defer q.deinit();
|
||||||
q.abs();
|
q.abs();
|
||||||
var r = try Int.init(allocator);
|
var r = try Int.init(allocator);
|
||||||
@ -522,15 +525,13 @@ pub const Int = struct {
|
|||||||
|
|
||||||
/// To allow `std.fmt.printf` to work with Int.
|
/// To allow `std.fmt.printf` to work with Int.
|
||||||
/// TODO make this non-allocating
|
/// TODO make this non-allocating
|
||||||
|
/// TODO support read-only fixed integers
|
||||||
pub fn format(
|
pub fn format(
|
||||||
self: Int,
|
self: Int,
|
||||||
comptime fmt: []const u8,
|
comptime fmt: []const u8,
|
||||||
options: std.fmt.FormatOptions,
|
options: std.fmt.FormatOptions,
|
||||||
out_stream: var,
|
out_stream: var,
|
||||||
) !void {
|
) !void {
|
||||||
self.assertWritable();
|
|
||||||
// TODO support read-only fixed integers
|
|
||||||
|
|
||||||
comptime var radix = 10;
|
comptime var radix = 10;
|
||||||
comptime var uppercase = false;
|
comptime var uppercase = false;
|
||||||
|
|
||||||
@ -550,8 +551,9 @@ pub const Int = struct {
|
|||||||
@compileError("Unknown format string: '" ++ fmt ++ "'");
|
@compileError("Unknown format string: '" ++ fmt ++ "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
const str = self.toString(self.allocator.?, radix, uppercase) catch @panic("TODO make this non allocating");
|
var buf: [4096]u8 = undefined;
|
||||||
defer self.allocator.?.free(str);
|
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);
|
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);
|
var new_zir_module = try text.emit_zir(allocator, analyzed_module);
|
||||||
defer new_zir_module.deinit(allocator);
|
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 } {
|
fn findLineColumn(source: []const u8, byte_offset: usize) struct { line: usize, column: usize } {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user