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

Re: Substitute the last match of a pattern during parameters expansion.

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