Skip to content

Commit a8656a3

Browse files
committed
Make NUM_TOCHAR_prepare and NUM_TOCHAR_finish macros declare "len".
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.
1 parent 9d140f7 commit a8656a3

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

src/backend/utils/adt/formatting.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4877,7 +4877,7 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number,
48774877
*/
48784878
#define NUM_TOCHAR_prepare \
48794879
do { \
4880-
len = VARSIZE_ANY_EXHDR(fmt); \
4880+
int len = VARSIZE_ANY_EXHDR(fmt); \
48814881
if (len <= 0 || len >= (INT_MAX-VARHDRSZ)/NUM_MAX_ITEM_SIZ) \
48824882
PG_RETURN_TEXT_P(cstring_to_text("")); \
48834883
result = (text *) palloc0((len * NUM_MAX_ITEM_SIZ) + 1 + VARHDRSZ); \
@@ -4890,6 +4890,8 @@ do { \
48904890
*/
48914891
#define NUM_TOCHAR_finish \
48924892
do { \
4893+
int len; \
4894+
\
48934895
NUM_processor(format, &Num, VARDATA(result), numstr, plen, sign, true, PG_GET_COLLATION()); \
48944896
\
48954897
if (shouldFree) \
@@ -4961,8 +4963,7 @@ numeric_to_char(PG_FUNCTION_ARGS)
49614963
FormatNode *format;
49624964
text *result;
49634965
bool shouldFree;
4964-
int len = 0,
4965-
plen = 0,
4966+
int plen = 0,
49664967
sign = 0;
49674968
char *numstr,
49684969
*orgnum,
@@ -5008,16 +5009,15 @@ numeric_to_char(PG_FUNCTION_ARGS)
50085009
numstr = (char *) palloc(strlen(orgnum) + 2);
50095010
*numstr = ' ';
50105011
strcpy(numstr + 1, orgnum);
5011-
len = strlen(numstr);
50125012
}
50135013
else
50145014
{
50155015
numstr = orgnum;
5016-
len = strlen(orgnum);
50175016
}
50185017
}
50195018
else
50205019
{
5020+
int len;
50215021
Numeric val = value;
50225022

50235023
if (IS_MULTI(&Num))
@@ -5084,8 +5084,7 @@ int4_to_char(PG_FUNCTION_ARGS)
50845084
FormatNode *format;
50855085
text *result;
50865086
bool shouldFree;
5087-
int len = 0,
5088-
plen = 0,
5087+
int plen = 0,
50895088
sign = 0;
50905089
char *numstr,
50915090
*orgnum;
@@ -5111,11 +5110,12 @@ int4_to_char(PG_FUNCTION_ARGS)
51115110
if (*orgnum == '+')
51125111
*orgnum = ' ';
51135112

5114-
len = strlen(orgnum);
51155113
numstr = orgnum;
51165114
}
51175115
else
51185116
{
5117+
int len;
5118+
51195119
if (IS_MULTI(&Num))
51205120
{
51215121
orgnum = DatumGetCString(DirectFunctionCall1(int4out,
@@ -5175,8 +5175,7 @@ int8_to_char(PG_FUNCTION_ARGS)
51755175
FormatNode *format;
51765176
text *result;
51775177
bool shouldFree;
5178-
int len = 0,
5179-
plen = 0,
5178+
int plen = 0,
51805179
sign = 0;
51815180
char *numstr,
51825181
*orgnum;
@@ -5211,16 +5210,16 @@ int8_to_char(PG_FUNCTION_ARGS)
52115210
numstr = (char *) palloc(strlen(orgnum) + 2);
52125211
*numstr = ' ';
52135212
strcpy(numstr + 1, orgnum);
5214-
len = strlen(numstr);
52155213
}
52165214
else
52175215
{
52185216
numstr = orgnum;
5219-
len = strlen(orgnum);
52205217
}
52215218
}
52225219
else
52235220
{
5221+
int len;
5222+
52245223
if (IS_MULTI(&Num))
52255224
{
52265225
double multi = pow((double) 10, (double) Num.multi);
@@ -5282,8 +5281,7 @@ float4_to_char(PG_FUNCTION_ARGS)
52825281
FormatNode *format;
52835282
text *result;
52845283
bool shouldFree;
5285-
int len = 0,
5286-
plen = 0,
5284+
int plen = 0,
52875285
sign = 0;
52885286
char *numstr,
52895287
*orgnum,
@@ -5317,13 +5315,13 @@ float4_to_char(PG_FUNCTION_ARGS)
53175315
if (*orgnum == '+')
53185316
*orgnum = ' ';
53195317

5320-
len = strlen(orgnum);
53215318
numstr = orgnum;
53225319
}
53235320
}
53245321
else
53255322
{
53265323
float4 val = value;
5324+
int len;
53275325

53285326
if (IS_MULTI(&Num))
53295327
{
@@ -5386,8 +5384,7 @@ float8_to_char(PG_FUNCTION_ARGS)
53865384
FormatNode *format;
53875385
text *result;
53885386
bool shouldFree;
5389-
int len = 0,
5390-
plen = 0,
5387+
int plen = 0,
53915388
sign = 0;
53925389
char *numstr,
53935390
*orgnum,
@@ -5421,13 +5418,13 @@ float8_to_char(PG_FUNCTION_ARGS)
54215418
if (*orgnum == '+')
54225419
*orgnum = ' ';
54235420

5424-
len = strlen(orgnum);
54255421
numstr = orgnum;
54265422
}
54275423
}
54285424
else
54295425
{
54305426
float8 val = value;
5427+
int len;
54315428

54325429
if (IS_MULTI(&Num))
54335430
{

0 commit comments

Comments
 (0)