Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH] zshexpn: Expand documentation of (S) (was: Re: [Bug] S-flag imposes non-greedy match where it shouldn't)



On Mon, Dec 30, 2019 at 8:32 PM Sebastian Gniazdowski
<sgniazdowski@xxxxxxxxx> wrote:
>
> Do you also understand why:
>
> str=aXXXb
> print ${str%%X##}
>
> Outputs aXXb?

I suppose you mean ${(S)str%%X##}. Yes, I understand why this prints aXXb.

> Outputs aXXb? The docs don't cover this.

They do with the latest patch for (S) from Daniel:

> With tt(%) or tt(%%), search for the match that starts closest to the end of
> the string

This means that ${(S)str%%X##} is going to find a match that starts
closest to the end of the string and remove it. X## matches one or
move X characters. We go backwards one character at a time until X##
matches. The first match starts at str[-2] and the match is X, so X
gets removed. This seems clear from the docs.

I think it would be beneficial to specify that with ${(S)str##pattern}
the first attempted match starts at str[-1] and that no attempt is
made to check if the empty string (the ultimate shortest suffix)
matches. I think you or someone else has recently raised this point as
this seems inconsistent. It's surprising to me that both ${str#*} and
${(S)str%*} expand to $str while ${(S)str%%*} doesn't.

Roman.



Messages sorted by: Reverse Date, Date, Thread, Author