authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-12 17:45:55-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-12 17:46:28-05:00
logb61f1a9f7d06a352239a95cf42f2116f5bd44f4a
tree3d1dcacbe44e0a9262499685f52c3b5d7e1a31d9
parent6dba1f1c8eee5e2f037c7ef216bc64423aef8e00

printf: only include + sign on signed ints if width specified

see #258

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

std/io.zig+8-6
...@@ -512,17 +512,19 @@ pub fn bufPrintInt(out_buf: []u8, x: var, base: u8, uppercase: bool, width: usiz...@@ -512,17 +512,19 @@ pub fn bufPrintInt(out_buf: []u8, x: var, base: u8, uppercase: bool, width: usiz
512512
513fn bufPrintSigned(out_buf: []u8, x: var, base: u8, uppercase: bool, width: usize) -> usize {513fn bufPrintSigned(out_buf: []u8, x: var, base: u8, uppercase: bool, width: usize) -> usize {
514 const uint = @intType(false, @typeOf(x).bit_count);514 const uint = @intType(false, @typeOf(x).bit_count);
515 // include the sign in the width
516 const new_width = if (width == 0) 0 else (width - 1);
517 var new_value: uint = undefined;
518 if (x < 0) {515 if (x < 0) {
519 out_buf[0] = '-';516 out_buf[0] = '-';
520 new_value = uint(-(x + 1)) + 1;517 const new_value = uint(-(x + 1)) + 1;
518 const new_width = if (width == 0) 0 else (width - 1);
519 return 1 + bufPrintUnsigned(out_buf[1...], new_value, base, uppercase, new_width);
520 } else if (width == 0) {
521 return bufPrintUnsigned(out_buf, uint(x), base, uppercase, width);
521 } else {522 } else {
522 out_buf[0] = '+';523 out_buf[0] = '+';
523 new_value = uint(x);524 const new_value = uint(x);
525 const new_width = if (width == 0) 0 else (width - 1);
526 return 1 + bufPrintUnsigned(out_buf[1...], new_value, base, uppercase, new_width);
524 }527 }
525 return 1 + bufPrintUnsigned(out_buf[1...], new_value, base, uppercase, new_width);
526}528}
527529
528fn bufPrintUnsigned(out_buf: []u8, x: var, base: u8, uppercase: bool, width: usize) -> usize {530fn bufPrintUnsigned(out_buf: []u8, x: var, base: u8, uppercase: bool, width: usize) -> usize {