authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-19 10:11:28-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-19 10:11:28-04:00
logaf9ac0d548a331fad3a8226dc627ebb66b92b0fa
tree0deedf4e862066ab84c07fdbfbff64ccb8f39445
parente508b85746391b724e2a3b8e166cea06d75fdae0
signaturelock-open Commit is signed but in an unrecognized format.

better buffer length for formatIntUnsigned

see #1358

1 files changed, 3 insertions(+), 6 deletions(-)

std/fmt.zig+3-6
...@@ -8,8 +8,6 @@ const builtin = @import("builtin");...@@ -8,8 +8,6 @@ const builtin = @import("builtin");
8const errol = @import("fmt/errol.zig");8const errol = @import("fmt/errol.zig");
9const lossyCast = std.math.lossyCast;9const lossyCast = std.math.lossyCast;
1010
11const max_int_digits = 65;
12
13/// Renders fmt string with args, calling output with slices of bytes.11/// Renders fmt string with args, calling output with slices of bytes.
14/// If `output` returns an error, the error is returned from `format` and12/// If `output` returns an error, the error is returned from `format` and
15/// `output` is not called again.13/// `output` is not called again.
...@@ -731,9 +729,8 @@ fn formatIntUnsigned(...@@ -731,9 +729,8 @@ fn formatIntUnsigned(
731 comptime Errors: type,729 comptime Errors: type,
732 output: fn (@typeOf(context), []const u8) Errors!void,730 output: fn (@typeOf(context), []const u8) Errors!void,
733) Errors!void {731) Errors!void {
734 // max_int_digits accounts for the minus sign. when printing an unsigned732 assert(base >= 2);
735 // number we don't need to do that.733 var buf: [math.max(@typeOf(value).bit_count, 1)]u8 = undefined;
736 var buf: [max_int_digits - 1]u8 = undefined;
737 const min_int_bits = comptime math.max(@typeOf(value).bit_count, @typeOf(base).bit_count);734 const min_int_bits = comptime math.max(@typeOf(value).bit_count, @typeOf(base).bit_count);
738 const MinInt = @IntType(@typeOf(value).is_signed, min_int_bits);735 const MinInt = @IntType(@typeOf(value).is_signed, min_int_bits);
739 var a: MinInt = value;736 var a: MinInt = value;
...@@ -913,7 +910,7 @@ fn countSize(size: *usize, bytes: []const u8) (error{}!void) {...@@ -913,7 +910,7 @@ fn countSize(size: *usize, bytes: []const u8) (error{}!void) {
913}910}
914911
915test "buf print int" {912test "buf print int" {
916 var buffer: [max_int_digits]u8 = undefined;913 var buffer: [100]u8 = undefined;
917 const buf = buffer[0..];914 const buf = buffer[0..];
918 testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110"));915 testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110"));
919 testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, 0), "-12345678"));916 testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, 0), "-12345678"));