summaryrefslogtreecommitdiff
path: root/AK/PrintfImplementation.h
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2021-02-02 00:23:03 +0330
committerLinus Groh <mail@linusgroh.de>2021-05-06 15:20:31 +0100
commitba6c9423b87a06c802355516f62e91f8da4c2274 (patch)
tree40d245fc87b8b394bf96062b1e6fda9e830070f0 /AK/PrintfImplementation.h
parent9f970c3459be761c6d1ac192eb494d630b4ca1ed (diff)
downloadserenity-ba6c9423b87a06c802355516f62e91f8da4c2274.zip
AK: Implement printf fraction length specification for strings
This adds support for '%.20s' and friends :^)
Diffstat (limited to 'AK/PrintfImplementation.h')
-rw-r--r--AK/PrintfImplementation.h15
1 files 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<typename PutChFunc>
-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<const char*>()(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<int>()(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;