summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorEmanuel Sprung <emanuel.sprung@gmail.com>2020-04-06 23:27:15 +0200
committerAndreas Kling <kling@serenityos.org>2020-04-07 09:02:02 +0200
commit154dcd1ed61243a7e8b0b54a75c27034298e4d1d (patch)
tree03d97008ea97481bd9010980741d39f9a7e9dea7 /AK
parent22f20cd51daebbeedf98b39ca2e5649051b0a016 (diff)
downloadserenity-154dcd1ed61243a7e8b0b54a75c27034298e4d1d.zip
AK: Allow %m.nf specifier for double/float in printf to set fraction with
This patch adds the missing part for the printf of double values to specify the length of the fraction part. For GVariant, a default of %.2 is used.
Diffstat (limited to 'AK')
-rw-r--r--AK/PrintfImplementation.h28
1 files changed, 19 insertions, 9 deletions
diff --git a/AK/PrintfImplementation.h b/AK/PrintfImplementation.h
index cab0e735b5..b46fff6c1a 100644
--- a/AK/PrintfImplementation.h
+++ b/AK/PrintfImplementation.h
@@ -178,7 +178,7 @@ template<typename PutChFunc>
}
template<typename PutChFunc>
-[[gnu::always_inline]] inline int print_double(PutChFunc putch, char*& bufptr, double number, bool leftPad, bool zeroPad, u32 fieldWidth)
+[[gnu::always_inline]] inline int print_double(PutChFunc putch, char*& bufptr, double number, bool leftPad, bool zeroPad, u32 fieldWidth, u32 fraction_length = 6)
{
int length = 0;
@@ -192,9 +192,11 @@ template<typename PutChFunc>
putch(bufptr, '.');
length++;
double fraction = number - (i64)number;
- // FIXME: Allow other fractions
- fraction = fraction * 1000000;
- return length + print_u64(putch, bufptr, (i64)fraction, leftPad, true, 6);
+
+ for (u32 i = 0; i < fraction_length; ++i)
+ fraction = fraction * 10;
+
+ return length + print_u64(putch, bufptr, (i64)fraction, false, true, fraction_length);
}
template<typename PutChFunc>
@@ -295,6 +297,7 @@ template<typename PutChFunc>
bool zeroPad = false;
bool dot = false;
unsigned fieldWidth = 0;
+ unsigned fraction_length = 0;
unsigned long_qualifiers = 0;
bool size_qualifier = false;
(void)size_qualifier;
@@ -324,10 +327,17 @@ template<typename PutChFunc>
goto one_more;
}
if (*p >= '0' && *p <= '9') {
- fieldWidth *= 10;
- fieldWidth += *p - '0';
- if (*(p + 1))
- goto one_more;
+ if (!dot) {
+ fieldWidth *= 10;
+ fieldWidth += *p - '0';
+ if (*(p + 1))
+ goto one_more;
+ } else {
+ fraction_length *= 10;
+ fraction_length += *p - '0';
+ if (*(p + 1))
+ goto one_more;
+ }
}
if (*p == '*') {
fieldWidth = va_arg(ap, int);
@@ -381,7 +391,7 @@ template<typename PutChFunc>
#if !defined(BOOTSTRAPPER) && !defined(KERNEL)
case 'g':
case 'f':
- ret += print_double(putch, bufptr, va_arg(ap, double), left_pad, zeroPad, fieldWidth);
+ ret += print_double(putch, bufptr, va_arg(ap, double), left_pad, zeroPad, fieldWidth, fraction_length);
break;
#endif