This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
S_F0convert(): remove Nan/Inf handling
authorDavid Mitchell <davem@iabyn.com>
Mon, 15 May 2017 13:49:50 +0000 (14:49 +0100)
committerDavid Mitchell <davem@iabyn.com>
Wed, 7 Jun 2017 08:11:01 +0000 (09:11 +0100)
This function handles sprintf "%.0f". It also handles Inf/Nan, but neither
of its callers will call it with such an nv. Its code for handling them is
also broken - it returns the \0 following the "Inf" or "Nan! string.

So just remove this unneeded and broken functionality.

At the same time document what S_F0convert() does.

sv.c

diff --git a/sv.c b/sv.c
index 0ffd29a..2f90e58 100644 (file)
--- a/sv.c
+++ b/sv.c
@@ -11000,6 +11000,12 @@ S_expect_number(pTHX_ char **const pattern)
     return var;
 }
 
+/* Implement a fast "%.0f": given a pointer to the end of a buffer (caller
+ * ensures it's big enough), back fill it with the rounded integer part of
+ * nv. Returns ptr to start of string, and sets *len to its length.
+ * Returns NULL if not convertible.
+ */
+
 STATIC char *
 S_F0convert(NV nv, char *const endbuf, STRLEN *const len)
 {
@@ -11008,11 +11014,7 @@ S_F0convert(NV nv, char *const endbuf, STRLEN *const len)
 
     PERL_ARGS_ASSERT_F0CONVERT;
 
-    if (UNLIKELY(Perl_isinfnan(nv))) {
-        STRLEN n = S_infnan_2pv(nv, endbuf - *len, *len, 0);
-        *len = n;
-        return endbuf - n;
-    }
+    assert(!Perl_isinfnan(nv));
     if (neg)
        nv = -nv;
     if (nv < UV_MAX) {
@@ -11518,7 +11520,7 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
     }
 
 #if !defined(USE_LONG_DOUBLE) && !defined(USE_QUADMATH)
-    /* special-case "%.<number>[gf]" */
+    /* special-case "%.0f" and "%.<number>g" */
     if ( !args && patlen <= 5 && pat[0] == '%' && pat[1] == '.'
         && (pat[patlen-1] == 'g' || pat[patlen-1] == 'f') ) {
        unsigned digits = 0;
@@ -12548,6 +12550,7 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
                PL_efloatbuf[0] = '\0';
            }
 
+            /* special-case "%.0f" and "%.<number>g" */
            if ( !(width || left || plus || alt) && fill != '0'
                 && has_precis && intsize != 'q'        /* Shortcuts */
                  && LIKELY(!Perl_isinfnan((NV)fv)) ) {