Skip to content

Commit 03fbb08

Browse files
committed
formatting.c cleanup: Move loop variables definitions into for statement
Reviewed-by: Chao Li <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/6dd9d208-a3ed-49b5-b03d-8617261da973%40eisentraut.org
1 parent 9592467 commit 03fbb08

File tree

1 file changed

+15
-30
lines changed

1 file changed

+15
-30
lines changed

src/backend/utils/adt/formatting.c

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,9 +1128,7 @@ index_seq_search(const char *str, const KeyWord *kw, const int *index)
11281128
static const KeySuffix *
11291129
suff_search(const char *str, const KeySuffix *suf, int type)
11301130
{
1131-
const KeySuffix *s;
1132-
1133-
for (s = suf; s->name != NULL; s++)
1131+
for (const KeySuffix *s = suf; s->name != NULL; s++)
11341132
{
11351133
if (s->type != type)
11361134
continue;
@@ -1865,14 +1863,13 @@ char *
18651863
asc_tolower(const char *buff, size_t nbytes)
18661864
{
18671865
char *result;
1868-
char *p;
18691866

18701867
if (!buff)
18711868
return NULL;
18721869

18731870
result = pnstrdup(buff, nbytes);
18741871

1875-
for (p = result; *p; p++)
1872+
for (char *p = result; *p; p++)
18761873
*p = pg_ascii_tolower((unsigned char) *p);
18771874

18781875
return result;
@@ -1888,14 +1885,13 @@ char *
18881885
asc_toupper(const char *buff, size_t nbytes)
18891886
{
18901887
char *result;
1891-
char *p;
18921888

18931889
if (!buff)
18941890
return NULL;
18951891

18961892
result = pnstrdup(buff, nbytes);
18971893

1898-
for (p = result; *p; p++)
1894+
for (char *p = result; *p; p++)
18991895
*p = pg_ascii_toupper((unsigned char) *p);
19001896

19011897
return result;
@@ -1911,15 +1907,14 @@ char *
19111907
asc_initcap(const char *buff, size_t nbytes)
19121908
{
19131909
char *result;
1914-
char *p;
19151910
int wasalnum = false;
19161911

19171912
if (!buff)
19181913
return NULL;
19191914

19201915
result = pnstrdup(buff, nbytes);
19211916

1922-
for (p = result; *p; p++)
1917+
for (char *p = result; *p; p++)
19231918
{
19241919
char c;
19251920

@@ -1994,13 +1989,12 @@ asc_toupper_z(const char *buff)
19941989
static void
19951990
dump_index(const KeyWord *k, const int *index)
19961991
{
1997-
int i,
1998-
count = 0,
1992+
int count = 0,
19991993
free_i = 0;
20001994

20011995
elog(DEBUG_elog_output, "TO-FROM_CHAR: Dump KeyWord Index:");
20021996

2003-
for (i = 0; i < KeyWord_INDEX_SIZE; i++)
1997+
for (int i = 0; i < KeyWord_INDEX_SIZE; i++)
20041998
{
20051999
if (index[i] != -1)
20062000
{
@@ -2282,7 +2276,6 @@ static int
22822276
seq_search_ascii(const char *name, const char *const *array, int *len)
22832277
{
22842278
unsigned char firstc;
2285-
const char *const *a;
22862279

22872280
*len = 0;
22882281

@@ -2293,17 +2286,14 @@ seq_search_ascii(const char *name, const char *const *array, int *len)
22932286
/* we handle first char specially to gain some speed */
22942287
firstc = pg_ascii_tolower((unsigned char) *name);
22952288

2296-
for (a = array; *a != NULL; a++)
2289+
for (const char *const *a = array; *a != NULL; a++)
22972290
{
2298-
const char *p;
2299-
const char *n;
2300-
23012291
/* compare first chars */
23022292
if (pg_ascii_tolower((unsigned char) **a) != firstc)
23032293
continue;
23042294

23052295
/* compare rest of string */
2306-
for (p = *a + 1, n = name + 1;; p++, n++)
2296+
for (const char *p = *a + 1, *n = name + 1;; p++, n++)
23072297
{
23082298
/* return success if we matched whole array entry */
23092299
if (*p == '\0')
@@ -2338,7 +2328,6 @@ seq_search_ascii(const char *name, const char *const *array, int *len)
23382328
static int
23392329
seq_search_localized(const char *name, char **array, int *len, Oid collid)
23402330
{
2341-
char **a;
23422331
char *upper_name;
23432332
char *lower_name;
23442333

@@ -2352,7 +2341,7 @@ seq_search_localized(const char *name, char **array, int *len, Oid collid)
23522341
* The case-folding processing done below is fairly expensive, so before
23532342
* doing that, make a quick pass to see if there is an exact match.
23542343
*/
2355-
for (a = array; *a != NULL; a++)
2344+
for (char **a = array; *a != NULL; a++)
23562345
{
23572346
int element_len = strlen(*a);
23582347

@@ -2371,7 +2360,7 @@ seq_search_localized(const char *name, char **array, int *len, Oid collid)
23712360
lower_name = str_tolower(upper_name, strlen(upper_name), collid);
23722361
pfree(upper_name);
23732362

2374-
for (a = array; *a != NULL; a++)
2363+
for (char **a = array; *a != NULL; a++)
23752364
{
23762365
char *upper_element;
23772366
char *lower_element;
@@ -2438,9 +2427,8 @@ from_char_seq_search(int *dest, const char **src, const char *const *array,
24382427
* any) to avoid including irrelevant data.
24392428
*/
24402429
char *copy = pstrdup(*src);
2441-
char *c;
24422430

2443-
for (c = copy; *c; c++)
2431+
for (char *c = copy; *c; c++)
24442432
{
24452433
if (scanner_isspace(*c))
24462434
{
@@ -2467,7 +2455,6 @@ from_char_seq_search(int *dest, const char **src, const char *const *array,
24672455
static void
24682456
DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid collid)
24692457
{
2470-
FormatNode *n;
24712458
char *s;
24722459
struct fmt_tm *tm = &in->tm;
24732460
int i;
@@ -2476,7 +2463,7 @@ DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid col
24762463
cache_locale_time();
24772464

24782465
s = out;
2479-
for (n = node; n->type != NODE_TYPE_END; n++)
2466+
for (FormatNode *n = node; n->type != NODE_TYPE_END; n++)
24802467
{
24812468
if (n->type != NODE_TYPE_ACTION)
24822469
{
@@ -3686,10 +3673,9 @@ DCH_prevent_counter_overflow(void)
36863673
static int
36873674
DCH_datetime_type(FormatNode *node)
36883675
{
3689-
FormatNode *n;
36903676
int flags = 0;
36913677

3692-
for (n = node; n->type != NODE_TYPE_END; n++)
3678+
for (FormatNode *n = node; n->type != NODE_TYPE_END; n++)
36933679
{
36943680
if (n->type != NODE_TYPE_ACTION)
36953681
continue;
@@ -5026,8 +5012,7 @@ int_to_roman(int number)
50265012
{
50275013
int len,
50285014
num;
5029-
char *p,
5030-
*result,
5015+
char *result,
50315016
numstr[12];
50325017

50335018
result = (char *) palloc(MAX_ROMAN_LEN + 1);
@@ -5048,7 +5033,7 @@ int_to_roman(int number)
50485033
len = snprintf(numstr, sizeof(numstr), "%d", number);
50495034
Assert(len > 0 && len <= 4);
50505035

5051-
for (p = numstr; *p != '\0'; p++, --len)
5036+
for (char *p = numstr; *p != '\0'; p++, --len)
50525037
{
50535038
num = *p - ('0' + 1);
50545039
if (num < 0)

0 commit comments

Comments
 (0)