From 9149a519f5f1bbe0ad821fb1a6a408663fd3741d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 18 Jun 2019 14:47:52 +0200 Subject: [PATCH] printf: Support printing negative values with %f or %g. --- AK/PrintfImplementation.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/AK/PrintfImplementation.h b/AK/PrintfImplementation.h index a115f78c19..091b5b4334 100644 --- a/AK/PrintfImplementation.h +++ b/AK/PrintfImplementation.h @@ -106,6 +106,16 @@ template return fieldWidth; } +template +[[gnu::always_inline]] inline int print_signed_qword(PutChFunc putch, char*& bufptr, signed_qword number, bool leftPad, bool zeroPad, dword fieldWidth) +{ + if (number < 0) { + putch(bufptr, '-'); + return print_qword(putch, bufptr, 0 - number, leftPad, zeroPad, fieldWidth) + 1; + } + return print_qword(putch, bufptr, number, leftPad, zeroPad, fieldWidth); +} + template [[gnu::always_inline]] inline int print_octal_number(PutChFunc putch, char*& bufptr, dword number, bool leftPad, bool zeroPad, dword fieldWidth) { @@ -246,7 +256,7 @@ template case 'g': case 'f': // FIXME: Print as float! - ret += print_number(putch, bufptr, (int)va_arg(ap, double), leftPad, zeroPad, fieldWidth); + ret += print_signed_qword(putch, bufptr, (qword)va_arg(ap, double), leftPad, zeroPad, fieldWidth); break; #endif