zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Philippe Altherr <philippe.altherr@gmail.com>,
	Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Subscript flag (i) not working correctly on empty strings?
Date: Fri, 6 Jan 2023 17:23:19 +0000 (GMT)	[thread overview]
Message-ID: <361494476.8470860.1673025799682@mail.virginmedia.com> (raw)
In-Reply-To: <CAGdYchuqW79Q3nMguaOrJKEcRwMnQgDRNhHmh3hF5V6sgp=q4g@mail.gmail.com>

> On 06/01/2023 16:01 Philippe Altherr <philippe.altherr@gmail.com> wrote:
> The following script
> 
> > s=aaa; echo $s[(i)z];
> > s=aa; echo $s[(i)z];
> > s=a; echo $s[(i)z];
> > s=; echo $s[(i)z];
> 
> prints this
> 
> > 4
> > 3
> > 2
> > 0
> 
> Why is the last line 0 and not 1?

Certainly looks plain wrong.  From the source it appears to be
an overzealous safety test --- if we get a NULL value, we should
give up trying to search the string, but not if we get an empty
string.

None of the existing tests fail with this change.

pws

diff --git a/Src/params.c b/Src/params.c
index 2e4a6eae2..6362b382c 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -1669,7 +1669,7 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w,
 	    /* Searching characters */
 	    int slen;
 	    d = getstrvalue(v);
-	    if (!d || !*d)
+	    if (!d)
 		return 0;
 	    /*
 	     * beg and len are character counts, not raw offsets.
diff --git a/Test/D06subscript.ztst b/Test/D06subscript.ztst
index 21127e641..57cdc027c 100644
--- a/Test/D06subscript.ztst
+++ b/Test/D06subscript.ztst
@@ -299,3 +299,12 @@ F:In math, (($i)) should be the same as ((i)), see workers/47748.
   echo ${string[(pws:\0:)1]}
 0:Word splitting by NUL
 >foo
+
+  string="a"
+  print ${string[(i)x]}
+  string=""
+  print ${string[(i)x]}
+0:Can check off end of zero length string
+F:Regression test for inconsistency of failed (i) on zero-length string
+>2
+>1


  reply	other threads:[~2023-01-06 17:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-06 16:01 Philippe Altherr
2023-01-06 17:23 ` Peter Stephenson [this message]
2023-01-06 18:12   ` Bart Schaefer
2023-01-07 13:45     ` Philippe Altherr
2023-01-07 13:52       ` Roman Perepelitsa
2023-01-07 13:54         ` Roman Perepelitsa
2023-01-08 19:47     ` Peter Stephenson

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=361494476.8470860.1673025799682@mail.virginmedia.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=philippe.altherr@gmail.com \
    --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).