zsh-users
 help / color / mirror / code / Atom feed
* using array slice as lvalue
@ 2010-12-21  7:42 Le Wang
  2010-12-21  8:45 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Le Wang @ 2010-12-21  7:42 UTC (permalink / raw)
  To: zsh-users

Hi,
I execute the following:

arr=(a b c)
arr[2,-1]=(z)
typeset arr # arr=(a z b c)
arr[1,-1]=(z)
typeset arr # arr=(z)

In the first slice assignment, the rvalue was inserted into the array
before arr[2], and the second slice assignment, the slice is assigned
to the rvalue.  Is the first behavior a bug?

--
Le


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: using array slice as lvalue
  2010-12-21  7:42 using array slice as lvalue Le Wang
@ 2010-12-21  8:45 ` Bart Schaefer
  2010-12-21 10:02   ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2010-12-21  8:45 UTC (permalink / raw)
  To: zsh-users

On Dec 21,  3:42pm, Le Wang wrote:
}
} arr=(a b c)
} arr[2,-1]=(z)
} typeset arr # arr=(a z b c)

I would have to say that is a bug.  [1,-1] is handled as a special case,
but it appears assignment botches other negative indices unless both
the start and the end are negative.

Rather amazing that this has never come up before (and of course it
can't help but appear immediately after a release is announced).

Potential patch -- fixes this case and still passes "make check" but
may break some other obscure corner:

Index: Src/params.c
--- zsh-forge/current/Src/params.c	2010-11-19 09:49:19.000000000 -0800
+++ Src/params.c	2010-12-21 00:40:29.000000000 -0800
@@ -2442,8 +2442,6 @@
 		v->start--;
 	    v->end--;
 	}
-	if (v->end < v->start)
-	    v->end = v->start;
 	q = old = v->pm->gsu.a->getfn(v->pm);
 	n = arrlen(old);
 	if (v->start < 0) {
@@ -2456,6 +2454,8 @@
 	    if (v->end < 0)
 		v->end = 0;
 	}
+	if (v->end < v->start)
+	    v->end = v->start;
 
 	ll = v->start + arrlen(val);
 	if (v->end <= n)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: using array slice as lvalue
  2010-12-21  8:45 ` Bart Schaefer
@ 2010-12-21 10:02   ` Peter Stephenson
  2010-12-21 16:58     ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2010-12-21 10:02 UTC (permalink / raw)
  To: zsh-users

On Tue, 21 Dec 2010 00:45:49 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Dec 21,  3:42pm, Le Wang wrote:
> }
> } arr=(a b c)
> } arr[2,-1]=(z)
> } typeset arr # arr=(a z b c)
> 
> I would have to say that is a bug.  [1,-1] is handled as a special
> case, but it appears assignment botches other negative indices unless
> both the start and the end are negative.
> 
> Rather amazing that this has never come up before (and of course it
> can't help but appear immediately after a release is announced).

The assignment tests are weighted towards +=, because it was a new
feature.

Here are some tests for partial array assignment.  Ones for mixed
indices can be added when the patch is committed.  All of these are
still working.

Index: Test/A06assign.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/A06assign.ztst,v
retrieving revision 1.6
diff -p -u -r1.6 A06assign.ztst
--- Test/A06assign.ztst	31 Aug 2010 19:32:57 -0000	1.6
+++ Test/A06assign.ztst	21 Dec 2010 09:59:29 -0000
@@ -7,6 +7,105 @@
 1:assign to association with odd no. of values
 ?(eval):2: bad set of key/value pairs for associative array
 
+# tests of array element assignment
+
+ array=(1 2 3 4 5)
+ array[1]=42
+ print $array
+0:Replacement of array element
+>42 2 3 4 5
+
+ array=(1 2 3 4 5)
+ array[1]=(42 43)
+ print $array
+0:Replacement of array element with array
+>42 43 2 3 4 5
+
+ array=(1 2 3 4 5)
+ array[1,2]=(42 43)
+ print $array
+0:Replacement of start of array
+>42 43 3 4 5
+
+ array=(1 2 3 4 5)
+ array[1,4]=(42 43)
+ print $array
+0:Replacement of start of array with shorter slice
+>42 43 5
+
+ array=(1 2 3 4 5)
+ array[1,6]=(42 43)
+ print $array
+0:Replacement of array by extending slice
+>42 43
+
+ array=(1 2 3 4 5)
+ array[3]=(42 43)
+ print $array
+0:Replacement of middle element with array
+>1 2 42 43 4 5
+
+ array=(1 2 3 4 5)
+ array[3,4]=(42 43 44)
+ print $array
+0:Replacement of slice in middle
+>1 2 42 43 44 5
+
+ array=(1 2 3 4 5)
+ array[7,8]=(42 43)
+ print $array
+ # check that [6] was left empty...
+ array[6]=41
+ print $array
+0:Appending by replacing elements off the end
+>1 2 3 4 5 42 43
+>1 2 3 4 5 41 42 43
+
+ array=(1 2 3 4 5)
+ array[-1]=42
+ print $array
+0:Replacement of last element of array, negative indices
+>1 2 3 4 42
+
+ array=(1 2 3 4 5)
+ array[-1]=(42 43)
+ print $array
+0:Replacement of last element of array with array, negative indices
+>1 2 3 4 42 43
+
+ array=(1 2 3 4 5)
+ array[-3,-2]=(42 43 44)
+ print $array
+0:Replacement of middle of array, negative indices
+>1 2 42 43 44 5
+
+ array=(1 2 3 4 5)
+ array[-5,-1]=(42 43)
+ print $array
+0:Replacement of entire array, negative indices
+>42 43
+
+ array=(1 2 3 4 5)
+ array[-7,-1]=(42 43)
+ print $array
+0:Replacement of more than entire array, negative indices
+>42 43
+
+ array=(1 2 3 4 5)
+ array[-7]=42
+ print $array
+0:Replacement of element off start of array.
+>42 1 2 3 4 5
+
+ array=(1 2 3 4 5)
+ array[-7]=42
+ array[-6]=43
+ print $array
+0:Replacement off start doesn't leave gaps.  Hope this is right.
+>43 1 2 3 4 5
+
+# TODO: mixed indices [-num,num] and [num,-num]
+
 # tests of var+=scalar
 
  s+=foo

-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: using array slice as lvalue
  2010-12-21 10:02   ` Peter Stephenson
@ 2010-12-21 16:58     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2010-12-21 16:58 UTC (permalink / raw)
  To: zsh-users

On Dec 21, 10:02am, Peter Stephenson wrote:
}
} Here are some tests for partial array assignment.  Ones for mixed
} indices can be added when the patch is committed.  All of these are
} still working.

I added some additional tests for mixed indices and commited u/15662.
All tests are still passing.  I didn't explicitly add tests for ranges
that start (or end) in the middle and go beyond the end (beginning) of
the array, but there are tests for running off both ends at once.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-12-21 16:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-12-21  7:42 using array slice as lvalue Le Wang
2010-12-21  8:45 ` Bart Schaefer
2010-12-21 10:02   ` Peter Stephenson
2010-12-21 16:58     ` Bart Schaefer

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).