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

Re: Pattern bug on (a*|)~^(*b)



> On 25/07/2023 14:19 Johan Grande <nahoj@xxxxxxxxx> wrote:
> In zsh 5.8.1 (x86_64-ubuntu-linux-gnu) with extended_glob,
> 
> [[ "ab" = (|a*)~^(*b) ]]
> 
> incorrectly (unless I'm mistaken) returns 1. However
> 
> [[ "ab" = (a*|)~^(*b) ]]
> 
> correctly returns 0.

You can see this is a real bug with

[[ ab = (a|a*)~^*b ]]

which also fails, while

[[ ab = (b*|a*)~^*b ]]

succeeds, so this is clearly inconsistent.

I think I've tracked down the problem.  Because of the unpleasant
hierarchy-violating properties of exclusion pointed out by Bart, we record the
point at which the match succeeded.  The point we do this isn't where the
entire pattern succeeds, however, only the point where the part we're going to
exclude has matched.  So at this point we've successfully matched "a" in the
case I showed, or the empty string in the original case.  But we haven't yet
taken account of the anchor at the end, which will later cause us to fail.

When we do fail and backtrack, the marker is still there saying we matched just
the "a" or the empty string.  We then successfully match a*, but when we get
to the point of trying to exclude ^*b the record saying what we're excluding
against is messed up --- ^*b DOES match either a or the empty string, so
the exclusion succeeds.

The fixes I can think of are either to find a point at which to undo the sync
record for the match that has failed after the event or, perhaps better as it's
a local change even though it does more work, reset any previous matches before
the sync point on the next alternative when we mark that as having matched
(we will only do that if the previous branch failed --- remember [irony
intended] that a branch succeeds only if all following patterns match too).

This is a bit fraught as some of the markers (not necessarily these ones, it's
almost a quarter of a century since the first version of this code) are there
to take account of some pathological cases so removing them might have hard
to discover effects.  This will take another load of wet towels and caffeine.

pws




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