Skip to content

Commit af9ac0d

Browse files
committed
better buffer length for formatIntUnsigned
see #1358
1 parent e508b85 commit af9ac0d

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

std/fmt.zig

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ const builtin = @import("builtin");
88
const errol = @import("fmt/errol.zig");
99
const lossyCast = std.math.lossyCast;
1010

11-
const max_int_digits = 65;
12-
1311
/// Renders fmt string with args, calling output with slices of bytes.
1412
/// If `output` returns an error, the error is returned from `format` and
1513
/// `output` is not called again.
@@ -731,9 +729,8 @@ fn formatIntUnsigned(
731729
comptime Errors: type,
732730
output: fn (@typeOf(context), []const u8) Errors!void,
733731
) Errors!void {
734-
// max_int_digits accounts for the minus sign. when printing an unsigned
735-
// number we don't need to do that.
736-
var buf: [max_int_digits - 1]u8 = undefined;
732+
assert(base >= 2);
733+
var buf: [math.max(@typeOf(value).bit_count, 1)]u8 = undefined;
737734
const min_int_bits = comptime math.max(@typeOf(value).bit_count, @typeOf(base).bit_count);
738735
const MinInt = @IntType(@typeOf(value).is_signed, min_int_bits);
739736
var a: MinInt = value;
@@ -913,7 +910,7 @@ fn countSize(size: *usize, bytes: []const u8) (error{}!void) {
913910
}
914911

915912
test "buf print int" {
916-
var buffer: [max_int_digits]u8 = undefined;
913+
var buffer: [100]u8 = undefined;
917914
const buf = buffer[0..];
918915
testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110"));
919916
testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, 0), "-12345678"));

0 commit comments

Comments
 (0)