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

AK: Support "%.*s" in format strings

Work towards #623.
This commit is contained in:
Andreas Kling 2020-02-19 22:07:37 +01:00
parent eaa680ab8e
commit 151467b569

View file

@ -228,23 +228,23 @@ template<typename PutChFunc>
} }
template<typename PutChFunc> template<typename PutChFunc>
[[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); size_t len = strlen(str);
if (!fieldWidth || fieldWidth < len) if (!dot && (!field_width || field_width < len))
fieldWidth = len; field_width = len;
if (!leftPad) { if (!leftPad && field_width > len) {
for (unsigned i = 0; i < fieldWidth - len; ++i) for (unsigned i = 0; i < field_width - len; ++i)
putch(bufptr, ' '); putch(bufptr, ' ');
} }
for (unsigned i = 0; i < len; ++i) { for (unsigned i = 0; i < field_width; ++i) {
putch(bufptr, str[i]); putch(bufptr, str[i]);
} }
if (leftPad) { if (leftPad && field_width > len) {
for (unsigned i = 0; i < fieldWidth - len; ++i) for (unsigned i = 0; i < field_width - len; ++i)
putch(bufptr, ' '); putch(bufptr, ' ');
} }
return fieldWidth; return field_width;
} }
template<typename PutChFunc> template<typename PutChFunc>
@ -270,6 +270,7 @@ template<typename PutChFunc>
for (p = fmt; *p; ++p) { for (p = fmt; *p; ++p) {
bool left_pad = false; bool left_pad = false;
bool zeroPad = false; bool zeroPad = false;
bool dot = false;
unsigned fieldWidth = 0; unsigned fieldWidth = 0;
unsigned long_qualifiers = 0; unsigned long_qualifiers = 0;
bool size_qualifier = false; bool size_qualifier = false;
@ -279,9 +280,11 @@ template<typename PutChFunc>
if (*p == '%' && *(p + 1)) { if (*p == '%' && *(p + 1)) {
one_more: one_more:
++p; ++p;
// FIXME: This is just a hack workaround to prevent choking on '.' specifiers if (*p == '.') {
if (*p == '.') dot = true;
goto one_more; if (*(p + 1))
goto one_more;
}
if (*p == '-') { if (*p == '-') {
left_pad = true; left_pad = true;
if (*(p + 1)) if (*(p + 1))
@ -326,7 +329,7 @@ template<typename PutChFunc>
switch (*p) { switch (*p) {
case 's': { case 's': {
const char* sp = va_arg(ap, const char*); 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; } break;
case 'd': case 'd':