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: Fri, 27 Dec 2019 06:09:15 +0100	[thread overview]
Message-ID: <CAKc7PVCfNW1gaebkYBReF4NWgK6UbDHXjtet9O98zKy_4puDtg@mail.gmail.com> (raw)
In-Reply-To: <CAKc7PVC8NAxT+k7SHsQmBbo7Gzg8MORJLoz_1awKZhFOxdpucQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2240 bytes --]

I've updated the trick. It's now:

str="abcXXXdefXXXghi"
out=${(S)str%%(#b)([^X])X##(*)}
out=$out$match[1]$match[2]

The man page looks like http://psprint.blinkenshell.org/S_flag-2.png

I think that the trick is minimal to get the job done, i.e.: to
actually match a substring greedily starting from the right.

On Fri, 27 Dec 2019 at 05:54, Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> The trick is not right. I'll try to update it or otherwise change the
> description.
>
> On Thu, 26 Dec 2019 at 19:35, Sebastian Gniazdowski
> <sgniazdowski@gmail.com> wrote:
> >
> > I've attached the extended description. It includes a trick to
> > work-around the unintuitive behavior of S. It looks as follows:
> >
> > http://psprint.blinkenshell.org/S_flag.png
> >
> > I think that the way the S flag works is a bit of an inconsistency,
> > Because ${str%%X##**} would not stop at the first from the right
> > match, it would try other matches starting from the right and go on up
> > to the final first from the left X. I think that (S) shouldn't change
> > this, but on the other hand should ${(S)str%%X##} match the first
> > three X? Rather not, as it would resemble ## then... Intuitively,
> > however, it should match all the three right X.
> >
> > On Thu, 19 Dec 2019 at 16:30, Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> > >
> > > Sebastian Gniazdowski wrote on Wed, 18 Dec 2019 20:44 +00:00:
> > > > Or rather not a bug… It seems that it's the result of how % searches
> > > > the substrings from the end – it stops at the first match, i.e.: after
> > > > finding a first X from the right.
> > >
> > > Could we improve the documentation of (S), then?
> >
> >
> >
> > --
> > Sebastian Gniazdowski
> > News: https://twitter.com/ZdharmaI
> > IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
> > Blog: http://zdharma.org
>
>
>
> --
> Sebastian Gniazdowski
> News: https://twitter.com/ZdharmaI
> IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
> Blog: http://zdharma.org



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

[-- Attachment #2: 0001-Extend-description-of-S-flag.patch.2.txt --]
[-- Type: text/plain, Size: 1399 bytes --]

From 8176ef315181dc38e41e80c6509d591bddf86db1 Mon Sep 17 00:00:00 2001
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
Date: Thu, 26 Dec 2019 19:22:41 +0100
Subject: [PATCH] Extend description of S flag

---
 Doc/Zsh/expn.yo | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index d7147dbd7..36813dc20 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -1399,6 +1399,19 @@ 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(%). 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]$match[2])
+
+The result is tt(abcXXXdefghi). It would have been tt(abcXXXdefXXghi)
+if the substitution would have been tt(${(S)str%%X##}), 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.
 )
 item(tt(I:)var(expr)tt(:))(
 Search the var(expr)th match (where var(expr) evaluates to a number).
-- 
2.21.0


  reply	other threads:[~2019-12-27  5:10 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 [this message]
2019-12-27  5:29       ` Daniel Shahaf
2019-12-28 19:04         ` Sebastian Gniazdowski
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=CAKc7PVCfNW1gaebkYBReF4NWgK6UbDHXjtet9O98zKy_4puDtg@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).