zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 7b0052abbb0919339315be7ae4eb293ef83566cd (tree)
parent 9813ae8586b2c33ebcb7f3f522665675b2901701
Author: Travis McDemus <travis@mcdem.us>
Date:   Sat, 14 May 2016 23:45:08 -0400

Add unsigned and signed generic print fns

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

Diffstat:
Mstd/io.zig | 13+++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)

diff --git 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