diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2017-02-10 14:19:22 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2017-02-10 14:19:22 +0000 |
commit | 51567a1d5c39ecc39504fdc27fa211f9517acbae (patch) | |
tree | 54ac50ea4207fa6ee22e69d3b257ceb66653e9b4 /src/libutil | |
parent | 02621b42c1a2459716f99a36c5ac4d05f07339dc (diff) | |
download | rspamd-51567a1d5c39ecc39504fdc27fa211f9517acbae.tar.gz rspamd-51567a1d5c39ecc39504fdc27fa211f9517acbae.zip |
[Minor] Use more precise system function to print floating point
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/printf.c | 90 |
1 files changed, 19 insertions, 71 deletions
diff --git a/src/libutil/printf.c b/src/libutil/printf.c index db21346ea..6935c03b0 100644 --- a/src/libutil/printf.c +++ b/src/libutil/printf.c @@ -375,14 +375,14 @@ rspamd_vprintf_common (rspamd_printf_append_func func, const gchar *fmt, va_list args) { - gchar zero, numbuf[G_ASCII_DTOSTR_BUF_SIZE], *p, *last, c; - const gchar *buf_start = fmt; + gchar zero, numbuf[G_ASCII_DTOSTR_BUF_SIZE], dtoabuf[8], *p, *last, c; + const gchar *buf_start = fmt, *fmt_start = NULL; gint d; - gdouble f, scale; + gdouble f; glong written = 0, wr, slen; gint64 i64; guint64 ui64; - guint width, sign, hex, humanize, bytes, frac_width, i, b32; + guint width, sign, hex, humanize, bytes, frac_width, b32; rspamd_fstring_t *v; rspamd_ftok_t *tok; GString *gs; @@ -407,6 +407,8 @@ rspamd_vprintf_common (rspamd_printf_append_func func, written += wr; } + fmt_start = fmt; + i64 = 0; ui64 = 0; @@ -700,77 +702,23 @@ rspamd_vprintf_common (rspamd_printf_append_func func, case 'f': - case 'F': - if (*fmt == 'f') { - f = (gdouble) va_arg (args, double); - } - else { - f = (gdouble) va_arg (args, long double); - } - - if (isfinite (f)) { - p = numbuf; - last = p + sizeof (numbuf); - if (f < 0) { - *p++ = '-'; - f = -f; - } - if (frac_width == 0) { - frac_width = 6; - } - - ui64 = (gint64) f; - - p = rspamd_sprintf_num (p, last, ui64, zero, 0, width); - - if (frac_width) { - - if (p < last) { - *p++ = '.'; - } - - scale = 1.0; - - for (i = 0; i < frac_width; i++) { - scale *= 10.0; - } - - /* - * (gint64) cast is required for msvc6: - * it can not convert guint64 to double - */ - ui64 = (guint64) ((f - (gint64) ui64) * scale); - - p = rspamd_sprintf_num (p, last, ui64, '0', 0, frac_width); - } - - slen = p - numbuf; - RSPAMD_PRINTF_APPEND (numbuf, slen); - } - else if (isnan (f)) { - RSPAMD_PRINTF_APPEND ("NaN", 3); - } - else { - if (signbit (f)) { - RSPAMD_PRINTF_APPEND ("-Inf", 4); - } - else { - RSPAMD_PRINTF_APPEND ("+Inf", 4); - } - } + case 'g': + f = (gdouble) va_arg (args, double); + rspamd_strlcpy (dtoabuf, fmt_start, MIN (sizeof (dtoabuf), + (fmt - fmt_start + 2))); + g_ascii_formatd (numbuf, sizeof (numbuf), dtoabuf, (double)f); + slen = strlen (numbuf); + RSPAMD_PRINTF_APPEND (numbuf, slen); continue; - case 'g': + case 'F': case 'G': - if (*fmt == 'g') { - f = (gdouble) va_arg (args, double); - } - else { - f = (gdouble) va_arg (args, long double); - } - - g_ascii_formatd (numbuf, sizeof (numbuf), "%g", (double)f); + f = (gdouble) va_arg (args, long double); + slen = rspamd_strlcpy (dtoabuf, fmt_start, MIN (sizeof (dtoabuf), + (fmt - fmt_start + 2))); + dtoabuf[slen - 1] = g_ascii_tolower (dtoabuf[slen - 1]); + g_ascii_formatd (numbuf, sizeof (numbuf), dtoabuf, (double)f); slen = strlen (numbuf); RSPAMD_PRINTF_APPEND (numbuf, slen); |