diff options
| author | Travis McDemus <travis@mcdem.us> | 2016-05-14 23:45:08 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2016-05-15 01:41:15 -0700 |
| commit | 7b0052abbb0919339315be7ae4eb293ef83566cd (patch) | |
| tree | 916668c731245fe7ba0ada16c9a1b12f1e02428a | |
| parent | 9813ae8586b2c33ebcb7f3f522665675b2901701 (diff) | |
| download | zig-7b0052abbb0919339315be7ae4eb293ef83566cd.tar.gz zig-7b0052abbb0919339315be7ae4eb293ef83566cd.zip | |
Add unsigned and signed generic print fns
Signed-off-by: Andrew Kelley <superjoe30@gmail.com>
| -rw-r--r-- | std/io.zig | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/std/io.zig b/std/io.zig index be4320ad31..432600ee04 100644 --- a/std/io.zig +++ b/std/io.zig @@ -248,16 +248,19 @@ fn char_to_digit(c: u8) -> u8 { } } -pub fn buf_print_i64(out_buf: []u8, x: i64) -> isize { +pub fn buf_print_signed(T: type)(out_buf: []u8, x: T) -> isize { + const uint = @int_type(false, T.bit_count, false); if (x < 0) { out_buf[0] = '-'; - return 1 + buf_print_u64(out_buf[1...], u64(-(x + 1)) + 1); + return 1 + buf_print_unsigned(uint)(out_buf[1...], uint(-(x + 1)) + 1); } else { - return buf_print_u64(out_buf, u64(x)); + return buf_print_unsigned(uint)(out_buf, uint(x)); } } -pub fn buf_print_u64(out_buf: []u8, x: u64) -> isize { +pub const buf_print_i64 = buf_print_signed(i64); + +pub fn buf_print_unsigned(T: type)(out_buf: []u8, x: T) -> isize { var buf: [max_u64_base10_digits]u8 = undefined; var a = x; var index: isize = buf.len; @@ -278,6 +281,8 @@ pub fn buf_print_u64(out_buf: []u8, x: u64) -> isize { return len; } +pub const buf_print_u64 = buf_print_unsigned(u64); + pub fn buf_print_f64(out_buf: []u8, x: f64, decimals: isize) -> isize { const numExpBits = 11; const numRawSigBits = 52; // not including implicit 1 bit |
