1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:48:11 +00:00

AK: Support '+' qualifier in printf() to force sign for positive %d's

This commit is contained in:
Andreas Kling 2020-01-19 11:00:02 +01:00
parent 39b3c0ef7e
commit 109727082c

View file

@ -248,12 +248,14 @@ template<typename PutChFunc>
} }
template<typename PutChFunc> template<typename PutChFunc>
[[gnu::always_inline]] inline int print_signed_number(PutChFunc putch, char*& bufptr, int number, bool leftPad, bool zeroPad, u32 fieldWidth) [[gnu::always_inline]] inline int print_signed_number(PutChFunc putch, char*& bufptr, int number, bool leftPad, bool zeroPad, u32 fieldWidth, bool always_sign)
{ {
if (number < 0) { if (number < 0) {
putch(bufptr, '-'); putch(bufptr, '-');
return print_number(putch, bufptr, 0 - number, leftPad, zeroPad, fieldWidth) + 1; return print_number(putch, bufptr, 0 - number, leftPad, zeroPad, fieldWidth) + 1;
} }
if (always_sign)
putch(bufptr, '+');
return print_number(putch, bufptr, number, leftPad, zeroPad, fieldWidth); return print_number(putch, bufptr, number, leftPad, zeroPad, fieldWidth);
} }
@ -273,6 +275,7 @@ template<typename PutChFunc>
bool size_qualifier = false; bool size_qualifier = false;
(void)size_qualifier; (void)size_qualifier;
bool alternate_form = 0; bool alternate_form = 0;
bool always_sign = false;
if (*p == '%' && *(p + 1)) { if (*p == '%' && *(p + 1)) {
one_more: one_more:
++p; ++p;
@ -284,6 +287,11 @@ template<typename PutChFunc>
if (*(p + 1)) if (*(p + 1))
goto one_more; goto one_more;
} }
if (*p == '+') {
always_sign = true;
if (*(p + 1))
goto one_more;
}
if (!zeroPad && !fieldWidth && *p == '0') { if (!zeroPad && !fieldWidth && *p == '0') {
zeroPad = true; zeroPad = true;
if (*(p + 1)) if (*(p + 1))
@ -323,7 +331,7 @@ template<typename PutChFunc>
case 'd': case 'd':
case 'i': case 'i':
ret += print_signed_number(putch, bufptr, va_arg(ap, int), left_pad, zeroPad, fieldWidth); ret += print_signed_number(putch, bufptr, va_arg(ap, int), left_pad, zeroPad, fieldWidth, always_sign);
break; break;
case 'u': case 'u':