zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: "zsh-workers@zsh.org" <zsh-workers@zsh.org>
Subject: Array slices that don't exist [was Optimization of getarrvalue()]
Date: Mon, 28 Nov 2016 22:11:44 -0800	[thread overview]
Message-ID: <161128221144.ZM10240@torch.brasslantern.com> (raw)
In-Reply-To: <D3BEF64B-BE34-4260-A4F3-A11EEC2E0D30@kba.biglobe.ne.jp>

On Nov 24,  8:49pm, Jun T. wrote:
}
} % a=(one two)
} and see what is returned by
} % nargs "${(@)a[i,j]}"
} 
} For i >= 4 or i <= -3 it returns 1 if j >= i,
} while for i=3 (=$#a+1) it returns 0 always.

Hmm.

Internally the array $a is stored as a[0]="one" a[1]="two" a[2]=NULL so
when we look at $a[i,j] we decrement i but not j to look at the internal
elements starting with a[i-1] and ending before (not including) a[j].

This means that if $i = $#a we're always going to take the branch that
now has the comment

        /* Copy to a point before the end of the source array:
         * arrdup_max will copy at most v->end - v->start elements,
         * starting from v->start element. Original code said:
	 *  s[v->end - v->start] = NULL

with v->start pointing at the NULL terminator of the internal array, so
we return an empty array.

If $i > $#a then we take the arrlen_lt(s, v->start) branch and always
return a single element.

So the question is ... is the following more consistent?

for i in 1 2 3 4; do
 for j in 1 2 3 4 5; do
  print -n "$i $j = "
  nargs "${(@)a[i,j]}"
 done
done

5.2       |  5.3 **
----------+----------
1 1 => 1  |  1 1 => 1
1 2 => 2  |  1 2 => 2
1 3 => 2  |  1 3 => 2
1 4 => 2  |  1 4 => 2
1 5 => 2  |  1 5 => 2
2 1 => 0  |  2 1 => 0
2 2 => 1  |  2 2 => 1
2 3 => 1  |  2 3 => 1
2 4 => 1  |  2 4 => 1
2 5 => 1  |  2 5 => 1
3 1 => 0  |  3 1 => 0
3 2 => 0  |  3 2 => 0
3 3 => 0  |  3 3 => 0
3 4 => 0  |  3 4 => 1   **
3 5 => 0  |  3 5 => 1   **
4 1 => 0  |  4 1 => 0
4 2 => 0  |  4 2 => 0
4 3 => 0  |  4 3 => 0
4 4 => 1  |  4 4 => 0   **
4 5 => 1  |  4 5 => 1

This means you have to slice more than one non-existent element to
get an empty string; a slice of one such element is an empty array.

All "make check" tests still pass with that behavior though I'm not
certain that all completions would work identically.

Code looks like this:

diff --git a/Src/params.c b/Src/params.c
index 45f398a..aa8b196 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -2299,9 +2299,16 @@ getarrvalue(Value v)
     if (v->end <= v->start) {
 	s = arrdup_max(nular, 0);
     }
-    else if (arrlen_lt(s, v->start) || v->start < 0) {
+    else if (v->start < 0) {
 	s = arrdup_max(nular, 1);
-    } else {
+    }
+    else if (arrlen_le(s, v->start)) {
+	/* Handle $ary[i,i] consistently for any $i > $#ary
+	 * and $ary[i,j] consistently for any $j > $i > $#ary
+	 */
+	s = arrdup_max(nular, v->end - (v->start + 1));
+    }
+    else {
         /* Copy to a point before the end of the source array:
          * arrdup_max will copy at most v->end - v->start elements,
          * starting from v->start element. Original code said:


  reply	other threads:[~2016-11-29  6:11 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20161108201233epcas1p1e2900e2d67af8b8558ebdb70eb7ad480@epcas1p1.samsung.com>
2016-11-08 20:11 ` [PATCH] Optimization of getarrvalue() Sebastian Gniazdowski
2016-11-08 21:58   ` Bart Schaefer
2016-11-09  7:11   ` Bart Schaefer
2016-11-09 11:42   ` Peter Stephenson
2016-11-09 16:03     ` Bart Schaefer
2016-11-14 12:32       ` Jun T.
2016-11-14 13:15         ` Jun T.
2016-11-14 13:57         ` Peter Stephenson
2016-11-14 15:35           ` Jun T.
2016-11-14 17:10           ` Bart Schaefer
2016-11-16  7:55             ` Sebastian Gniazdowski
2016-11-15 12:28         ` Peter Stephenson
2016-11-15 19:57         ` Peter Stephenson
2016-11-15 21:11           ` Bart Schaefer
2016-11-16 14:06           ` Jun T.
2016-11-16 16:14             ` Jun T.
2016-11-16 18:50             ` Bart Schaefer
2016-11-21 12:30               ` Jun T.
2016-11-24  0:55                 ` Bart Schaefer
2016-11-24 11:49                   ` Jun T.
2016-11-29  6:11                     ` Bart Schaefer [this message]
2016-11-29  9:34                       ` Array slices that don't exist [was Optimization of getarrvalue()] 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=161128221144.ZM10240@torch.brasslantern.com \
    --to=schaefer@brasslantern.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).