From ba6c9423b87a06c802355516f62e91f8da4c2274 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Tue, 2 Feb 2021 00:23:03 +0330 Subject: [PATCH] AK: Implement printf fraction length specification for strings This adds support for '%.20s' and friends :^) --- AK/PrintfImplementation.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/AK/PrintfImplementation.h b/AK/PrintfImplementation.h index 1609a2dadf..0ebeecb000 100644 --- a/AK/PrintfImplementation.h +++ b/AK/PrintfImplementation.h @@ -233,10 +233,17 @@ ALWAYS_INLINE int print_octal_number(PutChFunc putch, char*& bufptr, u32 number, } template -ALWAYS_INLINE int print_string(PutChFunc putch, char*& bufptr, const char* str, size_t len, bool left_pad, size_t field_width, bool dot) +ALWAYS_INLINE int print_string(PutChFunc putch, char*& bufptr, const char* str, size_t len, bool left_pad, size_t field_width, bool dot, size_t fraction_length, bool has_fraction) { + if (has_fraction) + len = min(len, fraction_length); + if (!dot && (!field_width || field_width < len)) field_width = len; + + if (has_fraction && !field_width) + field_width = len; + size_t pad_amount = field_width > len ? field_width - len : 0; if (!left_pad) { @@ -292,7 +299,7 @@ struct PrintfImpl { const char* sp = NextArgument()(ap); if (!sp) sp = "(null)"; - return print_string(m_putch, m_bufptr, sp, strlen(sp), state.left_pad, state.field_width, state.dot); + return print_string(m_putch, m_bufptr, sp, strlen(sp), state.left_pad, state.field_width, state.dot, state.fraction_length, state.has_fraction_length); } ALWAYS_INLINE int format_d(const ModifierState& state, ArgumentListRefT ap) const { @@ -367,7 +374,7 @@ struct PrintfImpl { ALWAYS_INLINE int format_c(const ModifierState& state, ArgumentListRefT ap) const { char c = NextArgument()(ap); - return print_string(m_putch, m_bufptr, &c, 1, state.left_pad, state.field_width, state.dot); + return print_string(m_putch, m_bufptr, &c, 1, state.left_pad, state.field_width, state.dot, state.fraction_length, state.has_fraction_length); } ALWAYS_INLINE int format_unrecognized(char format_op, const char* fmt, const ModifierState&, ArgumentListRefT) const { @@ -422,7 +429,7 @@ ALWAYS_INLINE int printf_internal(PutChFunc putch, char* buffer, const char*& fm if (*(p + 1)) goto one_more; } - if (!state.zero_pad && !state.field_width && *p == '0') { + if (!state.zero_pad && !state.field_width && !state.has_fraction_length && *p == '0') { state.zero_pad = true; if (*(p + 1)) goto one_more;