zsh-users
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: zsh-users@zsh.org
Subject: Re: globbing in conditional expressions
Date: Sat, 31 May 2014 17:34:06 +0000	[thread overview]
Message-ID: <20140531173406.GF2498@tarsus.local2> (raw)
In-Reply-To: <140530214320.ZM30732@torch.brasslantern.com>

Bart Schaefer wrote on Fri, May 30, 2014 at 21:43:20 -0700:
> On May 30,  8:55am, Bart Schaefer wrote:
> }
> } Is there a non-convoluted way to have the special case of a glob with
> } BOTH of the (NY) qualifiers return empty string rather than nularg when
> } it finds no match?  Empty string is an illegal file name so for most
> } purposes it wouldn't otherwise affect usage.

I beg to differ.  Making *(NY) return empty strings upon no match is
(by design) convenient for the [ -n *(NY) ] case, but it breaks other
use-cases.  For example,
    frob *.foo(NY) *.bar(NY)
would suddenly need to handle "" files in the argument list.

For testing whether the pattern expands to anything, this is already possible:
    if (){(($#@))} *(NY)
and after naming the anon func, it becomes:
    if has_any_matches *(NY)
which seems to me as readable as [ -n *(NY) ].  (For that matter, we could
define a unary test operator [ --is-not-null ] that acts like -n except
that it returns False rather than True in the nularg case, and write
[ --is-not-null *(NY) ].)

Perhaps an assignment-ish syntax would be useful?  Something like
    if [ -n ${(X)fname:=*(NY)} ]
as shorthand for:
    if _tmp=(*(NY)) && (( $#_tmp )) && fname=$_tmp[1] && [ -n $fname ]
?  That way also makes the matching filename available for the if's
body.  (The X stands for an expansion modifier that would make the RHS
be treated as a glob rather than as a literal string.)

Cheers,

Daniel

> This turns out to be pretty simple.  It'd additionally be nice if the
> CSH_NULL_GLOB option caused any such empty strings to be removed when
> some other pattern succeeds, but that seems rather far-reaching.  (The
> following will require a tweak to Daniel's test in D02glob.)
> 
> diff --git a/Src/glob.c b/Src/glob.c
> index 07dd7c2..5aa306b 100644
> --- a/Src/glob.c
> +++ b/Src/glob.c
> @@ -1748,6 +1761,11 @@ zglob(LinkList list, LinkNode np, int nountok)
>  	    matchptr++;
>  	    matchct = 1;
>  	}
> +    } else if (shortcircuit) {
> +      /* treat as an empty string */
> +      matchptr->name = dupstring("");
> +      matchptr++;
> +      matchct = 1;
>      }
>  
>      if (!(gf_sortlist[0].tp & GS_NONE)) {


  reply	other threads:[~2014-05-31 17:34 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-07 12:41 Roman Neuhauser
2014-05-07 14:44 ` Peter Stephenson
2014-05-08 10:55   ` Daniel Shahaf
2014-05-08 11:20     ` Peter Stephenson
2014-05-08 15:34       ` Bart Schaefer
2014-05-08 20:19         ` Roman Neuhauser
2014-05-13 15:41           ` Bart Schaefer
2014-05-14  4:19             ` Daniel Shahaf
2014-05-14  7:18               ` Bart Schaefer
2014-05-15  4:55                 ` Bart Schaefer
2014-05-15  9:35                   ` Daniel Shahaf
2014-05-15 14:33                     ` Bart Schaefer
2014-05-15  9:29                 ` Daniel Shahaf
2014-05-15 14:50                   ` Bart Schaefer
2014-05-26 23:52                     ` Daniel Shahaf
2014-05-30  3:59                       ` Bart Schaefer
2014-05-30  8:47                         ` Peter Stephenson
2014-05-30 15:55                           ` Bart Schaefer
2014-05-31  4:43                             ` Bart Schaefer
2014-05-31 17:34                               ` Daniel Shahaf [this message]
2014-05-31 17:59                                 ` Bart Schaefer
2014-06-02  9:40                                   ` Peter Stephenson
2014-06-02 12:57                                     ` Daniel Shahaf
2014-06-02 14:15                                       ` Peter Stephenson
2014-05-30 14:45                         ` Daniel Shahaf
2014-05-14  7:42               ` Christoph (Stucki) von Stuckrad
2014-05-14 21:09               ` Roman Neuhauser
2014-05-15  9:14                 ` Daniel Shahaf
2014-05-15 10:05                   ` Roman Neuhauser
2014-05-07 14:57 ` Mikael Magnusson
2014-05-07 15:03 ` Bart Schaefer

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=20140531173406.GF2498@tarsus.local2 \
    --to=d.s@daniel.shahaf.name \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-users@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).