zsh-workers
 help / color / mirror / code / Atom feed
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
To: Daniel Shahaf <d.s@daniel.shahaf.name>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: [Bug] S-flag imposes non-greedy match where it shouldn't
Date: Sat, 28 Dec 2019 20:04:21 +0100	[thread overview]
Message-ID: <CAKc7PVDx6WEFcZFS3TQ6+rF1YpA3jkD5sWwr0LCNE_c5MYkxVg@mail.gmail.com> (raw)
In-Reply-To: <20191227052923.yal2nnmxdxfgvfkr@tarpaulin.shahaf.local2>

On Fri, 27 Dec 2019 at 06:30, Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
>
> Sebastian Gniazdowski wrote on Thu, Dec 26, 2019 at 19:35:05 +0100:
> > +++ b/Doc/Zsh/expn.yo
> > @@ -1399,6 +1399,20 @@ from the beginning and with tt(%) start from the end of the string.
> >  With substitution via tt(${)...tt(/)...tt(}) or
> >  tt(${)...tt(//)...tt(}), specifies non-greedy matching, i.e. that the
> >  shortest instead of the longest match should be replaced.
> > +The substring search means that the pattern is matched skipping the
> > +parts of the input string starting from the direction set by the use
> > +of tt(#) or tt(%).
>
> I don't understand this sentence.  What does "skipping" mean?

It means that parts of the string are being skipped when they don't
match when moving to the other end. Does the sentence need an update?

> > +For example, to match a pattern starting from the
> > +end, one could use:
> > +
> > +example(str="abcXXXdefXXXghi"
> > +out=${(S)str%%(#b)([^X])X##}
> > +out=$out${match[1]}
> > +)
> > +
> > +The result is tt(abcXXXdefghi).
>
> That's not correct.  The output is abcXXXdefXXXghi (in 'zsh -f') or
> abcXXXdeghif (with extendedglob set), but not abcXXXdefghi.

I've sent an updated patch half hour before your email. It contains
the correct example.

> I doubt this example would clarify the meaning of ${(S)} to people who
> encounter it for the first time.  Please use a more minimal example.
> Specific issues:
>   - (...) This is documentation, not
>   a homework problem; the answer should be obvious.  Something like
>   «out="${out}+${match[1]}"» would address this — but…

I think that many examples in the man pages are like that – they don't
go the obvious path of just demonstrating the usage but instead, they
cover some edge case that, after (sometimes quite long) thinking
reveal something very peculiar about the feature. There are better
examples of this, however, the best that I've found currently is the
one used for the #b glob flag:

             foo="a string with a message"
             if [[ $foo = (a|an)' '(#b)(*)' '* ]]; then
               print ${foo[$mbegin[1],$mend[1]]}
            fi

The example prints `string with a', and the user has a "homework" of
untangling a few points:
- why it isn't "string with a message" (it's because the final ' '*
part that requires a space after the final word of the (*) part),
- why the answer isn't "message" (the same as above plus the fact that
there's no * before (a|an) and the greediness).

If not the homework-attitude of the examples in the man page, the
example would have been

             if [[ "a string with a message" = (#b)a' '(*) ]]; then

and would give the answer "string with a message". This would have
been the obvious-demonstration attitude that I've referred to.

> - … the use of advanced pattern matching features needlessly raises the
>   learning curve.

I can add the mention that the example needs EXTENDED_GLOB. Overall I
think that the example:
- is nice because it shows how to make the (S)...%% substitution
behave as the intuition would suggest,
- it's the only place in the documentation that uses the (#b) flag
with #/% substitution, showing that it's possible to use it in that
place,
- it isn't that complex for someone that knows #b flag and the $match parameter.

> > It would have been tt(abcXXXdefXXghif)
> > +if not the tt([^X]) part, as despite the tt(%%) specifies a greedy
> > +match, the substring matching works by trying matches from right to
> > +left and stops at a first valid match.
>
> There are some grammatical errors here (e.g., s/(?<=specif)ies/ying/), but
> let's not worry about them until the rest of the patch isn't a moving target.

I think that grammar is correct here. Did you maybe misread the sentence?

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

  reply	other threads:[~2019-12-28 19:05 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-18 20:41 Sebastian Gniazdowski
2019-12-18 20:44 ` Sebastian Gniazdowski
2019-12-19 15:29   ` Daniel Shahaf
2019-12-26 18:35     ` Sebastian Gniazdowski
2019-12-27  4:54       ` Sebastian Gniazdowski
2019-12-27  5:09         ` Sebastian Gniazdowski
2019-12-27  5:29       ` Daniel Shahaf
2019-12-28 19:04         ` Sebastian Gniazdowski [this message]
2019-12-28 20:34           ` Bart Schaefer
2019-12-28 21:00           ` Daniel Shahaf
2019-12-29  0:56             ` Sebastian Gniazdowski
2019-12-29  2:05               ` Daniel Shahaf
2019-12-29  3:14                 ` Sebastian Gniazdowski
2019-12-30 18:00                   ` [PATCH] zshexpn: Expand documentation of (S) (was: Re: [Bug] S-flag imposes non-greedy match where it shouldn't) Daniel Shahaf
2019-12-30 18:11                     ` Roman Perepelitsa
     [not found]                       ` <CAKc7PVAXLpKqZvmbazZK=mvcz8T-AHJXKusut6aEjkkSLzgdbw@mail.gmail.com>
2019-12-30 20:01                         ` Roman Perepelitsa
2019-12-30 20:20                           ` Sebastian Gniazdowski
2019-12-30 21:24                             ` ${(S)%%*} doesn't match the empty string (was: Re: [PATCH] zshexpn: Expand documentation of (S) (was: Re: [Bug] S-flag imposes non-greedy match where it shouldn't)) Daniel Shahaf
2019-12-30 21:44                               ` Roman Perepelitsa
2019-12-30 22:11                                 ` Sebastian Gniazdowski
2019-12-30 22:34                               ` Peter Stephenson
2019-12-30 21:40                             ` [PATCH] zshexpn: Expand documentation of (S) (was: Re: [Bug] S-flag imposes non-greedy match where it shouldn't) Roman Perepelitsa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAKc7PVDx6WEFcZFS3TQ6+rF1YpA3jkD5sWwr0LCNE_c5MYkxVg@mail.gmail.com \
    --to=sgniazdowski@gmail.com \
    --cc=d.s@daniel.shahaf.name \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).