Re: [PATCH v3 1/2] repo: factor out field printing to dedicated function
On Sun, Oct 26, 2025 at 7:53 PM Eric Sunshine <sunshine@sunshineco.com> wrote:
Show 26 quoted lines
> On Sun, Oct 26, 2025 at 6:54 PM Lucas Seiki Oshiro
> <lucasseikioshiro@gmail.com> wrote:
> > + switch (format) {
> > + case FORMAT_KEYVALUE:
> > + quote_c_style(valbuf->buf, quotbuf, NULL, 0);
> > + printf("%s=%s\n", key, quotbuf->buf);
> > + break;
> > + case FORMAT_NUL_TERMINATED:
> > + printf("%s\n%s%c", key, valbuf->buf, '\0');
> > + break;
> > + default:
> > + BUG("not a valid output format: %d", format);
> > + }
> > +}
>
> Moreover, I'd also say that since this is not on a critical path, you
> should avoid the premature optimization of passing in `strfbuf
> *quotebuf` and instead make `quotebuf` local to this function.
>
> static void print_field(enum output_format format,
> const char *key, const char *value)
> {
> struct strbuf quotbuf = STRBUF_INIT;
> ...stuff...
> strbuf_release("buf);
> }Or, even better, scope the strbuf just to the `case` branch which needs it:
case FORMAT_KEYVALUE: {
struct strbuf buf = STRBUF_INIT;
quote_c_style(value, buf, NULL, 0);
printf("%s=%s\n", key, buf->buf);
strbuf_release(&buf);
break;
}