Re: [RFC PATCH] quote: make sq_quote_buf_pretty() prettier
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Nov 28, 2026, 00:26 UTC
- Message-ID
- <xmqqmrqt3ior.fsf@gitster.g>
- In-Reply-To
- <ee7de58093c69fbbab686a68b5b781bc60d90a2b.1795798429.git.phillip.wood@dunelm.org.uk>
Phillip Wood <phillip.wood123@gmail.com> writes:
I forgot to mention a couple of things.
> ... It also adds unnecessary pairs > of empty quotes when a string starts or ends with a single quote.
"a single quote or an exclamation point." We can see the example in the table you have below, for the entry for 'danger!'.
Show 14 quoted lines
> + for (q = p; *q; q++) {
> + if (need_bs_quote(*q)) {
> + if (brace)
> + quote_it = true;
> + if (quote_it)
> + strbuf_addch(dst, '\'');
> + strbuf_add(dst, p, q - p);
> + p = q + 1;
> + if (quote_it)
> + strbuf_addch(dst, '\'');
> + strbuf_addch(dst, '\\');
> + strbuf_addch(dst, *q);
> + if (!brace)
> + quote_it = false;When brace > 0, quote_it stays true after emitting the backslash-escaped character.
If the next character also needs backslash quoting (such as "{a'!b}"), or if the string ends right there with an unclosed brace (such as "{!", p == q (i.e., an empty segment) but we emit it in a pair of single quotes.
Show 15 quoted lines
> + } else if (*q == '{') {
> + brace++;
> + } else if (*q == '}' && brace) {
> + brace--;
> + } else if (!quote_it && needs_sq_quote(*q, brace)) {
> + quote_it = true;
> + }
> + }
> + if (brace && !quote_it)
> + quote_it = true; /* Quote unpaired '{' */
> + if (quote_it)
> + strbuf_addch(dst, '\'');
> + strbuf_add(dst, p, q - p);
> + if (quote_it)
> + strbuf_addch(dst, '\'');After the loop, here also is an instance of single quote pair around potentially an empty segment. Perhaps a short helper or macro that does an equivalent of
if (p < q) {
if (quote_it)
strbuf_addch(dst, '\'');
strbuf_add(dst, p, q - p);
if (quote_it)
strbuf_addch(dst, '\'');
} and use it twice (the bs-quote code path needs to be slightly rewritten if we wanted to share this piece of code that way)?
> } > > void sq_quotef(struct strbuf *dst, const char *fmt, ...)