git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC PATCH] quote: make sq_quote_buf_pretty() prettier

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Nov 30, 2026, 16:38 UTC
Message-ID
<27a384cb-7279-4061-9925-c55107575f6d@gmail.com>
In-Reply-To
<CALnO6CDyTh+O_S6Q6Nq46wckLGXdvTKJmU6uK_SKn5S=p4X20g@mail.gmail.com>
Hi Ben
On 30/11/2026 15:19, D. Ben Knoble wrote:
> Hi Phillip, thanks for working on this!
Thanks
Show 26 quoted lines
> On Fri, Nov 27, 2026 at 11:54 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
> >> +       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;
>> +               } else if (*q == '{') {
>> +                       brace++;
>> +               } else if (*q == '}' && brace) {
>> +                       brace--;
> > I think this simple counter may count wrongly for scenarios like
> "}{a,b}", but I haven't tried it myself yet. My shells (Zsh and Bash)
> both do the following:
> >      $ echo }{a,b}
>      }a }b
> > and I *think* that brace==0 when we see the comma here, even though it
> would need quoted to avoid expansion.
We only decrement "brace" when we see '}' if "brace > 0". If we see '}' when "brace == 0" then we quote everything up to the next backslash escape or the end of the string. So we decide to quote this before we see the comma.
Thanks
Phillip
> > I think we are unlikely to emit such strange punctuation ourselves,
> but this would be one corner case of potentially many, I suspect.
Show 18 quoted lines
> >> +               } 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, '\'');
>>   }
>>
>>   void sq_quotef(struct strbuf *dst, const char *fmt, ...)
>> --
>> 2.56.1.576.g0cfbda1d5c7.dirty
>>
> > 
Previous: D. Ben KnobleNext: D. Ben Knoble
Message 9 of 10 in “quote: make sq_quote_buf_pretty() prettier”
  1. quote: make sq_quote_buf_pretty() prettierPhillip Wood, Nov 27, 2026
  2. Nahid hasan SagorNov 27, 2026
  3. Phillip WoodNov 30, 2026
  4. Junio C HamanoNov 27, 2026
  5. Phillip WoodNov 30, 2026
  6. Junio C HamanoNov 28, 2026
  7. Phillip WoodNov 30, 2026
  8. D. Ben KnobleNov 30, 2026
  9. Phillip WoodNov 30, 2026
  10. D. Ben KnobleNov 30, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.