Make NUM_TOCHAR_prepare and NUM_TOCHAR_finish macros declare "len".
authorRobert Haas
Mon, 2 Dec 2013 15:51:06 +0000 (10:51 -0500)
committerRobert Haas
Mon, 2 Dec 2013 15:51:06 +0000 (10:51 -0500)
Remove the variable from the enclosing scopes so that nothing can be
relying on it.  The net result of this refactoring is that we get rid
of a few unnecessary strlen() calls.

Original patch from Greg Jaskiewicz, substantially expanded by me.

src/backend/utils/adt/formatting.c

index 36353c39c16dab0254101cb9a840307d5d4751f1..946f3e28c651b1a317e9b126bb1d871928de2758 100644 (file)
@@ -4877,7 +4877,7 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number,
  */
 #define NUM_TOCHAR_prepare \
 do { \
-   len = VARSIZE_ANY_EXHDR(fmt); \
+   int len = VARSIZE_ANY_EXHDR(fmt); \
    if (len <= 0 || len >= (INT_MAX-VARHDRSZ)/NUM_MAX_ITEM_SIZ)     \
        PG_RETURN_TEXT_P(cstring_to_text("")); \
    result  = (text *) palloc0((len * NUM_MAX_ITEM_SIZ) + 1 + VARHDRSZ);    \
@@ -4890,6 +4890,8 @@ do { \
  */
 #define NUM_TOCHAR_finish \
 do { \
+   int     len; \
+                                   \
    NUM_processor(format, &Num, VARDATA(result), numstr, plen, sign, true, PG_GET_COLLATION()); \
                                    \
    if (shouldFree)                 \
@@ -4961,8 +4963,7 @@ numeric_to_char(PG_FUNCTION_ARGS)
    FormatNode *format;
    text       *result;
    bool        shouldFree;
-   int         len = 0,
-               plen = 0,
+   int         plen = 0,
                sign = 0;
    char       *numstr,
               *orgnum,
@@ -5008,16 +5009,15 @@ numeric_to_char(PG_FUNCTION_ARGS)
            numstr = (char *) palloc(strlen(orgnum) + 2);
            *numstr = ' ';
            strcpy(numstr + 1, orgnum);
-           len = strlen(numstr);
        }
        else
        {
            numstr = orgnum;
-           len = strlen(orgnum);
        }
    }
    else
    {
+       int         len;
        Numeric     val = value;
 
        if (IS_MULTI(&Num))
@@ -5084,8 +5084,7 @@ int4_to_char(PG_FUNCTION_ARGS)
    FormatNode *format;
    text       *result;
    bool        shouldFree;
-   int         len = 0,
-               plen = 0,
+   int         plen = 0,
                sign = 0;
    char       *numstr,
               *orgnum;
@@ -5111,11 +5110,12 @@ int4_to_char(PG_FUNCTION_ARGS)
        if (*orgnum == '+')
            *orgnum = ' ';
 
-       len = strlen(orgnum);
        numstr = orgnum;
    }
    else
    {
+       int         len;
+
        if (IS_MULTI(&Num))
        {
            orgnum = DatumGetCString(DirectFunctionCall1(int4out,
@@ -5175,8 +5175,7 @@ int8_to_char(PG_FUNCTION_ARGS)
    FormatNode *format;
    text       *result;
    bool        shouldFree;
-   int         len = 0,
-               plen = 0,
+   int         plen = 0,
                sign = 0;
    char       *numstr,
               *orgnum;
@@ -5211,16 +5210,16 @@ int8_to_char(PG_FUNCTION_ARGS)
            numstr = (char *) palloc(strlen(orgnum) + 2);
            *numstr = ' ';
            strcpy(numstr + 1, orgnum);
-           len = strlen(numstr);
        }
        else
        {
            numstr = orgnum;
-           len = strlen(orgnum);
        }
    }
    else
    {
+       int         len;
+
        if (IS_MULTI(&Num))
        {
            double      multi = pow((double) 10, (double) Num.multi);
@@ -5282,8 +5281,7 @@ float4_to_char(PG_FUNCTION_ARGS)
    FormatNode *format;
    text       *result;
    bool        shouldFree;
-   int         len = 0,
-               plen = 0,
+   int         plen = 0,
                sign = 0;
    char       *numstr,
               *orgnum,
@@ -5317,13 +5315,13 @@ float4_to_char(PG_FUNCTION_ARGS)
            if (*orgnum == '+')
                *orgnum = ' ';
 
-           len = strlen(orgnum);
            numstr = orgnum;
        }
    }
    else
    {
        float4      val = value;
+       int         len;
 
        if (IS_MULTI(&Num))
        {
@@ -5386,8 +5384,7 @@ float8_to_char(PG_FUNCTION_ARGS)
    FormatNode *format;
    text       *result;
    bool        shouldFree;
-   int         len = 0,
-               plen = 0,
+   int         plen = 0,
                sign = 0;
    char       *numstr,
               *orgnum,
@@ -5421,13 +5418,13 @@ float8_to_char(PG_FUNCTION_ARGS)
            if (*orgnum == '+')
                *orgnum = ' ';
 
-           len = strlen(orgnum);
            numstr = orgnum;
        }
    }
    else
    {
        float8      val = value;
+       int         len;
 
        if (IS_MULTI(&Num))
        {