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

see #258
This commit is contained in:
Andrew Kelley 2017-02-12 17:45:55 -05:00
parent 6dba1f1c8e
commit b61f1a9f7d

View File

@ -512,17 +512,19 @@ pub fn bufPrintInt(out_buf: []u8, x: var, base: u8, uppercase: bool, width: usiz
fn bufPrintSigned(out_buf: []u8, x: var, base: u8, uppercase: bool, width: usize) -> usize {
const uint = @intType(false, @typeOf(x).bit_count);
// include the sign in the width
const new_width = if (width == 0) 0 else (width - 1);
var new_value: uint = undefined;
if (x < 0) {
out_buf[0] = '-';
new_value = uint(-(x + 1)) + 1;
const new_value = uint(-(x + 1)) + 1;
const new_width = if (width == 0) 0 else (width - 1);
return 1 + bufPrintUnsigned(out_buf[1...], new_value, base, uppercase, new_width);
} else if (width == 0) {
return bufPrintUnsigned(out_buf, uint(x), base, uppercase, width);
} else {
out_buf[0] = '+';
new_value = uint(x);
const new_value = uint(x);
const new_width = if (width == 0) 0 else (width - 1);
return 1 + bufPrintUnsigned(out_buf[1...], new_value, base, uppercase, new_width);
}
return 1 + bufPrintUnsigned(out_buf[1...], new_value, base, uppercase, new_width);
}
fn bufPrintUnsigned(out_buf: []u8, x: var, base: u8, uppercase: bool, width: usize) -> usize {