Re: Simplify the way of appending comma to stringInfo - Mailing list pgsql-hackers

From Pavel Stehule
Subject Re: Simplify the way of appending comma to stringInfo
Date
Msg-id CAFj8pRB7xn_enWAFsZaJ+L+c6xPLRJ-G2teG-S-=DXLZuA=Q3w@mail.gmail.com
Whole thread Raw
In response to Simplify the way of appending comma to stringInfo  (Chao Li <li.evan.chao@gmail.com>)
List pgsql-hackers
Hi

po 8. 12. 2025 v 9:37 odesílatel Chao Li <li.evan.chao@gmail.com> napsal:
Hi Hackers,

In a lot places, there are logic of appending comma separators in a pattern like:

```
for (int i = 0; i < len; i ++)
{
    if (i > 0)
       appendStringInfoString(", ");
    appendStringInfo(some-item);
}

```
This pattern uses an "if" check and two appendStringInfoString() to build a comma-delimited string. 

This can be simplified as:

```
const char *sep = "";
for (int i = 0; i < len; i ++)
{
     appendStringInfo("%s%s", sep, some-item);
     sep = ", ";
}
```
The new pattern avoids the "if" check, and combines two appendStringInfoString() into a single appendStringInfo(). I think the new pattern is neater and faster.

The old patterns are used in a lot of places, and there are some usages of the new pattern as well. Instead of creating a big cleanup patch, I just applied the new pattern to a single file for now to see if the hacker group likes this change.

It doesn't look like a simplification.  

Regards

Pavel


Best regards,
==
Chao Li (Evan)
---------------------
HighGo Software Co., Ltd.
https://www.highgo.com/

pgsql-hackers by date:

Previous
From: Chao Li
Date:
Subject: Simplify the way of appending comma to stringInfo
Next
From: Heikki Linnakangas
Date:
Subject: Re: Simplify the way of appending comma to stringInfo