Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Substitute the last match of a pattern during parameters expansion.
- X-seq: zsh-users 28071
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Michele Venturi <dardo82@xxxxxxxxx>
- Cc: zsh-users@xxxxxxx
- Subject: Re: Substitute the last match of a pattern during parameters expansion.
- Date: Wed, 14 Sep 2022 11:56:47 -0700
- Archived-at: <https://zsh.org/users/28071>
- In-reply-to: <CA+Ds4NsGWJB1HaMMpUnmygTrS6J2NY_nuh=N_YEkEjNg4T0BQw@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- References: <CA+Ds4NsnE6HvV_Kf59KO5_=8Jb4STxwAzL8x+EUq1aiQdB91ZA@mail.gmail.com> <a0ea450b-eb84-47d1-a4ae-bd0bfbef51ea@www.fastmail.com> <CA+Ds4NuuAMiKoi9D=hgyY-RbySais1eEgZHbivx=L68KUWv1bg@mail.gmail.com> <4844b850-2ca6-4c90-95ae-aae00dc339cf@www.fastmail.com> <CAH+w=7YYqVH26weHQiRXCnoV8x63s_F56t=UnseN1q3K2=V95Q@mail.gmail.com> <CA+Ds4NsGWJB1HaMMpUnmygTrS6J2NY_nuh=N_YEkEjNg4T0BQw@mail.gmail.com>
On Tue, Sep 13, 2022 at 8:20 PM Michele Venturi <dardo82@xxxxxxxxx> wrote:
>
> Plus you seem quite good at non trivial tasks if you can
> write that monstrousity... Do you know instead what IS trivial? Counting, to know how many matches there are.
Definitely keep using that approach, and see how much help it gets you
in future.
> There are many examples of supporting negative index,
> so I still don't see a reason why it is so difficult to do it...
For benefit of other readers ...
Forward indexing walks over the string once, replacing each occurrence
of a match as it finds it.
To implement negative indexing, it is necessary to walk once to the
end of the string to count the number of occurrences, while
"remembering" where each one appears, and then backtrack to the
position of the Nth-from-last match and replace just that one.
Those who think this is both easy and worthwhile to implement will
find the code in Src/glob.c in the function igetmatch(). Be sure to
trace the call chain all the way back to Src/subst.c to be sure you're
not violating any other constraints.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author