From 151467b569eb4c0e98723ded9b3b6474b809a965 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 19 Feb 2020 22:07:37 +0100 Subject: [PATCH] AK: Support "%.*s" in format strings Work towards #623. --- AK/PrintfImplementation.h | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/AK/PrintfImplementation.h b/AK/PrintfImplementation.h index e7819733f3c..ae2633cf7ae 100644 --- a/AK/PrintfImplementation.h +++ b/AK/PrintfImplementation.h @@ -228,23 +228,23 @@ template } template -[[gnu::always_inline]] inline int print_string(PutChFunc putch, char*& bufptr, const char* str, bool leftPad, u32 fieldWidth) +[[gnu::always_inline]] inline int print_string(PutChFunc putch, char*& bufptr, const char* str, bool leftPad, u32 field_width, bool dot) { size_t len = strlen(str); - if (!fieldWidth || fieldWidth < len) - fieldWidth = len; - if (!leftPad) { - for (unsigned i = 0; i < fieldWidth - len; ++i) + if (!dot && (!field_width || field_width < len)) + field_width = len; + if (!leftPad && field_width > len) { + for (unsigned i = 0; i < field_width - len; ++i) putch(bufptr, ' '); } - for (unsigned i = 0; i < len; ++i) { + for (unsigned i = 0; i < field_width; ++i) { putch(bufptr, str[i]); } - if (leftPad) { - for (unsigned i = 0; i < fieldWidth - len; ++i) + if (leftPad && field_width > len) { + for (unsigned i = 0; i < field_width - len; ++i) putch(bufptr, ' '); } - return fieldWidth; + return field_width; } template @@ -270,6 +270,7 @@ template for (p = fmt; *p; ++p) { bool left_pad = false; bool zeroPad = false; + bool dot = false; unsigned fieldWidth = 0; unsigned long_qualifiers = 0; bool size_qualifier = false; @@ -279,9 +280,11 @@ template if (*p == '%' && *(p + 1)) { one_more: ++p; - // FIXME: This is just a hack workaround to prevent choking on '.' specifiers - if (*p == '.') - goto one_more; + if (*p == '.') { + dot = true; + if (*(p + 1)) + goto one_more; + } if (*p == '-') { left_pad = true; if (*(p + 1)) @@ -326,7 +329,7 @@ template switch (*p) { case 's': { const char* sp = va_arg(ap, const char*); - ret += print_string(putch, bufptr, sp ? sp : "(null)", left_pad, fieldWidth); + ret += print_string(putch, bufptr, sp ? sp : "(null)", left_pad, fieldWidth, dot); } break; case 'd':