Add unsigned and signed generic print fns

Signed-off-by: Andrew Kelley <superjoe30@gmail.com>
This commit is contained in:
Travis McDemus 2016-05-14 23:45:08 -04:00 committed by Andrew Kelley
parent 9813ae8586
commit 7b0052abbb

View File

@ -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