authorgravatar for travis@mcdem.usTravis McDemus <travis@mcdem.us> 2016-05-14 23:45:08-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-15 01:41:15-07:00
log7b0052abbb0919339315be7ae4eb293ef83566cd
tree916668c731245fe7ba0ada16c9a1b12f1e02428a
parent9813ae8586b2c33ebcb7f3f522665675b2901701

Add unsigned and signed generic print fns

Signed-off-by: Andrew Kelley <superjoe30@gmail.com>

1 files changed, 9 insertions(+), 4 deletions(-)

std/io.zig+9-4
...@@ -248,16 +248,19 @@ fn char_to_digit(c: u8) -> u8 {...@@ -248,16 +248,19 @@ fn char_to_digit(c: u8) -> u8 {
248 }248 }
249}249}
250250
251pub fn buf_print_i64(out_buf: []u8, x: i64) -> isize {251pub fn buf_print_signed(T: type)(out_buf: []u8, x: T) -> isize {
252 const uint = @int_type(false, T.bit_count, false);
252 if (x < 0) {253 if (x < 0) {
253 out_buf[0] = '-';254 out_buf[0] = '-';
254 return 1 + buf_print_u64(out_buf[1...], u64(-(x + 1)) + 1);255 return 1 + buf_print_unsigned(uint)(out_buf[1...], uint(-(x + 1)) + 1);
255 } else {256 } else {
256 return buf_print_u64(out_buf, u64(x));257 return buf_print_unsigned(uint)(out_buf, uint(x));
257 }258 }
258}259}
259260
260pub fn buf_print_u64(out_buf: []u8, x: u64) -> isize {261pub const buf_print_i64 = buf_print_signed(i64);
262
263pub fn buf_print_unsigned(T: type)(out_buf: []u8, x: T) -> isize {
261 var buf: [max_u64_base10_digits]u8 = undefined;264 var buf: [max_u64_base10_digits]u8 = undefined;
262 var a = x;265 var a = x;
263 var index: isize = buf.len;266 var index: isize = buf.len;
...@@ -278,6 +281,8 @@ pub fn buf_print_u64(out_buf: []u8, x: u64) -> isize {...@@ -278,6 +281,8 @@ pub fn buf_print_u64(out_buf: []u8, x: u64) -> isize {
278 return len;281 return len;
279}282}
280283
284pub const buf_print_u64 = buf_print_unsigned(u64);
285
281pub fn buf_print_f64(out_buf: []u8, x: f64, decimals: isize) -> isize {286pub fn buf_print_f64(out_buf: []u8, x: f64, decimals: isize) -> isize {
282 const numExpBits = 11;287 const numExpBits = 11;
283 const numRawSigBits = 52; // not including implicit 1 bit288 const numRawSigBits = 52; // not including implicit 1 bit