zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <pws@csr.com>
To: zsh-workers@sunsite.dk
Subject: Re: Bug in [[ "" == "" ]] expressions?
Date: Fri, 22 Oct 2004 16:32:22 +0100	[thread overview]
Message-ID: <200410221532.i9MFWMF8004963@news01.csr.com> (raw)
In-Reply-To: <dc507f4a041022075810003f7@mail.gmail.com>

Felix Rosencrantz wrote:
> It seems like zsh on 10-8 works like I'm use to, but a change since
> then has changed the behavior.    Not sure if Peter intended this.
> 
> Comparing an empty or unset string against an empty pattern returns
> false.  Here's the code:
> zsh -f
> % a=""
> % b="abc"
> % [[ "$a" == "" ]] ; echo $status
> 1
> % [[ "$b" == "abc" ]] ; echo $status
> 0
> % [[ "$c" == "" ]] ; echo $status
> 1

This is because of a feature I've never understood whereby zero-length
metafied strings are turned into a Nularg token plus the usual NULL.  I
didn't handle this properly.

This also makes sure that a zero-length string in a pattern is optimised
to be recognised as a pure string, for which memcmp rather than the full
pattern matcher can be used.  (Hmm, I hope memcmp'ing zero bytes is
portable.)

Index: Src/pattern.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/pattern.c,v
retrieving revision 1.22
diff -u -r1.22 pattern.c
--- Src/pattern.c	18 Oct 2004 11:56:17 -0000	1.22
+++ Src/pattern.c	22 Oct 2004 15:28:36 -0000
@@ -399,7 +399,7 @@
 	patendstr++;
 	patendseglen--;
 	patendstrlen--;
-	remnulargs(exp);
+	remnulargs(patparse);
 	patglobflags = 0;
     }
     /*
@@ -419,10 +419,20 @@
 	    || (!(patglobflags & ~GF_IGNCASE) && (patflags & PAT_FILE))
 #endif
 	    )
+	{
+	    /*
+	     * Waah!  I wish I understood this.
+	     * Empty metafied strings have an initial Nularg.
+	     * This never corresponds to a real character in
+	     * a glob pattern or string, so skip it.
+	     */
+	    if (*exp == Nularg)
+		exp++;
 	    for (strp = exp; *strp &&
 		     (!(patflags & PAT_FILE) || *strp != '/') && !itok(*strp);
 		 strp++)
 		;
+	}
 	if (!strp || (*strp && *strp != '/')) {
 	    /* No, do normal compilation. */
 	    strp = NULL;
@@ -1010,6 +1020,9 @@
 
 	/* Get length of string without metafication. */
 	nmeta = 0;
+	/* inherited from domatch, but why, exactly? */
+	if (*str0 == Nularg)
+	    str0++;
 	for (ptr = str0; ptr < patparse; ptr++) {
 	    if (*ptr == Meta) {
 		nmeta++;
Index: Test/D02glob.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/D02glob.ztst,v
retrieving revision 1.6
diff -u -r1.6 D02glob.ztst
--- Test/D02glob.ztst	18 Oct 2004 11:56:18 -0000	1.6
+++ Test/D02glob.ztst	22 Oct 2004 15:28:36 -0000
@@ -308,3 +308,7 @@
  print glob.tmp/**/*~*/dir3(/*|(#e))(/)
 0:Exclusions with complicated path specifications
 >glob.tmp/dir1 glob.tmp/dir2 glob.tmp/dir4
+
+ [[ "" = "" ]] && echo OK
+0:Empty strings
+>OK

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


      reply	other threads:[~2004-10-23 16:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-22 14:58 Felix Rosencrantz
2004-10-22 15:32 ` Peter Stephenson [this message]

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=200410221532.i9MFWMF8004963@news01.csr.com \
    --to=pws@csr.com \
    --cc=zsh-workers@sunsite.dk \
    /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).